Skip to content

Commit c8434b3

Browse files
committed
fix: Error when resolving raw external array
1 parent 2c81dfa commit c8434b3

3 files changed

Lines changed: 28 additions & 13 deletions

File tree

packages/typegpu/src/resolutionCtx.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -970,14 +970,6 @@ export class ResolutionCtxImpl implements ResolutionCtx {
970970
);
971971
}
972972

973-
if (Array.isArray(item)) {
974-
return snip(
975-
stitch`array(${item.map((element) => this.resolve(element))})`,
976-
UnknownData,
977-
/* origin */ 'runtime',
978-
) as ResolvedSnippet;
979-
}
980-
981973
if (schema && isWgslStruct(schema)) {
982974
return snip(
983975
stitch`${this.resolve(schema)}(${Object.entries(schema.propTypes).map(([key, propType]) =>
@@ -989,7 +981,7 @@ export class ResolutionCtxImpl implements ResolutionCtx {
989981
}
990982

991983
throw new WgslTypeError(
992-
`Value ${item} (as json: ${safeStringify(item)}) is not resolvable${
984+
`Value ${safeStringify(item)} is not resolvable${
993985
schema ? ` to type ${safeStringify(schema)}` : ''
994986
}`,
995987
);

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
@@ -193,18 +193,21 @@ describe('array', () => {
193193
it('generates correct code when array clone is used', () => {
194194
const ArraySchema = d.arrayOf(d.u32, 1);
195195

196-
const f = (arr: d.Infer<typeof ArraySchema>) => {
196+
function f(arr: d.InferGPU<typeof ArraySchema>) {
197197
'use gpu';
198198
const clone = ArraySchema(arr);
199-
};
199+
}
200200

201-
const testFn = () => {
201+
const external = [3];
202+
203+
function testFn() {
202204
'use gpu';
203205
const myArray = ArraySchema([d.u32(10)]);
204206
const myClone = ArraySchema(myArray);
207+
const myExternal = ArraySchema(external);
205208
f(myArray);
206209
return;
207-
};
210+
}
208211

209212
expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(`
210213
"fn f(arr: array<u32, 1>) {
@@ -214,6 +217,7 @@ describe('array', () => {
214217
fn testFn() {
215218
var myArray = array<u32, 1>(10u);
216219
var myClone = myArray;
220+
var myExternal = array<u32, 1>(3u);
217221
f(myArray);
218222
return;
219223
}"
@@ -538,6 +542,21 @@ describe('array', () => {
538542
}"
539543
`);
540544
});
545+
546+
it('throws when trying to resolve an untyped external array', () => {
547+
const arr = [1, 2, 3];
548+
function main() {
549+
'use gpu';
550+
arr;
551+
}
552+
553+
expect(() => tgpu.resolve([main])).toThrowErrorMatchingInlineSnapshot(`
554+
[Error: Resolution of the following tree failed:
555+
- <root>
556+
- fn*:main
557+
- fn*:main(): Value [1, 2, 3] is not resolvable]
558+
`);
559+
});
541560
});
542561

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

0 commit comments

Comments
 (0)