about summary refs log tree commit diff stats
path: root/src/js
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-05-04 19:43:42 +0200
committerbptato <nincsnevem662@gmail.com>2024-05-04 20:06:58 +0200
commit63442e5f8be17631a91cee352e441f59daa2df0a (patch)
treed546f8c0bf6884464ace32f2a083ce5098e064b1 /src/js
parent970378356d0d7239b332baa37470455391b5e6e4 (diff)
downloadchawan-63442e5f8be17631a91cee352e441f59daa2df0a.tar.gz
client: make quit() actually quit, misc fixes
* unwind the QJS stack with an uncatchable exception when quit is called
* clean up JS references in JSRuntime free even when the Nim
  counterparts are still alive
* simplify some tests
Diffstat (limited to 'src/js')
-rw-r--r--src/js/domexception.nim8
-rw-r--r--src/js/error.nim74
-rw-r--r--src/js/javascript.nim9
-rw-r--r--src/js/tojs.nim2
4 files changed, 37 insertions, 56 deletions
diff --git a/src/js/domexception.nim b/src/js/domexception.nim
index f916fb59..39537b0a 100644
--- a/src/js/domexception.nim
+++ b/src/js/domexception.nim
@@ -38,11 +38,7 @@ type
 jsDestructor(DOMException)
 
 proc newDOMException*(message = ""; name = "Error"): DOMException {.jsctor.} =
-  return DOMException(
-    e: JS_DOM_EXCEPTION,
-    name: name,
-    message: message
-  )
+  return DOMException(e: jeDOMException, name: name, message: message)
 
 template errDOMException*(message, name: string): untyped =
   err(newDOMException(message, name))
@@ -54,4 +50,4 @@ func code(this: DOMException): uint16 {.jsfget.} =
   return NamesTable.getOrDefault(this.name, 0u16)
 
 proc addDOMExceptionModule*(ctx: JSContext) =
-  ctx.registerType(DOMException, JS_CLASS_ERROR, errid = opt(JS_DOM_EXCEPTION))
+  ctx.registerType(DOMException, JS_CLASS_ERROR, errid = opt(jeDOMException))
diff --git a/src/js/error.nim b/src/js/error.nim
index 860f4f80..b4101830 100644
--- a/src/js/error.nim
+++ b/src/js/error.nim
@@ -7,77 +7,53 @@ type
 
   JSErrorEnum* = enum
     # QuickJS internal errors
-    JS_EVAL_ERROR0 = "EvalError"
-    JS_RANGE_ERROR0 = "RangeError"
-    JS_REFERENCE_ERROR0 = "ReferenceError"
-    JS_SYNTAX_ERROR0 = "SyntaxError"
-    JS_TYPE_ERROR0 = "TypeError"
-    JS_URI_ERROR0 = "URIError"
-    JS_INTERNAL_ERROR0 = "InternalError"
-    JS_AGGREGATE_ERROR0 = "AggregateError"
+    jeEvalError = "EvalError"
+    jeRangeError = "RangeError"
+    jeReferenceError = "ReferenceError"
+    jeSyntaxError = "SyntaxError"
+    jeTypeError = "TypeError"
+    jeURIError = "URIError"
+    jeInternalError = "InternalError"
+    jeAggregateError = "AggregateError"
     # Chawan errors
-    JS_DOM_EXCEPTION = "DOMException"
+    jeDOMException = "DOMException"
 
   JSResult*[T] = Result[T, JSError]
 
 const QuickJSErrors* = [
-  JS_EVAL_ERROR0,
-  JS_RANGE_ERROR0,
-  JS_REFERENCE_ERROR0,
-  JS_SYNTAX_ERROR0,
-  JS_TYPE_ERROR0,
-  JS_URI_ERROR0,
-  JS_INTERNAL_ERROR0,
-  JS_AGGREGATE_ERROR0
+  jeEvalError,
+  jeRangeError,
+  jeReferenceError,
+  jeSyntaxError,
+  jeTypeError,
+  jeURIError,
+  jeInternalError,
+  jeAggregateError
 ]
 
 proc newEvalError*(message: string): JSError =
-  return JSError(
-    e: JS_EVAL_ERROR0,
-    message: message
-  )
+  return JSError(e: jeEvalError, message: message)
 
 proc newRangeError*(message: string): JSError =
-  return JSError(
-    e: JS_RANGE_ERROR0,
-    message: message
-  )
+  return JSError(e: jeRangeError, message: message)
 
 proc newReferenceError*(message: string): JSError =
-  return JSError(
-    e: JS_REFERENCE_ERROR0,
-    message: message
-  )
+  return JSError(e: jeReferenceError, message: message)
 
 proc newSyntaxError*(message: string): JSError =
-  return JSError(
-    e: JS_SYNTAX_ERROR0,
-    message: message
-  )
+  return JSError(e: jeSyntaxError, message: message)
 
 proc newTypeError*(message: string): JSError =
-  return JSError(
-    e: JS_TYPE_ERROR0,
-    message: message
-  )
+  return JSError(e: jeTypeError, message: message)
 
 proc newURIError*(message: string): JSError =
-  return JSError(
-    e: JS_URI_ERROR0,
-    message: message
-  )
+  return JSError(e: jeURIError, message: message)
 
 proc newInternalError*(message: string): JSError =
-  return JSError(
-    e: JS_INTERNAL_ERROR0,
-    message: message
-  )
+  return JSError(e: jeInternalError, message: message)
 
 proc newAggregateError*(message: string): JSError =
-  return JSError(
-    e: JS_AGGREGATE_ERROR0,
-    message: message
-  )
+  return JSError(e: jeAggregateError, message: message)
 
 template errTypeError*(message: string): untyped =
   err(newTypeError(message))
diff --git a/src/js/javascript.nim b/src/js/javascript.nim
index 2266a3c7..44c7ef06 100644
--- a/src/js/javascript.nim
+++ b/src/js/javascript.nim
@@ -180,6 +180,15 @@ proc free*(ctx: JSContext) =
 proc free*(rt: JSRuntime) =
   let opaque = rt.getOpaque()
   GC_unref(opaque)
+  var ps: seq[pointer] = @[]
+  for p in opaque.plist.values:
+    ps.add(p)
+  opaque.plist.clear()
+  for p in ps:
+    #TODO maybe finalize?
+    let val = JS_MKPTR(JS_TAG_OBJECT, p)
+    JS_SetOpaque(val, nil)
+    JS_FreeValueRT(rt, val)
   JS_FreeRuntime(rt)
   runtimes.del(runtimes.find(rt))
 
diff --git a/src/js/tojs.nim b/src/js/tojs.nim
index 7831a6a9..ea6f35f2 100644
--- a/src/js/tojs.nim
+++ b/src/js/tojs.nim
@@ -299,7 +299,7 @@ proc toJSNew*(ctx: JSContext; obj: ref object; ctor: JSValue): JSValue =
     GC_ref(obj)
   return val
 
-proc toJSNew[T, E](ctx: JSContext; opt: Result[T, E], ctor: JSValue): JSValue =
+proc toJSNew[T, E](ctx: JSContext; opt: Result[T, E]; ctor: JSValue): JSValue =
   if opt.isSome:
     when not (T is void):
       return toJSNew(ctx, opt.get, ctor)