diff options
| author | bptato <nincsnevem662@gmail.com> | 2025-02-19 18:54:44 +0100 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2025-02-19 19:35:07 +0100 |
| commit | 3f663454031657610bddceb169cd881754ec232a (patch) | |
| tree | ae088232c3bdbe9f74cfaf19a5d9b99e0dfc8131 | |
| parent | b65993b77789ddbde72f65db648ab993a5097121 (diff) | |
| download | chawan-3f663454031657610bddceb169cd881754ec232a.tar.gz | |
fromjs, javascript: optimize out class name registry
Instead of hashing the class name for isInstanceOf, we now just reuse
the Nim type pointer -> JSClassID map, which should be more efficient.
This removes getClass and hasClass; these can be replaced by just
reusing the class ID returned from registerType.
| -rw-r--r-- | lib/monoucha0/monoucha/fromjs.nim | 27 | ||||
| -rw-r--r-- | lib/monoucha0/monoucha/javascript.nim | 21 | ||||
| -rw-r--r-- | lib/monoucha0/monoucha/jsopaque.nim | 1 | ||||
| -rw-r--r-- | src/html/dom.nim | 20 | ||||
| -rw-r--r-- | src/html/env.nim | 26 | ||||
| -rw-r--r-- | src/html/event.nim | 6 | ||||
| -rw-r--r-- | src/html/performance.nim | 3 | ||||
| -rw-r--r-- | src/html/xmlhttprequest.nim | 5 | ||||
| -rw-r--r-- | src/local/client.nim | 12 |
9 files changed, 55 insertions, 66 deletions
diff --git a/lib/monoucha0/monoucha/fromjs.nim b/lib/monoucha0/monoucha/fromjs.nim index 7cf5db9a..ece36119 100644 --- a/lib/monoucha0/monoucha/fromjs.nim +++ b/lib/monoucha0/monoucha/fromjs.nim @@ -7,6 +7,7 @@ import jsopaque import jstypes import optshim import quickjs +import tojs proc fromJS*(ctx: JSContext; val: JSValue; res: out string): Opt[void] proc fromJS*(ctx: JSContext; val: JSValue; res: out int32): Opt[void] @@ -31,10 +32,9 @@ proc fromJS*(ctx: JSContext; val: JSValue; res: out JSArrayBufferView): Opt[void] proc fromJS*(ctx: JSContext; val: JSValue; res: out JSValue): Opt[void] -func isInstanceOf*(ctx: JSContext; val: JSValue; class: cstring): bool = +func isInstanceOf*(ctx: JSContext; val: JSValue; tclassid: JSClassID): bool = let ctxOpaque = ctx.getOpaque() var classid = JS_GetClassID(val) - let tclassid = ctxOpaque.creg.getOrDefault(class, JS_CLASS_OBJECT) if classid == JS_CLASS_OBJECT: let p0 = JS_VALUE_GET_PTR(ctxOpaque.global) let p1 = JS_VALUE_GET_PTR(val) @@ -328,7 +328,7 @@ proc fromJS*[T: enum](ctx: JSContext; val: JSValue; res: out T): Opt[void] = res = default(T) return err() -proc fromJS(ctx: JSContext; val: JSValue; t: cstring; res: out pointer): +proc fromJS(ctx: JSContext; val: JSValue; nimt: pointer; res: out pointer): Opt[void] = if not JS_IsObject(val): if not JS_IsException(val): @@ -336,25 +336,34 @@ proc fromJS(ctx: JSContext; val: JSValue; t: cstring; res: out pointer): res = nil return err() let p = JS_GetOpaque(val, JS_GetClassID(val)) - if p == nil or not ctx.isInstanceOf(val, t): - JS_ThrowTypeError(ctx, "%s expected", t) + let ctxOpaque = ctx.getOpaque() + let tclassid = ctxOpaque.typemap.getOrDefault(nimt, JS_CLASS_OBJECT) + if p == nil or not ctx.isInstanceOf(val, tclassid): res = nil + let proto = JS_GetClassProto(ctx, tclassid) + let name = JS_GetProperty(ctx, proto, ctxOpaque.symRefs[jsyToStringTag]) + JS_FreeValue(ctx, proto) + defer: JS_FreeValue(ctx, name) + var s: string + if ctx.fromJS(name, s).isNone: + return err() + JS_ThrowTypeError(ctx, "%s expected", cstring(s)) return err() res = p return ok() proc fromJS*[T](ctx: JSContext; val: JSValue; res: out ptr T): Opt[void] = + let nimt = getTypePtr(T) var x: pointer - const tname = cstring($T) - ?ctx.fromJS(val, tname, x) + ?ctx.fromJS(val, nimt, x) res = cast[ptr T](x) return ok() proc fromJS*[T: ref object](ctx: JSContext; val: JSValue; res: out T): Opt[void] = + let nimt = getTypePtr(T) var x: pointer - const tname = cstring($T) - ?ctx.fromJS(val, tname, x) + ?ctx.fromJS(val, nimt, x) res = cast[T](x) return ok() diff --git a/lib/monoucha0/monoucha/javascript.nim b/lib/monoucha0/monoucha/javascript.nim index 75b1548a..1f921d5d 100644 --- a/lib/monoucha0/monoucha/javascript.nim +++ b/lib/monoucha0/monoucha/javascript.nim @@ -191,19 +191,6 @@ proc newJSContext*(rt: JSRuntime): JSContext = JS_SetContextOpaque(ctx, cast[pointer](opaque)) return ctx -func getClass*(ctx: JSContext; class: cstring): JSClassID = - ## Get the class ID of the registered class `class'. - ## Note: this uses the Nim type's name, **not** the JS type's name. - try: - return ctx.getOpaque().creg[class] - except KeyError: - raise newException(Defect, "Class does not exist") - -func hasClass*(ctx: JSContext; class: cstring): bool = - ## Check if `class' is registered. - ## Note: this uses the Nim type's name, **not** the JS type's name. - return class in ctx.getOpaque().creg - proc free*(ctx: JSContext) = ## Free the JSContext and associated resources. ## Note: this is not an alias of `JS_FreeContext`; `free` also frees various @@ -311,8 +298,8 @@ proc newCtorFunFromParentClass(ctx: JSContext; ctor: JSCFunction; 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; +func newJSClass*(ctx: JSContext; cdef: JSClassDefConst; nimt: pointer; + ctor: JSCFunction; funcs: JSFunctionList; parent: JSClassID; asglobal, nointerface, ishtmldda: bool; finalizer: JSFinalizerFunction; namespace: JSValue; errid: Opt[JSErrorEnum]; unforgeable, staticfuns: JSFunctionList): JSClassID @@ -327,7 +314,6 @@ func newJSClass*(ctx: JSContext; cdef: JSClassDefConst; tname: cstring; $cdef.class_name) ctxOpaque.typemap[nimt] = result rtOpaque.inverseTypemap[result] = nimt - ctxOpaque.creg[tname] = result if ctxOpaque.parents.len <= int(result): ctxOpaque.parents.setLen(int(result) + 1) ctxOpaque.parents[result] = parent @@ -1509,12 +1495,11 @@ macro registerType*(ctx: JSContext; t: typed; parent: JSClassID = 0; endstmts.bindEndStmts(info) let tabList = info.tabList let finName = info.finName - let tname = info.tname let unforgeable = info.tabUnforgeable let staticfuns = info.tabStatic let global = asglobal and not globalparent endstmts.add(quote do: - `ctx`.newJSClass(classDef, `tname`, getTypePtr(`t`), `sctr`, `tabList`, + `ctx`.newJSClass(classDef, getTypePtr(`t`), `sctr`, `tabList`, `parent`, `global`, `nointerface`, `ishtmldda`, `finName`, `namespace`, `errid`, `unforgeable`, `staticfuns`) ) diff --git a/lib/monoucha0/monoucha/jsopaque.nim b/lib/monoucha0/monoucha/jsopaque.nim index 7b90329a..e7920ea2 100644 --- a/lib/monoucha0/monoucha/jsopaque.nim +++ b/lib/monoucha0/monoucha/jsopaque.nim @@ -27,7 +27,6 @@ type jsvFunction = "Function" JSContextOpaque* = ref object - creg*: Table[cstring, JSClassID] typemap*: Table[pointer, JSClassID] ctors*: seq[JSValue] # JSClassID -> JSValue parents*: seq[JSClassID] # JSClassID -> JSClassID diff --git a/src/html/dom.nim b/src/html/dom.nim index 93278cec..5d9acbfa 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -4351,11 +4351,9 @@ const (ReflectTable, TagReflectMap, ReflectAllStartIndex) = (func(): ( proc jsReflectGet(ctx: JSContext; this: JSValue; magic: cint): JSValue {.cdecl.} = let entry = ReflectTable[uint16(magic)] - let op = this.getOpaque() - if unlikely(not ctx.isInstanceOf(this, "Element") or op == nil): - return JS_ThrowTypeError(ctx, - "Reflected getter called on a value that is not an element") - let element = cast[Element](op) + var element: Element + if ctx.fromJS(this, element).isNone: + return JS_EXCEPTION if element.tagType notin entry.tags: return JS_ThrowTypeError(ctx, "Invalid tag type %s", element.tagType) case entry.t @@ -4368,13 +4366,10 @@ proc jsReflectGet(ctx: JSContext; this: JSValue; magic: cint): JSValue proc jsReflectSet(ctx: JSContext; this, val: JSValue; magic: cint): JSValue {.cdecl.} = - if unlikely(not ctx.isInstanceOf(this, "Element")): - return JS_ThrowTypeError(ctx, - "Reflected getter called on a value that is not an element") + var element: Element + if ctx.fromJS(this, element).isNone: + return JS_EXCEPTION let entry = ReflectTable[uint16(magic)] - let op = this.getOpaque() - assert op != nil - let element = cast[Element](op) if element.tagType notin entry.tags: return JS_ThrowTypeError(ctx, "Invalid tag type %s", element.tagType) case entry.t @@ -6080,8 +6075,7 @@ proc registerElements(ctx: JSContext; nodeCID: JSClassID) = let svgElementCID = ctx.registerType(SVGElement, parent = elementCID) ctx.registerType(SVGSVGElement, parent = svgElementCID) -proc addDOMModule*(ctx: JSContext) = - let eventTargetCID = ctx.getClass("EventTarget") +proc addDOMModule*(ctx: JSContext; eventTargetCID: JSClassID) = let nodeCID = ctx.registerType(Node, parent = eventTargetCID) ctx.defineConsts(nodeCID, NodeType) let nodeListCID = ctx.registerType(NodeList) diff --git a/src/html/env.nim b/src/html/env.nim index 5198c89e..f55817c4 100644 --- a/src/html/env.nim +++ b/src/html/env.nim @@ -356,9 +356,9 @@ proc loadJSModule(ctx: JSContext; moduleName: cstringConst; opaque: pointer): response.close() return ctx.finishLoadModule(source, moduleName) -proc addWindowModule*(ctx: JSContext) = - ctx.addEventModule() - let eventTargetCID = ctx.getClass("EventTarget") +proc addWindowModule*(ctx: JSContext): + tuple[eventCID, eventTargetCID: JSClassID] = + let (eventCID, eventTargetCID) = ctx.addEventModule() const getset = [TabGetSet( name: "onload", get: eventReflectGet, @@ -368,12 +368,14 @@ proc addWindowModule*(ctx: JSContext) = ctx.registerType(Window, parent = eventTargetCID, asglobal = true, hasExtraGetSet = true, extraGetSet = getset) ctx.registerType(MediaQueryList) + return (eventCID, eventTargetCID) -proc addWindowModule2*(ctx: JSContext) = - ctx.addEventModule() - let eventTargetCID = ctx.getClass("EventTarget") - ctx.registerType(Window, parent = eventTargetCID, asglobal = true, - globalparent = true) +proc addWindowModule2*(ctx: JSContext): + tuple[windowCID, eventCID, eventTargetCID: JSClassID] = + let (eventCID, eventTargetCID) = ctx.addEventModule() + let windowCID = ctx.registerType(Window, parent = eventTargetCID, + asglobal = true, globalparent = true) + return (windowCID, eventCID, eventTargetCID) proc evalJSFree(opaque: RootRef; src, file: string) = let window = Window(opaque) @@ -402,23 +404,23 @@ proc addScripting*(window: Window) = window.scriptAttrsp = window.attrsp else: window.scriptAttrsp = unsafeAddr dummyAttrs - ctx.addWindowModule() + let (eventCID, eventTargetCID) = ctx.addWindowModule() ctx.setGlobal(window) ctx.addDOMExceptionModule() ctx.addConsoleModule() ctx.addNavigatorModule() - ctx.addDOMModule() + ctx.addDOMModule(eventTargetCID) ctx.addURLModule() ctx.addHTMLModule() ctx.addIntlModule() ctx.addBlobModule() ctx.addFormDataModule() - ctx.addXMLHttpRequestModule() + ctx.addXMLHttpRequestModule(eventCID, eventTargetCID) ctx.addHeadersModule() ctx.addRequestModule() ctx.addResponseModule() ctx.addEncodingModule() - ctx.addPerformanceModule() + ctx.addPerformanceModule(eventTargetCID) proc newWindow*(scripting: ScriptingMode; images, styling, autofocus: bool; attrsp: ptr WindowAttributes; loader: FileLoader; url: URL; diff --git a/src/html/event.nim b/src/html/event.nim index 46fee333..b797f572 100644 --- a/src/html/event.nim +++ b/src/html/event.nim @@ -523,7 +523,8 @@ proc dispatchEvent(ctx: JSContext; this: EventTarget; event: Event): event.isTrusted = false return ok(ctx.dispatch(this, event)) -proc addEventModule*(ctx: JSContext) = +proc addEventModule*(ctx: JSContext): + tuple[eventCID, eventTargetCID: JSClassID] = let eventCID = ctx.registerType(Event) ctx.registerType(CustomEvent, parent = eventCID) ctx.registerType(MessageEvent, parent = eventCID) @@ -531,4 +532,5 @@ proc addEventModule*(ctx: JSContext) = ctx.registerType(MouseEvent, parent = uiEventCID) ctx.registerType(InputEvent, parent = uiEventCID) ctx.defineConsts(eventCID, EventPhase) - ctx.registerType(EventTarget) + let eventTargetCID = ctx.registerType(EventTarget) + return (eventCID, eventTargetCID) diff --git a/src/html/performance.nim b/src/html/performance.nim index 4939095f..9bc29a3d 100644 --- a/src/html/performance.nim +++ b/src/html/performance.nim @@ -24,6 +24,5 @@ proc newPerformance*(scripting: ScriptingMode): Performance = proc now(performance: Performance): float64 {.jsfunc.} = return getTime(performance.scripting) - performance.timeOrigin -proc addPerformanceModule*(ctx: JSContext) = - let eventTargetCID = ctx.getClass("EventTarget") +proc addPerformanceModule*(ctx: JSContext; eventTargetCID: JSClassID) = ctx.registerType(Performance, parent = eventTargetCID) diff --git a/src/html/xmlhttprequest.nim b/src/html/xmlhttprequest.nim index cacba66d..96a2a19c 100644 --- a/src/html/xmlhttprequest.nim +++ b/src/html/xmlhttprequest.nim @@ -481,9 +481,8 @@ func xhretGetSet(): seq[TabGetSet] = magic: int16(i) )) -proc addXMLHttpRequestModule*(ctx: JSContext) = - let eventTargetCID = ctx.getClass("EventTarget") - let eventCID = ctx.getClass("Event") +proc addXMLHttpRequestModule*(ctx: JSContext; + eventCID, eventTargetCID: JSClassID) = const getset0 = xhretGetSet() let xhretCID = ctx.registerType(XMLHttpRequestEventTarget, eventTargetCID, hasExtraGetSet = true, extraGetSet = getset0) diff --git a/src/local/client.nim b/src/local/client.nim index ec2a6b0a..74472e75 100644 --- a/src/local/client.nim +++ b/src/local/client.nim @@ -133,18 +133,18 @@ proc sleep(client: Client; millis: int) {.jsfunc.} = func line(client: Client): LineEdit {.jsfget.} = return client.pager.lineedit -proc addJSModules(client: Client; ctx: JSContext) = - ctx.addWindowModule2() +proc addJSModules(client: Client; ctx: JSContext): JSClassID = + let (windowCID, eventCID, eventTargetCID) = ctx.addWindowModule2() ctx.addDOMExceptionModule() ctx.addConsoleModule() ctx.addNavigatorModule() - ctx.addDOMModule() + ctx.addDOMModule(eventTargetCID) ctx.addURLModule() ctx.addHTMLModule() ctx.addIntlModule() ctx.addBlobModule() ctx.addFormDataModule() - ctx.addXMLHttpRequestModule() + ctx.addXMLHttpRequestModule(eventCID, eventTargetCID) ctx.addHeadersModule() ctx.addRequestModule() ctx.addResponseModule() @@ -154,6 +154,7 @@ proc addJSModules(client: Client; ctx: JSContext) = ctx.addPagerModule() ctx.addContainerModule() ctx.addSelectModule() + return windowCID proc newClient*(config: Config; forkserver: ForkServer; loaderPid: int; jsctx: JSContext; warnings: seq[string]; urandom: PosixStream; @@ -177,7 +178,6 @@ proc newClient*(config: Config; forkserver: ForkServer; loaderPid: int; jsctx.definePropertyE(global, "cmd", config.cmd.jsObj) JS_FreeValue(jsctx, global) config.cmd.jsObj = JS_NULL - client.addJSModules(jsctx) - let windowCID = jsctx.getClass("Window") + let windowCID = client.addJSModules(jsctx) jsctx.registerType(Client, asglobal = true, parent = windowCID) return client |