Skip to content
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

Replace eval with indirect eval #23483

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

segevfiner
Copy link

Indirect eval doesn't expose the local scope to the evaluated code, and won't warn as much from bundlers

Fixes #23464

Indirect eval doesn't expose the local scope to the evaluated code, and won't warn as much from bundlers
@hoodmane
Copy link
Collaborator

Unfortunately I think indirect eval does not suffice. For instance if you use EM_JS you will probably want to use runtime functions like UTF8toString in all but the simplest examples. This will fail with indirect eval.

Maybe we could have a mode where we pass Module as an extra first argument and then as long as the code uses Module.UTF8toString it would with indirect eval. But this requires more work than is present in this patch. I'll approve the ci job so we can see which tests fail.

@hoodmane
Copy link
Collaborator

Wdyt @sbc100? I see you didn't express a concern about this in the linked issue...

@hoodmane
Copy link
Collaborator

See in ci there are many errors like:

ReferenceError: _malloc is not defined.

I think the bundler warnings may be an acceptable evil and assuming that people test the output they can find out whether the bundler broke anything or not. For many projects it's necessary to disable minification.

@segevfiner
Copy link
Author

It might still be possible to pass the needed references in for the code to use. Instead of exposing the entire scope of the module.

But I'm not sure what is expected to be defined by the eval-ed code.

@hoodmane
Copy link
Collaborator

Unfortunately what's expected is... everything defined on the module. We could add a compiler setting -sNO_DIRECT_EVAL which passes Module in explicitly and does an indirect eval. This would change the abi for shared libraries. It could also be solved transparently by passing in Module and then using a with statement as long as we can make sure not to eval the code in strict mode.

@sbc100
Copy link
Collaborator

sbc100 commented Jan 24, 2025

Unfortunately what's expected is... everything defined on the module. We could add a compiler setting -sNO_DIRECT_EVAL which passes Module in explicitly and does an indirect eval. This would change the abi for shared libraries. It could also be solved transparently by passing in Module and then using a with statement as long as we can make sure not to eval the code in strict mode.

I don't think that works either, because the Module object only contains the exported/public API. EM_JS code should be allowed to access all the internals, not just the things that are exported.

@sbc100
Copy link
Collaborator

sbc100 commented Jan 24, 2025

Perhaps some of the uses of in this PR can still land, even if EM_JS is not one of them?

@segevfiner segevfiner marked this pull request as draft January 24, 2025 18:28
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.

Consider using an indirect eval for DYNAMIC_EXECUTION
3 participants