Skip to content

Commit 164675c

Browse files
authored
feat: parse SharedArrayBuffer and validate AllowShared (#747)
* feat: parse SharedArrayBuffer and validate AllowShared * Update README.md * more test * Update type.js
1 parent eb68391 commit 164675c

File tree

11 files changed

+163
-2
lines changed

11 files changed

+163
-2
lines changed

.gitattributes

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
# Auto detect text files and perform LF normalization
2-
* text=auto
3-
/test/*/** text eol=lf
2+
* text=auto eol=lf

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ properties:
199199
* `no-constructible-global`: Interfaces with `[Global]` cannot have constructors.
200200
* `renamed-legacy`: Legacy extended attributes must use their new names.
201201
* `replace-void`: `void` type is replaced by `undefined` type.
202+
* `migrate-allowshared`: `[AllowShared] BufferSource` is replaced by `AllowSharedBufferSource`.
202203
* `input`: a short peek at the text at the point where the error happened
203204
* `tokens`: the five tokens at the point of error, as understood by the tokeniser
204205
(this is the same content as `input`, but seen from the tokeniser's point of view)

lib/productions/type.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,26 @@ export class Type extends Base {
194194
*validate(defs) {
195195
yield* this.extAttrs.validate(defs);
196196

197+
if (this.idlType === "BufferSource") {
198+
// XXX: For now this is a hack. Consider moving parents' extAttrs into types as the spec says:
199+
// https://webidl.spec.whatwg.org/#idl-annotated-types
200+
for (const extAttrs of [this.extAttrs, this.parent?.extAttrs]) {
201+
for (const extAttr of extAttrs) {
202+
if (extAttr.name !== "AllowShared") {
203+
continue;
204+
}
205+
const message = `\`[AllowShared] BufferSource\` is now replaced with AllowSharedBufferSource.`;
206+
yield validationError(
207+
this.tokens.base,
208+
this,
209+
"migrate-allowshared",
210+
message,
211+
{ autofix: replaceAllowShared(this, extAttr, extAttrs) }
212+
);
213+
}
214+
}
215+
}
216+
197217
if (this.idlType === "void") {
198218
const message = `\`void\` is now replaced by \`undefined\`. Refer to the \
199219
[relevant GitHub issue](https://github.com/whatwg/webidl/issues/60) \
@@ -273,6 +293,23 @@ for more information.`;
273293
}
274294
}
275295

296+
/**
297+
* @param {Type} type
298+
* @param {import("./extended-attributes.js").SimpleExtendedAttribute} extAttr
299+
* @param {ExtendedAttributes} extAttrs
300+
*/
301+
function replaceAllowShared(type, extAttr, extAttrs) {
302+
return () => {
303+
const index = extAttrs.indexOf(extAttr);
304+
extAttrs.splice(index, 1);
305+
if (!extAttrs.length && type.tokens.base.trivia.match(/^\s$/)) {
306+
type.tokens.base.trivia = ""; // (let's not remove comments)
307+
}
308+
309+
type.tokens.base.value = "AllowSharedBufferSource";
310+
};
311+
}
312+
276313
/**
277314
* @param {Type} type
278315
*/

lib/tokeniser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const tokenRe = {
1818

1919
export const typeNameKeywords = [
2020
"ArrayBuffer",
21+
"SharedArrayBuffer",
2122
"DataView",
2223
"Int8Array",
2324
"Int16Array",

test/autofix.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,22 @@ describe("Writer template functions", () => {
342342
`;
343343
expect(autofix(input)).toBe(output);
344344
});
345+
346+
it("should replace [AllowShared] BufferSource into undefined", () => {
347+
const input = `
348+
[Exposed=Window]
349+
interface Foo {
350+
undefined foo([AllowShared] /* Accept SharedArrayBuffer */ BufferSource source);
351+
undefined foo(optional [AllowShared] /* Accept SharedArrayBuffer */ BufferSource source);
352+
};
353+
`;
354+
const output = `
355+
[Exposed=Window]
356+
interface Foo {
357+
undefined foo( /* Accept SharedArrayBuffer */ AllowSharedBufferSource source);
358+
undefined foo(optional /* Accept SharedArrayBuffer */ AllowSharedBufferSource source);
359+
};
360+
`;
361+
expect(autofix(input)).toBe(output);
362+
});
345363
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(migrate-allowshared) Validation error at line 3 in invalid-allowshared.webidl:
2+
foo([AllowShared] BufferSource source);
3+
^ `[AllowShared] BufferSource` is now replaced with AllowSharedBufferSource.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Syntax error at line 1 in sharedarraybuffer.webidl:
2+
interface SharedArrayBuffer {};
3+
^ Missing name in interface
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[Exposed=Window]
2+
interface Foo {
3+
undefined foo([AllowShared] BufferSource source);
4+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
interface SharedArrayBuffer {};
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
[
2+
{
3+
"type": "interface",
4+
"name": "Foo",
5+
"inheritance": null,
6+
"members": [
7+
{
8+
"type": "operation",
9+
"name": "foo",
10+
"idlType": {
11+
"type": "return-type",
12+
"extAttrs": [],
13+
"generic": "",
14+
"nullable": false,
15+
"union": false,
16+
"idlType": "undefined"
17+
},
18+
"arguments": [
19+
{
20+
"type": "argument",
21+
"name": "buffer",
22+
"extAttrs": [],
23+
"idlType": {
24+
"type": "argument-type",
25+
"extAttrs": [],
26+
"generic": "",
27+
"nullable": false,
28+
"union": false,
29+
"idlType": "SharedArrayBuffer"
30+
},
31+
"default": null,
32+
"optional": false,
33+
"variadic": false
34+
}
35+
],
36+
"extAttrs": [],
37+
"special": ""
38+
},
39+
{
40+
"type": "operation",
41+
"name": "foo",
42+
"idlType": {
43+
"type": "return-type",
44+
"extAttrs": [],
45+
"generic": "",
46+
"nullable": false,
47+
"union": false,
48+
"idlType": "undefined"
49+
},
50+
"arguments": [
51+
{
52+
"type": "argument",
53+
"name": "source",
54+
"extAttrs": [],
55+
"idlType": {
56+
"type": "argument-type",
57+
"extAttrs": [],
58+
"generic": "",
59+
"nullable": false,
60+
"union": false,
61+
"idlType": "AllowSharedBufferSource"
62+
},
63+
"default": null,
64+
"optional": false,
65+
"variadic": false
66+
}
67+
],
68+
"extAttrs": [],
69+
"special": ""
70+
}
71+
],
72+
"extAttrs": [
73+
{
74+
"type": "extended-attribute",
75+
"name": "Exposed",
76+
"rhs": {
77+
"type": "identifier",
78+
"value": "Window"
79+
},
80+
"arguments": []
81+
}
82+
],
83+
"partial": false
84+
},
85+
{
86+
"type": "eof",
87+
"value": ""
88+
}
89+
]

0 commit comments

Comments
 (0)