Skip to content

Commit

Permalink
feat: parse SharedArrayBuffer and validate AllowShared (#747)
Browse files Browse the repository at this point in the history
* feat: parse SharedArrayBuffer and validate AllowShared

* Update README.md

* more test

* Update type.js
  • Loading branch information
saschanaz committed Jun 26, 2023
1 parent eb68391 commit 164675c
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 2 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
/test/*/** text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ properties:
* `no-constructible-global`: Interfaces with `[Global]` cannot have constructors.
* `renamed-legacy`: Legacy extended attributes must use their new names.
* `replace-void`: `void` type is replaced by `undefined` type.
* `migrate-allowshared`: `[AllowShared] BufferSource` is replaced by `AllowSharedBufferSource`.
* `input`: a short peek at the text at the point where the error happened
* `tokens`: the five tokens at the point of error, as understood by the tokeniser
(this is the same content as `input`, but seen from the tokeniser's point of view)
Expand Down
37 changes: 37 additions & 0 deletions lib/productions/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,26 @@ export class Type extends Base {
*validate(defs) {
yield* this.extAttrs.validate(defs);

if (this.idlType === "BufferSource") {
// XXX: For now this is a hack. Consider moving parents' extAttrs into types as the spec says:
// https://webidl.spec.whatwg.org/#idl-annotated-types
for (const extAttrs of [this.extAttrs, this.parent?.extAttrs]) {
for (const extAttr of extAttrs) {
if (extAttr.name !== "AllowShared") {
continue;
}
const message = `\`[AllowShared] BufferSource\` is now replaced with AllowSharedBufferSource.`;
yield validationError(
this.tokens.base,
this,
"migrate-allowshared",
message,
{ autofix: replaceAllowShared(this, extAttr, extAttrs) }
);
}
}
}

if (this.idlType === "void") {
const message = `\`void\` is now replaced by \`undefined\`. Refer to the \
[relevant GitHub issue](https://github.com/whatwg/webidl/issues/60) \
Expand Down Expand Up @@ -273,6 +293,23 @@ for more information.`;
}
}

/**
* @param {Type} type
* @param {import("./extended-attributes.js").SimpleExtendedAttribute} extAttr
* @param {ExtendedAttributes} extAttrs
*/
function replaceAllowShared(type, extAttr, extAttrs) {
return () => {
const index = extAttrs.indexOf(extAttr);
extAttrs.splice(index, 1);
if (!extAttrs.length && type.tokens.base.trivia.match(/^\s$/)) {
type.tokens.base.trivia = ""; // (let's not remove comments)
}

type.tokens.base.value = "AllowSharedBufferSource";
};
}

/**
* @param {Type} type
*/
Expand Down
1 change: 1 addition & 0 deletions lib/tokeniser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const tokenRe = {

export const typeNameKeywords = [
"ArrayBuffer",
"SharedArrayBuffer",
"DataView",
"Int8Array",
"Int16Array",
Expand Down
18 changes: 18 additions & 0 deletions test/autofix.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,22 @@ describe("Writer template functions", () => {
`;
expect(autofix(input)).toBe(output);
});

it("should replace [AllowShared] BufferSource into undefined", () => {
const input = `
[Exposed=Window]
interface Foo {
undefined foo([AllowShared] /* Accept SharedArrayBuffer */ BufferSource source);
undefined foo(optional [AllowShared] /* Accept SharedArrayBuffer */ BufferSource source);
};
`;
const output = `
[Exposed=Window]
interface Foo {
undefined foo( /* Accept SharedArrayBuffer */ AllowSharedBufferSource source);
undefined foo(optional /* Accept SharedArrayBuffer */ AllowSharedBufferSource source);
};
`;
expect(autofix(input)).toBe(output);
});
});
3 changes: 3 additions & 0 deletions test/invalid/baseline/invalid-allowshared.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(migrate-allowshared) Validation error at line 3 in invalid-allowshared.webidl:
foo([AllowShared] BufferSource source);
^ `[AllowShared] BufferSource` is now replaced with AllowSharedBufferSource.
3 changes: 3 additions & 0 deletions test/invalid/baseline/sharedarraybuffer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Syntax error at line 1 in sharedarraybuffer.webidl:
interface SharedArrayBuffer {};
^ Missing name in interface
4 changes: 4 additions & 0 deletions test/invalid/idl/invalid-allowshared.webidl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[Exposed=Window]
interface Foo {
undefined foo([AllowShared] BufferSource source);
};
1 change: 1 addition & 0 deletions test/invalid/idl/sharedarraybuffer.webidl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
interface SharedArrayBuffer {};
89 changes: 89 additions & 0 deletions test/syntax/baseline/sharedarraybuffer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
[
{
"type": "interface",
"name": "Foo",
"inheritance": null,
"members": [
{
"type": "operation",
"name": "foo",
"idlType": {
"type": "return-type",
"extAttrs": [],
"generic": "",
"nullable": false,
"union": false,
"idlType": "undefined"
},
"arguments": [
{
"type": "argument",
"name": "buffer",
"extAttrs": [],
"idlType": {
"type": "argument-type",
"extAttrs": [],
"generic": "",
"nullable": false,
"union": false,
"idlType": "SharedArrayBuffer"
},
"default": null,
"optional": false,
"variadic": false
}
],
"extAttrs": [],
"special": ""
},
{
"type": "operation",
"name": "foo",
"idlType": {
"type": "return-type",
"extAttrs": [],
"generic": "",
"nullable": false,
"union": false,
"idlType": "undefined"
},
"arguments": [
{
"type": "argument",
"name": "source",
"extAttrs": [],
"idlType": {
"type": "argument-type",
"extAttrs": [],
"generic": "",
"nullable": false,
"union": false,
"idlType": "AllowSharedBufferSource"
},
"default": null,
"optional": false,
"variadic": false
}
],
"extAttrs": [],
"special": ""
}
],
"extAttrs": [
{
"type": "extended-attribute",
"name": "Exposed",
"rhs": {
"type": "identifier",
"value": "Window"
},
"arguments": []
}
],
"partial": false
},
{
"type": "eof",
"value": ""
}
]
5 changes: 5 additions & 0 deletions test/syntax/idl/sharedarraybuffer.webidl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[Exposed=Window]
interface Foo {
undefined foo(SharedArrayBuffer buffer);
undefined foo(AllowSharedBufferSource source);
};

0 comments on commit 164675c

Please sign in to comment.