OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --expose-wasm |
| 6 |
| 7 'use strict'; |
| 8 |
| 9 load("test/mjsunit/wasm/wasm-constants.js"); |
| 10 load("test/mjsunit/wasm/wasm-module-builder.js"); |
| 11 |
| 12 function module(bytes) { |
| 13 let buffer = bytes; |
| 14 if (typeof buffer === 'string') { |
| 15 buffer = new ArrayBuffer(bytes.length); |
| 16 let view = new Uint8Array(buffer); |
| 17 for (let i = 0; i < bytes.length; ++i) { |
| 18 view[i] = bytes.charCodeAt(i); |
| 19 } |
| 20 } |
| 21 return new WebAssembly.Module(buffer); |
| 22 } |
| 23 |
| 24 function instance(bytes, imports = {}) { |
| 25 return new WebAssembly.Instance(module(bytes), imports); |
| 26 } |
| 27 |
| 28 function builder() { |
| 29 return new WasmModuleBuilder; |
| 30 } |
| 31 |
| 32 function assertCompileError(bytes) { |
| 33 assertThrows(() => module(bytes), WebAssembly.CompileError); |
| 34 } |
| 35 |
| 36 function assertLinkError(bytes, imports = {}) { |
| 37 assertThrows(() => instance(bytes, imports), TypeError); |
| 38 } |
| 39 |
| 40 function assertRuntimeError(bytes, imports = {}) { |
| 41 assertThrows(() => instance(bytes, imports).exports.run(), |
| 42 WebAssembly.RuntimeError); |
| 43 } |
| 44 |
| 45 function assertConversionError(bytes, imports = {}) { |
| 46 assertThrows(() => instance(bytes, imports).exports.run(), TypeError); |
| 47 } |
| 48 |
| 49 (function TestDecodingError() { |
| 50 assertCompileError(""); |
| 51 assertCompileError("X"); |
| 52 assertCompileError("\0x00asm"); |
| 53 })(); |
| 54 |
| 55 (function TestValidationError() { |
| 56 assertCompileError(builder().addFunction("f", kSig_i_v).end().toBuffer()); |
| 57 assertCompileError(builder().addFunction("f", kSig_i_v).addBody([ |
| 58 kExprReturn |
| 59 ]).end().toBuffer()); |
| 60 assertCompileError(builder().addFunction("f", kSig_v_v).addBody([ |
| 61 kExprGetLocal, 0 |
| 62 ]).end().toBuffer()); |
| 63 assertCompileError(builder().addStart(0).toBuffer()); |
| 64 })(); |
| 65 |
| 66 (function TestLinkingError() { |
| 67 let b; |
| 68 |
| 69 b = builder(); |
| 70 b.addImportWithModule("foo", "bar", kSig_v_v); |
| 71 assertLinkError(b.toBuffer(), {}); |
| 72 b = builder(); |
| 73 b.addImportWithModule("foo", "bar", kSig_v_v); |
| 74 assertLinkError(b.toBuffer(), {foo: {}}); |
| 75 b = builder(); |
| 76 b.addImportWithModule("foo", "bar", kSig_v_v); |
| 77 assertLinkError(b.toBuffer(), {foo: {bar: 9}}); |
| 78 |
| 79 b = builder(); |
| 80 b.addImportedGlobal("foo", "bar", kAstI32); |
| 81 assertLinkError(b.toBuffer(), {}); |
| 82 // TODO(titzer): implement stricter import checks for globals. |
| 83 // b = builder(); |
| 84 // b.addImportedGlobal("foo", "bar", kAstI32); |
| 85 // assertLinkError(b.toBuffer(), {foo: {}}); |
| 86 // b = builder(); |
| 87 // b.addImportedGlobal("foo", "bar", kAstI32); |
| 88 // assertLinkError(b.toBuffer(), {foo: {bar: ""}}); |
| 89 // b = builder(); |
| 90 // b.addImportedGlobal("foo", "bar", kAstI32); |
| 91 // assertLinkError(b.toBuffer(), {foo: {bar: () => 9}}); |
| 92 |
| 93 b = builder(); |
| 94 b.addImportedMemory("foo", "bar"); |
| 95 assertLinkError(b.toBuffer(), {}); |
| 96 b = builder(); |
| 97 b.addImportedMemory("foo", "bar"); |
| 98 assertLinkError(b.toBuffer(), {foo: {}}); |
| 99 // TODO(titzer): implement stricter import checks for globals. |
| 100 // b = builder(); |
| 101 // b.addImportedMemory("foo", "bar", 1); |
| 102 // assertLinkError(b.toBuffer(), |
| 103 // {foo: {bar: new WebAssembly.Memory({initial: 0})}}); |
| 104 })(); |
| 105 |
| 106 (function TestTrapError() { |
| 107 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([ |
| 108 kExprUnreachable |
| 109 ]).exportFunc().end().toBuffer()); |
| 110 |
| 111 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([ |
| 112 kExprI32Const, 1, |
| 113 kExprI32Const, 0, |
| 114 kExprI32DivS, |
| 115 kExprDrop |
| 116 ]).exportFunc().end().toBuffer()); |
| 117 |
| 118 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([ |
| 119 ]).exportFunc().end(). |
| 120 addFunction("start", kSig_v_v).addBody([ |
| 121 kExprUnreachable |
| 122 ]).end().addStart(1).toBuffer()); |
| 123 })(); |
| 124 |
| 125 (function TestConversionError() { |
| 126 let b = builder(); |
| 127 b.addImportWithModule("foo", "bar", kSig_v_l); |
| 128 assertConversionError(b.addFunction("run", kSig_v_v).addBody([ |
| 129 kExprI64Const, 0, kExprCallFunction, 0 |
| 130 ]).exportFunc().end().toBuffer()); |
| 131 assertConversionError(builder().addFunction("run", kSig_l_v).addBody([ |
| 132 kExprI64Const, 0 |
| 133 ]).exportFunc().end().toBuffer()); |
| 134 })(); |
OLD | NEW |