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-04 16:55:56 +0200
committerbptato <nincsnevem662@gmail.com>2023-07-04 16:58:19 +0200
commit01027f1749c80ce6def8546cb4ee43a5db064c1e (patch)
treea02d516b4361a4238a9f54cf02258ea658e15a29 /src/js/javascript.nim
parentc355c176863a930d2107254198749f420f272c06 (diff)
downloadchawan-01027f1749c80ce6def8546cb4ee43a5db064c1e.tar.gz
Event: add some properties, js: add defineConsts
Diffstat (limited to 'src/js/javascript.nim')
-rw-r--r--src/js/javascript.nim19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/js/javascript.nim b/src/js/javascript.nim
index 1f213f64..3b1661d2 100644
--- a/src/js/javascript.nim
+++ b/src/js/javascript.nim
@@ -192,7 +192,8 @@ func findClass*(ctx: JSContext, class: string): Option[JSClassID] =
     return some(opaque.creg[class])
   return none(JSClassID)
 
-func newJSCFunction*(ctx: JSContext, name: string, fun: JSCFunction, argc: int = 0, proto = JS_CFUNC_generic, magic = 0): JSValue =
+func newJSCFunction*(ctx: JSContext, name: string, fun: JSCFunction,
+    argc: int = 0, proto = JS_CFUNC_generic, magic = 0): JSValue =
   return JS_NewCFunction2(ctx, fun, cstring(name), cint(argc), proto, cint(magic))
 
 proc free*(ctx: var JSContext) =
@@ -717,6 +718,7 @@ proc toJS(ctx: JSContext, promise: EmptyPromise): JSValue
 proc toJSRefObj(ctx: JSContext, obj: ref object): JSValue
 proc toJS*(ctx: JSContext, obj: ref object): JSValue
 proc toJS*(ctx: JSContext, err: JSError): JSValue
+proc toJS*(ctx: JSContext, f: JSCFunction): JSValue
 
 #TODO varargs
 proc fromJSFunction1[T, U](ctx: JSContext, val: JSValue):
@@ -1039,6 +1041,21 @@ proc toJS*(ctx: JSContext, err: JSError): JSValue =
   let ctor = ctx.getOpaque().err_ctors[err.e]
   return JS_CallConstructor(ctx, ctor, 1, addr msg)
 
+proc toJS*(ctx: JSContext, f: JSCFunction): JSValue =
+  return ctx.newJSCFunction("", f)
+
+proc defineConsts*[T](ctx: JSContext, classid: JSClassID,
+    consts: static openarray[(string, T)]) =
+  let proto = ctx.getOpaque().ctors[classid]
+  for (k, v) in consts:
+    ctx.defineProperty(proto, k, v)
+
+proc defineConsts*(ctx: JSContext, classid: JSClassID,
+    consts: typedesc[enum], astype: typedesc) =
+  let proto = ctx.getOpaque().ctors[classid]
+  for e in consts:
+    ctx.defineProperty(proto, $e, astype(e))
+
 type
   JSFuncGenerator = object
     t: BoundFunctionType