Files
curl-wasm/native/extract_core.py
T
Sprite 2ff249ff89 Add wasm2c native compilation target and host runtime
Add native/extract_core.py to extract embedded core Wasm modules from curl.wasm and native/wasi_host.c providing a POSIX/OpenSSL WASI 0.2 host shim for wasm2c. Add native, test-native, and run-native recipes to justfile (with positional-arguments enabled), support CURL_WASM_NATIVE in tests/test_curl.py, ignore build artifacts in .gitignore, and document native compilation in README.md.
2026-11-20 00:07:33 +00:00

59 lines
1.7 KiB
Python

#!/usr/bin/env python3
"""Extract the two core WebAssembly modules from the curl.wasm WASI 0.2 component."""
import os
import sys
def read_uleb128(buf: bytes, pos: int) -> tuple[int, int]:
val = 0
shift = 0
while True:
b = buf[pos]
pos += 1
val |= (b & 0x7F) << shift
if not (b & 0x80):
return val, pos
shift += 7
def main() -> int:
if len(sys.argv) != 3:
sys.stderr.write("Usage: extract_core.py <component.wasm> <out_dir>\n")
return 2
comp_path, out_dir = sys.argv[1], sys.argv[2]
data = open(comp_path, "rb").read()
if len(data) < 8 or data[:8] != b"\x00asm\x0d\x00\x01\x00":
sys.stderr.write(f"Error: {comp_path} is not a WASI 0.2 component binary\n")
return 1
modules: list[bytes] = []
pos = 8
while pos < len(data):
sec_id = data[pos]
pos += 1
sec_size, pos = read_uleb128(data, pos)
payload = data[pos : pos + sec_size]
pos += sec_size
if sec_id == 1: # CoreModuleSection
if len(payload) < 8 or payload[:8] != b"\x00asm\x01\x00\x00\x00":
sys.stderr.write("Error: invalid core module payload in component\n")
return 1
modules.append(payload)
if len(modules) != 2:
sys.stderr.write(f"Error: expected 2 core modules in {comp_path}, found {len(modules)}\n")
return 1
os.makedirs(out_dir, exist_ok=True)
with open(os.path.join(out_dir, "mem_mod.wasm"), "wb") as f:
f.write(modules[0])
with open(os.path.join(out_dir, "curl_core.wasm"), "wb") as f:
f.write(modules[1])
return 0
if __name__ == "__main__":
sys.exit(main())