about summary refs log tree commit diff stats
path: root/src/js
diff options
context:
space:
mode:
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))