Skip to content

Commit 6b3f796

Browse files
committed
LibWeb: Propagate errors from module instantiation
1 parent a458a7d commit 6b3f796

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

Libraries/LibWeb/WebAssembly/WebAssembly.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -232,37 +232,38 @@ JS::ThrowCompletionOr<NonnullOwnPtr<Wasm::ModuleInstance>> instantiate_module(JS
232232
if (!module.import_section().imports().is_empty() && !import_object) {
233233
return vm.throw_completion<JS::TypeError>("ImportObject must be provided when module has imports"sv);
234234
}
235+
235236
// 2. Let imports be « ».
236237
HashMap<Wasm::Linker::Name, Wasm::ExternValue> resolved_imports;
237238
if (import_object) {
238239
dbgln_if(LIBWEB_WASM_DEBUG, "Trying to resolve stuff because import object was specified");
240+
239241
// 3. For each (moduleName, componentName, externtype) of module_imports(module),
240242
for (Wasm::Linker::Name const& import_name : linker.unresolved_imports()) {
241243
dbgln_if(LIBWEB_WASM_DEBUG, "Trying to resolve {}::{}", import_name.module, import_name.name);
244+
242245
// 3.1. Let o be ? Get(importObject, moduleName).
243-
auto value_or_error = import_object->get(MUST(String::from_byte_string(import_name.module)));
244-
if (value_or_error.is_error())
245-
break;
246-
auto value = value_or_error.release_value();
246+
auto value = TRY(import_object->get(MUST(String::from_byte_string(import_name.module))));
247+
247248
// 3.2. If o is not an Object, throw a TypeError exception.
248-
auto object_or_error = value.to_object(vm);
249-
if (object_or_error.is_error())
250-
break;
251-
auto object = object_or_error.release_value();
249+
if (!value.is_object())
250+
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObject, value);
251+
auto const& object = value.as_object();
252+
252253
// 3.3. Let v be ? Get(o, componentName).
253-
auto import_or_error = object->get(MUST(String::from_byte_string(import_name.name)));
254-
if (import_or_error.is_error())
255-
break;
256-
auto import_ = import_or_error.release_value();
254+
auto import_ = TRY(object.get(MUST(String::from_byte_string(import_name.name))));
255+
257256
TRY(import_name.type.visit(
258257
// 3.4. If externtype is of the form func functype,
259258
[&](Wasm::TypeIndex index) -> JS::ThrowCompletionOr<void> {
260259
dbgln_if(LIBWEB_WASM_DEBUG, "Trying to resolve a function {}::{}, type index {}", import_name.module, import_name.name, index.value());
261260
auto& type = module.type_section().types()[index.value()];
262-
// FIXME: 3.4.1. If IsCallable(v) is false, throw a LinkError exception.
261+
262+
// 3.4.1. If IsCallable(v) is false, throw a LinkError exception.
263263
if (!import_.is_function())
264-
return {};
264+
return vm.throw_completion<LinkError>(JS::ErrorType::NotAFunction, import_);
265265
auto& function = import_.as_function();
266+
266267
// 3.4.2. If v has a [[FunctionAddress]] internal slot, and therefore is an Exported Function,
267268
Optional<Wasm::FunctionAddress> address;
268269
if (is<ExportedWasmFunction>(function)) {

Tests/LibWeb/Text/expected/wpt-import/wasm/jsapi/constructor/instantiate-bad-imports.any.txt

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@ Harness status: OK
22

33
Found 212 tests
44

5-
18 Pass
6-
194 Fail
5+
38 Pass
6+
174 Fail
77
Pass WebAssembly.instantiate(module): Non-object imports argument: null
88
Pass WebAssembly.instantiate(module): Non-object imports argument: true
99
Pass WebAssembly.instantiate(module): Non-object imports argument: ""
1010
Pass WebAssembly.instantiate(module): Non-object imports argument: symbol "Symbol()"
1111
Pass WebAssembly.instantiate(module): Non-object imports argument: 1
1212
Pass WebAssembly.instantiate(module): Non-object imports argument: 0.1
1313
Pass WebAssembly.instantiate(module): Non-object imports argument: NaN
14-
Fail WebAssembly.instantiate(module): Non-object module: undefined
15-
Fail WebAssembly.instantiate(module): Non-object module: null
16-
Fail WebAssembly.instantiate(module): Non-object module: true
17-
Fail WebAssembly.instantiate(module): Non-object module: ""
18-
Fail WebAssembly.instantiate(module): Non-object module: symbol "Symbol()"
19-
Fail WebAssembly.instantiate(module): Non-object module: 1
20-
Fail WebAssembly.instantiate(module): Non-object module: 0.1
21-
Fail WebAssembly.instantiate(module): Non-object module: NaN
14+
Pass WebAssembly.instantiate(module): Non-object module: undefined
15+
Pass WebAssembly.instantiate(module): Non-object module: null
16+
Pass WebAssembly.instantiate(module): Non-object module: true
17+
Pass WebAssembly.instantiate(module): Non-object module: ""
18+
Pass WebAssembly.instantiate(module): Non-object module: symbol "Symbol()"
19+
Pass WebAssembly.instantiate(module): Non-object module: 1
20+
Pass WebAssembly.instantiate(module): Non-object module: 0.1
21+
Pass WebAssembly.instantiate(module): Non-object module: NaN
2222
Pass WebAssembly.instantiate(module): Missing imports argument
2323
Pass WebAssembly.instantiate(module): Imports argument with missing property: undefined
24-
Fail WebAssembly.instantiate(module): Imports argument with missing property: empty object
25-
Fail WebAssembly.instantiate(module): Imports argument with missing property: wrong property
24+
Pass WebAssembly.instantiate(module): Imports argument with missing property: empty object
25+
Pass WebAssembly.instantiate(module): Imports argument with missing property: wrong property
2626
Fail WebAssembly.instantiate(module): Importing a function with an incorrectly-typed value: undefined
2727
Fail WebAssembly.instantiate(module): Importing a function with an incorrectly-typed value: null
2828
Fail WebAssembly.instantiate(module): Importing a function with an incorrectly-typed value: true
@@ -117,18 +117,18 @@ Pass WebAssembly.instantiate(buffer): Non-object imports argument: symbol "Symbo
117117
Pass WebAssembly.instantiate(buffer): Non-object imports argument: 1
118118
Pass WebAssembly.instantiate(buffer): Non-object imports argument: 0.1
119119
Pass WebAssembly.instantiate(buffer): Non-object imports argument: NaN
120-
Fail WebAssembly.instantiate(buffer): Non-object module: undefined
121-
Fail WebAssembly.instantiate(buffer): Non-object module: null
122-
Fail WebAssembly.instantiate(buffer): Non-object module: true
123-
Fail WebAssembly.instantiate(buffer): Non-object module: ""
124-
Fail WebAssembly.instantiate(buffer): Non-object module: symbol "Symbol()"
125-
Fail WebAssembly.instantiate(buffer): Non-object module: 1
126-
Fail WebAssembly.instantiate(buffer): Non-object module: 0.1
127-
Fail WebAssembly.instantiate(buffer): Non-object module: NaN
120+
Pass WebAssembly.instantiate(buffer): Non-object module: undefined
121+
Pass WebAssembly.instantiate(buffer): Non-object module: null
122+
Pass WebAssembly.instantiate(buffer): Non-object module: true
123+
Pass WebAssembly.instantiate(buffer): Non-object module: ""
124+
Pass WebAssembly.instantiate(buffer): Non-object module: symbol "Symbol()"
125+
Pass WebAssembly.instantiate(buffer): Non-object module: 1
126+
Pass WebAssembly.instantiate(buffer): Non-object module: 0.1
127+
Pass WebAssembly.instantiate(buffer): Non-object module: NaN
128128
Pass WebAssembly.instantiate(buffer): Missing imports argument
129129
Pass WebAssembly.instantiate(buffer): Imports argument with missing property: undefined
130-
Fail WebAssembly.instantiate(buffer): Imports argument with missing property: empty object
131-
Fail WebAssembly.instantiate(buffer): Imports argument with missing property: wrong property
130+
Pass WebAssembly.instantiate(buffer): Imports argument with missing property: empty object
131+
Pass WebAssembly.instantiate(buffer): Imports argument with missing property: wrong property
132132
Fail WebAssembly.instantiate(buffer): Importing a function with an incorrectly-typed value: undefined
133133
Fail WebAssembly.instantiate(buffer): Importing a function with an incorrectly-typed value: null
134134
Fail WebAssembly.instantiate(buffer): Importing a function with an incorrectly-typed value: true

0 commit comments

Comments
 (0)