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-19 22:18:00 +0200
committerbptato <nincsnevem662@gmail.com>2023-06-19 22:18:00 +0200
commit08f548900f9a24051ec0d5d7fb59b7d5982d4d77 (patch)
tree0dfb77b90ea58a01ccaeb258d11d1b4ae2164a0d /src/js/javascript.nim
parent251b94699e0be4ec9f25257e6200fb2a1285796c (diff)
downloadchawan-08f548900f9a24051ec0d5d7fb59b7d5982d4d77.tar.gz
Fix fromJS Opt bugs
Diffstat (limited to 'src/js/javascript.nim')
-rw-r--r--src/js/javascript.nim19
1 files changed, 10 insertions, 9 deletions
diff --git a/src/js/javascript.nim b/src/js/javascript.nim
index c1028537..d304d260 100644
--- a/src/js/javascript.nim
+++ b/src/js/javascript.nim
@@ -471,7 +471,7 @@ macro fromJSTupleBody(a: tuple) =
       if JS_IsException(valueVal):
         return err()
       defer: JS_FreeValue(ctx, valueVal)
-      let genericRes = fromJS[typeof(result.get[`i`])](ctx, valueVal)
+      let genericRes = fromJS[typeof(`a`[`i`])](ctx, valueVal)
       if genericRes.isErr: # exception
         return err()
       `a`[`i`] = genericRes.get
@@ -537,7 +537,7 @@ proc fromJSSeq[T](ctx: JSContext, val: JSValue): Opt[seq[T]] =
   if JS_IsException(next_method):
     return err()
   defer: JS_FreeValue(ctx, next_method)
-  result = ok(newSeq[T]())
+  var s = newSeq[T]()
   while true:
     let next = JS_Call(ctx, next_method, it, 0, nil)
     if JS_IsException(next):
@@ -556,10 +556,11 @@ proc fromJSSeq[T](ctx: JSContext, val: JSValue): Opt[seq[T]] =
     if JS_IsException(valueVal):
       return err()
     defer: JS_FreeValue(ctx, valueVal)
-    let genericRes = fromJS[typeof(result.get[0])](ctx, valueVal)
+    let genericRes = fromJS[typeof(s[0])](ctx, valueVal)
     if genericRes.isnone: # exception
       return err()
-    result.get.add(genericRes.get)
+    s.add(genericRes.get)
+  return ok(s)
 
 proc fromJSSet[T](ctx: JSContext, val: JSValue): Opt[set[T]] =
   let itprop = JS_GetProperty(ctx, val, ctx.getOpaque().sym_iterator)
@@ -575,7 +576,6 @@ proc fromJSSet[T](ctx: JSContext, val: JSValue): Opt[set[T]] =
     return err()
   defer: JS_FreeValue(ctx, next_method)
   var s: set[T]
-  result = some(s)
   while true:
     let next = JS_Call(ctx, next_method, it, 0, nil)
     if JS_IsException(next):
@@ -594,10 +594,11 @@ proc fromJSSet[T](ctx: JSContext, val: JSValue): Opt[set[T]] =
     if JS_IsException(valueVal):
       return err()
     defer: JS_FreeValue(ctx, valueVal)
-    let genericRes = fromJS[typeof(result.get.items)](ctx, valueVal)
+    let genericRes = fromJS[typeof(s.items)](ctx, valueVal)
     if genericRes.isnone: # exception
       return err()
-    result.get.incl(genericRes.get)
+    s.incl(genericRes.get)
+  return ok(s)
 
 proc fromJSTable[A, B](ctx: JSContext, val: JSValue): Opt[Table[A, B]] =
   var ptab: ptr JSPropertyEnum
@@ -625,7 +626,7 @@ proc fromJSTable[A, B](ctx: JSContext, val: JSValue): Opt[Table[A, B]] =
     let vn = fromJS[B](ctx, v)
     if vn.isErr: # exception
       return err()
-    result.get[kn.get] = vn.get
+    res[kn.get] = vn.get
   return ok(res)
 
 proc toJS*(ctx: JSContext, s: cstring): JSValue
@@ -714,7 +715,7 @@ proc fromJS*[T](ctx: JSContext, val: JSValue): Opt[T] =
     if JS_IsUndefined(val):
       #TODO what about null?
       return err()
-    let res = fromJS[typeof(result.get.get)](ctx, val)
+    let res = fromJS[typeof(T.valType)](ctx, val)
     if res.isNone:
       return err()
     return ok(res)