1// META: global=window,dedicatedworker,jsshell 2// META: script=/wasm/jsapi/wasm-module-builder.js 3 4function assert_ModuleImportDescriptor(import_, expected) { 5 assert_equals(Object.getPrototypeOf(import_), Object.prototype, "Prototype"); 6 assert_true(Object.isExtensible(import_), "isExtensible"); 7 8 const module = Object.getOwnPropertyDescriptor(import_, "module"); 9 assert_true(module.writable, "module: writable"); 10 assert_true(module.enumerable, "module: enumerable"); 11 assert_true(module.configurable, "module: configurable"); 12 assert_equals(module.value, expected.module); 13 14 const name = Object.getOwnPropertyDescriptor(import_, "name"); 15 assert_true(name.writable, "name: writable"); 16 assert_true(name.enumerable, "name: enumerable"); 17 assert_true(name.configurable, "name: configurable"); 18 assert_equals(name.value, expected.name); 19 20 const kind = Object.getOwnPropertyDescriptor(import_, "kind"); 21 assert_true(kind.writable, "kind: writable"); 22 assert_true(kind.enumerable, "kind: enumerable"); 23 assert_true(kind.configurable, "kind: configurable"); 24 assert_equals(kind.value, expected.kind); 25} 26 27function assert_imports(imports, expected) { 28 assert_true(Array.isArray(imports), "Should be array"); 29 assert_equals(Object.getPrototypeOf(imports), Array.prototype, "Prototype"); 30 assert_true(Object.isExtensible(imports), "isExtensible"); 31 32 assert_equals(imports.length, expected.length); 33 for (let i = 0; i < expected.length; ++i) { 34 assert_ModuleImportDescriptor(imports[i], expected[i]); 35 } 36} 37 38let emptyModuleBinary; 39setup(() => { 40 emptyModuleBinary = new WasmModuleBuilder().toBuffer(); 41}); 42 43test(() => { 44 assert_throws_js(TypeError, () => WebAssembly.Module.imports()); 45}, "Missing arguments"); 46 47test(() => { 48 const invalidArguments = [ 49 undefined, 50 null, 51 true, 52 "", 53 Symbol(), 54 1, 55 {}, 56 WebAssembly.Module, 57 WebAssembly.Module.prototype, 58 ]; 59 for (const argument of invalidArguments) { 60 assert_throws_js(TypeError, () => WebAssembly.Module.imports(argument), 61 `imports(${format_value(argument)})`); 62 } 63}, "Non-Module arguments"); 64 65test(() => { 66 const module = new WebAssembly.Module(emptyModuleBinary); 67 const fn = WebAssembly.Module.imports; 68 const thisValues = [ 69 undefined, 70 null, 71 true, 72 "", 73 Symbol(), 74 1, 75 {}, 76 WebAssembly.Module, 77 WebAssembly.Module.prototype, 78 ]; 79 for (const thisValue of thisValues) { 80 assert_array_equals(fn.call(thisValue, module), []); 81 } 82}, "Branding"); 83 84test(() => { 85 const module = new WebAssembly.Module(emptyModuleBinary); 86 const imports = WebAssembly.Module.imports(module); 87 assert_true(Array.isArray(imports)); 88}, "Return type"); 89 90test(() => { 91 const module = new WebAssembly.Module(emptyModuleBinary); 92 const imports = WebAssembly.Module.imports(module); 93 assert_imports(imports, []); 94}, "Empty module"); 95 96test(() => { 97 const module = new WebAssembly.Module(emptyModuleBinary); 98 assert_not_equals(WebAssembly.Module.imports(module), WebAssembly.Module.imports(module)); 99}, "Empty module: array caching"); 100 101test(() => { 102 const builder = new WasmModuleBuilder(); 103 104 builder.addImport("module", "fn", kSig_v_v); 105 builder.addImportedGlobal("module", "global", kWasmI32); 106 builder.addImportedMemory("module", "memory", 0, 128); 107 builder.addImportedTable("module", "table", 0, 128); 108 109 const buffer = builder.toBuffer() 110 const module = new WebAssembly.Module(buffer); 111 const imports = WebAssembly.Module.imports(module); 112 const expected = [ 113 { "module": "module", "kind": "function", "name": "fn" }, 114 { "module": "module", "kind": "global", "name": "global" }, 115 { "module": "module", "kind": "memory", "name": "memory" }, 116 { "module": "module", "kind": "table", "name": "table" }, 117 ]; 118 assert_imports(imports, expected); 119}, "imports"); 120 121test(() => { 122 const builder = new WasmModuleBuilder(); 123 124 builder.addImport("", "fn", kSig_v_v); 125 builder.addImportedGlobal("", "global", kWasmI32); 126 builder.addImportedMemory("", "memory", 0, 128); 127 builder.addImportedTable("", "table", 0, 128); 128 129 const buffer = builder.toBuffer() 130 const module = new WebAssembly.Module(buffer); 131 const imports = WebAssembly.Module.imports(module); 132 const expected = [ 133 { "module": "", "kind": "function", "name": "fn" }, 134 { "module": "", "kind": "global", "name": "global" }, 135 { "module": "", "kind": "memory", "name": "memory" }, 136 { "module": "", "kind": "table", "name": "table" }, 137 ]; 138 assert_imports(imports, expected); 139}, "imports with empty module name"); 140 141test(() => { 142 const builder = new WasmModuleBuilder(); 143 144 builder.addImport("a", "", kSig_v_v); 145 builder.addImportedGlobal("b", "", kWasmI32); 146 builder.addImportedMemory("c", "", 0, 128); 147 builder.addImportedTable("d", "", 0, 128); 148 149 const buffer = builder.toBuffer() 150 const module = new WebAssembly.Module(buffer); 151 const imports = WebAssembly.Module.imports(module); 152 const expected = [ 153 { "module": "a", "kind": "function", "name": "" }, 154 { "module": "b", "kind": "global", "name": "" }, 155 { "module": "c", "kind": "memory", "name": "" }, 156 { "module": "d", "kind": "table", "name": "" }, 157 ]; 158 assert_imports(imports, expected); 159}, "imports with empty names"); 160 161test(() => { 162 const builder = new WasmModuleBuilder(); 163 164 builder.addImport("", "", kSig_v_v); 165 builder.addImportedGlobal("", "", kWasmI32); 166 builder.addImportedMemory("", "", 0, 128); 167 builder.addImportedTable("", "", 0, 128); 168 169 const buffer = builder.toBuffer() 170 const module = new WebAssembly.Module(buffer); 171 const imports = WebAssembly.Module.imports(module); 172 const expected = [ 173 { "module": "", "kind": "function", "name": "" }, 174 { "module": "", "kind": "global", "name": "" }, 175 { "module": "", "kind": "memory", "name": "" }, 176 { "module": "", "kind": "table", "name": "" }, 177 ]; 178 assert_imports(imports, expected); 179}, "imports with empty module names and names"); 180 181test(() => { 182 const module = new WebAssembly.Module(emptyModuleBinary); 183 const imports = WebAssembly.Module.imports(module, {}); 184 assert_imports(imports, []); 185}, "Stray argument"); 186