Bläddra i källkod

LibWeb: Treat BufferSource as a DataView/ArrayBuffer/TA in IDL overloads

Required by WebAssembly.instantiate.
Luke Wilde 1 år sedan
förälder
incheckning
7e8d3e370f

+ 16 - 0
Tests/LibWeb/Text/expected/Wasm/WebAssembly-instantiate.txt

@@ -0,0 +1,16 @@
+-------------
+ArrayBuffer
+-------------
+Hello from wasm!!!!!!
+FIXME: Run test for Uint8Array. Not running due to flakiness.
+FIXME: Run test for Uint8ClampedArray. Not running due to flakiness.
+FIXME: Run test for Uint16Array. Not running due to flakiness.
+FIXME: Run test for Uint32Array. Not running due to flakiness.
+FIXME: Run test for Int8Array. Not running due to flakiness.
+FIXME: Run test for Int16Array. Not running due to flakiness.
+FIXME: Run test for Float32Array. Not running due to flakiness.
+FIXME: Run test for Float64Array. Not running due to flakiness.
+FIXME: Run test for BigUint64Array. Not running due to flakiness.
+FIXME: Run test for BigInt64Array. Not running due to flakiness.
+FIXME: Run test for DataView. Not running due to flakiness.
+FIXME: Run test for WebAssembly.Module. Not running due to flakiness.

+ 111 - 0
Tests/LibWeb/Text/input/Wasm/WebAssembly-instantiate.html

@@ -0,0 +1,111 @@
+<script src="../include.js"></script>
+<script>
+    asyncTest(async (done) => {
+        let wasm;
+
+        const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;
+
+        let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
+
+        cachedTextDecoder.decode();
+
+        let cachedUint8Memory0 = null;
+
+        function getUint8Memory0() {
+            if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
+                cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
+            }
+            return cachedUint8Memory0;
+        }
+
+        function getStringFromWasm0(ptr, len) {
+            ptr = ptr >>> 0;
+            return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
+        }
+
+        const exports = {
+            "./wasm_test_bg.js": {
+                __wbg_println_95d984b86202de7b(arg0, arg1) {
+                    println(getStringFromWasm0(arg0, arg1));
+                },
+                greet() {
+                    wasm.greet();
+                },
+            },
+        };
+
+        const arrayBuffer = new Uint8Array([
+            0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x09, 0x02, 0x60, 0x02, 0x7f, 0x7f, 0x00,
+            0x60, 0x00, 0x00, 0x02, 0x34, 0x01, 0x11, 0x2e, 0x2f, 0x77, 0x61, 0x73, 0x6d, 0x5f, 0x74, 0x65,
+            0x73, 0x74, 0x5f, 0x62, 0x67, 0x2e, 0x6a, 0x73, 0x1e, 0x5f, 0x5f, 0x77, 0x62, 0x67, 0x5f, 0x70,
+            0x72, 0x69, 0x6e, 0x74, 0x6c, 0x6e, 0x5f, 0x39, 0x35, 0x64, 0x39, 0x38, 0x34, 0x62, 0x38, 0x36,
+            0x32, 0x30, 0x32, 0x64, 0x65, 0x37, 0x62, 0x00, 0x00, 0x03, 0x02, 0x01, 0x01, 0x05, 0x03, 0x01,
+            0x00, 0x11, 0x07, 0x12, 0x02, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x05, 0x67,
+            0x72, 0x65, 0x65, 0x74, 0x00, 0x01, 0x0a, 0x0d, 0x01, 0x0b, 0x00, 0x41, 0x80, 0x80, 0xc0, 0x00,
+            0x41, 0x15, 0x10, 0x00, 0x0b, 0x0b, 0x1e, 0x01, 0x00, 0x41, 0x80, 0x80, 0xc0, 0x00, 0x0b, 0x15,
+            0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x77, 0x61, 0x73, 0x6d, 0x21,
+            0x21, 0x21, 0x21, 0x21, 0x21, 0x00, 0x7b, 0x09, 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x65, 0x72,
+            0x73, 0x02, 0x08, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x01, 0x04, 0x52, 0x75, 0x73,
+            0x74, 0x00, 0x0c, 0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x65, 0x64, 0x2d, 0x62, 0x79, 0x03,
+            0x05, 0x72, 0x75, 0x73, 0x74, 0x63, 0x1d, 0x31, 0x2e, 0x37, 0x33, 0x2e, 0x30, 0x20, 0x28, 0x63,
+            0x63, 0x36, 0x36, 0x61, 0x64, 0x34, 0x36, 0x38, 0x20, 0x32, 0x30, 0x32, 0x33, 0x2d, 0x31, 0x30,
+            0x2d, 0x30, 0x33, 0x29, 0x06, 0x77, 0x61, 0x6c, 0x72, 0x75, 0x73, 0x06, 0x30, 0x2e, 0x31, 0x39,
+            0x2e, 0x30, 0x0c, 0x77, 0x61, 0x73, 0x6d, 0x2d, 0x62, 0x69, 0x6e, 0x64, 0x67, 0x65, 0x6e, 0x12,
+            0x30, 0x2e, 0x32, 0x2e, 0x38, 0x38, 0x20, 0x28, 0x30, 0x62, 0x35, 0x66, 0x30, 0x65, 0x65, 0x63,
+            0x32, 0x29, 0x00, 0x2c, 0x0f, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x5f, 0x66, 0x65, 0x61, 0x74,
+            0x75, 0x72, 0x65, 0x73, 0x02, 0x2b, 0x0f, 0x6d, 0x75, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x2d, 0x67,
+            0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x73, 0x2b, 0x08, 0x73, 0x69, 0x67, 0x6e, 0x2d, 0x65, 0x78, 0x74
+        ]).buffer;
+
+        const BUFFER_SOURCES = [
+            { constructor: Uint8Array, flaky: true },
+            { constructor: Uint8ClampedArray, flaky: true },
+            { constructor: Uint16Array, flaky: true },
+            { constructor: Uint32Array, flaky: true },
+            { constructor: Int8Array, flaky: true },
+            { constructor: Int16Array, flaky: true },
+            { constructor: Float32Array, flaky: true },
+            { constructor: Float64Array, flaky: true },
+            { constructor: BigUint64Array, flaky: true },
+            { constructor: BigInt64Array, flaky: true },
+            { constructor: DataView, flaky: true },
+        ];
+        
+        async function runTest(buffer) {
+            println("-------------")
+            println(buffer.constructor.name);
+            println("-------------")
+            const module = await WebAssembly.instantiate(buffer, exports);
+            wasm = module.instance?.exports ?? module.exports;
+            try {
+                wasm.greet();
+            } catch (e) {
+                println(`FIXME: Failed to execute with ${buffer.constructor.name}: ${e.name}: ${e.message}`);
+            }
+            return Promise.resolve();
+        }
+
+        await runTest(arrayBuffer);
+
+        for (const { constructor, flaky } of BUFFER_SOURCES) {
+            if (!flaky) {
+                await runTest(new constructor(arrayBuffer));
+            } else {
+                // The flakiness is the runtime either trapping with a RangeError, or the printed string being complete garbage,
+                // which more prominently happens with DataView and the arrays bigger than u8. However, the RangeError flake is
+                // still possible with u8.
+                println(`FIXME: Run test for ${constructor.name}. Not running due to flakiness.`);
+            }
+        }
+
+        if (false) {
+            const compiledModule = await WebAssembly.compile(arrayBuffer);
+            await runTest(compiledModule);
+        } else {
+            // Same issues as above.
+            println(`FIXME: Run test for WebAssembly.Module. Not running due to flakiness.`);
+        }
+
+        done();
+    });
+</script>

+ 3 - 3
Userland/Libraries/LibWeb/WebIDL/OverloadResolution.cpp

@@ -186,7 +186,7 @@ JS::ThrowCompletionOr<ResolvedOverload> resolve_overload(JS::VM& vm, IDL::Effect
         //    then remove from S all other entries.
         else if (value.is_object() && is<JS::ArrayBuffer>(value.as_object())
             && has_overload_with_argument_type_or_subtype_matching(overloads, i, [](IDL::Type const& type) {
-                   if (type.is_plain() && type.name() == "ArrayBuffer")
+                   if (type.is_plain() && (type.name() == "ArrayBuffer" || type.name() == "BufferSource"))
                        return true;
                    if (type.is_object())
                        return true;
@@ -204,7 +204,7 @@ JS::ThrowCompletionOr<ResolvedOverload> resolve_overload(JS::VM& vm, IDL::Effect
         //    then remove from S all other entries.
         else if (value.is_object() && is<JS::DataView>(value.as_object())
             && has_overload_with_argument_type_or_subtype_matching(overloads, i, [](IDL::Type const& type) {
-                   if (type.is_plain() && type.name() == "DataView")
+                   if (type.is_plain() && (type.name() == "DataView" || type.name() == "BufferSource"))
                        return true;
                    if (type.is_object())
                        return true;
@@ -222,7 +222,7 @@ JS::ThrowCompletionOr<ResolvedOverload> resolve_overload(JS::VM& vm, IDL::Effect
         //    then remove from S all other entries.
         else if (value.is_object() && value.as_object().is_typed_array()
             && has_overload_with_argument_type_or_subtype_matching(overloads, i, [&](IDL::Type const& type) {
-                   if (type.is_plain() && type.name() == static_cast<JS::TypedArrayBase const&>(value.as_object()).element_name())
+                   if (type.is_plain() && (type.name() == static_cast<JS::TypedArrayBase const&>(value.as_object()).element_name() || type.name() == "BufferSource"))
                        return true;
                    if (type.is_object())
                        return true;