about summary refs log tree commit diff stats
path: root/src/js/javascript.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-05-21 13:28:33 +0200
committerbptato <nincsnevem662@gmail.com>2023-05-21 13:28:33 +0200
commitd09319640c4b1e24b937d1fc37a3f8b82052e612 (patch)
tree45631e7ed2c23ce4a4217bfe2037cba2ba873231 /src/js/javascript.nim
parent7c612d65be345fd1ef156f95bb033d9bbf8aece8 (diff)
downloadchawan-d09319640c4b1e24b937d1fc37a3f8b82052e612.tar.gz
Rewrite new Request binding
Still far from perfect, but now at least it has a bit more to do
with what the standard mandates.
Diffstat (limited to 'src/js/javascript.nim')
-rw-r--r--src/js/javascript.nim27
1 files changed, 16 insertions, 11 deletions
diff --git a/src/js/javascript.nim b/src/js/javascript.nim
index 263fa8aa..438858ef 100644
--- a/src/js/javascript.nim
+++ b/src/js/javascript.nim
@@ -80,6 +80,10 @@ type
 
   JSFunctionList* = openArray[JSCFunctionListEntry]
 
+  JSObject* = object
+    ctx*: JSContext
+    val*: JSValue
+
 func getOpaque*(ctx: JSContext): JSContextOpaque =
   return cast[JSContextOpaque](JS_GetContextOpaque(ctx))
 
@@ -652,6 +656,8 @@ proc fromJS*[T](ctx: JSContext, val: JSValue): Option[T] =
       return none(T)
   elif T is JSValue:
     return some(val)
+  elif T is JSObject:
+    return some(JSObject(ctx: ctx, val: val))
   elif T is object:
     doAssert false, "Dictionary case has not been implemented yet!"
     #TODO TODO TODO implement dictionary case
@@ -751,7 +757,7 @@ proc toJS*(ctx: JSContext, obj: ref object): JSValue =
   return jsObj
 
 proc toJS(ctx: JSContext, e: enum): JSValue =
-  return toJS(ctx, int(e))
+  return toJS(ctx, $e)
 
 proc toJS(ctx: JSContext, j: JSValue): JSValue =
   return j
@@ -996,6 +1002,14 @@ proc addUnionParamBranch(gen: var JSFuncGenerator, query, newBranch: NimNode, fa
     gen.jsFunCallLists[i] = oldBranch
   gen.newBranchList.add(newBranch)
 
+func isSequence*(ctx: JSContext, o: JSValue): bool =
+  if not JS_IsObject(o):
+    return false
+  let prop = JS_GetProperty(ctx, o, ctx.getOpaque().sym_iterator)
+  # prop can't be exception (throws_ref_error is 0 and tag is object)
+  result = not JS_IsUndefined(prop)
+  JS_FreeValue(ctx, prop)
+
 proc addUnionParam0(gen: var JSFuncGenerator, tt: NimNode, s: NimNode, val: NimNode, fallback: NimNode = nil) =
   # Union types.
   #TODO quite a few types are still missing.
@@ -1028,16 +1042,7 @@ proc addUnionParam0(gen: var JSFuncGenerator, tt: NimNode, s: NimNode, val: NimN
   # Sequence:
   if seqg.issome:
     let query = quote do:
-      (
-        let o = `val`
-        JS_IsObject(o) and (
-          let prop = JS_GetProperty(ctx, o, ctx.getOpaque().sym_iterator)
-          # prop can't be exception (throws_ref_error is 0 and tag is object)
-          let ret = not JS_IsUndefined(prop)
-          JS_FreeValue(ctx, prop)
-          ret
-        )
-      )
+      isSequence(ctx, `val`)
     let a = seqg.get[1]
     gen.addUnionParamBranch(query, quote do:
       let `s` = fromJS_or_die(seq[`a`], ctx, `val`, `ev`, `dl`),