Skip to content

only initalize native ffi code on ffi import#487

Merged
saghul merged 1 commit intosaghul:masterfrom
lal12:load-module-internal-only-on-import
Apr 15, 2024
Merged

only initalize native ffi code on ffi import#487
saghul merged 1 commit intosaghul:masterfrom
lal12:load-module-internal-only-on-import

Conversation

@lal12
Copy link
Copy Markdown
Contributor

@lal12 lal12 commented Apr 13, 2024

I noticed ffi init was always executed on tjs startup. This is only necessary when actually being used / being imported.

Initially I planned adding some init functions in builtins.c, but since this currently only is useful for ffi, I took the small route.

@lal12 lal12 force-pushed the load-module-internal-only-on-import branch 2 times, most recently from 2007a9b to a41bdab Compare April 13, 2024 21:33
src/ffi.c Outdated

void tjs__mod_ffi_init(JSContext *ctx, JSValue ns) {
JSValue ffiobj = JS_NewObject(ctx);
static JSValue ffiobj;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note we allow threads, by way of Workers. This is not safe.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok, wasn't aware of that.

Given that I am not sure about the JS_NewClass(ID) thread safety and thread interaction, so a thread local variable for ffiobj probably wouldn't be enough. Having a mutex here also seems to be bit overkill. Maybe implementing such a solution later on when there are more places where it is useful (e.g. lws) might be preferable.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class IDs are thread safe, I fixed that in QuickJS-ng by making that field part of the runtime rather than a global.

@saghul
Copy link
Copy Markdown
Owner

saghul commented Apr 15, 2024

OK, I saw the whole file now, I had only seen the diff on my phone, which made it look weird in a way.

I think the simple route here is to just attach that initializer function to the core and then use it to initialize the global state. Since the core is not a public API we can rely on that thing only ever being called once, so you're good in that case.

A Worker is a whole new runtime (JSRuntime + JSContext) so it would be initialized there too, if used, but there is no problem in doing that, I think.

@lal12 lal12 force-pushed the load-module-internal-only-on-import branch from a41bdab to 0be86d0 Compare April 15, 2024 08:32
Copy link
Copy Markdown
Owner

@saghul saghul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Can you rebase pl? I updated QuickJS and had to regenerate the bundles.

@lal12 lal12 force-pushed the load-module-internal-only-on-import branch from 0be86d0 to bf88ec5 Compare April 15, 2024 08:39
@lal12 lal12 force-pushed the load-module-internal-only-on-import branch from bf88ec5 to 1304265 Compare April 15, 2024 08:41
@saghul saghul merged commit fc23b54 into saghul:master Apr 15, 2024
@lal12 lal12 deleted the load-module-internal-only-on-import branch April 15, 2024 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants