about summary refs log tree commit diff stats
path: root/src/js
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-01-08 19:54:47 +0100
committerbptato <nincsnevem662@gmail.com>2024-01-08 19:54:47 +0100
commit7e77bced54d29e5e909c0e587fbf9a0c689043dc (patch)
tree8ae078079e6c537f9998d7dc4bf0f8fb61e3ecd9 /src/js
parentb0547ba9f48bf402665b89f84b88b80ee58d8824 (diff)
downloadchawan-7e77bced54d29e5e909c0e587fbf9a0c689043dc.tar.gz
js: use Nim allocator
Diffstat (limited to 'src/js')
-rw-r--r--src/js/javascript.nim20
1 files changed, 19 insertions, 1 deletions
diff --git a/src/js/javascript.nim b/src/js/javascript.nim
index 56ba8fd3..47be1742 100644
--- a/src/js/javascript.nim
+++ b/src/js/javascript.nim
@@ -105,8 +105,26 @@ type
 
 var runtimes {.threadvar.}: seq[JSRuntime]
 
+proc bindMalloc(s: ptr JSMallocState, size: csize_t): pointer {.cdecl.} =
+  return alloc(size)
+
+proc bindFree(s: ptr JSMallocState, p: pointer) {.cdecl.} =
+  if p == nil:
+    return
+  dealloc(p)
+
+proc bindRealloc(s: ptr JSMallocState, p: pointer, size: csize_t): pointer
+    {.cdecl.} =
+  return realloc(p, size)
+
 proc newJSRuntime*(): JSRuntime =
-  let rt = JS_NewRuntime()
+  var mf {.global.} = JSMallocFunctions(
+    js_malloc: bindMalloc,
+    js_free: bindFree,
+    js_realloc: bindRealloc,
+    js_malloc_usable_size: nil
+  )
+  let rt = JS_NewRuntime2(addr mf, nil)
   let opaque = JSRuntimeOpaque()
   GC_ref(opaque)
   JS_SetRuntimeOpaque(rt, cast[pointer](opaque))