diff options
Diffstat (limited to 'src/js/javascript.nim')
-rw-r--r-- | src/js/javascript.nim | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/src/js/javascript.nim b/src/js/javascript.nim index 68ee7be7..e8c36e34 100644 --- a/src/js/javascript.nim +++ b/src/js/javascript.nim @@ -128,11 +128,14 @@ func getOpaque*(rt: JSRuntime): JSRuntimeOpaque = var runtimes {.threadVar.}: seq[JSRuntime] proc newJSRuntime*(): JSRuntime = - result = JS_NewRuntime() - runtimes.add(result) - var opaque = new(JSRuntimeOpaque) + let rt = JS_NewRuntime() + let opaque = new(JSRuntimeOpaque) GC_ref(opaque) - JS_SetRuntimeOpaque(result, cast[pointer](opaque)) + JS_SetRuntimeOpaque(rt, cast[pointer](opaque)) + # Must be added after opaque is set, or there is a chance of + # nim_finalize_for_js dereferencing it (at the new call). + runtimes.add(rt) + return rt proc newJSContext*(rt: JSRuntime): JSContext = let ctx = JS_NewContext(rt) |