Skip to content

Commit

Permalink
Cleanup add_emscripten_metadata. NFC. (emscripten-core#8482)
Browse files Browse the repository at this point in the history
get_js_data() used to parse the JS but its since been refactored
such that it can be removed.
  • Loading branch information
sbc100 authored and VirtualTim committed May 21, 2019
1 parent 38b0ea3 commit 32d5af6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
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

0 comments on commit 32d5af6

Please sign in to comment.