@@ -58,6 +58,42 @@ def install_emsdk():
58
58
Module["asm"](asmGlobalArg, asmLibraryArg, buffer, function after_wasm_load(asm) {
59
59
'''
60
60
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 ()
61
97
62
98
def pakoify (js , exe = 'tpython++' , wasmgz = None ):
63
99
assert exe in js
@@ -66,7 +102,19 @@ def pakoify(js, exe='tpython++', wasmgz=None):
66
102
#js = js.replace('(xhr.response)')
67
103
assert js .count ('return WebAssembly.instantiate(binary, info);' ) == 1
68
104
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 , '' )
69
112
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);' )
70
118
71
119
if wasmgz :
72
120
#assert js.count("function getBinary() {")==1
@@ -83,14 +131,10 @@ def pakoify(js, exe='tpython++', wasmgz=None):
83
131
"Module['asm'] = function __module_module_do_asm(global, env, providedBuffer, callback) {"
84
132
)
85
133
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 = {};'
90
134
#r += 'var exports = wasm.instance.exports;'
91
135
#r += "Module['asm'] = exports;"
92
136
#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 ) )
94
138
95
139
assert js .count (EMHACK )== 1
96
140
js = js .replace (EMHACK , EMHACK_NEW )
@@ -100,6 +144,21 @@ def pakoify(js, exe='tpython++', wasmgz=None):
100
144
101
145
js = js .replace ("Module['asm'] = asm;" , '' )
102
146
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
+
103
162
else :
104
163
js = js .replace (
105
164
'return WebAssembly.instantiate(binary, info);' ,
@@ -508,7 +567,10 @@ def rebuild(stage=None, exe_name='tpython++'):
508
567
## SDL1 is better
509
568
#exeopts += ' -s USE_SDL=2'
510
569
#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"""
512
574
pass
513
575
if '--html' in sys .argv :
514
576
exe += '.html'
0 commit comments