diff options
| -rw-r--r-- | lib/monoucha0/monoucha/javascript.nim | 25 | ||||
| -rw-r--r-- | lib/monoucha0/monoucha/jsopaque.nim | 4 | ||||
| -rw-r--r-- | lib/monoucha0/monoucha/tojs.nim | 14 |
3 files changed, 35 insertions, 8 deletions
diff --git a/lib/monoucha0/monoucha/javascript.nim b/lib/monoucha0/monoucha/javascript.nim index fc92ff29..0ec3f144 100644 --- a/lib/monoucha0/monoucha/javascript.nim +++ b/lib/monoucha0/monoucha/javascript.nim @@ -138,8 +138,8 @@ proc jsRuntimeCleanUp(rt: JSRuntime) {.cdecl.} = JS_RunGC(rt) assert rtOpaque.destroying == nil var np = 0 - for p in rtOpaque.plist.values: - rtOpaque.tmplist[np] = p + for it in rtOpaque.plist.values: + rtOpaque.tmplist[np] = it.p inc np rtOpaque.plist.clear() var nu = 0 @@ -1199,7 +1199,7 @@ proc nim_finalize_for_js*(obj: pointer) = for rt in runtimes: let rtOpaque = rt.getOpaque() rtOpaque.plist.withValue(obj, v): - let p = v[] + let p = v[].p let val = JS_MKPTR(JS_TAG_OBJECT, p) let classid = JS_GetClassID(val) rtOpaque.fins.withValue(classid, fin): @@ -1399,7 +1399,24 @@ proc jsCheckDestroyRef*(rt: JSRuntime; val: JSValue): JS_BOOL {.cdecl.} = # This means we can allow QJS to collect this JSValue. return true else: - rt.getOpaque().destroying = nil + let rtOpaque = rt.getOpaque() + rtOpaque.destroying = nil + # Set an internal flag to note that the JS object is owned by the + # Nim side. + # This means that if toJS is used again on the Nim object, JS will + # first get a non-dup'd object, and a reference will be set on + # the Nim object. + # + #TODO can we eliminate this hash somehow? + # at least I *think* in the common case of "no reference cycle", + # we could just elide the jsref set here, and add a reference + # in toJSP0 if the refcount on the JS pointer is 0. (however, + # we must do it here if the refcount is > 1, that means we have + # a cycle.) + # it sounds too hacky to try for now, but may be worth it if this + # turns out to be a bottleneck... + rtOpaque.plist.withValue(opaque, v): + v[].jsref = false # Returning false from this function signals to the QJS GC that it # should not be collected yet. Accordingly, the JSObject's refcount # will be set to one again. diff --git a/lib/monoucha0/monoucha/jsopaque.nim b/lib/monoucha0/monoucha/jsopaque.nim index 8510965d..785cf501 100644 --- a/lib/monoucha0/monoucha/jsopaque.nim +++ b/lib/monoucha0/monoucha/jsopaque.nim @@ -49,7 +49,7 @@ type JSEmptyOpaqueCallback* = (proc() {.closure, raises: [].}) JSRuntimeOpaque* = ref object - plist*: Table[pointer, pointer] # Nim, JS + plist*: Table[pointer, tuple[p: pointer; jsref: bool]] # Nim, JS flist*: seq[seq[JSCFunctionListEntry]] fins*: Table[JSClassID, JSFinalizerFunction] parentMap*: Table[pointer, pointer] @@ -96,7 +96,7 @@ proc setOpaque*(ctx: JSContext; val: JSValue; opaque: pointer) = let rt = JS_GetRuntime(ctx) let rtOpaque = rt.getOpaque() let p = JS_VALUE_GET_PTR(val) - rtOpaque.plist[opaque] = p + rtOpaque.plist[opaque] = (p, true) JS_SetOpaque(val, opaque) func getOpaque*(val: JSValue): pointer = diff --git a/lib/monoucha0/monoucha/tojs.nim b/lib/monoucha0/monoucha/tojs.nim index 2e4b73c9..f92f0165 100644 --- a/lib/monoucha0/monoucha/tojs.nim +++ b/lib/monoucha0/monoucha/tojs.nim @@ -254,13 +254,23 @@ proc toJSP0(ctx: JSContext; p, tp: pointer; ctor: JSValue; needsref: var bool): JSValue = JS_GetRuntime(ctx).getOpaque().plist.withValue(p, obj): # a JSValue already points to this object. - return JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, obj[])) + let p = obj[].p + if obj[].jsref: + # JS owns the Nim value, because it still holds an active + # reference to it. + return JS_DupValue(ctx, JS_MKPTR(JS_TAG_OBJECT, p)) + # Nim owned the JS value, but now JS wants to own Nim. + # This means we must release the JS reference, and add a reference + # to Nim. + needsref = true + obj[].jsref = true + return JS_MKPTR(JS_TAG_OBJECT, p) let ctxOpaque = ctx.getOpaque() let class = ctxOpaque.typemap.getOrDefault(tp, 0) let jsObj = JS_NewObjectFromCtor(ctx, ctor, class) if JS_IsException(jsObj): return jsObj - setOpaque(ctx, jsObj, p) + ctx.setOpaque(jsObj, p) # We are constructing a new JS object, so we must add unforgeable properties # here. if int(class) < ctxOpaque.unforgeable.len and |