From 31b359224632fd4e3c2aac667483ed249121efaa Mon Sep 17 00:00:00 2001 From: bptato Date: Sun, 21 Apr 2024 19:10:01 +0200 Subject: js: override default toString tag of global Some JS modules use this to check if they are running in a browser. --- src/js/javascript.nim | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/js') diff --git a/src/js/javascript.nim b/src/js/javascript.nim index f2f72979..8a1a0794 100644 --- a/src/js/javascript.nim +++ b/src/js/javascript.nim @@ -286,7 +286,8 @@ func newJSClass*(ctx: JSContext; cdef: JSClassDefConst; tname: string; doAssert JS_SetProperty(ctx, proto, ctxOpaque.sym_refs[ITERATOR], val) == 1 let news = JS_NewAtomString(ctx, cdef.class_name) doAssert not JS_IsException(news) - ctx.definePropertyC(proto, ctxOpaque.sym_refs[TO_STRING_TAG], news) + ctx.definePropertyC(proto, ctxOpaque.sym_refs[TO_STRING_TAG], + JS_DupValue(ctx, news)) JS_SetClassProto(ctx, result, proto) ctx.addClassUnforgeable(proto, result, parent, unforgeable) if asglobal: @@ -294,6 +295,8 @@ func newJSClass*(ctx: JSContext; cdef: JSClassDefConst; tname: string; assert ctxOpaque.gclaz == "" ctxOpaque.gclaz = tname ctxOpaque.gparent = parent + ctx.definePropertyC(global, ctxOpaque.sym_refs[TO_STRING_TAG], + JS_DupValue(ctx, news)) if JS_SetPrototype(ctx, global, proto) != 1: raise newException(Defect, "Failed to set global prototype: " & $cdef.class_name) @@ -301,6 +304,7 @@ func newJSClass*(ctx: JSContext; cdef: JSClassDefConst; tname: string; ctxOpaque.unforgeable.withValue(result, uf): JS_SetPropertyFunctionList(ctx, global, addr uf[][0], cint(uf[].len)) JS_FreeValue(ctx, global) + JS_FreeValue(ctx, news) let jctor = ctx.newJSCFunction($cdef.class_name, ctor, 0, JS_CFUNC_constructor) if staticfuns.len > 0: -- cgit 1.4.1-2-gfad0