From 3f2ac9960caedcd1809e1b32454108f802c1fdb1 Mon Sep 17 00:00:00 2001 From: tolauwae Date: Sat, 7 Dec 2024 21:00:28 +0100 Subject: [PATCH] Add basic sourcemap test --- tests/unit/artifacts/compile.output | 233 ++++++++++++++++++++++++++++ tests/unit/artifacts/dump | 100 ++++++++++++ tests/unit/artifacts/upload.wasm | Bin 0 -> 389 bytes tests/unit/sourcemap.test.ts | 19 +++ 4 files changed, 352 insertions(+) create mode 100644 tests/unit/artifacts/compile.output create mode 100644 tests/unit/artifacts/dump create mode 100644 tests/unit/artifacts/upload.wasm diff --git a/tests/unit/artifacts/compile.output b/tests/unit/artifacts/compile.output new file mode 100644 index 0000000..e98befe --- /dev/null +++ b/tests/unit/artifacts/compile.output @@ -0,0 +1,233 @@ +;; SOURCE MAP INFO +0000000: 0061 736d ; WASM_BINARY_MAGIC +0000004: 0100 0000 ; WASM_BINARY_VERSION +; section "Type" (1) +0000008: 01 ; section code +0000009: 0000 0000 00 ; section size (guess) +000000e: 03 ; num types +; func type 0 +000000f: 60 ; func +0000010: 02 ; num params +0000011: 7f ; i32 +0000012: 7f ; i32 +0000013: 00 ; num results +; func type 1 +0000014: 60 ; func +0000015: 01 ; num params +0000016: 7f ; i32 +0000017: 00 ; num results +; func type 2 +0000018: 60 ; func +0000019: 00 ; num params +000001a: 00 ; num results +0000009: 8d80 8080 00 ; FIXUP section size +; section "Import" (2) +000001b: 02 ; section code +000001c: 0000 0000 00 ; section size (guess) +0000021: 03 ; num imports +; import header 0 +0000022: 03 ; string length +0000023: 656e 76 env ; import module name +0000026: 0a ; string length +0000027: 6368 6970 5f64 656c 6179 chip_delay ; import field name +0000031: 00 ; import kind +0000032: 01 ; import signature index +; import header 1 +0000033: 03 ; string length +0000034: 656e 76 env ; import module name +0000037: 0d ; string length +0000038: 6368 6970 5f70 696e 5f6d 6f64 65 chip_pin_mode ; import field name +0000045: 00 ; import kind +0000046: 00 ; import signature index +; import header 2 +0000047: 03 ; string length +0000048: 656e 76 env ; import module name +000004b: 12 ; string length +000004c: 6368 6970 5f64 6967 6974 616c 5f77 7269 chip_digital_wri +000005c: 7465 te ; import field name +000005e: 00 ; import kind +000005f: 00 ; import signature index +000001c: bf80 8080 00 ; FIXUP section size +; section "Function" (3) +0000060: 03 ; section code +0000061: 0000 0000 00 ; section size (guess) +0000066: 02 ; num functions +0000067: 02 ; function 0 signature index +0000068: 02 ; function 1 signature index +0000061: 8380 8080 00 ; FIXUP section size +; section "Global" (6) +0000069: 06 ; section code +000006a: 0000 0000 00 ; section size (guess) +000006f: 03 ; num globals +0000070: 7f ; i32 +0000071: 00 ; global mutability +@ { "line": 13, "col_start": 21, "col_end": 30 } +0000072: 41 ; i32.const +0000073: 17 ; i32 literal +0000074: 0b ; end +0000075: 7f ; i32 +0000076: 00 ; global mutability +@ { "line": 14, "col_start": 20, "col_end": 29 } +0000077: 41 ; i32.const +0000078: 01 ; i32 literal +0000079: 0b ; end +000007a: 7f ; i32 +000007b: 00 ; global mutability +@ { "line": 15, "col_start": 21, "col_end": 30 } +000007c: 41 ; i32.const +000007d: 00 ; i32 literal +000007e: 0b ; end +000006a: 9080 8080 00 ; FIXUP section size +; section "Export" (7) +000007f: 07 ; section code +0000080: 0000 0000 00 ; section size (guess) +0000085: 01 ; num exports +0000086: 04 ; string length +0000087: 6d61 696e main ; export name +000008b: 00 ; export kind +000008c: 04 ; export func index +0000080: 8880 8080 00 ; FIXUP section size +; section "Code" (10) +000008d: 0a ; section code +000008e: 0000 0000 00 ; section size (guess) +0000093: 02 ; num functions +; function body 0 +0000094: 0000 0000 00 ; func body size (guess) +0000099: 00 ; local decl count +@ { "line": 20, "col_start": 5, "col_end": 15 } +000009a: 23 ; global.get +000009b: 00 ; global index +@ { "line": 21, "col_start": 5, "col_end": 14 } +000009c: 41 ; i32.const +000009d: 02 ; i32 literal +@ { "line": 22, "col_start": 5, "col_end": 9 } +000009e: 10 ; call +000009f: 01 ; function index +00000a0: 0b ; end +0000094: 8880 8080 00 ; FIXUP func body size +; function body 1 +00000a1: 0000 0000 00 ; func body size (guess) +00000a6: 01 ; local decl count +00000a7: 01 ; local type count +00000a8: 7f ; i32 +@ { "line": 28, "col_start": 5, "col_end": 14 } +00000a9: 41 ; i32.const +00000aa: e807 ; i32 literal +@ { "line": 29, "col_start": 5, "col_end": 14 } +00000ac: 21 ; local.set +00000ad: 00 ; local index +@ { "line": 32, "col_start": 5, "col_end": 9 } +00000ae: 10 ; call +00000af: 03 ; function index +@ { "line": 35, "col_start": 5, "col_end": 9 } +00000b0: 03 ; loop +00000b1: 40 ; void +@ { "line": 36, "col_start": 7, "col_end": 17 } +00000b2: 23 ; global.get +00000b3: 00 ; global index +@ { "line": 37, "col_start": 7, "col_end": 17 } +00000b4: 23 ; global.get +00000b5: 01 ; global index +@ { "line": 38, "col_start": 7, "col_end": 11 } +00000b6: 10 ; call +00000b7: 02 ; function index +@ { "line": 39, "col_start": 7, "col_end": 16 } +00000b8: 20 ; local.get +00000b9: 00 ; local index +@ { "line": 40, "col_start": 7, "col_end": 11 } +00000ba: 10 ; call +00000bb: 00 ; function index +@ { "line": 41, "col_start": 7, "col_end": 17 } +00000bc: 23 ; global.get +00000bd: 00 ; global index +@ { "line": 42, "col_start": 7, "col_end": 17 } +00000be: 23 ; global.get +00000bf: 02 ; global index +@ { "line": 43, "col_start": 7, "col_end": 11 } +00000c0: 10 ; call +00000c1: 02 ; function index +@ { "line": 44, "col_start": 7, "col_end": 16 } +00000c2: 20 ; local.get +00000c3: 00 ; local index +@ { "line": 45, "col_start": 7, "col_end": 11 } +00000c4: 10 ; call +00000c5: 00 ; function index +@ { "line": 46, "col_start": 7, "col_end": 9 } +00000c6: 0c ; br +00000c7: 00 ; break depth +00000c8: 0b ; end +00000c9: 0b ; end +00000a1: a480 8080 00 ; FIXUP func body size +000008e: b780 8080 00 ; FIXUP section size +; section "name" +00000ca: 00 ; section code +00000cb: 0000 0000 00 ; section size (guess) +00000d0: 04 ; string length +00000d1: 6e61 6d65 name ; custom section name +00000d5: 01 ; name subsection type +00000d6: 0000 0000 00 ; subsection size (guess) +00000db: 05 ; num names +00000dc: 00 ; elem index +00000dd: 0e ; string length +00000de: 656e 762e 6368 6970 5f64 656c 6179 env.chip_delay ; elem name 0 +00000ec: 01 ; elem index +00000ed: 11 ; string length +00000ee: 656e 762e 6368 6970 5f70 696e 5f6d 6f64 env.chip_pin_mod +00000fe: 65 e ; elem name 1 +00000ff: 02 ; elem index +0000100: 16 ; string length +0000101: 656e 762e 6368 6970 5f64 6967 6974 616c env.chip_digital +0000111: 5f77 7269 7465 _write ; elem name 2 +0000117: 03 ; elem index +0000118: 04 ; string length +0000119: 696e 6974 init ; elem name 3 +000011d: 04 ; elem index +000011e: 05 ; string length +000011f: 626c 696e 6b blink ; elem name 4 +00000d6: c980 8080 00 ; FIXUP subsection size +0000124: 02 ; local name type +0000125: 0000 0000 00 ; subsection size (guess) +000012a: 05 ; num functions +000012b: 00 ; function index +000012c: 00 ; num locals +000012d: 01 ; function index +000012e: 00 ; num locals +000012f: 02 ; function index +0000130: 00 ; num locals +0000131: 03 ; function index +0000132: 00 ; num locals +0000133: 04 ; function index +0000134: 01 ; num locals +0000135: 00 ; local index +0000136: 05 ; string length +0000137: 6465 6c61 79 delay ; local name 0 +0000125: 9280 8080 00 ; FIXUP subsection size +000013c: 04 ; name subsection type +000013d: 0000 0000 00 ; subsection size (guess) +0000142: 03 ; num names +0000143: 00 ; elem index +0000144: 12 ; string length +0000145: 696e 7433 322d 3e69 6e74 3332 2d3e 766f int32->int32->vo +0000155: 6964 id ; elem name 0 +0000157: 01 ; elem index +0000158: 0b ; string length +0000159: 696e 7433 322d 3e76 6f69 64 int32->void ; elem name 1 +0000164: 02 ; elem index +0000165: 0a ; string length +0000166: 766f 6964 2d3e 766f 6964 void->void ; elem name 2 +000013d: ae80 8080 00 ; FIXUP subsection size +0000170: 07 ; name subsection type +0000171: 0000 0000 00 ; subsection size (guess) +0000176: 03 ; num names +0000177: 00 ; elem index +0000178: 03 ; string length +0000179: 6c65 64 led ; elem name 0 +000017c: 01 ; elem index +000017d: 02 ; string length +000017e: 6f6e on ; elem name 1 +0000180: 02 ; elem index +0000181: 03 ; string length +0000182: 6f66 66 off ; elem name 2 +0000171: 8f80 8080 00 ; FIXUP subsection size +00000cb: b581 8080 00 ; FIXUP section size + diff --git a/tests/unit/artifacts/dump b/tests/unit/artifacts/dump new file mode 100644 index 0000000..9baf762 --- /dev/null +++ b/tests/unit/artifacts/dump @@ -0,0 +1,100 @@ + +blink.wasm: file format wasm 0x1 +, , , , , +Section Details: + +Type[4]: + - type[0] (i32, i32) -> nil + - type[1] (i32) -> i32 + - type[2] (i32) -> nil + - type[3] () -> nil +Import[4]: + - func[0] sig=2 <- env.chip_delay + - func[1] sig=0 <- env.chip_pin_mode + - func[2] sig=1 <- env.chip_digital_read + - func[3] sig=0 <- env.chip_digital_write +Function[2]: + - func[4] sig=3 + - func[5] sig=3 +Global[3]: + - global[0] i32 mutable=0 - init i32=23 + - global[1] i32 mutable=0 - init i32=1 + - global[2] i32 mutable=0 - init i32=0 +Export[1]: + - func[5] -> "main" +Code[2]: + - func[4] size=8 + - func[5] size=44 +Custom: + - name: "name" + - func[0] +, - func[1] +, - func[2] +, - func[3] +, - func[4] +, - func[5] + - func[5] local[0] + - type[0] int32->void> + - type[1] int32> + - type[2] void> + - type[3] void> + - global[0] + - global[1] + - global[2] + +Code Disassembly: + +0000b6 func[4] : + 0000b7: 23 00 | global.get 0 + 0000b9: 41 02 | i32.const 2 + 0000bb: 10 01 | call 1 + 0000bd: 0b | end +0000c3 func[5] : + 0000c4: 01 7f | local[0] type=i32 + 0000c6: 41 e8 07 | i32.const 1000 + 0000c9: 21 00 | local.set 0 + 0000cb: 10 04 | call 4 + 0000cd: 03 40 | loop + 0000cf: 23 00 | global.get 0 + 0000d1: 23 01 | global.get 1 + 0000d3: 10 03 | call 3 + 0000d5: 20 00 | local.get 0 + 0000d7: 10 00 | call 0 + 0000d9: 23 00 | global.get 0 + 0000db: 10 02 | call 2 + 0000dd: 23 00 | global.get 0 + 0000df: 23 02 | global.get 2 + 0000e1: 10 03 | call 3 + 0000e3: 20 00 | local.get 0 + 0000e5: 10 00 | call 0 + 0000e7: 23 00 | global.get 0 + 0000e9: 10 02 | call 2 + 0000eb: 0c 00 | br 0 + 0000ed: 0b | end + 0000ee: 0b | end +, , , , , +Sourcemap JSON: + +{ + "Functions": [ + {"name": "env.chip_delay", + "locals": [] + }, + {"name": "env.chip_pin_mode", + "locals": [] + }, + {"name": "env.chip_digital_read", + "locals": [] + }, + {"name": "env.chip_digital_write", + "locals": [] + }, + {"name": "init", + "locals": [] + }, + {"name": "blink", + "locals": [{"name": "delay","idx":0}] + } + ] +} + diff --git a/tests/unit/artifacts/upload.wasm b/tests/unit/artifacts/upload.wasm new file mode 100644 index 0000000000000000000000000000000000000000..312589a62e88a5c4b2377e438d5e10896f989458 GIT binary patch literal 389 zcmYk2u};G<5QhIdJ4A{Bg$}TSGM54NiatpiND3zsr-G=6DM2hO2*fimvmr)i-h{^> zcA#|d`R?<7zkV6nbwvQ^vFkdh8MiHF)E|I9JyL~TMrRkYNb{o1Zb1%gd9VCKfi?&02EFM}Nj3 { t.is(WASM.leb128(0), '00'); @@ -9,4 +15,17 @@ test('test wasm/leb128', t => { // t.is(WASM.leb128(64), '40'); t.is(WASM.leb128(128), '8001'); // t.is(WASM.leb128(1202), 'b209'); +}); + +function initialize(): string { + const tmp: string = mkdtempSync(''); + copyFileSync(`${artifacts}/upload.wasm`, `${tmp}/upload.wasm`); + return tmp; +} + +test('test WatMapper/extractLineInfo', async t => { + const dummy = readFileSync(`${artifacts}/compile.output`).toString(); + const mapper: WatMapper = new WatMapper(dummy, initialize(), WABT); + const mapping: SourceMap.Mapping = await mapper.mapping(); + t.true(mapping.lines.length > 0); }); \ No newline at end of file