about summary refs log tree commit diff stats
path: root/lib/monoucha0/monoucha/javascript.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2025-01-28 23:18:44 +0100
committerbptato <nincsnevem662@gmail.com>2025-01-28 23:31:25 +0100
commit77207b9c1780685a6f23be3672c41f8d26601c76 (patch)
tree1f732e1ea3bba06a8afabfef1521a1860699b9ed /lib/monoucha0/monoucha/javascript.nim
parentb557402db961ea6d367dca097dffeb05a2676ad7 (diff)
downloadchawan-77207b9c1780685a6f23be3672c41f8d26601c76.tar.gz
javascript: fix .jsget on JSValue
Since toJS doesn't create a new value, `node' must be dup'd if it is
a JSValue.

Also, forbid .jsset on JSValue as it was broken as well and it's not
obvious what the intention is when using it.  (Probably it should free
the old value, but did the user think of setting it? :P)

I can see why I made toJS not dup the value - to make returning
consistent with Nim's semantics - but it really makes things
confusing... implicitly behaving as lent might be better, albeit less
convenient.
Diffstat (limited to 'lib/monoucha0/monoucha/javascript.nim')
-rw-r--r--lib/monoucha0/monoucha/javascript.nim15
1 files changed, 10 insertions, 5 deletions
diff --git a/lib/monoucha0/monoucha/javascript.nim b/lib/monoucha0/monoucha/javascript.nim
index f6496e70..fc92ff29 100644
--- a/lib/monoucha0/monoucha/javascript.nim
+++ b/lib/monoucha0/monoucha/javascript.nim
@@ -1271,7 +1271,9 @@ proc registerGetters(stmts: NimNode; info: RegistryInfo;
           return JS_ThrowTypeError(ctx,
             "'%s' called on an object that is not an instance of %s", `fn`,
             `jsname`)
-        when typeof(arg_0.`node`) is object:
+        when arg0.`node` is JSValue:
+          return JS_DupValue(ctx, arg0.`node`)
+        elif arg_0.`node` is object:
           return toJSP(ctx, arg_0, arg_0.`node`)
         else:
           return toJS(ctx, arg_0.`node`)
@@ -1293,8 +1295,7 @@ proc registerSetters(stmts: NimNode; info: RegistryInfo;
     let fn = op.name
     let id = ident($bfSetter & "_" & tname & "_" & fn)
     stmts.add(quote do:
-      proc `id`(ctx: JSContext; this: JSValue; val: JSValue): JSValue
-          {.cdecl.} =
+      proc `id`(ctx: JSContext; this, val: JSValue): JSValue {.cdecl.} =
         when `t` is object:
           var arg_0: ptr `t`
         else:
@@ -1305,8 +1306,12 @@ proc registerSetters(stmts: NimNode; info: RegistryInfo;
             `jsname`)
         # We can't just set arg_0.`node` directly, or fromJS may damage it.
         var nodeVal: typeof(arg_0.`node`)
-        if ctx.fromJS(val, nodeVal).isNone:
-          return JS_EXCEPTION
+        when nodeVal is JSValue:
+          static:
+            error(".jsset is not supported on JSValue; use jsfset")
+        else:
+          if ctx.fromJS(val, nodeVal).isNone:
+            return JS_EXCEPTION
         arg_0.`node` = move(nodeVal)
         return JS_DupValue(ctx, val)
     )