Skip to content

Commit 0225cd1

Browse files
committed
Add modularize to file_packager.py
1 parent df9dfe0 commit 0225cd1

File tree

1 file changed

+60
-26
lines changed

1 file changed

+60
-26
lines changed

tools/file_packager.py

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
2222
Usage:
2323
24-
file_packager TARGET [--preload A [B..]] [--embed C [D..]] [--exclude E [F..]] [--js-output=OUTPUT.js] [--no-force] [--use-preload-cache] [--indexedDB-name=EM_PRELOAD_CACHE] [--separate-metadata] [--lz4] [--use-preload-plugins] [--no-node] [--help]
24+
file_packager TARGET [--preload A [B..]] [--embed C [D..]] [--exclude E [F..]]] [--js-output=OUTPUT.js] [--no-force] [--use-preload-cache] [--indexedDB-name=EM_PRELOAD_CACHE] [--separate-metadata] [--lz4] [--use-preload-plugins] [--no-node]
2525
2626
--preload ,
2727
--embed See emcc --help for more details on those options.
@@ -129,6 +129,7 @@ def __init__(self):
129129
self.use_preload_plugins = False
130130
self.support_node = True
131131
self.wasm64 = False
132+
self.modularize = False
132133

133134

134135
class DataFile:
@@ -158,7 +159,8 @@ def has_hidden_attribute(filepath):
158159
return False
159160

160161
try:
161-
attrs = ctypes.windll.kernel32.GetFileAttributesW(filepath)
162+
attrs = ctypes.windll.kernel32.GetFileAttributesW(
163+
u'%s' % filepath)
162164
assert attrs != -1
163165
result = bool(attrs & 2)
164166
except Exception:
@@ -284,7 +286,7 @@ def generate_object_file(data_files):
284286
# The name of file
285287
{f.c_symbol_name}_name:
286288
.asciz "{dstpath}"
287-
.size {f.c_symbol_name}_name, {len(dstpath) + 1}
289+
.size {f.c_symbol_name}_name, {len(dstpath)+1}
288290
289291
# The size of the file followed by the content itself
290292
{f.c_symbol_name}:
@@ -318,6 +320,8 @@ def generate_object_file(data_files):
318320
# A list of triples of:
319321
# (file_name_ptr, file_data_size, file_data_ptr)
320322
# The list in null terminate with a single 0
323+
.globl __emscripten_embedded_file_data
324+
.export_name __emscripten_embedded_file_data, __emscripten_embedded_file_data
321325
.section .rodata.__emscripten_embedded_file_data,"",@
322326
__emscripten_embedded_file_data:
323327
.p2align {align}
@@ -362,26 +366,16 @@ def main(): # noqa: C901, PLR0912, PLR0915
362366
To revalidate these numbers, run `ruff check --select=C901,PLR091`.
363367
"""
364368
if len(sys.argv) == 1:
365-
err('''Usage: file_packager TARGET [--preload A [B..]] [--embed C [D..]] [--exclude E [F..]] [--js-output=OUTPUT.js] [--no-force] [--use-preload-cache] [--indexedDB-name=EM_PRELOAD_CACHE] [--separate-metadata] [--lz4] [--use-preload-plugins] [--no-node] [--help]
366-
Try 'file_packager --help' for more details.''')
369+
err('''Usage: file_packager TARGET [--preload A [B..]] [--embed C [D..]] [--exclude E [F..]]] [--js-output=OUTPUT.js] [--no-force] [--use-preload-cache] [--indexedDB-name=EM_PRELOAD_CACHE] [--separate-metadata] [--lz4] [--use-preload-plugins] [--no-node]
370+
See the source for more details.''')
367371
return 1
368372

369-
# read response files very early on
370-
try:
371-
args = substitute_response_files(sys.argv[1:])
372-
except OSError as e:
373-
shared.exit_with_error(e)
374-
375-
if '--help' in args:
376-
print(__doc__.strip())
377-
return 0
378-
379-
data_target = args[0]
373+
data_target = sys.argv[1]
380374
data_files = []
381375
plugins = []
382376
leading = ''
383377

384-
for arg in args[1:]:
378+
for arg in sys.argv[2:]:
385379
if arg == '--preload':
386380
leading = 'preload'
387381
elif arg == '--embed':
@@ -391,6 +385,9 @@ def main(): # noqa: C901, PLR0912, PLR0915
391385
elif arg == '--no-force':
392386
options.force = False
393387
leading = ''
388+
elif arg == '--modularize':
389+
options.modularize = True
390+
leading = ''
394391
elif arg == '--use-preload-cache':
395392
options.use_preload_cache = True
396393
leading = ''
@@ -604,6 +601,7 @@ def was_seen(name):
604601
f.write(escape_for_makefile(dependency))
605602
f.write(' \\\n')
606603

604+
607605
return 0
608606

609607

@@ -621,17 +619,30 @@ def generate_js(data_target, data_files, metadata):
621619
if options.from_emcc:
622620
ret = ''
623621
else:
624-
ret = '''
622+
if options.modularize:
623+
ret = '''
624+
var createModule = (() => {
625+
626+
return (async function(moduleArg = {}) {
627+
var Module = moduleArg;
628+
'''
629+
else:
630+
ret = '''
625631
var Module = typeof %(EXPORT_NAME)s != 'undefined' ? %(EXPORT_NAME)s : {};\n''' % {"EXPORT_NAME": options.export_name}
626632

627-
ret += '''
633+
ret += '''
628634
Module['expectedDataFileDownloads'] ??= 0;
629-
Module['expectedDataFileDownloads']++;
630-
(() => {
631-
// Do not attempt to redownload the virtual filesystem data when in a pthread or a Wasm Worker context.
632-
var isPthread = typeof ENVIRONMENT_IS_PTHREAD != 'undefined' && ENVIRONMENT_IS_PTHREAD;
633-
var isWasmWorker = typeof ENVIRONMENT_IS_WASM_WORKER != 'undefined' && ENVIRONMENT_IS_WASM_WORKER;
634-
if (isPthread || isWasmWorker) return;\n'''
635+
Module['expectedDataFileDownloads']++;'''
636+
637+
if not options.modularize and not options.from_emcc:
638+
ret += '''
639+
(() => {'''
640+
641+
ret += '''
642+
// Do not attempt to redownload the virtual filesystem data when in a pthread or a Wasm Worker context.
643+
var isPthread = typeof ENVIRONMENT_IS_PTHREAD != 'undefined' && ENVIRONMENT_IS_PTHREAD;
644+
var isWasmWorker = typeof ENVIRONMENT_IS_WASM_WORKER != 'undefined' && ENVIRONMENT_IS_WASM_WORKER;
645+
if (isPthread || isWasmWorker) return;\n'''
635646

636647
if options.support_node:
637648
ret += " var isNode = typeof process === 'object' && typeof process.versions === 'object' && typeof process.versions.node === 'string';\n"
@@ -812,7 +823,13 @@ def generate_js(data_target, data_files, metadata):
812823
return errback();
813824
}'''
814825
code += '''
815-
if (typeof indexedDB == 'undefined') {
826+
var indexedDB;
827+
if (typeof window === 'object') {
828+
indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
829+
} else if (typeof location !== 'undefined') {
830+
// worker
831+
indexedDB = self.indexedDB;
832+
} else {
816833
throw 'using IndexedDB to cache data can only be done on a web page or in a web worker';
817834
}
818835
try {
@@ -1152,10 +1169,27 @@ def generate_js(data_target, data_files, metadata):
11521169
ret += '''
11531170
}
11541171
loadPackage(%s);\n''' % json.dumps(metadata)
1172+
1173+
if options.modularize and not options.from_emcc:
1174+
ret += '''
1175+
});'''
11551176

11561177
ret += '''
11571178
})();\n'''
11581179

1180+
if options.modularize and not options.from_emcc:
1181+
ret += '''
1182+
(() => {
1183+
var real_createModule = createModule;
1184+
1185+
createModule = function (moduleArg) {
1186+
if (new.target) throw new Error("createModule() should not be called with `new createModule()`");
1187+
return real_createModule(moduleArg);
1188+
}
1189+
})();
1190+
1191+
export default createModule;'''
1192+
11591193
return ret
11601194

11611195

0 commit comments

Comments
 (0)