| // REQUIRES: node | |
| // Verify that --js and --text cannot be used together. | |
| // RUN: not wasm-reduce %s --js --text -t %t.test.js -w %t.working.js '--command=node %t.test.js' 2>&1 | filecheck %s --check-prefix=TEXT-ERR | |
| // TEXT-ERR: Fatal: --js and --text cannot be used together | |
| // Verify that an error is reported when no embedded Wasm modules are found. | |
| // RUN: echo "console.log('no wasm');" > %t.empty.js | |
| // RUN: not wasm-reduce %t.empty.js --js -t %t.test.js -w %t.working.js '--command=node %t.test.js' 2>&1 | filecheck %s --check-prefix=EMPTY-ERR | |
| // EMPTY-ERR: Fatal: no embedded wasm modules found | |
| // Reduce a single embedded Wasm module in a JS file and verify the reduced JS | |
| // output and execution behavior. | |
| // RUN: wasm-reduce %s --js -t %t.test.js -w %t.working.js '--command=node %t.test.js' 2>&1 | filecheck %s --check-prefix=LOG | |
| // RUN: filecheck %s < %t.working.js | |
| // RUN: node %t.working.js | filecheck %s --check-prefix=OUT | |
| // LOG: |reducing embedded module 0 of 1 | |
| // LOG: |input size: 57 | |
| // LOG: |finished, final size: 39 | |
| // Reduced module: | |
| // (module | |
| // (func (export "answer") (result i32) | |
| // (i32.const 42) | |
| // ) | |
| // ) | |
| // CHECK: const bytes = new Uint8Array([ | |
| // CHECK-NEXT: 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x05, 0x01, 0x60, 0x00, 0x01, 0x7f, 0x03, | |
| // CHECK-NEXT: 0x02, 0x01, 0x00, 0x07, 0x0a, 0x01, 0x06, 0x61, 0x6e, 0x73, 0x77, 0x65, 0x72, 0x00, 0x00, 0x0a, | |
| // CHECK-NEXT: 0x06, 0x01, 0x04, 0x00, 0x41, 0x2a, 0x0b | |
| // CHECK-NEXT: ]); | |
| // CHECK-NEXT: const inst = new WebAssembly.Instance(new WebAssembly.Module(bytes)); | |
| // CHECK-NEXT: console.log(inst.exports.answer()); | |
| // OUT: 42 | |
| // Original module (57 bytes): | |
| // (module | |
| // (func $unused (param i32) (result i32) | |
| // (i32.mul (local.get 0) (i32.const 99)) | |
| // ) | |
| // (func (export "answer") (result i32) | |
| // (i32.add (i32.const 40) (i32.const 2)) | |
| // ) | |
| // ) | |
| const bytes = new Uint8Array([ | |
| // Magic ("\0asm") and version (1) | |
| 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, | |
| // Type section: 2 types | |
| 0x01, 0x0a, 0x02, | |
| 0x60, 0x01, 0x7f, 0x01, 0x7f, // type 0: (func (param i32) (result i32)) [removed with $unused] | |
| 0x60, 0x00, 0x01, 0x7f, // type 1: (func (result i32)) [kept] | |
| // Function section: 2 functions (type 0 [removed], type 1 [kept]) | |
| 0x03, 0x03, 0x02, 0x00, 0x01, | |
| // Export section: export "answer" -> func 1 | |
| 0x07, 0x0a, 0x01, 0x06, 0x61, 0x6e, 0x73, 0x77, 0x65, 0x72, 0x00, 0x01, | |
| // Code section: 2 function bodies | |
| 0x0a, 0x12, 0x02, | |
| 0x08, 0x00, 0x20, 0x00, 0x41, 0xe3, 0x00, 0x6c, 0x0b, // func 0 ($unused): (i32.mul (local.get 0) (i32.const 99)) [removed] | |
| 0x07, 0x00, 0x41, 0x28, 0x41, 0x02, 0x6a, 0x0b // func 1 ("answer"): (i32.add (i32.const 40) (i32.const 2)) [simplified to (i32.const 42)] | |
| ]); | |
| const inst = new WebAssembly.Instance(new WebAssembly.Module(bytes)); | |
| console.log(inst.exports.answer()); |