about summary refs log tree commit diff stats
path: root/src/js/tojs.nim
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/tojs.nim
parentb1e2a9ce0c996de83b161a21d4e6ebb188a74699 (diff)
downloadchawan-f8dbdbc8777cda6d45e8e05f9807319db4542572.tar.gz
dom: add onload content attribute to body
Diffstat (limited to 'src/js/tojs.nim')
-rw-r--r--src/js/tojs.nim20
1 files changed, 19 insertions, 1 deletions
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)