about summary refs log tree commit diff stats
path: root/src/js/javascript.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-07-02 14:13:02 +0200
committerbptato <nincsnevem662@gmail.com>2023-07-02 14:13:02 +0200
commit341299a491183c7c99f98b0ed3ce13307ab20b17 (patch)
tree9d96d9b18284c3d35bd0bdca6afdd833a68a5b73 /src/js/javascript.nim
parentb1fde40677d0cfeb5a5c28bdd0aeca775b6aa0e1 (diff)
downloadchawan-341299a491183c7c99f98b0ed3ce13307ab20b17.tar.gz
Add platform object conversion to union type
And with that, implement Request constructor with a Request init.
Diffstat (limited to 'src/js/javascript.nim')
-rw-r--r--src/js/javascript.nim22
1 files changed, 19 insertions, 3 deletions
diff --git a/src/js/javascript.nim b/src/js/javascript.nim
index 222e3ca5..02d22903 100644
--- a/src/js/javascript.nim
+++ b/src/js/javascript.nim
@@ -315,6 +315,10 @@ func isInstanceOf*(ctx: JSContext, val: JSValue, class: static string): bool =
       break
   return found
 
+func isPlatformObject(ctx: JSContext, val: JSValue): bool =
+  let classid = JS_GetClassID(val)
+  return classid in ctx.getOpaque().parents
+
 proc setProperty*(ctx: JSContext, val: JSValue, name: string, prop: JSValue) =
   if JS_SetPropertyStr(ctx, val, cstring(name), prop) <= 0:
     raise newException(Defect, "Failed to set property string: " & name)
@@ -1256,8 +1260,9 @@ proc addUnionParam0(gen: var JSFuncGenerator, tt: NimNode, s: NimNode, val: NimN
   var tableg = none(NimNode)
   var seqg = none(NimNode)
   var numg = none(NimNode)
+  var objg = none(NimNode)
   var hasString = false
-  var hasJSObject = false
+  var hasJSValue = false
   var hasBoolean = false
   let ev = gen.errval
   let dl = gen.dielabel
@@ -1269,14 +1274,25 @@ proc addUnionParam0(gen: var JSFuncGenerator, tt: NimNode, s: NimNode, val: NimN
     elif g == string.getTypeInst():
       hasString = true
     elif g == JSValue.getTypeInst():
-      hasJSObject = true
+      hasJSValue = true
     elif g == bool.getTypeInst():
       hasBoolean = true
     elif g == int.getTypeInst(): #TODO should be SomeNumber
       numg = some(g)
+    elif g.getTypeInst().getTypeImpl().kind == nnkRefTy:
+      # Assume it's ref object.
+      objg = some(g)
     else:
       error("Type not supported yet")
 
+  # 5. If V is a platform object, then:
+  if objg.isSome:
+    let query = quote do:
+      isPlatformObject(ctx, `val`)
+    let t = objg.get
+    gen.addUnionParamBranch(query, quote do:
+      let `s` = fromJS_or_die(`t`, ctx, `val`, `ev`, `dl`),
+      fallback)
   # 10. If Type(V) is Object, then:
   # Sequence:
   if seqg.issome:
@@ -1297,7 +1313,7 @@ proc addUnionParam0(gen: var JSFuncGenerator, tt: NimNode, s: NimNode, val: NimN
       fallback)
   # Object (JSObject variant):
   #TODO non-JS objects (i.e. ref object)
-  if hasJSObject:
+  if hasJSValue:
     let query = quote do:
       JS_IsObject(`val`)
     gen.addUnionParamBranch(query, quote do: