about summary refs log tree commit diff stats
path: root/src/js/javascript.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-06-24 19:29:15 +0200
committerbptato <nincsnevem662@gmail.com>2023-06-24 19:29:15 +0200
commit69d5478716054a031b6c27f5c5d6fee56bd5d2c4 (patch)
tree32bbcd17c75d6e9882752837def16582bea5e59f /src/js/javascript.nim
parent644f8256db9c7216cfc8a67ba6cb380a96ba28c5 (diff)
downloadchawan-69d5478716054a031b6c27f5c5d6fee56bd5d2c4.tar.gz
Fix potential crash on startup
Diffstat (limited to 'src/js/javascript.nim')
-rw-r--r--src/js/javascript.nim11
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)