Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup add_emscripten_metadata. NFC. #8482

Merged
merged 1 commit into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2739,8 +2739,7 @@ def do_binaryen(target, asm_target, options, memfile, wasm_binary_target,

# after generating the wasm, do some final operations
if shared.Settings.EMIT_EMSCRIPTEN_METADATA:
wso = shared.WebAssembly.add_emscripten_metadata(final, wasm_binary_target)
shutil.move(wso, wasm_binary_target)
shared.WebAssembly.add_emscripten_metadata(final, wasm_binary_target)

if shared.Settings.SIDE_MODULE:
sys.exit(0) # and we are done.
Expand Down
40 changes: 15 additions & 25 deletions tools/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -2978,26 +2978,10 @@ def delebify(buf, offset):
return (result, offset)

@staticmethod
def get_js_data(js_file, shared=False):
def add_emscripten_metadata(js_file, wasm_file):
mem_size = Settings.STATIC_BUMP
table_size = Settings.WASM_TABLE_SIZE
if shared:
mem_align = Settings.MAX_GLOBAL_ALIGN
else:
mem_align = None
return (mem_size, table_size, mem_align)

@staticmethod
def add_emscripten_metadata(js_file, wasm_file):
(mem_size, table_size, _) = WebAssembly.get_js_data(js_file)
logger.debug('creating wasm emscripten metadata section with mem size %d, table size %d' % (mem_size, table_size,))
wso = js_file + '.wso'
wasm = open(wasm_file, 'rb').read()
f = open(wso, 'wb')
f.write(wasm[0:8]) # copy magic number and version
# write the special section
f.write(b'\0') # user section is code 0
# need to find the size of this section
name = b'\x13emscripten_metadata' # section name, including prefixed size
contents = (
# metadata section version
Expand All @@ -3019,19 +3003,25 @@ def add_emscripten_metadata(js_file, wasm_file):
# the EMSCRIPTEN_METADATA_MINOR
)

size = len(name) + len(contents)
f.write(WebAssembly.lebify(size))
f.write(name)
f.write(contents)
f.write(wasm[8:])
f.close()
return wso
orig = open(wasm_file, 'rb').read()
with open(wasm_file, 'wb') as f:
f.write(orig[0:8]) # copy magic number and version
# write the special section
f.write(b'\0') # user section is code 0
# need to find the size of this section
size = len(name) + len(contents)
f.write(WebAssembly.lebify(size))
f.write(name)
f.write(contents)
f.write(orig[8:])

@staticmethod
def make_shared_library(js_file, wasm_file, needed_dynlibs):
# a wasm shared library has a special "dylink" section, see tools-conventions repo
assert not Settings.WASM_BACKEND
mem_size, table_size, mem_align = WebAssembly.get_js_data(js_file, True)
mem_align = Settings.MAX_GLOBAL_ALIGN
mem_size = Settings.STATIC_BUMP
table_size = Settings.WASM_TABLE_SIZE
mem_align = int(math.log(mem_align, 2))
logger.debug('creating wasm dynamic library with mem size %d, table size %d, align %d' % (mem_size, table_size, mem_align))

Expand Down