Skip to content

Commit

Permalink
Revert "Cleanup add_emscripten_metadata. NFC. (emscripten-core#8482)"
Browse files Browse the repository at this point in the history
This reverts commit 32d5af6.
  • Loading branch information
VirtualTim authored May 23, 2019
1 parent 5299572 commit 4ae6f9b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
3 changes: 2 additions & 1 deletion emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2739,7 +2739,8 @@ 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:
shared.WebAssembly.add_emscripten_metadata(final, wasm_binary_target)
wso = shared.WebAssembly.add_emscripten_metadata(final, wasm_binary_target)
shutil.move(wso, wasm_binary_target)

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

@staticmethod
def add_emscripten_metadata(js_file, wasm_file):
def get_js_data(js_file, shared=False):
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 @@ -3003,25 +3019,19 @@ def add_emscripten_metadata(js_file, wasm_file):
# the EMSCRIPTEN_METADATA_MINOR
)

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:])
size = len(name) + len(contents)
f.write(WebAssembly.lebify(size))
f.write(name)
f.write(contents)
f.write(wasm[8:])
f.close()
return wso

@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_align = Settings.MAX_GLOBAL_ALIGN
mem_size = Settings.STATIC_BUMP
table_size = Settings.WASM_TABLE_SIZE
mem_size, table_size, mem_align = WebAssembly.get_js_data(js_file, True)
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 4ae6f9b

Please sign in to comment.