Extract the first core module from a component-model WASM.
(component_path, output_path)
| 183 | |
| 184 | |
| 185 | def extract_core_module(component_path, output_path): |
| 186 | """Extract the first core module from a component-model WASM.""" |
| 187 | dump = run(["wasm-tools", "objdump", str(component_path)]) |
| 188 | |
| 189 | for line in dump.splitlines(): |
| 190 | m = re.match( |
| 191 | r"\s*module\b.*?\|\s*(0x[0-9a-fA-F]+)\s*-\s*(0x[0-9a-fA-F]+)\s*\|\s*(\d+)\s*bytes", |
| 192 | line, |
| 193 | ) |
| 194 | if m: |
| 195 | offset = int(m.group(1), 16) |
| 196 | end = int(m.group(2), 16) |
| 197 | size = int(m.group(3)) |
| 198 | break |
| 199 | else: |
| 200 | data = component_path.read_bytes() |
| 201 | pos = data.find(MODULE_MAGIC, len(COMPONENT_MAGIC)) |
| 202 | if pos < 0: |
| 203 | print(f"{RED}Error: could not find core module in component{RESET}", file=sys.stderr) |
| 204 | sys.exit(1) |
| 205 | print(f"{YELLOW}Warning: using fallback module extraction (size may be approximate){RESET}") |
| 206 | output_path.write_bytes(data[pos:]) |
| 207 | return output_path |
| 208 | |
| 209 | data = component_path.read_bytes() |
| 210 | output_path.write_bytes(data[offset:end]) |
| 211 | return output_path |
| 212 | |
| 213 | |
| 214 | # --------------------------------------------------------------------------- |