about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-04-21 19:17:10 +0200
committerbptato <nincsnevem662@gmail.com>2024-04-21 19:17:10 +0200
commit470aeef2d72614cb8f904d8ef19583312410d981 (patch)
tree20cebeb73b89000d4ee43b1d95c98d271ae84483
parent31b359224632fd4e3c2aac667483ed249121efaa (diff)
downloadchawan-470aeef2d72614cb8f904d8ef19583312410d981.tar.gz
js: fix some incorrect defineProperty usage
It consumes a value, so we must dup those that we pass.
-rw-r--r--src/config/config.nim5
-rw-r--r--src/html/dom.nim4
-rw-r--r--src/js/module.nim4
-rw-r--r--src/js/tojs.nim1
4 files changed, 7 insertions, 7 deletions
diff --git a/src/config/config.nim b/src/config/config.nim
index 63bc50ab..1ac0393e 100644
--- a/src/config/config.nim
+++ b/src/config/config.nim
@@ -773,16 +773,17 @@ proc initCommands*(config: Config): Err[string] =
     if k in config.cmd.map:
       # already in map; skip
       continue
-    var objIt = obj
+    var objIt = JS_DupValue(ctx, obj)
     let name = k.afterLast('.')
     if name.len < k.len:
       for ss in k.substr(0, k.high - name.len - 1).split('.'):
         var prop = JS_GetPropertyStr(ctx, objIt, cstring(ss))
         if JS_IsUndefined(prop):
           prop = JS_NewObject(ctx)
-          ctx.definePropertyE(objIt, ss, prop)
+          ctx.definePropertyE(objIt, ss, JS_DupValue(ctx, prop))
         if JS_IsException(prop):
           return err(ctx.getExceptionMsg())
+        JS_FreeValue(ctx, objIt)
         objIt = prop
     if cmd == "":
       config.cmd.map[k] = JS_UNDEFINED
diff --git a/src/html/dom.nim b/src/html/dom.nim
index 417237bb..8767ac6e 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -1446,7 +1446,7 @@ proc newLocation*(window: Window): Location =
   if ctx != nil:
     let val = toJS(ctx, location)
     let valueOf = ctx.getOpaque().Object_prototype_valueOf
-    defineProperty(ctx, val, "valueOf", valueOf)
+    defineProperty(ctx, val, "valueOf", JS_DupValue(ctx, valueOf))
     defineProperty(ctx, val, "toPrimitive", JS_UNDEFINED)
     #TODO [[DefaultProperties]]
     JS_FreeValue(ctx, val)
@@ -2855,7 +2855,7 @@ proc reflectEvent(element: Element; target: EventTarget; name: StaticAtom;
       urls, ctx.getExceptionMsg())
   else:
     let jsTarget = ctx.toJS(target)
-    ctx.definePropertyC(jsTarget, $name, fun)
+    ctx.definePropertyC(jsTarget, $name, JS_DupValue(ctx, fun))
     JS_FreeValue(ctx, jsTarget)
     #TODO this is subtly wrong. In fact, we should not pass `fun'
     # directly here, but a wrapper function that calls fun. Currently
diff --git a/src/js/module.nim b/src/js/module.nim
index 2e15ead3..636065b8 100644
--- a/src/js/module.nim
+++ b/src/js/module.nim
@@ -6,13 +6,11 @@ import js/tojs
 proc setImportMeta(ctx: JSContext; funcVal: JSValue; isMain: bool) =
   let m = cast[JSModuleDef](JS_VALUE_GET_PTR(funcVal))
   let moduleNameAtom = JS_GetModuleName(ctx, m)
-  let moduleName = JS_AtomToCString(ctx, moduleNameAtom)
   let metaObj = JS_GetImportMeta(ctx, m)
-  definePropertyCWE(ctx, metaObj, "url", moduleName)
+  definePropertyCWE(ctx, metaObj, "url", JS_AtomToValue(ctx, moduleNameAtom))
   definePropertyCWE(ctx, metaObj, "main", isMain)
   JS_FreeValue(ctx, metaObj)
   JS_FreeAtom(ctx, moduleNameAtom)
-  JS_FreeCString(ctx, moduleName)
 
 proc finishLoadModule*(ctx: JSContext; f: string; name: cstring): JSModuleDef =
   let funcVal = compileModule(ctx, f, name)
diff --git a/src/js/tojs.nim b/src/js/tojs.nim
index 4cbd2045..2ee8fd47 100644
--- a/src/js/tojs.nim
+++ b/src/js/tojs.nim
@@ -106,6 +106,7 @@ makeToJSP(Result)
 makeToJSP(JSValue)
 makeToJSP(JSDict)
 
+# Note: this consumes `prop'.
 proc defineProperty(ctx: JSContext; this: JSValue; name: JSAtom;
     prop: JSValue; flags = cint(0)) =
   if JS_DefinePropertyValue(ctx, this, name, prop, flags) <= 0: