Skip to content

Commit

Permalink
Bug 1804575 [wpt PR 37393] - [rab/gsab] Add WPT tests for resizable A…
Browse files Browse the repository at this point in the history
…rrayBuffer, a=testonly

Automatic update from web-platform-tests
[rab/gsab] Add WPT tests for resizable ArrayBuffer

Bug: v8:11111
Change-Id: I531f10cae15dd09e7934ad698acf9dcae225ca4c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4082152
Reviewed-by: Domenic Denicola <[email protected]>
Commit-Queue: Shu-yu Guo <[email protected]>
Reviewed-by: Mason Freed <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1086557}

--

wpt-commits: 5656a2f4653b5894c500b724778009ca9a26e48c
wpt-pr: 37393
  • Loading branch information
syg authored and moz-wptsync-bot committed Jan 5, 2023
1 parent 679f28f commit c82b487
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 3 deletions.
6 changes: 3 additions & 3 deletions testing/web-platform/tests/common/sab.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ const createBuffer = (() => {
} catch(e) {
sabConstructor = null;
}
return (type, length) => {
return (type, length, opts) => {
if (type === "ArrayBuffer") {
return new ArrayBuffer(length);
return new ArrayBuffer(length, opts);
} else if (type === "SharedArrayBuffer") {
if (sabConstructor && sabConstructor.name !== "SharedArrayBuffer") {
throw new Error("WebAssembly.Memory does not support shared:true");
}
return new sabConstructor(length);
return new sabConstructor(length, opts);
} else {
throw new Error("type has to be ArrayBuffer or SharedArrayBuffer");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// META: global=window,worker
// META: script=/common/sab.js
// META: script=/html/webappapis/structured-clone/structured-clone-battery-of-tests.js
// META: script=/html/webappapis/structured-clone/structured-clone-battery-of-tests-with-transferables.js
// META: script=/html/webappapis/structured-clone/structured-clone-battery-of-tests-harness.js
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// META: script=/common/sab.js
// META: script=/html/webappapis/structured-clone/structured-clone-battery-of-tests.js
// META: script=/html/webappapis/structured-clone/structured-clone-battery-of-tests-with-transferables.js
// META: script=/html/webappapis/structured-clone/structured-clone-battery-of-tests-harness.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,73 @@ structuredCloneBatteryOfTests.push({
assert_equals(Object.getPrototypeOf(transfer), ReadableStream.prototype);
}
});

structuredCloneBatteryOfTests.push({
description: 'Resizable ArrayBuffer is transferable',
async f(runner) {
const buffer = new ArrayBuffer(16, { maxByteLength: 1024 });
const copy = await runner.structuredClone(buffer, [buffer]);
assert_equals(buffer.byteLength, 0);
assert_equals(copy.byteLength, 16);
assert_equals(copy.maxByteLength, 1024);
assert_true(copy.resizable);
}
});

structuredCloneBatteryOfTests.push({
description: 'Length-tracking TypedArray is transferable',
async f(runner) {
const ab = new ArrayBuffer(16, { maxByteLength: 1024 });
const ta = new Uint8Array(ab);
const copy = await runner.structuredClone(ta, [ab]);
assert_equals(ab.byteLength, 0);
assert_equals(copy.buffer.byteLength, 16);
assert_equals(copy.buffer.maxByteLength, 1024);
assert_true(copy.buffer.resizable);
copy.buffer.resize(32);
assert_equals(copy.byteLength, 32);
}
});

structuredCloneBatteryOfTests.push({
description: 'Length-tracking DataView is transferable',
async f(runner) {
const ab = new ArrayBuffer(16, { maxByteLength: 1024 });
const dv = new DataView(ab);
const copy = await runner.structuredClone(dv, [ab]);
assert_equals(ab.byteLength, 0);
assert_equals(copy.buffer.byteLength, 16);
assert_equals(copy.buffer.maxByteLength, 1024);
assert_true(copy.buffer.resizable);
copy.buffer.resize(32);
assert_equals(copy.byteLength, 32);
}
});

structuredCloneBatteryOfTests.push({
description: 'Transferring OOB TypedArray throws',
async f(runner, t) {
const ab = new ArrayBuffer(16, { maxByteLength: 1024 });
const ta = new Uint8Array(ab, 8);
ab.resize(0);
await promise_rejects_dom(
t,
"DataCloneError",
runner.structuredClone(ta, [ab])
);
}
});

structuredCloneBatteryOfTests.push({
description: 'Transferring OOB DataView throws',
async f(runner, t) {
const ab = new ArrayBuffer(16, { maxByteLength: 1024 });
const dv = new DataView(ab, 8);
ab.resize(0);
await promise_rejects_dom(
t,
"DataCloneError",
runner.structuredClone(dv, [ab])
);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,23 @@ check('FileList empty', func_FileList_empty, compare_FileList, true);
check('Array FileList object, FileList empty', () => ([func_FileList_empty()]), compare_Array(enumerate_props(compare_FileList)), true);
check('Object FileList object, FileList empty', () => ({'x':func_FileList_empty()}), compare_Object(enumerate_props(compare_FileList)), true);

function compare_ArrayBuffer(actual, input) {
assert_true(actual instanceof ArrayBuffer, 'instanceof ArrayBuffer');
assert_equals(actual.byteLength, input.byteLength, 'byteLength');
assert_equals(actual.maxByteLength, input.maxByteLength, 'maxByteLength');
assert_equals(actual.resizable, input.resizable, 'resizable');
assert_equals(actual.growable, input.growable, 'growable');
}

function compare_ArrayBufferView(view) {
const Type = self[view];
return function(actual, input) {
if (typeof actual === 'string')
assert_unreached(actual);
assert_true(actual instanceof Type, 'instanceof '+view);
assert_equals(actual.length, input.length, 'length');
assert_equals(actual.byteLength, input.byteLength, 'byteLength');
assert_equals(actual.byteOffset, input.byteOffset, 'byteOffset');
assert_not_equals(actual.buffer, input.buffer, 'buffer');
for (let i = 0; i < actual.length; ++i) {
assert_equals(actual[i], input[i], 'actual['+i+']');
Expand Down Expand Up @@ -667,3 +677,77 @@ check(
assert_equals(Object.getPrototypeOf(copy), File.prototype);
}
);

check(
'Resizable ArrayBuffer',
() => {
const ab = new ArrayBuffer(16, { maxByteLength: 1024 });
assert_true(ab.resizable);
return ab;
},
compare_ArrayBuffer);

structuredCloneBatteryOfTests.push({
description: 'Growable SharedArrayBuffer',
async f(runner) {
const sab = createBuffer('SharedArrayBuffer', 16, { maxByteLength: 1024 });
assert_true(sab.growable);
try {
const copy = await runner.structuredClone(sab);
compare_ArrayBuffer(sab, copy);
} catch (e) {
// If we're cross-origin isolated, cloning SABs should not fail.
if (e instanceof DOMException && e.code === DOMException.DATA_CLONE_ERR) {
assert_false(self.crossOriginIsolated);
} else {
throw e;
}
}
}
});

check(
'Length-tracking TypedArray',
() => {
const ab = new ArrayBuffer(16, { maxByteLength: 1024 });
assert_true(ab.resizable);
return new Uint8Array(ab);
},
compare_ArrayBufferView('Uint8Array'));

check(
'Length-tracking DataView',
() => {
const ab = new ArrayBuffer(16, { maxByteLength: 1024 });
assert_true(ab.resizable);
return new DataView(ab);
},
compare_ArrayBufferView('DataView'));

structuredCloneBatteryOfTests.push({
description: 'Serializing OOB TypedArray throws',
async f(runner, t) {
const ab = new ArrayBuffer(16, { maxByteLength: 1024 });
const ta = new Uint8Array(ab, 8);
ab.resize(0);
await promise_rejects_dom(
t,
"DataCloneError",
runner.structuredClone(ta)
);
}
});

structuredCloneBatteryOfTests.push({
description: 'Serializing OOB DataView throws',
async f(runner, t) {
const ab = new ArrayBuffer(16, { maxByteLength: 1024 });
const dv = new DataView(ab, 8);
ab.resize(0);
await promise_rejects_dom(
t,
"DataCloneError",
runner.structuredClone(dv)
);
}
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// META: title=structuredClone() tests
// META: script=/common/sab.js
// META: script=/html/webappapis/structured-clone/structured-clone-battery-of-tests.js
// META: script=/html/webappapis/structured-clone/structured-clone-battery-of-tests-with-transferables.js
// META: script=/html/webappapis/structured-clone/structured-clone-battery-of-tests-harness.js
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/sab.js"></script>
<script type="module">
test(t => {
// Fixed-length ABs should not throw
const ab = new ArrayBuffer(16);
new Response(new Uint8Array(ab));

const rab = new ArrayBuffer(16, { maxByteLength: 1024 });
// Response doesn't have [AllowResizable] or [AllowShared]
assert_throws_js(TypeError, () => {
new Response(new Uint8Array(rab));
});
}, "APIs without [AllowResizable] throw when passed resizable ArrayBuffers");

test(t => {
const enc = new TextEncoder();

// Fixed-length SABs should not throw
const sab = createBuffer('SharedArrayBuffer', 16, { maxByteLength: 1024 });
enc.encodeInto("foobar", new Uint8Array(sab));

const gsab = createBuffer('SharedArrayBuffer', 16, { maxByteLength: 1024 });
// TextEncoder.encodeInto doesn't have [AllowResizable] but has [AllowShared]
assert_throws_js(TypeError, () => {
enc.encodeInto("foobar", new Uint8Array(gsab));
});
}, "APIs with [AllowShared] but without [AllowResizable] throw when passed growable SharedArrayBuffers");
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<title>structured clone to dedicated worker</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src=/common/sab.js></script>
<script src=/html/webappapis/structured-clone/structured-clone-battery-of-tests.js></script>
<script src=/html/webappapis/structured-clone/structured-clone-battery-of-tests-with-transferables.js></script>
<script src=/html/webappapis/structured-clone/structured-clone-battery-of-tests-harness.js></script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<title>structured clone to shared worker</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src=/common/sab.js></script>
<script src=/html/webappapis/structured-clone/structured-clone-battery-of-tests.js></script>
<script src=/html/webappapis/structured-clone/structured-clone-battery-of-tests-with-transferables.js></script>
<script src=/html/webappapis/structured-clone/structured-clone-battery-of-tests-harness.js></script>
Expand Down

0 comments on commit c82b487

Please sign in to comment.