diff options
-rw-r--r-- | lib/monoucha0/monoucha/javascript.nim | 12 | ||||
-rw-r--r-- | lib/monoucha0/monoucha/qjs/quickjs.c | 7 | ||||
-rw-r--r-- | lib/monoucha0/monoucha/qjs/quickjs.h | 4 | ||||
-rw-r--r-- | lib/monoucha0/monoucha/quickjs.nim | 3 | ||||
-rw-r--r-- | src/html/script.nim | 5 | ||||
-rw-r--r-- | test/js/text.html | 1 |
6 files changed, 22 insertions, 10 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) diff --git a/lib/monoucha0/monoucha/qjs/quickjs.c b/lib/monoucha0/monoucha/qjs/quickjs.c index 12dc51e1..e8a5f6fc 100644 --- a/lib/monoucha0/monoucha/qjs/quickjs.c +++ b/lib/monoucha0/monoucha/qjs/quickjs.c @@ -5180,10 +5180,9 @@ static int js_method_set_properties(JSContext *ctx, JSValue func_obj, /* Note: at least 'length' arguments will be readable in 'argv' */ /* `name` may be NULL, pure ASCII or UTF-8 encoded */ -static JSValue JS_NewCFunction3(JSContext *ctx, JSCFunction *func, - const char *name, - int length, JSCFunctionEnum cproto, int magic, - JSValue proto_val) +JSValue JS_NewCFunction3(JSContext *ctx, JSCFunction *func, const char *name, + int length, JSCFunctionEnum cproto, int magic, + JSValue proto_val) { JSValue func_obj; JSObject *p; diff --git a/lib/monoucha0/monoucha/qjs/quickjs.h b/lib/monoucha0/monoucha/qjs/quickjs.h index 5ac75942..ad1cfa40 100644 --- a/lib/monoucha0/monoucha/qjs/quickjs.h +++ b/lib/monoucha0/monoucha/qjs/quickjs.h @@ -954,6 +954,10 @@ typedef union JSCFunctionType { JS_EXTERN JSValue JS_NewCFunction2(JSContext *ctx, JSCFunction *func, const char *name, int length, JSCFunctionEnum cproto, int magic); +JS_EXTERN JSValue JS_NewCFunction3(JSContext *ctx, JSCFunction *func, + const char *name, + int length, JSCFunctionEnum cproto, int magic, + JSValue proto_val); JS_EXTERN JSValue JS_NewCFunctionData(JSContext *ctx, JSCFunctionData *func, int length, int magic, int data_len, JSValue *data); diff --git a/lib/monoucha0/monoucha/quickjs.nim b/lib/monoucha0/monoucha/quickjs.nim index ea70672c..81b620ce 100644 --- a/lib/monoucha0/monoucha/quickjs.nim +++ b/lib/monoucha0/monoucha/quickjs.nim @@ -780,6 +780,9 @@ proc JS_LoadModule*(ctx: JSContext; basename, filename: cstringConst): JSValue # C function definition proc JS_NewCFunction2*(ctx: JSContext; cfunc: JSCFunction; name: cstring; length: cint; proto: JSCFunctionEnum; magic: cint): JSValue +proc JS_NewCFunction3*(ctx: JSContext; cfunc: JSCFunction; name: cstring; + length: cint; proto: JSCFunctionEnum; magic: cint; proto_val: JSValue): + JSValue proc JS_NewCFunctionData*(ctx: JSContext; cfunc: JSCFunctionData; length, magic, data_len: cint; data: ptr UncheckedArray[JSValue]): JSValue proc JS_NewCFunction*(ctx: JSContext; cfunc: JSCFunction; name: cstring; diff --git a/src/html/script.nim b/src/html/script.nim index 65601d9c..6ee89da2 100644 --- a/src/html/script.nim +++ b/src/html/script.nim @@ -175,12 +175,11 @@ func uninitIfNull*(val: JSValue): JSValue = proc defineConsts*(ctx: JSContext; classid: JSClassID; consts: typedesc[enum]) = let proto = JS_GetClassProto(ctx, classid) - let ctorProto = JS_GetPrototype(ctx, ctx.getOpaque().ctors[classid]) + let ctor = ctx.getOpaque().ctors[classid] #TODO it should be enough to define on the proto only, but apparently # it isn't... for e in consts: let s = $e ctx.definePropertyE(proto, s, uint16(e)) - ctx.definePropertyE(ctorProto, s, uint16(e)) - JS_FreeValue(ctx, ctorProto) + ctx.definePropertyE(ctor, s, uint16(e)) JS_FreeValue(ctx, proto) diff --git a/test/js/text.html b/test/js/text.html index c2688bfb..020b1c1c 100644 --- a/test/js/text.html +++ b/test/js/text.html @@ -3,5 +3,6 @@ <script src=asserts.js></script> <script> assertEquals(new Text("data").ownerDocument, document); +assertEquals(Text.__proto__, CharacterData); document.getElementById("x").textContent = "Success"; </script> |