-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document ownership rules of C API #944
Comments
Yes, I think the API docs are the right place. Taking a page out of (C)Python's book: https://docs.python.org/3/c-api/list.html#c.PyList_SetItem : There is nuance in what happens to the existing property if you override it for example, and I think we cannot "document" that with a macro, so docs is better IMHO. |
(I'm probably missing something obvious here, but...) why not just use JSValueConst?
My understanding of JSValueConst is
So when overriding an existing property, the reference to the old value is already owned by the engine - I don't have to care about what happens to it as long as I follow the above rules. (I guess documenting that "the old property gets unref'd" would still be useful for users unfamiliar with the API's design, but that doesn't decrease the value of JSValueConst, which had simple rules that in theory could be mechanically interpreted.) |
I don't think it worked like that, not 100% of the time anyway. For example, the old definition of typedef JSValue JSCFunctionData(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic, JSValue *func_data) But you don't get to keep the reference to
That's right.
Yes, it was originally intended for CONFIG_CHECK_JSVALUE builds, see commit 4d57997, although I don't remember a time where it actually worked. It certainly was broken when @saghul and I started working on quickjs in earnest. I wouldn't mind bringing back JSValueConst if we could resolve the outstanding issues. Requires that someone goes over the APIs with a fine tooth comb and revive the CONFIG_CHECK_JSVALUE build mode. |
After going through the old code, I'm > 50% hopeful the inconsistencies are not insurmountable. Let me give it a try. |
Ah, glad to hear that. Thanks for looking into it. I've just tried to switch func_data to JSValueConst in bellard/quickjs; it adds a few casts and removes some, and there are weird cases like js_proxy_revoke which deletes a ref from its func_data array. But overall it just seems like an oversight. (A drawback of the const hack that I learned from this: you have to cast arrays a lot...) |
Reintroduce JSValueConst and add a JS_CHECK_JSVALUE build mode that catches reference counting bugs by making JSValue and JSValueConst different types. The *vast* majority of this commit is rote work to make our own sources compile cleanly in said mode. Refs: quickjs-ng#944
Reintroduce JSValueConst and add a JS_CHECK_JSVALUE build mode that catches reference counting bugs by making JSValue and JSValueConst different types. The *vast* majority of this commit is rote work to make our own sources compile cleanly in said mode. Refs: #944
Example: it's not really documented anywhere that
JS_SetProperty(ctx, obj, key, val)
takes ownership ofval
but notkey
. One must callJS_FreeAtom(ctx, key)
afterwards but notJS_FreeValue(ctx, val)
.Another example of an undocumented constraint: you should not
JS_FreeValue
the elements of theargv
array to your C callback. But you shouldJS_FreeValue
the return value ofJS_GetProperty(ctx, obj, key)
. Here too, one mustJS_FreeAtom(ctx, key)
.Neither is it documented that the first
length
elements ofargv
are valid (set to undefined) when JS calls a C function with too few arguments. Or thatargc
can be greater thanlength
- which makes perfect sense but is undocumented.Should probably go into
docs/docs/developer-guide/api.md
?The text was updated successfully, but these errors were encountered: