about summary refs log tree commit diff stats
path: root/lib/monoucha0/monoucha/javascript.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2025-02-02 17:02:09 +0100
committerbptato <nincsnevem662@gmail.com>2025-02-02 18:34:44 +0100
commit50d013362893f3f3a0f2a1d232f73508e3933fe0 (patch)
tree23cf7f67bb1196f17e296bb275190badd698e4e9 /lib/monoucha0/monoucha/javascript.nim
parent20eb8b870b4062e50e473e959d2af964c33f4e6a (diff)
downloadchawan-50d013362893f3f3a0f2a1d232f73508e3933fe0.tar.gz
javascript: derive ctor functions from parent ctor functions
no idea why JS_NewCFunction3 is not public...
Diffstat (limited to 'lib/monoucha0/monoucha/javascript.nim')
-rw-r--r--lib/monoucha0/monoucha/javascript.nim12
1 files changed, 9 insertions, 3 deletions
diff --git a/lib/monoucha0/monoucha/javascript.nim b/lib/monoucha0/monoucha/javascript.nim
index 1a32e315..23a6ed35 100644
--- a/lib/monoucha0/monoucha/javascript.nim
+++ b/lib/monoucha0/monoucha/javascript.nim
@@ -302,6 +302,13 @@ proc newProtoFromParentClass(ctx: JSContext; parent: JSClassID): JSValue =
     return proto
   return JS_NewObject(ctx)
 
+proc newCtorFunFromParentClass(ctx: JSContext; ctor: JSCFunction;
+    className: cstring; parent: JSClassID): JSValue =
+  if parent != 0:
+    return JS_NewCFunction3(ctx, ctor, className, 0, JS_CFUNC_constructor,
+      0, ctx.getOpaque().ctors[parent])
+  return JS_NewCFunction2(ctx, ctor, className, 0, JS_CFUNC_constructor, 0)
+
 func newJSClass*(ctx: JSContext; cdef: JSClassDefConst; tname: cstring;
     nimt: pointer; ctor: JSCFunction; funcs: JSFunctionList; parent: JSClassID;
     asglobal: bool; nointerface: bool; finalizer: JSFinalizerFunction;
@@ -362,8 +369,7 @@ func newJSClass*(ctx: JSContext; cdef: JSClassDefConst; tname: cstring;
       JS_SetPropertyFunctionList(ctx, global, ufp,
         cint(ctxOpaque.unforgeable[int(result)].len))
   JS_FreeValue(ctx, news)
-  let jctor = JS_NewCFunction2(ctx, ctor, cstring($cdef.class_name), 0,
-    JS_CFUNC_constructor, 0)
+  let jctor = ctx.newCtorFunFromParentClass(ctor, cdef.class_name, parent)
   if staticfuns.len > 0:
     rtOpaque.flist.add(@staticfuns)
     let fp0 = addr rtOpaque.flist[^1][0]
@@ -371,7 +377,7 @@ func newJSClass*(ctx: JSContext; cdef: JSClassDefConst; tname: cstring;
     JS_SetPropertyFunctionList(ctx, jctor, fp, cint(staticfuns.len))
   JS_SetConstructor(ctx, jctor, proto)
   if errid.isSome:
-    ctx.getOpaque().errCtorRefs[errid.get] = JS_DupValue(ctx, jctor)
+    ctxOpaque.errCtorRefs[errid.get] = JS_DupValue(ctx, jctor)
   while ctxOpaque.ctors.len <= int(result):
     ctxOpaque.ctors.add(JS_UNDEFINED)
   ctxOpaque.ctors[result] = JS_DupValue(ctx, jctor)