unplugin-typegpu 0.11.6 → 0.12.0

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 (69) hide show
  1. package/{babel-B7h6V8Bs.js → babel-C62qx26_.js} +16 -5
  2. package/{babel-ZVrKmopN.d.cts → babel-CYu2-eXe.d.cts} +1 -1
  3. package/{babel--cjzbyRs.d.ts → babel-DOOIkQDL.d.ts} +1 -1
  4. package/{babel-C7MhY2Q5.cjs → babel-DpCnkpmi.cjs} +16 -5
  5. package/babel.cjs +2 -2
  6. package/babel.d.cts +2 -2
  7. package/babel.d.ts +2 -2
  8. package/babel.js +2 -2
  9. package/{bun-D_FFJG6e.cjs → bun-Bijjhb7w.cjs} +3 -3
  10. package/{bun-f_IcPQ-M.js → bun-CJralo3D.js} +3 -3
  11. package/{bun-CF67VgNO.d.ts → bun-DKuUuTVo.d.ts} +1 -1
  12. package/{bun-iEmr0VLQ.d.cts → bun-YVuHXBJq.d.cts} +1 -1
  13. package/bun.cjs +3 -3
  14. package/bun.d.cts +2 -2
  15. package/bun.d.ts +2 -2
  16. package/bun.js +3 -3
  17. package/{common-CmJ77T6N.d.cts → common-BriEOmpZ.d.cts} +10 -4
  18. package/{common-BZrmpQ_X.d.ts → common-CWaD5wkm.d.ts} +10 -4
  19. package/common-Dilxou2I.js +710 -0
  20. package/common-hMfUmUk0.cjs +790 -0
  21. package/esbuild.cjs +6 -6
  22. package/esbuild.d.cts +5 -5
  23. package/esbuild.d.ts +5 -5
  24. package/esbuild.js +6 -6
  25. package/{factory-W6dKP7m4.js → factory-BSXzHM_n.js} +10 -70
  26. package/{factory-BgweY_8-.cjs → factory-CXjNN-uX.cjs} +11 -71
  27. package/farm.cjs +6 -6
  28. package/farm.d.cts +5 -5
  29. package/farm.d.ts +5 -5
  30. package/farm.js +6 -6
  31. package/{index-BkLJ1amC.d.ts → index-COouFDDA.d.cts} +1 -1
  32. package/{index-zDnadEd0.d.cts → index-Hki1C6Ab.d.ts} +1 -1
  33. package/index.cjs +6 -6
  34. package/index.d.cts +5 -5
  35. package/index.d.ts +5 -5
  36. package/index.js +6 -6
  37. package/package.json +2 -2
  38. package/{rolldown-browser-DQtD9pV1.cjs → rolldown-browser-0UeIgV94.cjs} +1 -1
  39. package/{rolldown-browser-CvOoUvEH.d.cts → rolldown-browser-ClzBk-Ua.d.ts} +14 -10
  40. package/{rolldown-browser-D3BM6oXF.d.ts → rolldown-browser-Da9eNbZo.d.cts} +14 -10
  41. package/{rolldown-browser-NmW1NQnD.js → rolldown-browser-XH90sRcH.js} +1 -1
  42. package/rolldown-browser.cjs +3 -3
  43. package/rolldown-browser.d.cts +2 -2
  44. package/rolldown-browser.d.ts +2 -2
  45. package/rolldown-browser.js +3 -3
  46. package/rolldown.cjs +6 -6
  47. package/rolldown.d.cts +5 -5
  48. package/rolldown.d.ts +5 -5
  49. package/rolldown.js +6 -6
  50. package/rollup.cjs +6 -6
  51. package/rollup.d.cts +5 -5
  52. package/rollup.d.ts +5 -5
  53. package/rollup.js +6 -6
  54. package/rspack.cjs +6 -6
  55. package/rspack.d.cts +5 -5
  56. package/rspack.d.ts +5 -5
  57. package/rspack.js +6 -6
  58. package/{src-YdrXaMF1.js → src-BPzkBd5O.js} +1 -1
  59. package/{src-NN6mroxq.cjs → src-CYnpqmx5.cjs} +2 -2
  60. package/vite.cjs +6 -6
  61. package/vite.d.cts +5 -5
  62. package/vite.d.ts +5 -5
  63. package/vite.js +6 -6
  64. package/webpack.cjs +6 -6
  65. package/webpack.d.cts +5 -5
  66. package/webpack.d.ts +5 -5
  67. package/webpack.js +6 -6
  68. package/common-CK7zOjbK.js +0 -279
  69. package/common-ddv56_tk.cjs +0 -346
@@ -0,0 +1,790 @@
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+
23
+ //#endregion
24
+ let tinyest_for_wgsl = require("tinyest-for-wgsl");
25
+ tinyest_for_wgsl = __toESM(tinyest_for_wgsl);
26
+ let __babel_types = require("@babel/types");
27
+ __babel_types = __toESM(__babel_types);
28
+ let magic_string = require("magic-string");
29
+ magic_string = __toESM(magic_string);
30
+ let tinyest = require("tinyest");
31
+ tinyest = __toESM(tinyest);
32
+
33
+ //#region src/core/embeddedMetadata.ts
34
+ const embeddedTypegpuMetadataCache = /* @__PURE__ */ new WeakMap();
35
+ /**
36
+ * Returns the node after unwrapping any parenthesized expressions.
37
+ */
38
+ function unwrapParentheses(node) {
39
+ let current = node;
40
+ while (__babel_types.isParenthesizedExpression(current)) current = current.expression;
41
+ return current;
42
+ }
43
+ /**
44
+ * Returns the property name of a member expression.
45
+ */
46
+ function memberPropertyName(node) {
47
+ if (!node.computed && __babel_types.isIdentifier(node.property)) return node.property.name;
48
+ if (node.computed && __babel_types.isStringLiteral(node.property)) return node.property.value;
49
+ }
50
+ /**
51
+ * Returns whether the node is a global TypeGPU metadata expression (`globalThis.__TYPEGPU_META__`).
52
+ */
53
+ function isGlobalTypegpuMetadata(node) {
54
+ const unwrapped = unwrapParentheses(node);
55
+ return __babel_types.isMemberExpression(unwrapped) && __babel_types.isIdentifier(unwrapped.object, { name: "globalThis" }) && memberPropertyName(unwrapped) === "__TYPEGPU_META__";
56
+ }
57
+ /**
58
+ * Returns whether the node is a TypeGPU metadata set call (`(globalThis.__TYPEGPU_META__ ??= new WeakMap()).set`)
59
+ */
60
+ function isTypegpuMetadataSetCall(node) {
61
+ const callee = unwrapParentheses(node.callee);
62
+ if (!__babel_types.isMemberExpression(callee) || memberPropertyName(callee) !== "set") return false;
63
+ const receiver = unwrapParentheses(callee.object);
64
+ return __babel_types.isAssignmentExpression(receiver, { operator: "??=" }) && isGlobalTypegpuMetadata(receiver.left);
65
+ }
66
+ /**
67
+ * Returns the node of the property with the given name in the object, if it exists.
68
+ */
69
+ function objectPropertyValue(object, expectedName) {
70
+ for (const property of object.properties) {
71
+ if (!__babel_types.isObjectProperty(property) || property.computed) continue;
72
+ if ((__babel_types.isIdentifier(property.key) ? property.key.name : __babel_types.isStringLiteral(property.key) ? property.key.value : void 0) === expectedName) return property.value;
73
+ }
74
+ }
75
+ /**
76
+ * Returns metadata embedded by unplugin-typegpu for this exact function.
77
+ * Externals are required as part of the emitted shape, but are intentionally not evaluated.
78
+ *
79
+ * Consider:
80
+ * ```ts
81
+ * const f = ($ => (globalThis.__TYPEGPU_META__ ??= new WeakMap()).set($.f = () => {
82
+ * 'use gpu';
83
+ * }, {
84
+ * v: 2,
85
+ * name: "f",
86
+ * ast: {
87
+ * params: [],
88
+ * body: [0, []]
89
+ * },
90
+ * externals: {}
91
+ * }) && $.f)({});
92
+ * ```
93
+ */
94
+ function getEmbeddedTypegpuMetadata(path) {
95
+ var _expressionPath$paren, _assignedPath$parentP;
96
+ const cached = embeddedTypegpuMetadataCache.get(path);
97
+ if (cached !== void 0) return cached;
98
+ let expressionPath = path;
99
+ while ((_expressionPath$paren = expressionPath.parentPath) === null || _expressionPath$paren === void 0 ? void 0 : _expressionPath$paren.isParenthesizedExpression()) expressionPath = expressionPath.parentPath;
100
+ const assignmentPath = expressionPath.parentPath;
101
+ if (!(assignmentPath === null || assignmentPath === void 0 ? void 0 : assignmentPath.isAssignmentExpression({ operator: "=" }))) return;
102
+ let assignedPath = assignmentPath;
103
+ while ((_assignedPath$parentP = assignedPath.parentPath) === null || _assignedPath$parentP === void 0 ? void 0 : _assignedPath$parentP.isParenthesizedExpression()) assignedPath = assignedPath.parentPath;
104
+ const callPath = assignedPath.parentPath;
105
+ if (!((callPath === null || callPath === void 0 ? void 0 : callPath.isCallExpression()) && isTypegpuMetadataSetCall(callPath.node))) return;
106
+ const metadataNode = callPath.node.arguments[1];
107
+ if (!__babel_types.isObjectExpression(metadataNode)) return;
108
+ const versionNode = objectPropertyValue(metadataNode, "v");
109
+ const nameNode = objectPropertyValue(metadataNode, "name");
110
+ const astNode = objectPropertyValue(metadataNode, "ast");
111
+ const externalsNode = objectPropertyValue(metadataNode, "externals");
112
+ if (!__babel_types.isNumericLiteral(versionNode) || nameNode === void 0 || astNode === void 0 || externalsNode === void 0) return;
113
+ const name = __babel_types.isStringLiteral(nameNode) ? nameNode.value : void 0;
114
+ const embeddedTypegpuMetadata = {
115
+ v: versionNode.value,
116
+ name
117
+ };
118
+ embeddedTypegpuMetadataCache.set(path, embeddedTypegpuMetadata);
119
+ return embeddedTypegpuMetadata;
120
+ }
121
+
122
+ //#endregion
123
+ //#region \0@oxc-project+runtime@0.95.0/helpers/checkPrivateRedeclaration.js
124
+ function _checkPrivateRedeclaration(e, t) {
125
+ if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object");
126
+ }
127
+
128
+ //#endregion
129
+ //#region \0@oxc-project+runtime@0.95.0/helpers/classPrivateMethodInitSpec.js
130
+ function _classPrivateMethodInitSpec(e, a) {
131
+ _checkPrivateRedeclaration(e, a), a.add(e);
132
+ }
133
+
134
+ //#endregion
135
+ //#region \0@oxc-project+runtime@0.95.0/helpers/classPrivateFieldInitSpec.js
136
+ function _classPrivateFieldInitSpec(e, t, a) {
137
+ _checkPrivateRedeclaration(e, t), t.set(e, a);
138
+ }
139
+
140
+ //#endregion
141
+ //#region \0@oxc-project+runtime@0.95.0/helpers/assertClassBrand.js
142
+ function _assertClassBrand(e, t, n) {
143
+ if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n;
144
+ throw new TypeError("Private element is not present on this object");
145
+ }
146
+
147
+ //#endregion
148
+ //#region \0@oxc-project+runtime@0.95.0/helpers/classPrivateFieldGet2.js
149
+ function _classPrivateFieldGet2(s, a) {
150
+ return s.get(_assertClassBrand(s, a));
151
+ }
152
+
153
+ //#endregion
154
+ //#region \0@oxc-project+runtime@0.95.0/helpers/typeof.js
155
+ function _typeof(o) {
156
+ "@babel/helpers - typeof";
157
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o$1) {
158
+ return typeof o$1;
159
+ } : function(o$1) {
160
+ return o$1 && "function" == typeof Symbol && o$1.constructor === Symbol && o$1 !== Symbol.prototype ? "symbol" : typeof o$1;
161
+ }, _typeof(o);
162
+ }
163
+
164
+ //#endregion
165
+ //#region \0@oxc-project+runtime@0.95.0/helpers/toPrimitive.js
166
+ function toPrimitive(t, r) {
167
+ if ("object" != _typeof(t) || !t) return t;
168
+ var e = t[Symbol.toPrimitive];
169
+ if (void 0 !== e) {
170
+ var i = e.call(t, r || "default");
171
+ if ("object" != _typeof(i)) return i;
172
+ throw new TypeError("@@toPrimitive must return a primitive value.");
173
+ }
174
+ return ("string" === r ? String : Number)(t);
175
+ }
176
+
177
+ //#endregion
178
+ //#region \0@oxc-project+runtime@0.95.0/helpers/toPropertyKey.js
179
+ function toPropertyKey(t) {
180
+ var i = toPrimitive(t, "string");
181
+ return "symbol" == _typeof(i) ? i : i + "";
182
+ }
183
+
184
+ //#endregion
185
+ //#region \0@oxc-project+runtime@0.95.0/helpers/defineProperty.js
186
+ function _defineProperty(e, r, t) {
187
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
188
+ value: t,
189
+ enumerable: !0,
190
+ configurable: !0,
191
+ writable: !0
192
+ }) : e[r] = t, e;
193
+ }
194
+
195
+ //#endregion
196
+ //#region \0@oxc-project+runtime@0.95.0/helpers/objectSpread2.js
197
+ function ownKeys(e, r) {
198
+ var t = Object.keys(e);
199
+ if (Object.getOwnPropertySymbols) {
200
+ var o = Object.getOwnPropertySymbols(e);
201
+ r && (o = o.filter(function(r$1) {
202
+ return Object.getOwnPropertyDescriptor(e, r$1).enumerable;
203
+ })), t.push.apply(t, o);
204
+ }
205
+ return t;
206
+ }
207
+ function _objectSpread2(e) {
208
+ for (var r = 1; r < arguments.length; r++) {
209
+ var t = null != arguments[r] ? arguments[r] : {};
210
+ r % 2 ? ownKeys(Object(t), !0).forEach(function(r$1) {
211
+ _defineProperty(e, r$1, t[r$1]);
212
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function(r$1) {
213
+ Object.defineProperty(e, r$1, Object.getOwnPropertyDescriptor(t, r$1));
214
+ });
215
+ }
216
+ return e;
217
+ }
218
+
219
+ //#endregion
220
+ //#region src/core/obfuscate.ts
221
+ const { NodeTypeCatalog: NODE } = tinyest;
222
+ /**
223
+ * Generates all strings consisting of lowercase letters of the given length.
224
+ */
225
+ function* fixedLengthNameGenerator(length) {
226
+ if (length === 0) {
227
+ yield "";
228
+ return;
229
+ }
230
+ for (let i = 97; i <= 122; i++) for (const name of fixedLengthNameGenerator(length - 1)) yield `${String.fromCharCode(i)}${name}`;
231
+ }
232
+ /**
233
+ * Generates all strings consisting of lowercase letters.
234
+ */
235
+ function* nameGenerator() {
236
+ for (let i = 1;; i++) for (const name of fixedLengthNameGenerator(i)) yield name;
237
+ }
238
+ var _nameMap = /* @__PURE__ */ new WeakMap();
239
+ var _nameGenerator = /* @__PURE__ */ new WeakMap();
240
+ var _Obfuscator_brand = /* @__PURE__ */ new WeakSet();
241
+ var Obfuscator = class {
242
+ constructor() {
243
+ _classPrivateMethodInitSpec(this, _Obfuscator_brand);
244
+ _classPrivateFieldInitSpec(this, _nameMap, /* @__PURE__ */ new Map());
245
+ _classPrivateFieldInitSpec(this, _nameGenerator, nameGenerator());
246
+ }
247
+ /**
248
+ * If `name` wasn't obfuscated before, give it a new obfuscated name.
249
+ * Then, returns the obfuscated version of `name`.
250
+ */
251
+ obfuscate(name) {
252
+ let obfuscatedName = _classPrivateFieldGet2(_nameMap, this).get(name);
253
+ if (!obfuscatedName) {
254
+ obfuscatedName = _assertClassBrand(_Obfuscator_brand, this, _generateFreshName).call(this);
255
+ _classPrivateFieldGet2(_nameMap, this).set(name, obfuscatedName);
256
+ }
257
+ return obfuscatedName;
258
+ }
259
+ };
260
+ function _generateFreshName() {
261
+ return _classPrivateFieldGet2(_nameGenerator, this).next().value;
262
+ }
263
+ var Context = class {
264
+ constructor() {
265
+ _defineProperty(this, "obfuscator", void 0);
266
+ this.obfuscator = new Obfuscator();
267
+ }
268
+ };
269
+ function obfuscate(fn) {
270
+ const ctx = new Context();
271
+ const params = fn.params.map((param) => {
272
+ if (param.type === "i") return _objectSpread2(_objectSpread2({}, param), {}, { name: ctx.obfuscator.obfuscate(param.name) });
273
+ return _objectSpread2(_objectSpread2({}, param), {}, { props: param.props.map((prop) => _objectSpread2(_objectSpread2({}, prop), {}, { alias: ctx.obfuscator.obfuscate(prop.alias) })) });
274
+ });
275
+ const body = obf(ctx, fn.body);
276
+ const externalNames = /* @__PURE__ */ new Map();
277
+ fn.externalNames.forEach((value, key) => externalNames.set(ctx.obfuscator.obfuscate(key), value));
278
+ return {
279
+ params,
280
+ body,
281
+ externalNames
282
+ };
283
+ }
284
+ const visitors = {
285
+ block(ctx, node) {
286
+ return [NODE.block, node[1].map((node$1) => obf(ctx, node$1))];
287
+ },
288
+ binaryExpr(ctx, node) {
289
+ return [
290
+ NODE.binaryExpr,
291
+ obf(ctx, node[1]),
292
+ node[2],
293
+ obf(ctx, node[3])
294
+ ];
295
+ },
296
+ assignmentExpr(ctx, node) {
297
+ return [
298
+ NODE.assignmentExpr,
299
+ obf(ctx, node[1]),
300
+ node[2],
301
+ obf(ctx, node[3])
302
+ ];
303
+ },
304
+ logicalExpr(ctx, node) {
305
+ return [
306
+ NODE.logicalExpr,
307
+ obf(ctx, node[1]),
308
+ node[2],
309
+ obf(ctx, node[3])
310
+ ];
311
+ },
312
+ unaryExpr(ctx, node) {
313
+ return [
314
+ NODE.unaryExpr,
315
+ node[1],
316
+ obf(ctx, node[2])
317
+ ];
318
+ },
319
+ numericLiteral(_ctx, node) {
320
+ return [NODE.numericLiteral, node[1]];
321
+ },
322
+ call(ctx, node) {
323
+ return [
324
+ NODE.call,
325
+ obf(ctx, node[1]),
326
+ node[2].map((node$1) => obf(ctx, node$1))
327
+ ];
328
+ },
329
+ memberAccess(ctx, node) {
330
+ return [
331
+ NODE.memberAccess,
332
+ obf(ctx, node[1]),
333
+ node[2]
334
+ ];
335
+ },
336
+ indexAccess(ctx, node) {
337
+ return [
338
+ NODE.indexAccess,
339
+ obf(ctx, node[1]),
340
+ obf(ctx, node[2])
341
+ ];
342
+ },
343
+ return(ctx, node) {
344
+ return node.length === 1 ? [NODE.return] : [NODE.return, obf(ctx, node[1])];
345
+ },
346
+ if(ctx, node) {
347
+ return node.length === 3 ? [
348
+ NODE.if,
349
+ obf(ctx, node[1]),
350
+ obf(ctx, node[2])
351
+ ] : [
352
+ NODE.if,
353
+ obf(ctx, node[1]),
354
+ obf(ctx, node[2]),
355
+ obf(ctx, node[3])
356
+ ];
357
+ },
358
+ let(ctx, node) {
359
+ return node.length === 2 ? [NODE.let, obf(ctx, node[1])] : [
360
+ NODE.let,
361
+ obf(ctx, node[1]),
362
+ obf(ctx, node[2])
363
+ ];
364
+ },
365
+ const(ctx, node) {
366
+ return node.length === 2 ? [NODE.const, obf(ctx, node[1])] : [
367
+ NODE.const,
368
+ obf(ctx, node[1]),
369
+ obf(ctx, node[2])
370
+ ];
371
+ },
372
+ for(ctx, node) {
373
+ return [
374
+ NODE.for,
375
+ obf(ctx, node[1]),
376
+ obf(ctx, node[2]),
377
+ obf(ctx, node[3]),
378
+ obf(ctx, node[4])
379
+ ];
380
+ },
381
+ while(ctx, node) {
382
+ return [
383
+ NODE.while,
384
+ obf(ctx, node[1]),
385
+ obf(ctx, node[2])
386
+ ];
387
+ },
388
+ continue(_ctx, _node) {
389
+ return [NODE.continue];
390
+ },
391
+ break(_ctx, _node) {
392
+ return [NODE.break];
393
+ },
394
+ forOf(ctx, node) {
395
+ return [
396
+ NODE.forOf,
397
+ obf(ctx, node[1]),
398
+ obf(ctx, node[2]),
399
+ obf(ctx, node[3])
400
+ ];
401
+ },
402
+ arrayExpr(ctx, node) {
403
+ return [NODE.arrayExpr, node[1].map((node$1) => obf(ctx, node$1))];
404
+ },
405
+ preUpdate(ctx, node) {
406
+ return [
407
+ NODE.preUpdate,
408
+ node[1],
409
+ obf(ctx, node[2])
410
+ ];
411
+ },
412
+ postUpdate(ctx, node) {
413
+ return [
414
+ NODE.postUpdate,
415
+ node[1],
416
+ obf(ctx, node[2])
417
+ ];
418
+ },
419
+ stringLiteral(_ctx, node) {
420
+ return [NODE.stringLiteral, node[1]];
421
+ },
422
+ objectExpr(ctx, node) {
423
+ return [NODE.objectExpr, Object.fromEntries(Object.entries(node[1]).map(([key, value]) => [key, obf(ctx, value)]))];
424
+ },
425
+ conditionalExpr(ctx, node) {
426
+ return [
427
+ NODE.conditionalExpr,
428
+ obf(ctx, node[1]),
429
+ obf(ctx, node[2]),
430
+ obf(ctx, node[3])
431
+ ];
432
+ }
433
+ };
434
+ const nodeIdToName = new Map(Object.entries(NODE).map(([key, value]) => [value, key]));
435
+ /**
436
+ * Traverses the AST and generates a new one that is obfuscated.
437
+ * Copies old AST when identifiers cannot appear in a subtree,
438
+ * e.g. in a member access property, or for operator nodes ('=', '<', ...).
439
+ */
440
+ function obf(ctx, node) {
441
+ if (node === null) return node;
442
+ if (typeof node === "string") return ctx.obfuscator.obfuscate(node);
443
+ if (typeof node === "boolean") return node;
444
+ const nodeName = nodeIdToName.get(node[0]);
445
+ if (nodeName === void 0) throw new Error(`Internal error, no name for node type ${node[0]}.`);
446
+ const visitor = visitors[nodeName];
447
+ if (!visitor) throw new Error(`Internal error, no visitor for node '${nodeName}'.`);
448
+ return visitor(ctx, node);
449
+ }
450
+
451
+ //#endregion
452
+ //#region src/core/common.ts
453
+ /**
454
+ * Each breaking change to the metadata format requires a bump to this number.
455
+ * It's used at runtime by `typegpu` to determine how to interpret a function's metadata.
456
+ */
457
+ const METADATA_FORMAT_VERSION = 2;
458
+ function checkOpts(opts) {
459
+ if (opts.unstable_obfuscate && opts.autoNamingEnabled) throw new Error(`Options 'unstable_obfuscate' and 'autoNamingEnabled' cannot be enabled at the same time.`);
460
+ return opts;
461
+ }
462
+ function initPluginState(state, methods) {
463
+ var _state$opts$autoNamin;
464
+ state.tgpuAliases = new Set(state.opts.forceTgpuAlias ? [state.opts.forceTgpuAlias] : []);
465
+ state.autoNamingEnabled = (_state$opts$autoNamin = state.opts.autoNamingEnabled) !== null && _state$opts$autoNamin !== void 0 ? _state$opts$autoNamin : true;
466
+ state.inUseGpuScope = false;
467
+ Object.assign(state, methods);
468
+ }
469
+ /** Regular expressions used for early pruning (to avoid unnecessary parsing, which is expensive) */
470
+ const earlyPruneRegex = [/["']use gpu["']/, /t(ype)?gpu/];
471
+ const defaultOptions = {
472
+ include: /\.m?[jt]sx?(?:\?.*)?$/,
473
+ autoNamingEnabled: true,
474
+ earlyPruning: true,
475
+ unstable_obfuscate: false
476
+ };
477
+ /**
478
+ * Returns the block scope of a function declaration, if one exists.
479
+ * Used to hoist a function declaration to the top of the scope it's visible in.
480
+ */
481
+ function getBlockScope(path) {
482
+ if (!path.node.id) return;
483
+ const binding = path.scope.getBinding(path.node.id.name);
484
+ if (!binding) return;
485
+ let scopePath = binding.scope.path;
486
+ if (__babel_types.isNode(scopePath.node.body)) scopePath = scopePath.get("body");
487
+ if (__babel_types.isBlockStatement(scopePath.node) || __babel_types.isProgram(scopePath.node)) return scopePath;
488
+ }
489
+ /**
490
+ * Checks if `node` is an alias for the 'tgpu' object, traditionally
491
+ * available via `import { tgpu } from 'typegpu'`.
492
+ */
493
+ function isTgpu(state, node) {
494
+ let path = "";
495
+ let tail = node;
496
+ while (true) if (tail.type === "MemberExpression") {
497
+ if (tail.property.type === "StringLiteral" && tail.property.value === "~unstable") {
498
+ tail = tail.object;
499
+ continue;
500
+ }
501
+ if (tail.property.type !== "Identifier") break;
502
+ path = path ? `${tail.property.name}.${path}` : tail.property.name;
503
+ tail = tail.object;
504
+ } else if (tail.type === "Identifier") {
505
+ path = path ? `${tail.name}.${path}` : tail.name;
506
+ break;
507
+ } else break;
508
+ return state.tgpuAliases.has(path);
509
+ }
510
+ function gatherTgpuAliases(state, node) {
511
+ if (node.source.value !== "typegpu") return;
512
+ for (const spec of node.specifiers) if (spec.type === "ImportDefaultSpecifier" || spec.type === "ImportSpecifier" && spec.imported.type === "Identifier" && spec.imported.name === "tgpu") state.tgpuAliases.add(spec.local.name);
513
+ else if (spec.type === "ImportNamespaceSpecifier") state.tgpuAliases.add(`${spec.local.name}.tgpu`);
514
+ }
515
+ const fnShellFunctionNames = [
516
+ "fn",
517
+ "vertexFn",
518
+ "fragmentFn",
519
+ "computeFn"
520
+ ];
521
+ function isShellImplementationCall(node, state) {
522
+ return node.callee.type === "CallExpression" && node.callee.callee.type === "MemberExpression" && node.callee.callee.property.type === "Identifier" && fnShellFunctionNames.includes(node.callee.callee.property.name) && node.arguments.length === 1 && isTgpu(state, node.callee.callee.object);
523
+ }
524
+ /**
525
+ * Extracts a name and expression from nodes that contain a label and an expression,
526
+ * such as VariableDeclarator or PropertyDefinition.
527
+ * Returns a tuple of [name, expression] if found, otherwise undefined.
528
+ *
529
+ * @example
530
+ * extractLabelledExpression(node`let name = tgpu.bindGroupLayout({});`)
531
+ * // ["name", node`tgpu.bindGroupLayout({})`]
532
+ */
533
+ function extractLabelledExpression(path) {
534
+ if (path.node.type === "VariableDeclarator" && path.node.id.type === "Identifier" && path.node.init) return [path.node.id.name, path.get("init")];
535
+ else if (path.node.type === "AssignmentExpression") {
536
+ const maybeName = tryFindIdentifier(path.node.left);
537
+ if (maybeName) return [maybeName, path.get("right")];
538
+ } else if (path.node.type === "ObjectProperty" && path.node.key.type === "Identifier") return [path.node.key.name, path.get("value")];
539
+ else if (path.node.type === "ClassProperty" && path.node.value && path.node.key.type === "Identifier") return [path.node.key.name, path.get("value")];
540
+ else if (path.node.type === "ClassPrivateProperty" && path.node.value && path.node.key.type === "PrivateName") return [path.node.key.id.name, path.get("value")];
541
+ }
542
+ function getFunctionName(path) {
543
+ var _extractLabelledExpre, _path$node$id;
544
+ const maybeName = path.parentPath ? (_extractLabelledExpre = extractLabelledExpression(path.parentPath)) === null || _extractLabelledExpre === void 0 ? void 0 : _extractLabelledExpre[0] : void 0;
545
+ return maybeName !== null && maybeName !== void 0 ? maybeName : path.node.type === "FunctionDeclaration" || path.node.type === "FunctionExpression" ? (_path$node$id = path.node.id) === null || _path$node$id === void 0 ? void 0 : _path$node$id.name : void 0;
546
+ }
547
+ const resourceConstructors = [
548
+ "bindGroupLayout",
549
+ "vertexLayout",
550
+ "privateVar",
551
+ "workgroupVar",
552
+ "const",
553
+ "slot",
554
+ "accessor",
555
+ "mutableAccessor",
556
+ "comptime",
557
+ ...fnShellFunctionNames,
558
+ "createBuffer",
559
+ "createMutable",
560
+ "createReadonly",
561
+ "createUniform",
562
+ "createQuerySet",
563
+ "createPipeline",
564
+ "createComputePipeline",
565
+ "createGuardedComputePipeline",
566
+ "createRenderPipeline",
567
+ "createTexture",
568
+ "createSampler",
569
+ "createComparisonSampler",
570
+ "struct",
571
+ "unstruct",
572
+ "createView"
573
+ ];
574
+ /**
575
+ * Checks if `node` should be wrapped in an autoname function.
576
+ * Since it is mostly for debugging and clean WGSL generation,
577
+ * some false positives and false negatives are admissible.
578
+ */
579
+ function containsResourceConstructorCall(node, state) {
580
+ if (node.type === "CallExpression") {
581
+ if (isShellImplementationCall(node, state)) return true;
582
+ if (node.callee.type === "Identifier" && resourceConstructors.includes(node.callee.name)) return true;
583
+ if (node.callee.type === "MemberExpression") {
584
+ if (node.callee.property.type === "Identifier") {
585
+ if (resourceConstructors.includes(node.callee.property.name)) return true;
586
+ if (node.callee.property.name === "$name") return false;
587
+ }
588
+ return containsResourceConstructorCall(node.callee.object, state);
589
+ }
590
+ }
591
+ if (node.type === "TaggedTemplateExpression") return containsResourceConstructorCall(node.tag, state);
592
+ return false;
593
+ }
594
+ /**
595
+ * Tries to find an identifier in a node.
596
+ *
597
+ * @example
598
+ * // syntax is simplified, imagine the arguments are appropriate nodes instead
599
+ * tryFindIdentifier('myBuffer'); // 'myBuffer'
600
+ * tryFindIdentifier('buffers.myBuffer'); // 'myBuffer'
601
+ * tryFindIdentifier('this.myBuffer'); // 'myBuffer'
602
+ * tryFindIdentifier('[a, b]'); // undefined
603
+ */
604
+ function tryFindIdentifier(node) {
605
+ if (node.type === "Identifier") return node.name;
606
+ if (node.type === "PrivateName") return tryFindIdentifier(node.id);
607
+ if (node.type === "MemberExpression") return tryFindIdentifier(node.property);
608
+ }
609
+ /**
610
+ * Checks if `node` contains a label and a tgpu expression that could be named.
611
+ * If so, it calls the provided callback. Nodes selected for naming include (but are not limited to):
612
+ *
613
+ * `let name = tgpu.bindGroupLayout({});` (VariableDeclarator)
614
+ *
615
+ * `name = tgpu.bindGroupLayout({});` (AssignmentExpression)
616
+ *
617
+ * `property: tgpu.bindGroupLayout({})` (Property/ObjectProperty)
618
+ *
619
+ * Since it is mostly for debugging and clean WGSL generation,
620
+ * some false positives and false negatives are admissible.
621
+ *
622
+ * This function is NOT used for auto-naming shell-less functions.
623
+ * Those are handled separately.
624
+ *
625
+ * @privateRemarks
626
+ * When adding new checks, you need to call this method in the corresponding node in Babel.
627
+ */
628
+ function performExpressionNaming(state, path, namingCallback) {
629
+ if (!state.autoNamingEnabled) return;
630
+ const labelledExpression = extractLabelledExpression(path);
631
+ if (labelledExpression) {
632
+ const [label, expressionPath] = labelledExpression;
633
+ if (containsResourceConstructorCall(expressionPath.node, state)) namingCallback(expressionPath, label);
634
+ }
635
+ }
636
+ const operators = {
637
+ "+": "__tsover_add",
638
+ "-": "__tsover_sub",
639
+ "*": "__tsover_mul",
640
+ "/": "__tsover_div",
641
+ "%": "__tsover_mod",
642
+ "+=": "__tsover_add",
643
+ "-=": "__tsover_sub",
644
+ "*=": "__tsover_mul",
645
+ "/=": "__tsover_div",
646
+ "%=": "__tsover_mod"
647
+ };
648
+ function containsUseGpuDirective(node) {
649
+ var _node$body$directives, _node$body;
650
+ return ("directives" in node.body ? (_node$body$directives = (_node$body = node.body) === null || _node$body === void 0 ? void 0 : _node$body.directives) !== null && _node$body$directives !== void 0 ? _node$body$directives : [] : []).map((directive) => directive.value.value).includes("use gpu");
651
+ }
652
+ const fnNodeToTranspiledMap = /* @__PURE__ */ new WeakMap();
653
+ function functionOnExit(path, state) {
654
+ const node = path.node;
655
+ if (!containsUseGpuDirective(node) || getEmbeddedTypegpuMetadata(path) !== void 0) return;
656
+ state.inUseGpuScope = false;
657
+ const ast = fnNodeToTranspiledMap.get(path.node);
658
+ const maybeName = getFunctionName(path);
659
+ if (!ast) throw new Error(`No metadata found for function ${maybeName !== null && maybeName !== void 0 ? maybeName : "<unnamed>"}`);
660
+ state.assignMetadata(path, maybeName, ast);
661
+ path.skip();
662
+ }
663
+ function transpile(rootNode, obf$1) {
664
+ const result = (0, tinyest_for_wgsl.transpileFn)(rootNode);
665
+ if (obf$1) return obfuscate(result);
666
+ return result;
667
+ }
668
+ const functionVisitor = {
669
+ ImportDeclaration(path, state) {
670
+ gatherTgpuAliases(state, path.node);
671
+ },
672
+ VariableDeclarator(path, state) {
673
+ performExpressionNaming(state, path, (pathToName, name) => {
674
+ state.wrapInAutoName(pathToName, name);
675
+ });
676
+ },
677
+ ObjectProperty(path, state) {
678
+ performExpressionNaming(state, path, (pathToName, name) => state.wrapInAutoName(pathToName, name));
679
+ },
680
+ ClassProperty(path, state) {
681
+ performExpressionNaming(state, path, (pathToName, name) => state.wrapInAutoName(pathToName, name));
682
+ },
683
+ ClassPrivateProperty(path, state) {
684
+ performExpressionNaming(state, path, (pathToName, name) => state.wrapInAutoName(pathToName, name));
685
+ },
686
+ AssignmentExpression: { exit(path, state) {
687
+ const runtimeFn = operators[path.node.operator];
688
+ if (state.inUseGpuScope && runtimeFn) state.replaceWithAssignmentOverload(path, runtimeFn);
689
+ performExpressionNaming(state, path, (pathToName, name) => state.wrapInAutoName(pathToName, name));
690
+ path.skip();
691
+ } },
692
+ BinaryExpression: { exit(path, state) {
693
+ const runtimeFn = operators[path.node.operator];
694
+ if (state.inUseGpuScope && runtimeFn) state.replaceWithBinaryOverload(path, runtimeFn);
695
+ path.skip();
696
+ } },
697
+ ArrowFunctionExpression: {
698
+ enter(path, state) {
699
+ if (containsUseGpuDirective(path.node) && getEmbeddedTypegpuMetadata(path) === void 0) {
700
+ fnNodeToTranspiledMap.set(path.node, transpile(path.node, this.opts.unstable_obfuscate));
701
+ if (state.inUseGpuScope) throw new Error(`Nesting 'use gpu' functions is not allowed`);
702
+ state.inUseGpuScope = true;
703
+ }
704
+ },
705
+ exit: functionOnExit
706
+ },
707
+ FunctionExpression: {
708
+ enter(path, state) {
709
+ if (containsUseGpuDirective(path.node) && getEmbeddedTypegpuMetadata(path) === void 0) {
710
+ fnNodeToTranspiledMap.set(path.node, transpile(path.node, this.opts.unstable_obfuscate));
711
+ if (state.inUseGpuScope) throw new Error(`Nesting 'use gpu' functions is not allowed`);
712
+ state.inUseGpuScope = true;
713
+ }
714
+ },
715
+ exit: functionOnExit
716
+ },
717
+ FunctionDeclaration: {
718
+ enter(path, state) {
719
+ if (containsUseGpuDirective(path.node) && getEmbeddedTypegpuMetadata(path) === void 0) {
720
+ fnNodeToTranspiledMap.set(path.node, transpile(path.node, this.opts.unstable_obfuscate));
721
+ if (state.inUseGpuScope) throw new Error(`Nesting 'use gpu' functions is not allowed`);
722
+ state.inUseGpuScope = true;
723
+ }
724
+ },
725
+ exit: functionOnExit
726
+ },
727
+ CallExpression: { exit(path, state) {
728
+ const node = path.node;
729
+ if (isShellImplementationCall(node, state)) {
730
+ const implementation = node.arguments[0];
731
+ if (implementation && (implementation.type === "FunctionExpression" || implementation.type === "ArrowFunctionExpression") && !containsUseGpuDirective(implementation)) state.assignMetadata(path.get("arguments.0"), getFunctionName(path.get("arguments.0")), transpile(implementation, this.opts.unstable_obfuscate));
732
+ }
733
+ } }
734
+ };
735
+
736
+ //#endregion
737
+ Object.defineProperty(exports, 'METADATA_FORMAT_VERSION', {
738
+ enumerable: true,
739
+ get: function () {
740
+ return METADATA_FORMAT_VERSION;
741
+ }
742
+ });
743
+ Object.defineProperty(exports, '__toESM', {
744
+ enumerable: true,
745
+ get: function () {
746
+ return __toESM;
747
+ }
748
+ });
749
+ Object.defineProperty(exports, '_objectSpread2', {
750
+ enumerable: true,
751
+ get: function () {
752
+ return _objectSpread2;
753
+ }
754
+ });
755
+ Object.defineProperty(exports, 'checkOpts', {
756
+ enumerable: true,
757
+ get: function () {
758
+ return checkOpts;
759
+ }
760
+ });
761
+ Object.defineProperty(exports, 'defaultOptions', {
762
+ enumerable: true,
763
+ get: function () {
764
+ return defaultOptions;
765
+ }
766
+ });
767
+ Object.defineProperty(exports, 'earlyPruneRegex', {
768
+ enumerable: true,
769
+ get: function () {
770
+ return earlyPruneRegex;
771
+ }
772
+ });
773
+ Object.defineProperty(exports, 'functionVisitor', {
774
+ enumerable: true,
775
+ get: function () {
776
+ return functionVisitor;
777
+ }
778
+ });
779
+ Object.defineProperty(exports, 'getBlockScope', {
780
+ enumerable: true,
781
+ get: function () {
782
+ return getBlockScope;
783
+ }
784
+ });
785
+ Object.defineProperty(exports, 'initPluginState', {
786
+ enumerable: true,
787
+ get: function () {
788
+ return initPluginState;
789
+ }
790
+ });