{"record":{"id":"6d93d1440ceb515a","repo":"mrdoob/three.js","slug":"three-wgslnodefunction-function-is-not-a-wgsl-cod","errorCode":null,"errorMessage":"THREE.WGSLNodeFunction: Function is not a WGSL code.","messagePattern":"THREE\\.WGSLNodeFunction: Function is not a WGSL code\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/renderers/webgpu/nodes/WGSLNodeFunction.js","lineNumber":142,"sourceCode":"\n\t\tconst blockCode = source.substring( declaration[ 0 ].length );\n\t\tconst outputType = declaration[ 3 ] || 'void';\n\n\t\tconst name = declaration[ 1 ] !== undefined ? declaration[ 1 ] : '';\n\t\tconst type = wgslTypeLib[ outputType ] || outputType;\n\n\t\treturn {\n\t\t\ttype,\n\t\t\tinputs,\n\t\t\tname,\n\t\t\tinputsCode,\n\t\t\tblockCode,\n\t\t\toutputType\n\t\t};\n\n\t} else {\n\n\t\tthrow new Error( 'THREE.WGSLNodeFunction: Function is not a WGSL code.' );\n\n\t}\n\n};\n\n/**\n * This class represents a WSL node function.\n *\n * @augments NodeFunction\n */\nclass WGSLNodeFunction extends NodeFunction {\n\n\t/**\n\t * Constructs a new WGSL node function.\n\t *\n\t * @param {string} source - The WGSL source.\n\t */\n\tconstructor( source ) {","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/mrdoob/three.js/blob/da05705fa31f02710c19772a2aa70c7454807f0c/src/renderers/webgpu/nodes/WGSLNodeFunction.js#L124-L160","documentation":"Thrown by the WGSLNodeFunction parser when the supplied source string does not match the WGSL function-declaration regex (`^[fn]*\\s*(name)?\\s*(...)\\s*[->]?\\s*returnType`). The parser expects a single WGSL function signature with a parameter list and a return type; anything else (a GLSL snippet, a bare expression, a struct, malformed syntax) fails to parse.","triggerScenarios":"Passing non-WGSL code (e.g. GLSL) to a WGSLNodeFunction / wgslFn builder. Passing a struct declaration, a statement, or a malformed function. Using wgslFn with a template string that doesn't begin with a valid function signature.","commonSituations":"Mixing up WGSL and GLSL node-function builders (e.g. calling wgslFn with glsl source). Copying a snippet missing the `fn name(...) -> ret` prefix. Passing a multi-statement WGSL module instead of a single function. Whitespace/character issues that break the anchored regex.","solutions":["Ensure the source is a single WGSL function: `fn myFn(a: f32) -> f32 { return a; }`.","Use the GLSL equivalent builder (glslFn) for GLSL source instead of wgslFn.","Extract only the function you want parsed; don't pass structs, globals, or multiple functions.","Validate the string starts with `fn` (or matches the declaration regex) before constructing WGSLNodeFunction."],"exampleFix":"// before - passing GLSL to a WGSL builder\nconst fn = wgslFn(`float myFn(float a) { return a; }`); // throws\n\n// after - valid WGSL\nconst fn = wgslFn(`fn myFn(a: f32) -> f32 { return a; }`);","handlingStrategy":"try-catch","validationCode":"const WGSL_FN_RE = /^[fn]*\\s*([a-z_0-9]+)?\\s*\\(([\\s\\S]*?)\\)\\s*[\\->]*\\s*([a-z_0-9]+(?:<[\\s\\S]+?>)?)/i;\nfunction assertWgslFunction(source) {\n  if (!WGSL_FN_RE.test(source.trim())) throw new Error('Source is not a WGSL function declaration');\n}","typeGuard":"function looksLikeWgslFunction(source) {\n  const re = /^[fn]*\\s*([a-z_0-9]+)?\\s*\\(([\\s\\S]*?)\\)\\s*[\\->]*\\s*([a-z_0-9]+(?:<[\\s\\S]+?>)?)/i;\n  return re.test(String(source).trim());\n}","tryCatchPattern":"try {\n  const fn = wgslFn(source);\n} catch (e) {\n  if (/Function is not a WGSL code/.test(e.message)) { /* switch to glslFn for GLSL, or rewrite source as `fn name(...) -> ret { ... }` */ }\n  else throw e;\n}","preventionTips":["Use wgslFn only with WGSL source that begins with `fn name(params) -> retType { ... }`.","Use glslFn for GLSL source to avoid confusing the parsers.","Pass a single function — extract structs/globals into separate declarations."],"tags":["webgpu","wgsl","tsl","nodes","shader","parsing"],"backgroundTag":null,"analyzedSha":"da05705fa31f02710c19772a2aa70c7454807f0c","analyzedAt":"2026-08-12T22:51:37.160Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}