-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add an experimental way to export a instance ES module. #22867
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
Merged
brendandahl
merged 9 commits into
emscripten-core:main
from
brendandahl:modularize-static
Nov 19, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5c0d413
Add an experimental way to export a "static" ES module.
brendandahl b9e19a0
Review comments and missing test.
brendandahl f6b40ee
Support exporting library functions with EXPORTED_FUNCTIONS.
brendandahl ecf9e4f
Change name to instance. Add check for correct setting name.
brendandahl 9dcdfed
Merge branch 'main' into modularize-static
brendandahl dcc5a8a
Review comments.
brendandahl 8c8ef1d
Merge branch 'main' into modularize-static
brendandahl 694fb8a
comment.
brendandahl a7d28ef
docs
brendandahl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <stdio.h> | ||
#include <emscripten.h> | ||
#ifdef __EMSCRIPTEN_PTHREADS__ | ||
#include <pthread.h> | ||
#include <string.h> | ||
#endif | ||
|
||
EMSCRIPTEN_KEEPALIVE void foo() { | ||
printf("foo\n"); | ||
} | ||
|
||
void bar() { | ||
printf("bar\n"); | ||
} | ||
|
||
void *thread_function(void *arg) { | ||
printf("main2\n"); | ||
return NULL; | ||
} | ||
|
||
int main() { | ||
printf("main1\n"); | ||
#ifdef __EMSCRIPTEN_PTHREADS__ | ||
pthread_t thread_id; | ||
int result = pthread_create(&thread_id, NULL, thread_function, NULL); | ||
if (result != 0) { | ||
fprintf(stderr, "Error creating thread: %s\n", strerror(result)); | ||
return 1; | ||
} | ||
pthread_join(thread_id, NULL); | ||
#else | ||
printf("main2\n"); | ||
#endif | ||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import init, { _foo as foo } from "./modularize_static.mjs"; | ||
await init(); | ||
foo(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -754,7 +754,15 @@ def phase_linker_setup(options, state, newargs): | |
|
||
if options.oformat == OFormat.MJS: | ||
settings.EXPORT_ES6 = 1 | ||
settings.MODULARIZE = 1 | ||
default_setting('MODULARIZE', 1) | ||
|
||
if settings.MODULARIZE and settings.MODULARIZE not in [1, 'instance']: | ||
exit_with_error(f'Invalid setting "{settings.MODULARIZE}" for MODULARIZE.') | ||
|
||
if settings.MODULARIZE == 'instance': | ||
diagnostics.warning('experimental', '-sMODULARIZE=instance is still experimental. Many features may not work or will change.') | ||
if options.oformat != OFormat.MJS: | ||
exit_with_error('emcc: MODULARIZE instance is only compatible with .mjs output files') | ||
|
||
if options.oformat in (OFormat.WASM, OFormat.BARE): | ||
if options.emit_tsd: | ||
|
@@ -2391,7 +2399,20 @@ def modularize(): | |
if async_emit != '' and settings.EXPORT_NAME == 'config': | ||
diagnostics.warning('emcc', 'EXPORT_NAME should not be named "config" when targeting Safari') | ||
|
||
src = ''' | ||
if settings.MODULARIZE == 'instance': | ||
src = ''' | ||
export default async function init(moduleArg = {}) { | ||
var moduleRtn; | ||
|
||
%(src)s | ||
|
||
return await moduleRtn; | ||
} | ||
''' % { | ||
'src': src, | ||
} | ||
else: | ||
src = ''' | ||
%(maybe_async)sfunction(moduleArg = {}) { | ||
var moduleRtn; | ||
|
||
|
@@ -2400,9 +2421,9 @@ def modularize(): | |
return moduleRtn; | ||
} | ||
''' % { | ||
'maybe_async': async_emit, | ||
'src': src, | ||
} | ||
'maybe_async': async_emit, | ||
'src': src, | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this indentation better than before? Maybe revert this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Flake wants this because of the new if/else above. |
||
|
||
if settings.MINIMAL_RUNTIME and not settings.PTHREADS: | ||
# Single threaded MINIMAL_RUNTIME programs do not need access to | ||
|
@@ -2421,19 +2442,31 @@ def modularize(): | |
script_url = "typeof document != 'undefined' ? document.currentScript?.src : undefined" | ||
if shared.target_environment_may_be('node'): | ||
script_url_node = "if (typeof __filename != 'undefined') _scriptName = _scriptName || __filename;" | ||
src = '''%(node_imports)s | ||
if settings.MODULARIZE == 'instance': | ||
src = '''%(node_imports)s | ||
var _scriptName = %(script_url)s; | ||
%(script_url_node)s | ||
%(src)s | ||
''' % { | ||
'node_imports': node_es6_imports(), | ||
'script_url': script_url, | ||
'script_url_node': script_url_node, | ||
'src': src, | ||
} | ||
else: | ||
src = '''%(node_imports)s | ||
var %(EXPORT_NAME)s = (() => { | ||
var _scriptName = %(script_url)s; | ||
%(script_url_node)s | ||
return (%(src)s); | ||
})(); | ||
''' % { | ||
'node_imports': node_es6_imports(), | ||
'EXPORT_NAME': settings.EXPORT_NAME, | ||
'script_url': script_url, | ||
'script_url_node': script_url_node, | ||
'src': src, | ||
} | ||
'node_imports': node_es6_imports(), | ||
'EXPORT_NAME': settings.EXPORT_NAME, | ||
'script_url': script_url, | ||
'script_url_node': script_url_node, | ||
'src': src, | ||
} | ||
|
||
# Given the async nature of how the Module function and Module object | ||
# come into existence in AudioWorkletGlobalScope, store the Module | ||
|
@@ -2446,8 +2479,16 @@ def modularize(): | |
|
||
# Export using a UMD style export, or ES6 exports if selected | ||
if settings.EXPORT_ES6: | ||
src += 'export default %s;\n' % settings.EXPORT_NAME | ||
|
||
if settings.MODULARIZE == 'instance': | ||
exports = settings.EXPORTED_FUNCTIONS + settings.EXPORTED_RUNTIME_METHODS | ||
# Declare a top level var for each export so that code in the init function | ||
# can assign to it and update the live module bindings. | ||
src += 'var ' + ', '.join(['__exp_' + export for export in exports]) + ';\n' | ||
# Export the functions with their original name. | ||
exports = ['__exp_' + export + ' as ' + export for export in exports] | ||
src += 'export {' + ', '.join(exports) + '};\n' | ||
else: | ||
src += 'export default %s;\n' % settings.EXPORT_NAME | ||
elif not settings.MINIMAL_RUNTIME: | ||
src += '''\ | ||
if (typeof exports === 'object' && typeof module === 'object') | ||
|
@@ -2470,7 +2511,10 @@ def modularize(): | |
elif settings.ENVIRONMENT_MAY_BE_NODE: | ||
src += f'var isPthread = {node_pthread_detection()}\n' | ||
src += '// When running as a pthread, construct a new instance on startup\n' | ||
src += 'isPthread && %s();\n' % settings.EXPORT_NAME | ||
if settings.MODULARIZE == 'instance': | ||
src += 'isPthread && init();\n' | ||
else: | ||
src += 'isPthread && %s();\n' % settings.EXPORT_NAME | ||
|
||
final_js += '.modular.js' | ||
write_file(final_js, src) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.