diff options
| author | bptato <nincsnevem662@gmail.com> | 2024-05-03 01:41:38 +0200 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2024-05-03 01:58:12 +0200 |
| commit | 970378356d0d7239b332baa37470455391b5e6e4 (patch) | |
| tree | 87d93162295b12652137193982c5b3c88e1a3758 /src/html | |
| parent | c48f2caedabbcda03724c43935f4175aac3ecf90 (diff) | |
| download | chawan-970378356d0d7239b332baa37470455391b5e6e4.tar.gz | |
js: fix various leaks etc.
Previously we didn't actually free the main JS runtime, probably because
you can't do this without first waiting for JS to unwind the stack.
(This has the unfortunate effect that code now *can* run after quit().
TODO: find a fix for this.)
This isn't a huge problem per se, we only have one of these and the OS
can clean it up. However, it also disabled the JS_FreeRuntime leak
check, which resulted in sieve-like behavior (manual refcounting is
a pain).
So now we choose the other tradeoff: quit no longer runs exitnow, but
it waits for the event loop to run to the end and only then exits the
browser. Then, before exit we free the JS context & runtime, and also
all JS values allocated by config.
Fixes:
* fix `ad' flag not being set for just one siteconf/omnirule
* fix various leaks (since leak check is enabled now)
* use ptr UncheckedArray[JSValue] for QJS bindings that take an array
* allow JSAtom in jsgetprop etc., also disallow int types other than
uint32
* do not set a destructor for globals
Diffstat (limited to 'src/html')
| -rw-r--r-- | src/html/dom.nim | 37 | ||||
| -rw-r--r-- | src/html/env.nim | 6 | ||||
| -rw-r--r-- | src/html/event.nim | 17 |
3 files changed, 34 insertions, 26 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim index 6f57960a..739d01fe 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -25,6 +25,7 @@ import js/domexception import js/error import js/fromjs import js/javascript +import js/jsutils import js/opaque import js/propertyenumlist import js/timeout @@ -370,7 +371,6 @@ jsDestructor(Navigator) jsDestructor(PluginArray) jsDestructor(MimeTypeArray) jsDestructor(Screen) -jsDestructor(Window) jsDestructor(Element) jsDestructor(HTMLElement) @@ -1276,21 +1276,22 @@ const SupportedTokensMap = { "next", "pingback", "preconnect", "prefetch", "preload", "search", "stylesheet" ] -}.toTable() +} func supports(tokenList: DOMTokenList; token: string): JSResult[bool] {.jsfunc.} = let localName = tokenList.element.document.toStaticAtom(tokenList.localName) - if localName in SupportedTokensMap: - let lowercase = token.toLowerAscii() - return ok(lowercase in SupportedTokensMap[localName]) + for it in SupportedTokensMap: + if it[0] == localName: + let lowercase = token.toLowerAscii() + return ok(lowercase in it[1]) return err(newTypeError("No supported tokens defined for attribute")) func value(tokenList: DOMTokenList): string {.jsfget.} = return $tokenList -func getter(tokenList: DOMTokenList; i: int): Option[string] {.jsgetprop.} = - return tokenList.item(i) +func getter(tokenList: DOMTokenList; i: uint32): Option[string] {.jsgetprop.} = + return tokenList.item(int(i)) # DOMStringMap func validateAttributeName(name: string): Err[DOMException] = @@ -1352,15 +1353,15 @@ func names(ctx: JSContext; map: ptr DOMStringMap): JSPropertyEnumList func length(nodeList: NodeList): uint32 {.jsfget.} = return uint32(nodeList.len) -func hasprop(nodeList: NodeList; i: int): bool {.jshasprop.} = - return i < nodeList.len +func hasprop(nodeList: NodeList; i: uint32): bool {.jshasprop.} = + return int(i) < nodeList.len func item(nodeList: NodeList; i: int): Node {.jsfunc.} = if i < nodeList.len: return nodeList.snapshot[i] -func getter(nodeList: NodeList; i: int): Option[Node] {.jsgetprop.} = - return option(nodeList.item(i)) +func getter(nodeList: NodeList; i: uint32): Option[Node] {.jsgetprop.} = + return option(nodeList.item(int(i))) func names(ctx: JSContext; nodeList: NodeList): JSPropertyEnumList {.jspropnames.} = @@ -1417,14 +1418,15 @@ func names(ctx: JSContext; collection: HTMLCollection): JSPropertyEnumList proc length(collection: HTMLAllCollection): uint32 {.jsfget.} = return uint32(collection.len) -func hasprop(collection: HTMLAllCollection; i: int): bool {.jshasprop.} = - return i < collection.len +func hasprop(collection: HTMLAllCollection; i: uint32): bool {.jshasprop.} = + return int(i) < collection.len -func item(collection: HTMLAllCollection; i: int): Element {.jsfunc.} = +func item(collection: HTMLAllCollection; i: uint32): Element {.jsfunc.} = + let i = int(i) if i < collection.len: return Element(collection.snapshot[i]) -func getter(collection: HTMLAllCollection; i: int): Option[Element] +func getter(collection: HTMLAllCollection; i: uint32): Option[Element] {.jsgetprop.} = return option(collection.item(i)) @@ -1452,7 +1454,7 @@ proc newLocation*(window: Window): Location = let ctx = window.jsctx if ctx != nil: let val = toJS(ctx, location) - let valueOf = ctx.getOpaque().Object_prototype_valueOf + let valueOf = ctx.getOpaque().valRefs[jsvObjectPrototypeValueOf] defineProperty(ctx, val, "valueOf", JS_DupValue(ctx, valueOf)) defineProperty(ctx, val, "toPrimitive", JS_UNDEFINED) #TODO [[DefaultProperties]] @@ -4179,7 +4181,8 @@ proc toBlob(ctx: JSContext; this: HTMLCanvasElement; callback: JSValue; let buf = this.bitmap.toPNG(outlen) let blob = newBlob(buf, outlen, "image/png", proc() = dealloc(buf)) var jsBlob = toJS(ctx, blob) - let res = JS_Call(ctx, callback, JS_UNDEFINED, 1, addr jsBlob) + let res = JS_Call(ctx, callback, JS_UNDEFINED, 1, jsBlob.toJSValueArray()) + JS_FreeValue(ctx, jsBlob) # Hack. TODO: implement JSValue to callback if res == JS_EXCEPTION: return JS_EXCEPTION diff --git a/src/html/env.nim b/src/html/env.nim index c9d99dc2..00b6f18c 100644 --- a/src/html/env.nim +++ b/src/html/env.nim @@ -68,10 +68,10 @@ proc item(pluginArray: ptr PluginArray): JSValue {.jsfunc.} = JS_NULL proc length(pluginArray: ptr PluginArray): uint32 {.jsfget.} = 0 proc item(mimeTypeArray: ptr MimeTypeArray): JSValue {.jsfunc.} = JS_NULL proc length(mimeTypeArray: ptr MimeTypeArray): uint32 {.jsfget.} = 0 -proc getter(pluginArray: ptr PluginArray; i: int): Option[JSValue] +proc getter(pluginArray: ptr PluginArray; i: uint32): Option[JSValue] {.jsgetprop.} = discard -proc getter(mimeTypeArray: ptr MimeTypeArray; i: int): Option[JSValue] +proc getter(mimeTypeArray: ptr MimeTypeArray; i: uint32): Option[JSValue] {.jsgetprop.} = discard @@ -193,7 +193,7 @@ proc addScripting*(window: Window; selector: Selector[int]) = ctx.addEventModule() let eventTargetCID = ctx.getClass("EventTarget") ctx.registerType(Window, asglobal = true, parent = eventTargetCID) - ctx.setGlobal(global, window) + ctx.setGlobal(window) JS_FreeValue(ctx, global) ctx.addDOMExceptionModule() ctx.addConsoleModule() diff --git a/src/html/event.nim b/src/html/event.nim index 9616d55e..b74649e8 100644 --- a/src/html/event.nim +++ b/src/html/event.nim @@ -7,6 +7,7 @@ import js/error import js/fromjs import js/javascript import js/jstypes +import js/jsutils import js/tojs import types/opt @@ -198,9 +199,10 @@ proc invoke*(ctx: JSContext; listener: EventListener; event: Event): if JS_IsNull(listener.callback): return JS_UNDEFINED let jsTarget = ctx.toJS(event.currentTarget) - var jsEvent = ctx.toJS(event) + let jsEvent = ctx.toJS(event) if JS_IsFunction(ctx, listener.callback): - let ret = JS_Call(ctx, listener.callback, jsTarget, 1, addr jsEvent) + let ret = JS_Call(ctx, listener.callback, jsTarget, 1, + jsEvent.toJSValueArray()) JS_FreeValue(ctx, jsTarget) JS_FreeValue(ctx, jsEvent) return ret @@ -210,7 +212,7 @@ proc invoke*(ctx: JSContext; listener: EventListener; event: Event): JS_FreeValue(ctx, jsTarget) JS_FreeValue(ctx, jsEvent) return handler - let ret = JS_Call(ctx, handler, jsTarget, 1, addr jsEvent) + let ret = JS_Call(ctx, handler, jsTarget, 1, jsEvent.toJSValueArray()) JS_FreeValue(ctx, jsTarget) JS_FreeValue(ctx, jsEvent) return ret @@ -255,9 +257,12 @@ proc flattenMore(ctx: JSContext; options: JSValue): var once = false var passive: Option[bool] if JS_IsObject(options): - once = fromJS[bool](ctx, JS_GetPropertyStr(ctx, options, "once")) - .get(false) - let x = fromJS[bool](ctx, JS_GetPropertyStr(ctx, options, "passive")) + let jsOnce = JS_GetPropertyStr(ctx, options, "once") + once = fromJS[bool](ctx, jsOnce).get(false) + JS_FreeValue(ctx, jsOnce) + let jsPassive = JS_GetPropertyStr(ctx, options, "passive") + let x = fromJS[bool](ctx, jsPassive) + JS_FreeValue(ctx, jsPassive) if x.isSome: passive = some(x.get) return (capture, once, passive) |