emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

Python `TypeError` when trying to add a `sourceMappingURL` section to a WASM file

Open sterzy opened this issue 1 year ago • 0 comments

Please include the following in your bug report:

Version of emscripten/emsdk:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.6 ()
Debian clang version 14.0.6
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /usr/bin

I'm also using Python 3.11.2.

Failing command line in full:

When running this command:

/usr/share/emscripten/tools/wasm-sourcemap.py --dwarfdump-output main.dwarf main.wasm --output main.wasm.sourcemap -u http://example.com/main.wasm.sourcemap -w main-out.wasm

I get this output:

Traceback (most recent call last):
  File "/usr/share/emscripten/tools/wasm-sourcemap.py", line 356, in <module>
    sys.exit(main())
             ^^^^^^
  File "/usr/share/emscripten/tools/wasm-sourcemap.py", line 343, in main
    wasm = append_source_mapping(wasm, options.source_map_url)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/share/emscripten/tools/wasm-sourcemap.py", line 142, in append_source_mapping
    section_content = encode_uint_var(len(section_name)) + section_name + encode_uint_var(len(url)) + url
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
TypeError: can't concat str to bytes

This error happens because the following line tries to concatenate section_name and url with values of type bytes.

https://github.com/emscripten-core/emscripten/blob/493649dc4caa19d75d9650988137810d4ed80eb7/tools/wasm-sourcemap.py#L143

To fix this one could simply encode the two strings and get their values as bytes like so:

section_content = (
        encode_uint_var(len(section_name))
        + bytes(section_name, encoding="utf-8")
        + encode_uint_var(len(url))
        + bytes(url, encoding="utf-8")
    )

I've tested my proposed fix locally and it seems to work as intended.

sterzy avatar Jul 10 '24 13:07 sterzy