Truly independent web browser
LibWeb: Keep a WebAssembly.Module's compile options when cloning it
Andreas Kling07c261dcc1d4
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/module.rs b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/module.rsindex 585179f9994..889d834cad6 100644--- a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/module.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/module.rs@@ -317,6 +317,17 @@ impl Serializable for Module {// 3. Set serialized.[[AgentCluster]] to the current Realm’s corresponding agent cluster.serialized.write_optional_u64(current_agent_cluster());+ // AD-HOC: Recompiling needs the options the module was compiled with, or its builtins and imported strings+ // would turn into ordinary imports.+ serialized.write_u64(self.builtin_sets.len() as u64);+ for builtin_set in &self.builtin_sets {+ serialized.write_utf16_string(builtin_set.as_utf16_string());+ }+ serialized.write_bool(self.imported_string_module.is_some());+ if let Some(imported_string_module) = &self.imported_string_module {+ serialized.write_utf16_string(imported_string_module.as_utf16_string());+ }+Ok(())}@@ -335,8 +346,20 @@ impl Serializable for Module {));}+ // AD-HOC: See the serialization steps.+ let builtin_set_count = serialized.read_u64()?;+ let mut builtin_sets = Vec::new();+ for _ in 0..builtin_set_count {+ builtin_sets.push(UsvString::from_utf16_string(&serialized.read_utf16_string()?));+ }+ let imported_string_module = if serialized.read_bool()? {+ Some(UsvString::from_utf16_string(&serialized.read_utf16_string()?))+ } else {+ None+ };+// 4. Compile a WebAssembly module from bytes and set value.[[Module]] to the result.let module = compile_a_webassembly_module(cx.vm(), cx.realm(), &bytes)?;- Ok(Self::new(module, bytes, Vec::new(), None))+ Ok(Self::new(module, bytes, builtin_sets, imported_string_module))}}diff --git a/Tests/LibWeb/TestStructuredSerializeFormat.cpp b/Tests/LibWeb/TestStructuredSerializeFormat.cppindex 0f961b96b45..11cdb470365 100644--- a/Tests/LibWeb/TestStructuredSerializeFormat.cpp+++ b/Tests/LibWeb/TestStructuredSerializeFormat.cpp@@ -327,6 +327,8 @@ TEST_CASE(webassembly_module_encodes_to_frozen_ipc_bytes)expected.append(agent_cluster_id.has_value() ? 1 : 0);if (agent_cluster_id.has_value())append_little_endian_u64(expected, *agent_cluster_id);+ append_little_endian_u64(expected, 0);+ expected.append(0);EXPECT_EQ(record.data.span(), expected.span());}diff --git a/Tests/LibWeb/Text/expected/Wasm/WebAssembly-Module-serialization-of-compile-options.txt b/Tests/LibWeb/Text/expected/Wasm/WebAssembly-Module-serialization-of-compile-options.txtnew file mode 100644index 00000000000..66f198f18c6--- /dev/null+++ b/Tests/LibWeb/Text/expected/Wasm/WebAssembly-Module-serialization-of-compile-options.txt@@ -0,0 +1,2 @@+structuredClone: hello 3+worker: hello 3diff --git a/Tests/LibWeb/Text/input/Wasm/WebAssembly-Module-serialization-of-compile-options.html b/Tests/LibWeb/Text/input/Wasm/WebAssembly-Module-serialization-of-compile-options.htmlnew file mode 100644index 00000000000..79e1519786b--- /dev/null+++ b/Tests/LibWeb/Text/input/Wasm/WebAssembly-Module-serialization-of-compile-options.html@@ -0,0 +1,51 @@+<!doctype html>+<script type="worker">+ onmessage = ({ data }) => {+ const { exports } = new WebAssembly.Instance(data, {});+ postMessage(`${exports.hello.value} ${exports.length("abc")}`);+ };+ onmessageerror = () => postMessage("messageerror");+</script>+<script src="../include.js"></script>+<script>+ asyncTest(done => {+ const string = text => [text.length, ...new TextEncoder().encode(text)];+ const section = (id, contents) => [id, contents.length, ...contents];++ // (module+ // (import "wasm:js-string" "length" (func $length (param externref) (result i32)))+ // (import "strings" "hello" (global $hello externref))+ // (export "length" (func $length))+ // (export "hello" (global $hello)))+ const bytes = new Uint8Array([+ ...[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00],+ ...section(1, [0x01, 0x60, 0x01, 0x6f, 0x01, 0x7f]),+ ...section(2, [+ 0x02,+ ...string("wasm:js-string"),+ ...string("length"),+ ...[0x00, 0x00],+ ...string("strings"),+ ...string("hello"),+ ...[0x03, 0x6f, 0x00],+ ]),+ ...section(7, [0x02, ...string("length"), ...[0x00, 0x00], ...string("hello"), ...[0x03, 0x00]]),+ ]);+ const module = new WebAssembly.Module(bytes, { builtins: ["js-string"], importedStringConstants: "strings" });++ try {+ const { exports } = new WebAssembly.Instance(structuredClone(module), {});+ println(`structuredClone: ${exports.hello.value} ${exports.length("abc")}`);+ } catch (error) {+ println(`structuredClone: ${error}`);+ }++ const source = document.querySelector("[type=worker]").innerText;+ const worker = new Worker(URL.createObjectURL(new Blob([source], { type: "text/javascript" })));+ worker.onmessage = ({ data }) => {+ println(`worker: ${data}`);+ done();+ };+ worker.postMessage(module);+ });+</script>