about summary refs log tree commit diff stats
path: root/src/js
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-02-24 04:47:07 +0100
committerbptato <nincsnevem662@gmail.com>2024-02-24 04:47:49 +0100
commitf8dbdbc8777cda6d45e8e05f9807319db4542572 (patch)
tree6ec03ba78634e6cecd551e0127883de9e819c7c6 /src/js
parentb1e2a9ce0c996de83b161a21d4e6ebb188a74699 (diff)
downloadchawan-f8dbdbc8777cda6d45e8e05f9807319db4542572.tar.gz
dom: add onload content attribute to body
Diffstat (limited to 'src/js')
-rw-r--r--src/js/javascript.nim1
-rw-r--r--src/js/opaque.nim8
-rw-r--r--src/js/tojs.nim20
3 files changed, 26 insertions, 3 deletions
diff --git a/src/js/javascript.nim b/src/js/javascript.nim
index 2327660d..b4c68729 100644
--- a/src/js/javascript.nim
+++ b/src/js/javascript.nim
@@ -171,6 +171,7 @@ proc free*(ctx: var JSContext) =
     JS_FreeValue(ctx, opaque.Object_prototype_valueOf)
     JS_FreeValue(ctx, opaque.Uint8Array_ctor)
     JS_FreeValue(ctx, opaque.Set_ctor)
+    JS_FreeValue(ctx, opaque.Function_ctor)
     for v in opaque.err_ctors:
       JS_FreeValue(ctx, v)
     GC_unref(opaque)
diff --git a/src/js/opaque.nim b/src/js/opaque.nim
index bae2bd5a..bff250d0 100644
--- a/src/js/opaque.nim
+++ b/src/js/opaque.nim
@@ -34,6 +34,7 @@ type
     Object_prototype_valueOf*: JSValue
     Uint8Array_ctor*: JSValue
     Set_ctor*: JSValue
+    Function_ctor*: JSValue
     err_ctors*: array[JSErrorEnum, JSValue]
     htmldda*: JSClassID # only one of these exists: document.all.
 
@@ -72,11 +73,14 @@ func newJSContextOpaque*(ctx: JSContext): JSContextOpaque =
       opaque.Object_prototype_valueOf = JS_GetPropertyStr(ctx, objproto, "valueOf")
       JS_FreeValue(ctx, objproto)
     block:
+      opaque.Uint8Array_ctor = JS_GetPropertyStr(ctx, global, "Uint8Array")
+      assert not JS_IsException(opaque.Uint8Array_ctor)
+    block:
       opaque.Set_ctor = JS_GetPropertyStr(ctx, global, "Set")
       assert not JS_IsException(opaque.Set_ctor)
     block:
-      opaque.Uint8Array_ctor = JS_GetPropertyStr(ctx, global, "Uint8Array")
-      assert not JS_IsException(opaque.Uint8Array_ctor)
+      opaque.Function_ctor = JS_GetPropertyStr(ctx, global, "Function")
+      assert not JS_IsException(opaque.Function_ctor)
     for e in JSErrorEnum:
       let s = $e
       let err = JS_GetPropertyStr(ctx, global, cstring(s))
diff --git a/src/js/tojs.nim b/src/js/tojs.nim
index 757552eb..746dccbb 100644
--- a/src/js/tojs.nim
+++ b/src/js/tojs.nim
@@ -120,6 +120,10 @@ proc defineProperty(ctx: JSContext, this: JSValue, name: string,
   if JS_DefinePropertyValueStr(ctx, this, cstring(name), prop, flags) <= 0:
     raise newException(Defect, "Failed to define property string: " & name)
 
+proc definePropertyC*(ctx: JSContext, this: JSValue, name: string,
+    prop: JSValue) =
+  ctx.defineProperty(this, name, prop, JS_PROP_CONFIGURABLE)
+
 proc defineProperty*[T](ctx: JSContext, this: JSValue, name: string, prop: T,
     flags = cint(0)) =
   defineProperty(ctx, this, name, toJS(ctx, prop), flags)
@@ -137,6 +141,18 @@ proc definePropertyCWE*[T](ctx: JSContext, this: JSValue, name: string,
     prop: T) =
   defineProperty(ctx, this, name, prop, JS_PROP_C_W_E)
 
+proc newFunction*(ctx: JSContext, args: openArray[string], body: string):
+    JSValue =
+  var paramList: seq[JSValue] = @[]
+  for arg in args:
+    paramList.add(toJS(ctx, arg))
+  paramList.add(toJS(ctx, body))
+  let fun = JS_CallConstructor(ctx, ctx.getOpaque().Function_ctor,
+    cint(paramList.len), addr paramList[0])
+  for param in paramList:
+    JS_FreeValue(ctx, param)
+  return fun
+
 proc toJS*(ctx: JSContext, s: cstring): JSValue =
   return JS_NewString(ctx, s)
 
@@ -218,7 +234,9 @@ proc toJS*[T](ctx: JSContext, s: set[T]): JSValue =
   var a = toJS(ctx, x)
   if JS_IsException(a):
     return a
-  return JS_CallConstructor(ctx, ctx.getOpaque().Set_ctor, 1, addr a)
+  let ret = JS_CallConstructor(ctx, ctx.getOpaque().Set_ctor, 1, addr a)
+  JS_FreeValue(ctx, a)
+  return ret
 
 proc toJS(ctx: JSContext, t: tuple): JSValue =
   let a = JS_NewArray(ctx)