-
Notifications
You must be signed in to change notification settings - Fork 216
Add C Closures #1228
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
base: master
Are you sure you want to change the base?
Add C Closures #1228
Conversation
This allows C objects to be attached to functions, and it also allows C code to be notified when these functions are finalized. Co-Authored-By: Andrew Krieger <[email protected]>
|
Thank you! Could you please add a test to api-test.c? |
|
|
||
| /* XXX: could add the function on the stack for debug */ | ||
| if (unlikely(argc < s->length)) { | ||
| arg_buf = alloca(sizeof(arg_buf[0]) * s->length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs a stack check:
| arg_buf = alloca(sizeof(arg_buf[0]) * s->length); | |
| stack_size = arg_buf[0]) * s->length; | |
| if (js_check_stack_overflow(rt, stack_size)) | |
| return JS_ThrowStackOverflow(ctx); | |
| arg_buf = alloca(sizeof(stack_size); |
As well, add a temporary JSStackFrame here, otherwise the function doesn't show up in stack traces. I fixed that recently for JSCFunctionData callbacks so you can just crib it from there: c85fd87
| JSValue JS_NewCClosure(JSContext *ctx, JSCClosure *func, | ||
| int length, int magic, void *opaque, | ||
| void (*opaque_finalize)(void*)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency with e.g. JS_SetModuleLoaderFunc:
| JSValue JS_NewCClosure(JSContext *ctx, JSCClosure *func, | |
| int length, int magic, void *opaque, | |
| void (*opaque_finalize)(void*)) | |
| JSValue JS_NewCClosure(JSContext *ctx, JSCClosure *func, | |
| void (*opaque_finalize)(void*), | |
| int length, int magic, void *opaque) |
And I'd add a typedef for the finalizer callback.
| js_function_set_properties(ctx, func_obj, | ||
| JS_ATOM_empty_string, length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added JS_NewCFunctionData2 recently because people do in fact sometimes want to set the function name, so let's do that right from the beginning here. :-)
This allows C objects to be attached to functions, and it also allows C code to be notified when these functions are finalized.
Import of https://github.com/tbluemel/quickjscpp/blob/98bf1ddb00e39d93840c4b67211c9260943b5a83/patches/2024-01-13/0001-Add-C-Closures.patch. Trivial renaming of functions and function annotations to match local style.
Closes #1212. Closes tbluemel/quickjscpp#3.