-
-
Notifications
You must be signed in to change notification settings - Fork 43
feat: Callable unstructs and disarrays #1584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
615b4d8
Make unstructs callable
e3e3289
Update unstruct type
414a45b
Refactor unstruct tests
0a27975
Merge schemaDefaultWrapper and schemaCloneWrapper into schemaCallWrapper
bcbdf95
Make disarray callable
e9926e9
Add disarray tests
84e9f6d
🦖
29d9adf
Merge branch 'main' into feat/callable-unstructs-and-disarrays
aleksanderkatan f1db795
Change `schemaCallWrapper` arg type from `unknown` to `AnyData`
daa54c9
Add tests for `schemaCallWrapper`
86ce21a
🦖
b873cb0
Update packages/typegpu/src/data/disarray.ts
aleksanderkatan 109bda7
Merge remote-tracking branch 'origin/main' into feat/callable-unstruc…
bdd0065
🦖
e5e4d9a
Update packages/typegpu/src/data/struct.ts
aleksanderkatan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,28 @@ | ||
| import type { AnyData } from './index.ts'; | ||
| import { formatToWGSLType } from './vertexFormatData.ts'; | ||
|
|
||
| /** | ||
| * A wrapper for `schema(item)` call. | ||
| * A wrapper for `schema(item)` or `schema()` call. | ||
| * If the schema is a TgpuVertexFormatData, it instead calls the corresponding constructible schema. | ||
| * Throws an error if the schema is not callable. | ||
| */ | ||
| export function schemaCloneWrapper<T>(schema: unknown, item: T): T { | ||
| export function schemaCallWrapper<T>(schema: AnyData, item?: T): T { | ||
| const maybeType = (schema as { type: string })?.type; | ||
|
|
||
| try { | ||
| return (schema as unknown as ((item: T) => T))(item); | ||
| } catch { | ||
| const maybeType = (schema as { type: string })?.type; | ||
| // TgpuVertexFormatData are not callable | ||
| const callSchema = (maybeType in formatToWGSLType | ||
| ? formatToWGSLType[maybeType as keyof typeof formatToWGSLType] | ||
| : schema) as unknown as ((item?: T) => T); | ||
| if (item === undefined) { | ||
| return callSchema(); | ||
| } | ||
| return callSchema(item); | ||
| } catch (e) { | ||
| throw new Error( | ||
| `Schema of type ${ | ||
| maybeType ?? '<unknown>' | ||
| } is not callable or was called with invalid arguments.`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A wrapper for `schema()` call. | ||
| * Throws an error if the schema is not callable. | ||
| */ | ||
| export function schemaDefaultWrapper<T>(schema: unknown): T { | ||
| try { | ||
| return (schema as unknown as (() => T))(); | ||
| } catch { | ||
| const maybeType = (schema as { type: string })?.type; | ||
| throw new Error( | ||
| `Schema of type ${maybeType ?? '<unknown>'} is not callable.`, | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import * as d from '../../src/data/index.ts'; | ||
| import { schemaCallWrapper } from '../../src/data/utils.ts'; | ||
|
|
||
| describe('schemaCallWrapper', () => { | ||
| it('throws when the schema is not callable', () => { | ||
| expect(() => schemaCallWrapper(d.Void)) | ||
| .toThrowErrorMatchingInlineSnapshot( | ||
| '[Error: Schema of type void is not callable or was called with invalid arguments.]', | ||
| ); | ||
| }); | ||
|
|
||
| it('calls schema without arguments', () => { | ||
| const TestStruct = d.struct({ v: d.vec2f }); | ||
|
|
||
| expect(schemaCallWrapper(TestStruct)).toStrictEqual({ v: d.vec2f() }); | ||
| }); | ||
|
|
||
| it('calls schema with arguments', () => { | ||
| const TestStruct = d.struct({ v: d.vec2f }); | ||
| const testInstance = { v: d.vec2f(1, 2), u: d.vec3u() }; | ||
|
|
||
| expect(schemaCallWrapper(TestStruct, testInstance)) | ||
| .toStrictEqual({ v: d.vec2f(1, 2) }); | ||
| }); | ||
|
|
||
| it('works with loose data', () => { | ||
| const TestUnstruct = d.unstruct({ v: d.float32x3 }); | ||
| const testInstance = { v: d.vec3f(1, 2, 3), u: d.vec3u() }; | ||
|
|
||
| expect(schemaCallWrapper(TestUnstruct, testInstance)) | ||
| .toStrictEqual({ v: d.vec3f(1, 2, 3) }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
ifis necessary because vector constructors actually distinguish betweenvec2f()andvec2f(undefined)(the second throws an error, I'm not sure, do we consider this an issue?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say the error is correct. Calling
vec2f(undefined)is not valid imo.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just that in most cases,

f()is equivalent tof(undefined).Though, now that I researched some more, I see that there are more exceptions to this, like destructuring a default parameter, so I guess we may just conclude
vec2f(undefined)as invalid.