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

Function create_esm_wrapper

tools/link.py:2152–2191  ·  view source on GitHub ↗
(wrapper_file, support_target, wasm_target)

Source from the content-addressed store, hash-verified

2150
2151
2152def create_esm_wrapper(wrapper_file, support_target, wasm_target):
2153 js_exports = building.user_requested_exports.union(settings.EXPORTED_RUNTIME_METHODS)
2154 js_exports = ', '.join(sorted(js_exports))
2155
2156 wrapper = []
2157 wrapper.append('// The wasm module must be imported here first before the support file')
2158 wrapper.append('// in order to avoid issues with circular dependencies.')
2159 wrapper.append(f"import * as unused from './{settings.WASM_BINARY_FILE}';")
2160 support_url = f'./{os.path.basename(support_target)}'
2161 if js_exports:
2162 wrapper.append(f"export {{ default, {js_exports} }} from '{support_url}';")
2163 else:
2164 wrapper.append(f"export {{ default }} from '{support_url}';")
2165
2166 if settings.ENVIRONMENT_MAY_BE_NODE:
2167 wrapper.append(f'''
2168// When run as the main module under node, create the module directly. This will
2169// execute any startup code along with main (if it exists).
2170import init from '{support_url}';
2171const isNode = {node_detection_code()};
2172if (isNode) {{
2173 const url = await import('node:url');
2174 const isMainModule = url.pathToFileURL(process.argv[1]).href === import.meta.url;
2175 if (isMainModule) await init();
2176}}''')
2177
2178 write_file(wrapper_file, '\n'.join(wrapper) + '\n')
2179
2180 # FIXME(sbc): This is a huge hack to rename the imports in the
2181 # wasm file. Find a better way to do this.
2182 wasm_dis = os.path.join(building.get_binaryen_bin(), 'wasm-dis')
2183 mod = shared.check_call([wasm_dis, wasm_target], stdout=PIPE).stdout
2184 mod = mod.replace('(import "env"', f'(import "{support_url}"')
2185 mod = mod.replace('(import "wasi_snapshot_preview1"', f'(import "{support_url}"')
2186
2187 wasm_as = os.path.join(building.get_binaryen_bin(), 'wasm-as')
2188 cmd = [wasm_as, '--all-features', '-o', wasm_target, '-']
2189 if settings.EMIT_NAME_SECTION:
2190 cmd.append('-g')
2191 shared.check_call(cmd, input=mod)
2192
2193
2194def convert_line_endings_in_file(filename, to_eol):

Callers 1

phase_final_emittingFunction · 0.85

Calls 4

node_detection_codeFunction · 0.85
appendMethod · 0.80
write_fileFunction · 0.70
joinMethod · 0.45

Tested by

no test coverage detected