Skip to content

Commit 9e52a0f

Browse files
committed
uninext: fixed wasm build
1 parent 6841bb4 commit 9e52a0f

File tree

1 file changed

+68
-6
lines changed

1 file changed

+68
-6
lines changed

rebuild.py

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,42 @@ def install_emsdk():
5858
Module["asm"](asmGlobalArg, asmLibraryArg, buffer, function after_wasm_load(asm) {
5959
'''
6060

61+
EMHACK_INIT = '''
62+
var info = {'env': env, 'global': {'NaN': NaN, 'Infinity': Infinity }, 'global.Math': Math, 'asm2wasm': asm2wasmImports};
63+
var wasm = WebAssembly.instantiate(pako.inflate(atob("%s")),info);
64+
console.log(wasm);
65+
wasm.then( function (result){
66+
Module["asm"]=result.instance.exports;
67+
callback(Module.asm); // init global ctors //
68+
//for (var i=0; i<Module.preRun.length; i++)
69+
// Module.preRun[i]();
70+
run();
71+
});
72+
73+
74+
var exports = {};
75+
'''
76+
77+
EMHACK_FS_HEAD = '''
78+
(function() {
79+
var loadPackage = function(metadata) {
80+
81+
function runWithFS() {
82+
'''.strip()
83+
84+
EMHACK_FS_TAIL = '''
85+
if (Module['calledRun']) {
86+
runWithFS();
87+
} else {
88+
if (!Module['preRun']) Module['preRun'] = [];
89+
Module["preRun"].push(runWithFS); // FS is not initialized yet, wait for it
90+
}
91+
92+
}
93+
loadPackage({"files": []});
94+
95+
})();
96+
'''.strip()
6197

6298
def pakoify(js, exe='tpython++', wasmgz=None):
6399
assert exe in js
@@ -66,7 +102,19 @@ def pakoify(js, exe='tpython++', wasmgz=None):
66102
#js = js.replace('(xhr.response)')
67103
assert js.count('return WebAssembly.instantiate(binary, info);') == 1
68104
assert js.count('var exports = createWasm(env);') == 1
105+
assert js.count("Module['preRun'] = [];") == 2
106+
assert js.count("var Module = typeof Module !== 'undefined' ? Module : {};") == 1
107+
108+
assert js.count(EMHACK_FS_HEAD)==1
109+
assert js.count(EMHACK_FS_TAIL)==1
110+
111+
js = js.replace(EMHACK_FS_HEAD, 'function runWithFS() {').replace(EMHACK_FS_TAIL, '')
69112

113+
assert js.count('initRuntime();')==1
114+
js = js.replace('initRuntime();', 'initRuntime(); runWithFS();')
115+
116+
assert js.count('var path = NODEFS.realPath(stream.node);') ==1
117+
js = js.replace('var path = NODEFS.realPath(stream.node);', 'var path = NODEFS.realPath(stream.node);console.log("realPath="+path);')
70118

71119
if wasmgz:
72120
#assert js.count("function getBinary() {")==1
@@ -83,14 +131,10 @@ def pakoify(js, exe='tpython++', wasmgz=None):
83131
"Module['asm'] = function __module_module_do_asm(global, env, providedBuffer, callback) {"
84132
)
85133

86-
r = """var info = {'env': env, 'global': {'NaN': NaN, 'Infinity': Infinity }, 'global.Math': Math, 'asm2wasm': asm2wasmImports};"""
87-
r += 'var wasm = WebAssembly.instantiate(pako.inflate(atob("%s")),info);' % base64.b64encode(wasmgz)
88-
r += 'console.log(wasm);'
89-
r += 'wasm.then( function (result){ Module["asm"]=result.instance.exports; callback(Module.asm); run();} ); var exports = {};'
90134
#r += 'var exports = wasm.instance.exports;'
91135
#r += "Module['asm'] = exports;"
92136
#r += "callback(exports);"
93-
js = js.replace('var exports = createWasm(env);', r)
137+
js = js.replace('var exports = createWasm(env);', EMHACK_INIT % base64.b64encode(wasmgz))
94138

95139
assert js.count(EMHACK)==1
96140
js = js.replace(EMHACK, EMHACK_NEW)
@@ -100,6 +144,21 @@ def pakoify(js, exe='tpython++', wasmgz=None):
100144

101145
js = js.replace("Module['asm'] = asm;", '')
102146

147+
148+
js = js.replace(
149+
"Module['preRun'] = [];",
150+
'/*bypassed preRun=[]*/;'
151+
)
152+
js = js.replace("Module['postRun'] = [];", "")
153+
js = js.replace(
154+
"var Module = typeof Module !== 'undefined' ? Module : {};",
155+
"var Module = typeof Module !== 'undefined' ? Module : {preRun:[], postRun:[]};"
156+
)
157+
158+
assert js.count("var callback = callbacks.shift();") == 1
159+
js = js.replace("var callback = callbacks.shift();", "var callback = callbacks.shift();console.log('callRuntimeCallbacks', callback);")
160+
161+
103162
else:
104163
js = js.replace(
105164
'return WebAssembly.instantiate(binary, info);',
@@ -508,7 +567,10 @@ def rebuild(stage=None, exe_name='tpython++'):
508567
## SDL1 is better
509568
#exeopts += ' -s USE_SDL=2'
510569
#if '--sdl-image' in sys.argv:
511-
exeopts += """ -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='["png"]' -s USE_SDL_MIXER=2"""
570+
571+
## note: png requires zlib
572+
#exeopts += """ -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='["png"]' -s USE_SDL_MIXER=2"""
573+
exeopts += """ -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='["gif"]' -s USE_SDL_MIXER=2 -s TOTAL_MEMORY=512MB"""
512574
pass
513575
if '--html' in sys.argv:
514576
exe += '.html'

0 commit comments

Comments
 (0)