-
-
Notifications
You must be signed in to change notification settings - Fork 21
feat: Allow destructuring arguments in TGSL function implementations #1257
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
Conversation
pkg.pr.new packages
benchmark commit |
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.
Very nice feature to have! I was a little worried about shadowing but it works well 🙇♂️
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.
Amazing change! 💜
const argAlias = layer.argAliases[id]; | ||
if (argAlias !== undefined) { | ||
return argAlias; | ||
} |
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.
const argAlias = layer.argAliases[id]; | |
if (argAlias !== undefined) { | |
return argAlias; | |
} | |
if (layer.argAliases[id]) { | |
return layer.argAliases[id]; | |
} |
applyExternals( | ||
externalMap, | ||
Object.fromEntries( | ||
ast.argNames.props.map(({ prop, alias }) => [alias, prop]), | ||
), | ||
); | ||
} | ||
|
||
if ( | ||
!Array.isArray(shell.argTypes) && | ||
ast.argNames.type === 'identifiers' && | ||
ast.argNames.names[0] !== undefined | ||
) { | ||
applyExternals(externalMap, { | ||
[ast.argNames.names[0]]: Object.fromEntries( | ||
Object.keys(shell.argTypes).map((arg) => [arg, arg]), | ||
), | ||
}); | ||
} | ||
|
||
// Verifying all required externals are present. | ||
// verify all required externals are present | ||
const missingExternals = ast.externalNames.filter( | ||
(name) => !(name in externalMap), | ||
); | ||
|
||
if (missingExternals.length > 0) { | ||
throw new MissingLinksError(getName(this), missingExternals); | ||
} | ||
|
||
const args: Snippet[] = Array.isArray(shell.argTypes) | ||
? ast.argNames.type === 'identifiers' | ||
? shell.argTypes.map((arg, i) => ({ | ||
value: (ast.argNames.type === 'identifiers' | ||
? ast.argNames.names[i] | ||
: undefined) ?? `arg_${i}`, | ||
dataType: arg as AnyWgslData, | ||
})) | ||
: [] | ||
: Object.entries(shell.argTypes).map(([name, dataType]) => ({ | ||
value: name, | ||
dataType: dataType as AnyWgslData, | ||
})); | ||
|
||
// generate wgsl string | ||
const { head, body } = ctx.fnToWgsl({ | ||
args, | ||
args: shell.argTypes.map((arg, i) => ({ | ||
value: ast.params[i]?.type === FuncParameterType.identifier | ||
? ast.params[i].name | ||
: `_arg_${i}`, | ||
dataType: arg as AnyWgslData, | ||
})), | ||
argAliases: Object.fromEntries( | ||
ast.params.flatMap((param, i) => | ||
param.type === FuncParameterType.destructuredObject | ||
? param.props.map(({ name, alias }) => [ | ||
alias, | ||
{ | ||
value: `_arg_${i}.${name}`, | ||
dataType: (shell.argTypes[i] as AnyWgslStruct) | ||
.propTypes[name] as AnyWgslData, | ||
}, | ||
]) | ||
: [] | ||
), | ||
), |
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'm gonna hang this diff on my wall😍
params: functionNode.params.filter((param) => | ||
param.type === 'ObjectPattern' || param.type === 'Identifier' | ||
).map((param) => |
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.
Instead of filtering here we could check if there are any other and have a nice early error
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.
Great!!
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.
Wooh, this is really nice! A pleasure to read 👏
closes #1242
closes #1270
closes #1271