wesl 0.6.0-pre10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/README.md +31 -0
  2. package/dist/index.js +4468 -0
  3. package/dist/index.js.map +1 -0
  4. package/dist/minified.js +3426 -0
  5. package/dist/minified.js.map +1 -0
  6. package/dist/tools/packages/wesl/src/AbstractElems.d.ts +322 -0
  7. package/dist/tools/packages/wesl/src/Assertions.d.ts +27 -0
  8. package/dist/tools/packages/wesl/src/BindIdents.d.ts +70 -0
  9. package/dist/tools/packages/wesl/src/Conditions.d.ts +6 -0
  10. package/dist/tools/packages/wesl/src/FlattenTreeImport.d.ts +11 -0
  11. package/dist/tools/packages/wesl/src/LinkedWesl.d.ts +50 -0
  12. package/dist/tools/packages/wesl/src/Linker.d.ts +87 -0
  13. package/dist/tools/packages/wesl/src/LinkerUtil.d.ts +3 -0
  14. package/dist/tools/packages/wesl/src/LiveDeclarations.d.ts +12 -0
  15. package/dist/tools/packages/wesl/src/LowerAndEmit.d.ts +31 -0
  16. package/dist/tools/packages/wesl/src/Mangler.d.ts +39 -0
  17. package/dist/tools/packages/wesl/src/ParseWESL.d.ts +60 -0
  18. package/dist/tools/packages/wesl/src/ParsedRegistry.d.ts +29 -0
  19. package/dist/tools/packages/wesl/src/PathUtil.d.ts +6 -0
  20. package/dist/tools/packages/wesl/src/RawEmit.d.ts +6 -0
  21. package/dist/tools/packages/wesl/src/Reflection.d.ts +45 -0
  22. package/dist/tools/packages/wesl/src/Scope.d.ts +81 -0
  23. package/dist/tools/packages/wesl/src/StandardTypes.d.ts +13 -0
  24. package/dist/tools/packages/wesl/src/TransformBindingStructs.d.ts +52 -0
  25. package/dist/tools/packages/wesl/src/Util.d.ts +43 -0
  26. package/dist/tools/packages/wesl/src/WESLCollect.d.ts +94 -0
  27. package/dist/tools/packages/wesl/src/WeslBundle.d.ts +13 -0
  28. package/dist/tools/packages/wesl/src/WeslDevice.d.ts +25 -0
  29. package/dist/tools/packages/wesl/src/debug/ASTtoString.d.ts +5 -0
  30. package/dist/tools/packages/wesl/src/debug/ImportToString.d.ts +2 -0
  31. package/dist/tools/packages/wesl/src/debug/LineWrapper.d.ts +21 -0
  32. package/dist/tools/packages/wesl/src/debug/ScopeToString.d.ts +6 -0
  33. package/dist/tools/packages/wesl/src/index.d.ts +11 -0
  34. package/dist/tools/packages/wesl/src/parse/ImportGrammar.d.ts +5 -0
  35. package/dist/tools/packages/wesl/src/parse/Keywords.d.ts +4 -0
  36. package/dist/tools/packages/wesl/src/parse/WeslBaseGrammar.d.ts +5 -0
  37. package/dist/tools/packages/wesl/src/parse/WeslExpression.d.ts +13 -0
  38. package/dist/tools/packages/wesl/src/parse/WeslGrammar.d.ts +80 -0
  39. package/dist/tools/packages/wesl/src/parse/WeslStream.d.ts +44 -0
  40. package/dist/tools/packages/wesl/src/test/BindWESL.test.d.ts +1 -0
  41. package/dist/tools/packages/wesl/src/test/ConditionLinking.test.d.ts +1 -0
  42. package/dist/tools/packages/wesl/src/test/ConditionalTranslationCases.test.d.ts +1 -0
  43. package/dist/tools/packages/wesl/src/test/ErrorLogging.test.d.ts +1 -0
  44. package/dist/tools/packages/wesl/src/test/Expression.test.d.ts +1 -0
  45. package/dist/tools/packages/wesl/src/test/FlattenTreeImport.test.d.ts +1 -0
  46. package/dist/tools/packages/wesl/src/test/ImportCases.test.d.ts +1 -0
  47. package/dist/tools/packages/wesl/src/test/ImportSyntaxCases.test.d.ts +1 -0
  48. package/dist/tools/packages/wesl/src/test/LinkGlob.test.d.ts +1 -0
  49. package/dist/tools/packages/wesl/src/test/LinkPackage.test.d.ts +1 -0
  50. package/dist/tools/packages/wesl/src/test/Linker.test.d.ts +1 -0
  51. package/dist/tools/packages/wesl/src/test/Mangling.test.d.ts +1 -0
  52. package/dist/tools/packages/wesl/src/test/ParseComments.test.d.ts +1 -0
  53. package/dist/tools/packages/wesl/src/test/ParseConditions.test.d.ts +1 -0
  54. package/dist/tools/packages/wesl/src/test/ParseError.test.d.ts +1 -0
  55. package/dist/tools/packages/wesl/src/test/ParseWESL.test.d.ts +1 -0
  56. package/dist/tools/packages/wesl/src/test/PathUtil.test.d.ts +1 -0
  57. package/dist/tools/packages/wesl/src/test/PrettyGrammar.test.d.ts +1 -0
  58. package/dist/tools/packages/wesl/src/test/Reflection.test.d.ts +1 -0
  59. package/dist/tools/packages/wesl/src/test/ScopeWESL.test.d.ts +1 -0
  60. package/dist/tools/packages/wesl/src/test/TestLink.d.ts +21 -0
  61. package/dist/tools/packages/wesl/src/test/TestSetup.d.ts +1 -0
  62. package/dist/tools/packages/wesl/src/test/TestUtil.d.ts +40 -0
  63. package/dist/tools/packages/wesl/src/test/Tokenizer.test.d.ts +1 -0
  64. package/dist/tools/packages/wesl/src/test/TransformBindingStructs.test.d.ts +1 -0
  65. package/dist/tools/packages/wesl/src/test/Util.test.d.ts +1 -0
  66. package/dist/tools/packages/wesl/src/test/VirtualModules.test.d.ts +1 -0
  67. package/dist/tools/packages/wesl/src/test/WeslDevice.test.d.ts +1 -0
  68. package/dist/tools/packages/wesl/src/test/WgslTests.d.ts +0 -0
  69. package/dist/tools/packages/wesl/src/vlq/vlq.d.ts +11 -0
  70. package/package.json +46 -0
  71. package/src/AbstractElems.ts +446 -0
  72. package/src/Assertions.ts +51 -0
  73. package/src/BindIdents.ts +523 -0
  74. package/src/Conditions.ts +74 -0
  75. package/src/FlattenTreeImport.ts +55 -0
  76. package/src/LinkedWesl.ts +184 -0
  77. package/src/Linker.ts +284 -0
  78. package/src/LinkerUtil.ts +29 -0
  79. package/src/LiveDeclarations.ts +31 -0
  80. package/src/LowerAndEmit.ts +413 -0
  81. package/src/Mangler.ts +94 -0
  82. package/src/ParseWESL.ts +157 -0
  83. package/src/ParsedRegistry.ts +120 -0
  84. package/src/PathUtil.ts +31 -0
  85. package/src/RawEmit.ts +102 -0
  86. package/src/Reflection.ts +334 -0
  87. package/src/Scope.ts +162 -0
  88. package/src/StandardTypes.ts +97 -0
  89. package/src/TransformBindingStructs.ts +319 -0
  90. package/src/Util.ts +194 -0
  91. package/src/WESLCollect.ts +614 -0
  92. package/src/WeslBundle.ts +16 -0
  93. package/src/WeslDevice.ts +209 -0
  94. package/src/debug/ASTtoString.ts +290 -0
  95. package/src/debug/ImportToString.ts +29 -0
  96. package/src/debug/LineWrapper.ts +70 -0
  97. package/src/debug/ScopeToString.ts +79 -0
  98. package/src/index.ts +11 -0
  99. package/src/parse/ImportGrammar.ts +157 -0
  100. package/src/parse/Keywords.ts +26 -0
  101. package/src/parse/WeslBaseGrammar.ts +8 -0
  102. package/src/parse/WeslExpression.ts +207 -0
  103. package/src/parse/WeslGrammar.ts +856 -0
  104. package/src/parse/WeslStream.ts +279 -0
  105. package/src/test/BindWESL.test.ts +57 -0
  106. package/src/test/ConditionLinking.test.ts +91 -0
  107. package/src/test/ConditionalTranslationCases.test.ts +56 -0
  108. package/src/test/ErrorLogging.test.ts +30 -0
  109. package/src/test/Expression.test.ts +22 -0
  110. package/src/test/FlattenTreeImport.test.ts +74 -0
  111. package/src/test/ImportCases.test.ts +56 -0
  112. package/src/test/ImportSyntaxCases.test.ts +24 -0
  113. package/src/test/LinkGlob.test.ts +25 -0
  114. package/src/test/LinkPackage.test.ts +26 -0
  115. package/src/test/Linker.test.ts +125 -0
  116. package/src/test/Mangling.test.ts +45 -0
  117. package/src/test/ParseComments.test.ts +36 -0
  118. package/src/test/ParseConditions.test.ts +183 -0
  119. package/src/test/ParseError.test.ts +36 -0
  120. package/src/test/ParseWESL.test.ts +1572 -0
  121. package/src/test/PathUtil.test.ts +34 -0
  122. package/src/test/PrettyGrammar.test.ts +20 -0
  123. package/src/test/Reflection.test.ts +172 -0
  124. package/src/test/ScopeWESL.test.ts +462 -0
  125. package/src/test/TestLink.ts +82 -0
  126. package/src/test/TestSetup.ts +4 -0
  127. package/src/test/TestUtil.ts +126 -0
  128. package/src/test/Tokenizer.test.ts +135 -0
  129. package/src/test/TransformBindingStructs.test.ts +230 -0
  130. package/src/test/Util.test.ts +22 -0
  131. package/src/test/VirtualModules.test.ts +37 -0
  132. package/src/test/WeslDevice.test.ts +265 -0
  133. package/src/test/WgslTests.ts +0 -0
  134. package/src/test/__snapshots__/ParseDirectives.test.ts.snap +25 -0
  135. package/src/test/__snapshots__/ParseWESL.test.ts.snap +119 -0
  136. package/src/test/__snapshots__/RustDirective.test.ts.snap +359 -0
  137. package/src/test/wgsl_1/main.wgsl +3 -0
  138. package/src/test/wgsl_1/util.wgsl +1 -0
  139. package/src/test/wgsl_2/main2.wgsl +3 -0
  140. package/src/test/wgsl_2/util2.wgsl +1 -0
  141. package/src/vlq/vlq.ts +94 -0
@@ -0,0 +1,265 @@
1
+ import { SrcMap } from "mini-parse";
2
+ import { setTimeout } from "node:timers";
3
+ import { expect, test, vi } from "vitest";
4
+ import { LinkedWesl } from "../LinkedWesl";
5
+ import { makeWeslDevice } from "../WeslDevice";
6
+
7
+ test("WeslDevice doesn't conflict with uncapturederror", async () => {
8
+ const GPUDeviceMock = vi.fn(function (this: GPUDevice) {
9
+ let errorListener: EventListener;
10
+ this.createShaderModule = () => {
11
+ let errorEvent: Partial<GPUUncapturedErrorEvent> = {
12
+ error: {
13
+ message: "shader compilation failed",
14
+ },
15
+ defaultPrevented: false,
16
+ };
17
+ errorListener(errorEvent as any);
18
+ return {} as any;
19
+ };
20
+ this.pushErrorScope = () => {
21
+ throw new Error("Should not be called");
22
+ };
23
+ this.popErrorScope = () => {
24
+ throw new Error("Should not be called");
25
+ };
26
+ this.addEventListener = (type: string, listener: EventListener) => {
27
+ expect(type).toBe("uncapturederror");
28
+
29
+ errorListener = listener;
30
+ };
31
+ });
32
+ const device = makeWeslDevice(new GPUDeviceMock() as any);
33
+
34
+ const errorPromise = new Promise<GPUError>((resolve, reject) => {
35
+ const TIMEOUT = setTimeout(() => {
36
+ reject();
37
+ }, 1000);
38
+ device.addEventListener("uncapturederror", ev => {
39
+ clearTimeout(TIMEOUT);
40
+ resolve(ev.error);
41
+ });
42
+ });
43
+ const shader = device.createShaderModule({
44
+ code: "🐈",
45
+ });
46
+
47
+ const error = await errorPromise;
48
+
49
+ expect(error.message).toBe("shader compilation failed");
50
+ });
51
+
52
+ test("WeslDevice doesn't conflict with popErrorsScope", async () => {
53
+ const GPUDeviceMock = vi.fn(function (this: GPUDevice) {
54
+ this.createShaderModule = () => {
55
+ return {} as any;
56
+ };
57
+ this.pushErrorScope = () => {};
58
+ this.popErrorScope = () => {
59
+ return Promise.resolve({
60
+ message: ":1:1 shader compilation failed",
61
+ });
62
+ };
63
+ });
64
+ const device = makeWeslDevice(new GPUDeviceMock() as any);
65
+ const createShaderModuleSpy = vi.spyOn(device, "createShaderModule");
66
+ device.pushErrorScope("validation");
67
+ const shader = device.createShaderModule({
68
+ code: "🐈",
69
+ });
70
+ const errorPromise = new Promise<GPUError | null>((resolve, reject) => {
71
+ const TIMEOUT = setTimeout(() => {
72
+ reject();
73
+ }, 1000);
74
+ device.popErrorScope().then(v => {
75
+ clearTimeout(TIMEOUT);
76
+ resolve(v);
77
+ });
78
+ });
79
+
80
+ const error = await errorPromise;
81
+
82
+ expect(error).not.toBe(null);
83
+ expect(error?.message).toContain("shader compilation failed");
84
+ expect(error?.message).toContain(":1:1");
85
+ expect(createShaderModuleSpy).toHaveBeenCalledTimes(1);
86
+ });
87
+
88
+ test("LinkedWesl createShaderModule skips if it's not a WeslDevice", async () => {
89
+ const GPUDeviceMock = vi.fn(function (this: GPUDevice) {
90
+ this.createShaderModule = () => {
91
+ return {} as any;
92
+ };
93
+ this.pushErrorScope = () => {
94
+ throw new Error("Should not be called");
95
+ };
96
+ this.popErrorScope = () => {
97
+ throw new Error("Should not be called");
98
+ };
99
+ this.addEventListener = () => {
100
+ throw new Error("Should not be called");
101
+ };
102
+ this.createRenderPipeline = () => {
103
+ return null as any;
104
+ };
105
+ });
106
+ const device: GPUDevice = new GPUDeviceMock() as any;
107
+
108
+ const createShaderModuleSpy = vi.spyOn(device, "createShaderModule");
109
+ const linkedWesl = new LinkedWesl(
110
+ new SrcMap(
111
+ {
112
+ text: "cute generated code",
113
+ },
114
+ [],
115
+ ),
116
+ );
117
+
118
+ // Test that this doesnt' throw
119
+ linkedWesl.createShaderModule(device, {});
120
+
121
+ expect(createShaderModuleSpy).toHaveBeenCalledTimes(1);
122
+ });
123
+
124
+ test("Point at WESL code", async () => {
125
+ const GPUValidationErrorMock = vi.fn(function (this: any, message: string) {
126
+ this.message = message;
127
+ });
128
+ vi.stubGlobal("GPUValidationError", GPUValidationErrorMock);
129
+
130
+ const GPUDeviceMock = vi.fn(function (this: GPUDevice) {
131
+ this.createShaderModule = () => {
132
+ return {
133
+ getCompilationInfo(): Promise<GPUCompilationInfo> {
134
+ return Promise.resolve({
135
+ __brand: "GPUCompilationInfo",
136
+ messages: [
137
+ {
138
+ __brand: "GPUCompilationMessage",
139
+ type: "error",
140
+ offset: 0,
141
+ length: 4,
142
+ lineNum: 1,
143
+ linePos: 1,
144
+ message: "shader compilation failed",
145
+ },
146
+ ],
147
+ });
148
+ },
149
+ } as any;
150
+ };
151
+ this.pushErrorScope = () => {};
152
+ this.popErrorScope = () => {
153
+ return Promise.resolve({
154
+ message: "this message gets ignored",
155
+ });
156
+ };
157
+ this.dispatchEvent = () => {
158
+ throw new Error("Should not be called");
159
+ };
160
+ });
161
+ const device = makeWeslDevice(new GPUDeviceMock() as any);
162
+
163
+ const linkedWesl = new LinkedWesl(
164
+ new SrcMap(
165
+ {
166
+ text: "cute generated code",
167
+ },
168
+ [
169
+ {
170
+ src: {
171
+ text: "\ncute source code",
172
+ path: "main.wesl",
173
+ },
174
+ // Point at start of line 2
175
+ srcStart: 1,
176
+ srcEnd: 11,
177
+ destStart: 0,
178
+ destEnd: 10,
179
+ },
180
+ ],
181
+ ),
182
+ );
183
+
184
+ device.pushErrorScope("validation");
185
+ linkedWesl.createShaderModule(device, {});
186
+ let result = await device.popErrorScope();
187
+
188
+ // Expect that it's not the original, but instead the changed version
189
+ expect(result?.message).not.toContain("this message gets ignored");
190
+ expect(result?.message).not.toContain(":1:1");
191
+ expect(result?.message).toContain(":2:1");
192
+ expect(result?.message).toContain("shader compilation failed");
193
+
194
+ vi.unstubAllGlobals();
195
+ });
196
+
197
+ test("Invokes error throwing", async () => {
198
+ const GPUValidationErrorMock = vi.fn(function (this: any, message: string) {
199
+ this.message = message;
200
+ });
201
+ vi.stubGlobal("GPUValidationError", GPUValidationErrorMock);
202
+ const GPUUncapturedErrorEventMock = vi.fn(function () {});
203
+ vi.stubGlobal("GPUUncapturedErrorEvent", GPUUncapturedErrorEventMock);
204
+
205
+ const dispatchEventPromise = Promise.withResolvers();
206
+ const dispatchEventTimer = setTimeout(() => {
207
+ dispatchEventPromise.reject();
208
+ }, 500);
209
+ const GPUDeviceMock = vi.fn(function (this: GPUDevice) {
210
+ this.createShaderModule = () => {
211
+ return {
212
+ getCompilationInfo(): Promise<GPUCompilationInfo> {
213
+ return Promise.resolve({
214
+ __brand: "GPUCompilationInfo",
215
+ messages: [
216
+ {
217
+ __brand: "GPUCompilationMessage",
218
+ type: "error",
219
+ offset: 1,
220
+ length: 4,
221
+ lineNum: 1,
222
+ linePos: 2,
223
+ message: "shader compilation failed",
224
+ },
225
+ ],
226
+ });
227
+ },
228
+ } as any;
229
+ };
230
+ this.pushErrorScope = () => {};
231
+ this.popErrorScope = () => {
232
+ return Promise.resolve({
233
+ message: "this message gets ignored",
234
+ });
235
+ };
236
+ this.addEventListener = () => {};
237
+ this.dispatchEvent = () => {
238
+ clearTimeout(dispatchEventTimer);
239
+ dispatchEventPromise.resolve(true);
240
+ return true;
241
+ };
242
+ });
243
+ const device = makeWeslDevice(new GPUDeviceMock() as any);
244
+ const dispatchEventSpy = vi.spyOn(device, "dispatchEvent");
245
+ const injectErrorSpy = vi.spyOn(device, "injectError");
246
+
247
+ const linkedWesl = new LinkedWesl(
248
+ new SrcMap({
249
+ text: "cute generated code",
250
+ }),
251
+ );
252
+
253
+ linkedWesl.createShaderModule(device, {});
254
+
255
+ expect(injectErrorSpy).toHaveBeenCalledTimes(1);
256
+
257
+ await dispatchEventPromise.promise;
258
+ expect(dispatchEventSpy).toHaveBeenCalledTimes(1);
259
+
260
+ vi.unstubAllGlobals();
261
+ });
262
+
263
+ // LATER
264
+ // Test injecterror
265
+ // Test mapGPUCompilationInfo
File without changes
@@ -0,0 +1,25 @@
1
+ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
+
3
+ exports[`parse import foo/bar 1`] = `
4
+ [
5
+ {
6
+ "end": 14,
7
+ "imports": ImportTree {
8
+ "segments": [
9
+ SimpleSegment {
10
+ "args": undefined,
11
+ "as": undefined,
12
+ "name": "foo",
13
+ },
14
+ SimpleSegment {
15
+ "args": undefined,
16
+ "as": undefined,
17
+ "name": "bar",
18
+ },
19
+ ],
20
+ },
21
+ "kind": "treeImport",
22
+ "start": 0,
23
+ },
24
+ ]
25
+ `;
@@ -0,0 +1,119 @@
1
+ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
+
3
+ exports[`parse foo::bar(); 1`] = `
4
+ [
5
+ {
6
+ "calls": [
7
+ {
8
+ "end": 20,
9
+ "kind": "call",
10
+ "name": "foo::bar",
11
+ "start": 12,
12
+ },
13
+ ],
14
+ "end": 25,
15
+ "kind": "fn",
16
+ "name": "main",
17
+ "nameElem": {
18
+ "end": 7,
19
+ "kind": "fnName",
20
+ "name": "main",
21
+ "start": 3,
22
+ },
23
+ "start": 0,
24
+ "typeRefs": [],
25
+ },
26
+ ]
27
+ `;
28
+
29
+ exports[`parse let x: foo::bar; 1`] = `
30
+ [
31
+ {
32
+ "calls": [],
33
+ "end": 34,
34
+ "kind": "fn",
35
+ "name": "main",
36
+ "nameElem": {
37
+ "end": 7,
38
+ "kind": "fnName",
39
+ "name": "main",
40
+ "start": 3,
41
+ },
42
+ "start": 0,
43
+ "typeRefs": [
44
+ {
45
+ "end": 27,
46
+ "kind": "typeRef",
47
+ "name": "foo::bar",
48
+ "start": 19,
49
+ },
50
+ ],
51
+ },
52
+ ]
53
+ `;
54
+
55
+ exports[`parse unicode ident 1`] = `
56
+ "module
57
+ text '
58
+ '
59
+ fn Δέλτα()
60
+ decl %Δέλτα
61
+ statement
62
+ text '{}'
63
+ text '
64
+ '
65
+ fn réflexion()
66
+ decl %réflexion
67
+ statement
68
+ text '{}'
69
+ text '
70
+ '
71
+ fn Кызыл()
72
+ decl %Кызыл
73
+ statement
74
+ text '{}'
75
+ text '
76
+ '
77
+ fn 𐰓𐰏𐰇()
78
+ decl %𐰓𐰏𐰇
79
+ statement
80
+ text '{}'
81
+ text '
82
+ '
83
+ fn 朝焼け()
84
+ decl %朝焼け
85
+ statement
86
+ text '{}'
87
+ text '
88
+ '
89
+ fn سلام()
90
+ decl %سلام
91
+ statement
92
+ text '{}'
93
+ text '
94
+ '
95
+ fn 검정()
96
+ decl %검정
97
+ statement
98
+ text '{}'
99
+ text '
100
+ '
101
+ fn שָׁלוֹם()
102
+ decl %שָׁלוֹם
103
+ statement
104
+ text '{}'
105
+ text '
106
+ '
107
+ fn गुलाबी()
108
+ decl %गुलाबी
109
+ statement
110
+ text '{}'
111
+ text '
112
+ '
113
+ fn փիրուզ()
114
+ decl %փիրուզ
115
+ statement
116
+ text '{}'
117
+ text '
118
+ '"
119
+ `;