chore: Use public API in test utilities (extractSnippet, ...)#2388
chore: Use public API in test utilities (extractSnippet, ...)#2388
Conversation
|
pkg.pr.new packages benchmark commit |
📊 Bundle Size Comparison
👀 Notable resultsStatic test results:No major changes. Dynamic test results:No major changes. 📋 All resultsClick to reveal the results table (350 entries).
If you wish to run a comparison for other, slower bundlers, run the 'Tree-shake test' from the GitHub Actions menu. |
Resolution Time Benchmark---
config:
themeVariables:
xyChart:
plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
title "Random Branching (🔴 PR | 🔵 main | 🟢 release)"
x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
y-axis "time (ms)"
line [0.93, 1.83, 3.95, 6.29, 7.33, 11.29, 21.31, 23.01]
line [0.97, 1.93, 3.93, 6.59, 7.43, 10.78, 22.33, 22.82]
line [0.98, 1.99, 4.33, 6.32, 7.22, 10.90, 20.69, 21.29]
---
config:
themeVariables:
xyChart:
plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
title "Linear Recursion (🔴 PR | 🔵 main | 🟢 release)"
x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
y-axis "time (ms)"
line [0.30, 0.54, 0.70, 0.78, 1.09, 1.17, 1.42, 1.55]
line [0.35, 0.50, 0.65, 0.83, 1.19, 1.24, 1.51, 1.63]
line [0.32, 0.52, 0.68, 0.83, 1.12, 1.21, 1.43, 1.58]
---
config:
themeVariables:
xyChart:
plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
title "Full Tree (🔴 PR | 🔵 main | 🟢 release)"
x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
y-axis "time (ms)"
line [0.79, 1.95, 4.00, 6.06, 12.44, 25.98, 54.46, 109.76]
line [0.85, 1.92, 3.41, 6.11, 12.16, 25.45, 54.69, 115.74]
line [0.92, 1.85, 4.08, 6.07, 12.47, 25.84, 54.31, 110.42]
|
c426c82 to
edc606f
Compare
edc606f to
123d4b5
Compare
There was a problem hiding this comment.
Pull request overview
Updates TypeGPU’s test snippet-extraction utilities to rely on the public tgpu.resolve + public generator APIs, enabling reuse of extractSnippetFromFn / expectSnippetOf / expectDataTypeOf in public API test suites.
Changes:
- Refactors
extractSnippetFromFnto usetgpu.resolve(..., { unstable_shaderGenerator })and a customWgslGeneratorsubclass to capture returned snippets. - Updates affected tests to
returnthe inspected expression instead of relying on the last-expression-in-block behavior. - Extracts return-statement codegen into
WgslGenerator._returnand exportsSnippettype viaShaderGeneratormembers.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/typegpu/tests/utils/parseResolved.ts | Switches snippet extraction from internal ctx/generator wiring to public tgpu.resolve with a capturing generator. |
| packages/typegpu/tests/tgsl/wgslGenerator.test.ts | Adjusts a test function to return the array expression for the new extraction approach. |
| packages/typegpu/tests/tgsl/memberAccess.test.ts | Updates snippet expectations by adding explicit return statements in inspected functions. |
| packages/typegpu/tests/std/numeric/add.test.ts | Updates inspected functions to explicitly return expressions for type inference assertions. |
| packages/typegpu/src/tgsl/wgslGenerator.ts | Refactors return-statement generation into a dedicated _return method used by _statement. |
| packages/typegpu/src/tgsl/shaderGenerator_members.ts | Re-exports Snippet type to make it available via the ShaderGenerator public surface. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| this.returnedSnippet = this._expression(statement[1]); | ||
| return super._return([NODE.return]); |
There was a problem hiding this comment.
In the top-level return interception, the snippet is captured via _expression(statement[1]) and then the actual return statement is replaced with super._return([NODE.return]). This means (1) typed functions (with ctx.topFunctionReturnType set) won’t apply the same return typing/conversion as real codegen, and (2) the generator won’t report the return type / run return validations for the inspected return. Consider capturing using the same logic as WgslGenerator._return (use _typedExpression when topFunctionReturnType is set, apply the same conversion/validation, and/or delegate to super._return(statement) so return-type inference remains accurate) while still storing the snippet for inspection.
| this.returnedSnippet = this._expression(statement[1]); | |
| return super._return([NODE.return]); | |
| this.returnedSnippet = this.ctx.topFunctionReturnType | |
| ? this._typedExpression(statement[1], this.ctx.topFunctionReturnType) | |
| : this._expression(statement[1]); | |
| return super._return(statement); |
| throw new WgslTypeError( | ||
| stitch`Cannot return references to arguments, returning '${returnSnippet}'. Copy the argument before returning it.`, |
There was a problem hiding this comment.
This error message interpolates returnSnippet directly ('${returnSnippet}'), but Snippet doesn’t implement a custom toString(), so the message will likely contain [object Object] and be hard to debug. Prefer interpolating the resolved WGSL string (e.g., ctx.resolve(returnSnippet.value, returnSnippet.dataType).value) or another explicit representation.
| throw new WgslTypeError( | |
| stitch`Cannot return references to arguments, returning '${returnSnippet}'. Copy the argument before returning it.`, | |
| const returnStr = this.ctx.resolve( | |
| returnSnippet.value, | |
| returnSnippet.dataType, | |
| ).value; | |
| throw new WgslTypeError( | |
| stitch`Cannot return references to arguments, returning '${returnStr}'. Copy the argument before returning it.`, |
123d4b5 to
17b7556
Compare
This will allow us to use
expectSnippetOf,expectDataTypeOfandextractSnippetin public API tests