Skip to content

Commit 6a465c3

Browse files
committed
fix: Error when resolving raw external array
1 parent c96d9d4 commit 6a465c3

4 files changed

Lines changed: 29 additions & 14 deletions

File tree

packages/typegpu/src/resolutionCtx.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -953,14 +953,6 @@ export class ResolutionCtxImpl implements ResolutionCtx {
953953
);
954954
}
955955

956-
if (Array.isArray(item)) {
957-
return snip(
958-
stitch`array(${item.map((element) => this.resolve(element))})`,
959-
UnknownData,
960-
/* origin */ 'runtime',
961-
) as ResolvedSnippet;
962-
}
963-
964956
if (schema && isWgslStruct(schema)) {
965957
return snip(
966958
stitch`${this.resolve(schema)}(${Object.entries(schema.propTypes).map(([key, propType]) =>
@@ -972,7 +964,7 @@ export class ResolutionCtxImpl implements ResolutionCtx {
972964
}
973965

974966
throw new WgslTypeError(
975-
`Value ${item} (as json: ${safeStringify(item)}) is not resolvable${
967+
`Value ${safeStringify(item)} is not resolvable${
976968
schema ? ` to type ${safeStringify(schema)}` : ''
977969
}`,
978970
);

packages/typegpu/src/shared/stringify.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { isMatInstance, isVecInstance } from '../data/wgslTypes.ts';
22

33
export function safeStringify(item: unknown): string {
4+
if (Array.isArray(item)) {
5+
return `[${item.map(safeStringify).join(', ')}]`;
6+
}
7+
48
const asString = String(item);
59
if (asString !== '[object Object]') {
610
return asString;

packages/typegpu/tests/array.test.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,21 @@ describe('array', () => {
192192
it('generates correct code when array clone is used', () => {
193193
const ArraySchema = d.arrayOf(d.u32, 1);
194194

195-
const f = (arr: d.Infer<typeof ArraySchema>) => {
195+
function f(arr: d.InferGPU<typeof ArraySchema>) {
196196
'use gpu';
197197
const clone = ArraySchema(arr);
198-
};
198+
}
199+
200+
const external = [3];
199201

200-
const testFn = () => {
202+
function testFn() {
201203
'use gpu';
202204
const myArray = ArraySchema([d.u32(10)]);
203205
const myClone = ArraySchema(myArray);
206+
const myExternal = ArraySchema(external);
204207
f(myArray);
205208
return;
206-
};
209+
}
207210

208211
expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(`
209212
"fn f(arr: array<u32, 1>) {
@@ -213,6 +216,7 @@ describe('array', () => {
213216
fn testFn() {
214217
var myArray = array<u32, 1>(10u);
215218
var myClone = myArray;
219+
var myExternal = array<u32, 1>(3u);
216220
f(myArray);
217221
return;
218222
}"
@@ -443,6 +447,21 @@ describe('array', () => {
443447
}"
444448
`);
445449
});
450+
451+
it('throws when trying to resolve an untyped external array', () => {
452+
const arr = [1, 2, 3];
453+
function main() {
454+
'use gpu';
455+
arr;
456+
}
457+
458+
expect(() => tgpu.resolve([main])).toThrowErrorMatchingInlineSnapshot(`
459+
[Error: Resolution of the following tree failed:
460+
- <root>
461+
- fn*:main
462+
- fn*:main(): Value [1, 2, 3] is not resolvable]
463+
`);
464+
});
446465
});
447466

448467
describe('array.length', () => {

packages/typegpu/tests/tgsl/wgslGenerator.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1757,7 +1757,7 @@ describe('wgslGenerator', () => {
17571757
expect(() => tgpu.resolve([testFn])).toThrowErrorMatchingInlineSnapshot(`
17581758
[Error: Resolution of the following tree failed:
17591759
- <root>
1760-
- fn:testFn: Value undefined (as json: undefined) is not resolvable to type u32]
1760+
- fn:testFn: Value undefined is not resolvable to type u32]
17611761
`);
17621762
});
17631763

0 commit comments

Comments
 (0)