Truly independent web browser
LibWeb: Throw a TypeError for a missing WebAssembly table value
Shannon Booth076026cc441d
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/global.rs b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/global.rsindex 759dfaef146..67f89df933f 100644--- a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/global.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/global.rs@@ -79,10 +79,13 @@ impl Global {return Err(Exception::type_error("V128 is not supported as a global value type"));}- // FIXME: https://github.com/WebAssembly/spec/issues/1861- // Is there a difference between *missing* and undefined for optional any values?+ // 4. If v is missing,+ // 1. Let value be DefaultValue(valuetype).+ // 2. Assert: value is not error.+ // 5. Otherwise,+ // 1. Let value be ? ToWebAssemblyValue(v, valuetype).let value = match v {- None => default_webassembly_value(vm, value_type),+ None => default_webassembly_value(vm, value_type).expect("ToValueType gives types with a default value"),Some(v) => to_webassembly_value(vm, v, value_type)?,};diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/table.rs b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/table.rsindex 4addf99958e..8842af34745 100644--- a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/table.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/table.rs@@ -107,7 +107,13 @@ impl Table {));}- let reference = reference_of(Self::value_or_default(vm, ValType::Ref(element), value)?);+ // 8. If value is missing,+ // 1. Let ref be DefaultValue(elementtype).+ // 2. Assert: ref is not error.+ // 9. Otherwise,+ // 1. Let ref be ? ToWebAssemblyValue(value, elementtype).+ let reference = Self::value_or_default(vm, ValType::Ref(element), value)?+ .expect("ToValueType gives types with a default value");// 11. Let (store, tableaddr) be table_alloc(store, type, ref). If allocation fails, throw a RangeError exception.// 12. Set the surrounding agent's associated store to store.@@ -135,10 +141,11 @@ impl Table {Ok(Self::initialize(vm, table))}- fn value_or_default(vm: &Vm, ty: ValType, value: Option<Value>) -> WebIdlResult<WasmValue> {+ /// Returns `None` where DefaultValue(ty) is an error.+ fn value_or_default(vm: &Vm, ty: ValType, value: Option<Value>) -> WebIdlResult<Option<u64>> {Ok(match value {- None => default_webassembly_value(vm, ty),- Some(value) => to_webassembly_value(vm, value, ty)?,+ None => default_webassembly_value(vm, ty).map(reference_of),+ Some(value) => Some(reference_of(to_webassembly_value(vm, value, ty)?)),})}@@ -192,8 +199,13 @@ impl Table {let delta = address_value_to_u64(vm, delta, address64)?;+ // 6. If value is missing,+ // 1. Let ref be DefaultValue(elementtype).+ // 2. If ref is error, throw a TypeError exception.// 7. Otherwise,- let reference = reference_of(Self::value_or_default(vm, ValType::Ref(ty.element), value)?);+ // 1. Let ref be ? ToWebAssemblyValue(value, elementtype).+ let reference = Self::value_or_default(vm, ValType::Ref(ty.element), value)?+ .ok_or_else(|| Exception::type_error("The table's element type has no default value"))?;// 10. Set the surrounding agent's associated store to result.let initial_size = Agent::the(vm)@@ -257,8 +269,13 @@ impl Table {let index = address_value_to_u64(vm, index, ty.limits.address_type == CoreAddressType::I64)?;+ // 6. If value is missing,+ // 1. Let ref be DefaultValue(elementtype).+ // 2. If ref is error, throw a TypeError exception.// 7. Otherwise,- let reference = reference_of(Self::value_or_default(vm, ValType::Ref(ty.element), value)?);+ // 1. Let ref be ? ToWebAssemblyValue(value, elementtype).+ let reference = Self::value_or_default(vm, ValType::Ref(ty.element), value)?+ .ok_or_else(|| Exception::type_error("The table's element type has no default value"))?;// 8. Let store be table_write(store, tableaddr, index64, ref).// 9. If store is error, throw a RangeError exception.diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/values.rs b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/values.rsindex 55a5457653a..c840292a54f 100644--- a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/values.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/values.rs@@ -203,17 +203,24 @@ pub(crate) fn to_webassembly_value(vm: &Vm, value: Value, ty: ValType) -> ThrowC}/// DefaultValue(valuetype), https://webassembly.github.io/spec/js-api/#defaultvalue-pub(crate) fn default_webassembly_value(vm: &Vm, ty: ValType) -> WasmValue {- // 1. If valuetype equals externref, return ToWebAssemblyValue(undefined, valuetype).- if let ValType::Ref(RefType {- heap: HeapType::Extern, ..- }) = ty+/// Returns `None` where it is an error.+pub(crate) fn default_webassembly_value(vm: &Vm, ty: ValType) -> Option<WasmValue> {+ // 1. If valuetype equals externref, return ! ToWebAssemblyValue(undefined, valuetype).+ if ty+ == ValType::Ref(RefType {+ nullable: true,+ heap: HeapType::Extern,+ }){- return WasmValue::Ref(host_reference_of(vm, Value::UNDEFINED));+ return Some(WasmValue::Ref(host_reference_of(vm, Value::UNDEFINED)));}- // 2. Return DefaultValue(valuetype).- WasmValue::default_for(ty)+ // 2. Return val_default(valuetype).+ // NB: A non-nullable reference type has no default value.+ match ty {+ ValType::Ref(RefType { nullable: false, .. }) => None,+ _ => Some(WasmValue::default_for(ty)),+ }}/// ToJSValue(w), https://webassembly.github.io/spec/js-api/#tojsvalue, for a value of the type, which is in the store'sdiff --git a/Tests/LibWeb/Text/expected/Wasm/WebAssembly-Table-non-nullable-default.txt b/Tests/LibWeb/Text/expected/Wasm/WebAssembly-Table-non-nullable-default.txtnew file mode 100644index 00000000000..4028a6eb54d--- /dev/null+++ b/Tests/LibWeb/Text/expected/Wasm/WebAssembly-Table-non-nullable-default.txt@@ -0,0 +1,7 @@+(ref func) set(0): TypeError+(ref func) grow(1): TypeError+(ref func) length: 1, get(0) is f: true+(ref extern) set(0): TypeError+(ref extern) grow(1): TypeError+(ref extern) length: 1+externref grow(1): returned 0, get(0): undefineddiff --git a/Tests/LibWeb/Text/input/Wasm/WebAssembly-Table-non-nullable-default.html b/Tests/LibWeb/Text/input/Wasm/WebAssembly-Table-non-nullable-default.htmlnew file mode 100644index 00000000000..3af7b719bb3--- /dev/null+++ b/Tests/LibWeb/Text/input/Wasm/WebAssembly-Table-non-nullable-default.html@@ -0,0 +1,42 @@+<!doctype html>+<script src="../include.js"></script>+<script>+ // (module+ // (func $f (export "f"))+ // (table (export "t") 1 10 (ref func) (ref.func $f)))+ const funcTableBytes = new Uint8Array([+ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00, 0x03, 0x02,+ 0x01, 0x00, 0x04, 0x0b, 0x01, 0x40, 0x00, 0x64, 0x70, 0x01, 0x01, 0x0a, 0xd2, 0x00, 0x0b, 0x07,+ 0x09, 0x02, 0x01, 0x74, 0x01, 0x00, 0x01, 0x66, 0x00, 0x00, 0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,+ ]);++ // (module+ // (table (export "t") 1 10 (ref extern) (extern.convert_any (ref.i31 (i32.const 0)))))+ const externTableBytes = new Uint8Array([+ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x04, 0x0f, 0x01, 0x40, 0x00, 0x64, 0x6f, 0x01,+ 0x01, 0x0a, 0x41, 0x00, 0xfb, 0x1c, 0xfb, 0x1b, 0x0b, 0x07, 0x05, 0x01, 0x01, 0x74, 0x01, 0x00,+ ]);++ function attempt(fn) {+ try {+ return `returned ${fn()}`;+ } catch (e) {+ return e.name;+ }+ }++ test(() => {+ const funcTable = new WebAssembly.Instance(new WebAssembly.Module(funcTableBytes)).exports;+ println(`(ref func) set(0): ${attempt(() => funcTable.t.set(0))}`);+ println(`(ref func) grow(1): ${attempt(() => funcTable.t.grow(1))}`);+ println(`(ref func) length: ${funcTable.t.length}, get(0) is f: ${funcTable.t.get(0) === funcTable.f}`);++ const externTable = new WebAssembly.Instance(new WebAssembly.Module(externTableBytes)).exports;+ println(`(ref extern) set(0): ${attempt(() => externTable.t.set(0))}`);+ println(`(ref extern) grow(1): ${attempt(() => externTable.t.grow(1))}`);+ println(`(ref extern) length: ${externTable.t.length}`);++ const nullableTable = new WebAssembly.Table({ element: "externref", initial: 0 });+ println(`externref grow(1): ${attempt(() => nullableTable.grow(1))}, get(0): ${nullableTable.get(0)}`);+ });+</script>