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-05 18:56:45 +0200
committerbptato <nincsnevem662@gmail.com>2023-06-05 18:56:45 +0200
commit9316348a1fbd73c1b9f696cfdb06ca5e08175922 (patch)
treeffa638131f28ec081154826ba035b02fa386f67a /src/js/javascript.nim
parent4981c6f2d365363a78a4eb3cdea3776a19dd41cb (diff)
downloadchawan-9316348a1fbd73c1b9f696cfdb06ca5e08175922.tar.gz
js: fix string regression, throw on invalid ctor
Diffstat (limited to 'src/js/javascript.nim')
-rw-r--r--src/js/javascript.nim8
1 files changed, 5 insertions, 3 deletions
diff --git a/src/js/javascript.nim b/src/js/javascript.nim
index 0d86bd71..6b83517e 100644
--- a/src/js/javascript.nim
+++ b/src/js/javascript.nim
@@ -227,9 +227,11 @@ proc setInterruptHandler*(rt: JSRuntime, cb: JSInterruptHandler, opaque: pointer
 func toString*(ctx: JSContext, val: JSValue): Option[string] =
   var plen: csize_t
   let outp = JS_ToCStringLen(ctx, addr plen, val) # cstring
-  if outp != nil and plen != 0:
+  if outp != nil:
     var ret = newString(plen)
-    copyMem(addr ret[0], outp, plen)
+    if plen != 0:
+      prepareMutation(ret)
+      copyMem(addr ret[0], outp, plen)
     result = some(ret)
     JS_FreeCString(ctx, outp)
 
@@ -1413,7 +1415,7 @@ macro jsctor*(fun: typed) =
   gen.jsCallAndRet = quote do:
     block `dl`:
       return ctx.toJS(`jfcl`)
-    return JS_UNDEFINED
+    return JS_ThrowTypeError(ctx, "Invalid parameters passed to constructor")
   discard gen.newJSProc(getJSParams())
   gen.registerConstructor()
   result = newStmtList(fun)