-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix loop protection breaking shader strings and p5.strands functions #4083
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
raclim
merged 9 commits into
processing:develop
from
Nixxx19:nityam/fix-loop-protect-shader-strings-4080
Apr 20, 2026
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6273dca
fix loop protection breaking shader strings and p5.strands functions
Nixxx19 78eee42
Merge branch 'develop' into nityam/fix-loop-protect-shader-strings-4080
Nixxx19 cc3ef43
address review feedback on loop protection
Nixxx19 a0b8d98
Merge branch 'develop' into nityam/fix-loop-protect-shader-strings-4080
Nixxx19 a1872ad
simplify loop protection to single ast pass
Nixxx19 e967dfc
Merge branch 'develop' into nityam/fix-loop-protect-shader-strings-4080
raclim 47b6bf2
add forceExit to jest to prevent ci hang
Nixxx19 bb853d3
fix infinite loop in ast walk by applying modifications after walk co…
Nixxx19 7cc69ea
remove forceExit flag now that infinite loop is fixed
Nixxx19 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| import * as acorn from 'acorn'; | ||
| import * as walk from 'acorn-walk'; | ||
| import escodegen from 'escodegen'; | ||
|
|
||
| const LOOP_TIMEOUT_MS = 100; | ||
|
|
||
| function isShaderCall(node) { | ||
| const { callee } = node; | ||
| const isBuildShader = | ||
| callee.type === 'Identifier' && /^build\w*Shader$/.test(callee.name); | ||
| const isModifyCall = | ||
| callee.type === 'MemberExpression' && callee.property.name === 'modify'; | ||
| return isBuildShader || isModifyCall; | ||
| } | ||
|
|
||
| function collectShaderFunctionNames(ast) { | ||
| const names = new Set(); | ||
| walk.simple(ast, { | ||
| CallExpression(node) { | ||
| if (isShaderCall(node)) { | ||
| node.arguments.forEach((arg) => { | ||
| if (arg.type === 'Identifier') { | ||
| names.add(arg.name); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
| return names; | ||
| } | ||
|
|
||
| function makeVarDecl(varName) { | ||
| return { | ||
| type: 'VariableDeclaration', | ||
| kind: 'var', | ||
| declarations: [ | ||
| { | ||
| type: 'VariableDeclarator', | ||
| id: { type: 'Identifier', name: varName }, | ||
| init: { | ||
| type: 'CallExpression', | ||
| callee: { | ||
| type: 'MemberExpression', | ||
| object: { type: 'Identifier', name: 'Date' }, | ||
| property: { type: 'Identifier', name: 'now' }, | ||
| computed: false | ||
| }, | ||
| arguments: [] | ||
| } | ||
| } | ||
| ] | ||
| }; | ||
| } | ||
|
|
||
| function makeCheckStatement(varName, line) { | ||
| return { | ||
| type: 'IfStatement', | ||
| test: { | ||
| type: 'BinaryExpression', | ||
| operator: '>', | ||
| left: { | ||
| type: 'BinaryExpression', | ||
| operator: '-', | ||
| left: { | ||
| type: 'CallExpression', | ||
| callee: { | ||
| type: 'MemberExpression', | ||
| object: { type: 'Identifier', name: 'Date' }, | ||
| property: { type: 'Identifier', name: 'now' }, | ||
| computed: false | ||
| }, | ||
| arguments: [] | ||
| }, | ||
| right: { type: 'Identifier', name: varName } | ||
| }, | ||
| right: { | ||
| type: 'Literal', | ||
| value: LOOP_TIMEOUT_MS, | ||
| raw: String(LOOP_TIMEOUT_MS) | ||
| } | ||
| }, | ||
| consequent: { | ||
| type: 'BlockStatement', | ||
| body: [ | ||
| { | ||
| type: 'ExpressionStatement', | ||
| expression: { | ||
| type: 'CallExpression', | ||
| callee: { | ||
| type: 'MemberExpression', | ||
| object: { | ||
| type: 'MemberExpression', | ||
| object: { type: 'Identifier', name: 'window' }, | ||
| property: { type: 'Identifier', name: 'loopProtect' }, | ||
| computed: false | ||
| }, | ||
| property: { type: 'Identifier', name: 'hit' }, | ||
| computed: false | ||
| }, | ||
| arguments: [{ type: 'Literal', value: line, raw: String(line) }] | ||
| } | ||
| }, | ||
| { type: 'BreakStatement', label: null } | ||
| ] | ||
| }, | ||
| alternate: null | ||
| }; | ||
| } | ||
|
|
||
| function collectLoopsToProtect(ast, shaderNames) { | ||
| const loops = []; | ||
|
|
||
| function visitNode(node, ancestors) { | ||
| const isInsideShader = ancestors.some((ancestor, idx) => { | ||
| if ( | ||
| ancestor.type === 'FunctionDeclaration' && | ||
| shaderNames.has(ancestor.id?.name) | ||
| ) { | ||
| return true; | ||
| } | ||
| if ( | ||
| ancestor.type === 'FunctionExpression' || | ||
| ancestor.type === 'ArrowFunctionExpression' | ||
| ) { | ||
| const parent = ancestors[idx - 1]; | ||
| if ( | ||
| parent?.type === 'CallExpression' && | ||
| isShaderCall(parent) && | ||
| parent.arguments.includes(ancestor) | ||
| ) { | ||
| return true; | ||
| } | ||
| if ( | ||
| parent?.type === 'VariableDeclarator' && | ||
| shaderNames.has(parent.id?.name) | ||
| ) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| }); | ||
|
|
||
| if (isInsideShader) return; | ||
|
|
||
| let parentBlock = null; | ||
| for (let i = ancestors.length - 1; i >= 0; i--) { | ||
| const ancestor = ancestors[i]; | ||
| if ( | ||
| ancestor !== node && | ||
| (ancestor.type === 'BlockStatement' || ancestor.type === 'Program') | ||
| ) { | ||
| parentBlock = ancestor; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| loops.push({ loop: node, parentBlock }); | ||
| } | ||
|
|
||
| walk.ancestor(ast, { | ||
| ForStatement: visitNode, | ||
| WhileStatement: visitNode, | ||
| DoWhileStatement: visitNode | ||
| }); | ||
|
|
||
| return loops; | ||
| } | ||
|
|
||
| function injectProtection(loops) { | ||
| loops.forEach(({ loop, parentBlock }, idx) => { | ||
| const varName = `_LP${idx}`; | ||
| const { line } = loop.loc.start; | ||
| const check = makeCheckStatement(varName, line); | ||
|
|
||
| if (loop.body.type === 'BlockStatement') { | ||
| loop.body.body.unshift(check); | ||
| } else { | ||
| loop.body = { type: 'BlockStatement', body: [check, loop.body] }; | ||
| } | ||
|
|
||
| if (parentBlock) { | ||
| const varDecl = makeVarDecl(varName); | ||
| const nodeIdx = parentBlock.body.indexOf(loop); | ||
| if (nodeIdx !== -1) { | ||
| parentBlock.body.splice(nodeIdx, 0, varDecl); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function parseJs(jsText) { | ||
| const options = { ecmaVersion: 'latest', locations: true }; | ||
| try { | ||
| return acorn.parse(jsText, { ...options, sourceType: 'script' }); | ||
| } catch (e) { | ||
| try { | ||
| return acorn.parse(jsText, { ...options, sourceType: 'module' }); | ||
| } catch (e2) { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export function jsPreprocess(jsText) { | ||
| if (/\/\/\s*noprotect/.test(jsText)) { | ||
| return jsText; | ||
| } | ||
|
|
||
| const ast = parseJs(jsText); | ||
| if (!ast) return jsText; | ||
|
|
||
| const shaderNames = collectShaderFunctionNames(ast); | ||
| const loops = collectLoopsToProtect(ast, shaderNames); | ||
|
|
||
| if (loops.length === 0) return jsText; | ||
|
|
||
| injectProtection(loops); | ||
|
|
||
| return escodegen.generate(ast); | ||
| } | ||
Oops, something went wrong.
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.
nice, looks good!
Uh oh!
There was an error while loading. Please reload this page.
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.
thank you!