MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / wasm2js

Function wasm2js

tools/building.py:970–1029  ·  view source on GitHub ↗
(js_file, wasm_file, opt_level, use_closure_compiler, debug_info, symbols_file=None, symbols_file_js=None)

Source from the content-addressed store, hash-verified

968
969
970def wasm2js(js_file, wasm_file, opt_level, use_closure_compiler, debug_info, symbols_file=None, symbols_file_js=None):
971 logger.debug('wasm2js')
972 args = ['--emscripten']
973 if opt_level > 0:
974 args += ['-O']
975 if symbols_file:
976 args += ['--symbols-file=%s' % symbols_file]
977 wasm2js_js = run_binaryen_command('wasm2js', wasm_file,
978 args=args,
979 debug=debug_info,
980 stdout=PIPE)
981 if DEBUG:
982 utils.write_file(os.path.join(get_emscripten_temp_dir(), 'wasm2js-output.js'), wasm2js_js)
983 # JS optimizations
984 if opt_level >= 2:
985 passes = []
986 if not debug_info and not settings.PTHREADS:
987 passes += ['minifyNames']
988 if symbols_file_js:
989 passes += ['symbolMap=%s' % symbols_file_js]
990 if settings.MINIFY_WHITESPACE:
991 passes += ['--minify-whitespace']
992 if passes:
993 # hackish fixups to work around wasm2js style and the js optimizer FIXME
994 wasm2js_js = f'// EMSCRIPTEN_START_ASM\n{wasm2js_js}// EMSCRIPTEN_END_ASM\n'
995 wasm2js_js = wasm2js_js.replace('\n function $', '\nfunction $')
996 wasm2js_js = wasm2js_js.replace('\n }', '\n}')
997 temp = shared.get_temp_files().get('.js').name
998 utils.write_file(temp, wasm2js_js)
999 temp = run_js_optimizer(temp, passes)
1000 wasm2js_js = utils.read_file(temp)
1001 # Closure compiler: in mode 1, we just minify the shell. In mode 2, we
1002 # minify the wasm2js output as well, which is ok since it isn't
1003 # validating asm.js.
1004 # TODO: in the non-closure case, we could run a lightweight general-
1005 # purpose JS minifier here.
1006 if use_closure_compiler == 2:
1007 temp = shared.get_temp_files().get('.js').name
1008 with open(temp, 'a', encoding='utf-8') as f:
1009 f.write(wasm2js_js)
1010 temp = closure_compiler(temp, advanced=False)
1011 wasm2js_js = utils.read_file(temp)
1012 # closure may leave a trailing `;`, which would be invalid given where we place
1013 # this code (inside parens)
1014 wasm2js_js = wasm2js_js.strip()
1015 if wasm2js_js[-1] == ';':
1016 wasm2js_js = wasm2js_js[:-1]
1017 all_js = utils.read_file(js_file)
1018 # quoted notation, something like Module['__wasm2jsInstantiate__']
1019 finds = re.findall(r'''[\w\d_$]+\[['"]__wasm2jsInstantiate__['"]\]''', all_js)
1020 if not finds:
1021 # post-closure notation, something like a.__wasm2jsInstantiate__
1022 finds = re.findall(r'''[\w\d_$]+\.__wasm2jsInstantiate__''', all_js)
1023 assert len(finds) == 1
1024 marker = finds[0]
1025 all_js = all_js.replace(marker, f'(\n{wasm2js_js}\n)')
1026 # replace the placeholder with the actual code
1027 js_file += '.wasm2js.js'

Callers

nothing calls this directly

Calls 9

run_binaryen_commandFunction · 0.85
get_emscripten_temp_dirFunction · 0.85
run_js_optimizerFunction · 0.85
closure_compilerFunction · 0.85
write_fileMethod · 0.80
getMethod · 0.65
openFunction · 0.50
joinMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected