Skip to content

Commit 012b46e

Browse files
committed
Use unprefixed hook name in scope error message
The scope error message now uses the display name alias (e.g. worldInputs) instead of the internal shader function name (e.g. getWorldInputs). This matches the primary API that users interact with via begin()/end(). Also added a unit test verifying the error uses the unprefixed name.
1 parent f458618 commit 012b46e

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

src/strands/strands_api.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -809,16 +809,20 @@ export function createShaderHooksFunctions(strandsContext, fn, shader) {
809809
if (argIdx >= 0) {
810810
const structParam = hookType.parameters[argIdx];
811811
if (structParam.type.properties) {
812+
const nameMatch = /^get([A-Z0-9]\w*)$/.exec(hookType.name);
813+
const displayName = nameMatch
814+
? nameMatch[1][0].toLowerCase() + nameMatch[1].slice(1)
815+
: hookType.name;
812816
for (const prop of structParam.type.properties) {
813817
const key = prop.name;
814818
Object.defineProperty(hook, key, {
815819
get() {
816820
if (!this._active) {
817821
FES.userError(
818822
'scope error',
819-
`It looks like you're trying to access "${hookType.name}.${key}" outside of its begin()/end() block.\n\n` +
820-
`Properties of ${hookType.name} are only available between ` +
821-
`${hookType.name}.begin() and ${hookType.name}.end().\n\n` +
823+
`It looks like you're trying to access "${displayName}.${key}" outside of its begin()/end() block.\n\n` +
824+
`Properties of ${displayName} are only available between ` +
825+
`${displayName}.begin() and ${displayName}.end().\n\n` +
822826
`To share data between hooks, use sharedVec3() or sharedFloat() ` +
823827
`to pass values between them.`
824828
);
@@ -829,7 +833,7 @@ export function createShaderHooksFunctions(strandsContext, fn, shader) {
829833
if (!this._active) {
830834
FES.userError(
831835
'scope error',
832-
`It looks like you're trying to set "${hookType.name}.${key}" outside of its begin()/end() block.`
836+
`It looks like you're trying to set "${displayName}.${key}" outside of its begin()/end() block.`
833837
);
834838
}
835839
this._args[this._argIdx][key] = val;

test/unit/webgl/p5.Shader.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2759,5 +2759,26 @@ test('returns numbers for builtin globals outside hooks and a strandNode when ca
27592759
}, { myp5 });
27602760
});
27612761
});
2762+
2763+
test('scope error uses unprefixed hook name', () => {
2764+
myp5.createCanvas(50, 50, myp5.WEBGL);
2765+
2766+
try {
2767+
myp5.baseMaterialShader().modify(() => {
2768+
myp5.getWorldInputs.begin();
2769+
myp5.getWorldInputs.end();
2770+
const pos = myp5.getWorldInputs.position;
2771+
}, { myp5 });
2772+
} catch (e) { /* expected */ }
2773+
2774+
assert.isAbove(mockUserError.mock.calls.length, 0, 'FES.userError should have been called');
2775+
const scopeCall = mockUserError.mock.calls.find(call => call[0] === 'scope error');
2776+
assert.isDefined(scopeCall, 'scope error should have been called');
2777+
const errMsg = scopeCall[1];
2778+
assert.include(errMsg, 'worldInputs');
2779+
assert.notInclude(errMsg, 'getWorldInputs');
2780+
assert.include(errMsg, 'begin()');
2781+
assert.include(errMsg, 'end()');
2782+
});
27622783
});
27632784
});

0 commit comments

Comments
 (0)