Skip to content

Commit d516e81

Browse files
authored
Merge pull request #8741 from LalitNarayanYadav/refactor/strands-binary-logical-dedup
Refactor: Deduplicate BinaryExpression and LogicalExpression transformation logic
2 parents bbfd9ab + 48b9509 commit d516e81

1 file changed

Lines changed: 33 additions & 66 deletions

File tree

src/strands/strands_transpiler.js

Lines changed: 33 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,35 @@ function replaceReferences(node, tempVarMap) {
235235
internalReplaceReferences(node);
236236
}
237237

238+
// Shared handler for both BinaryExpression and LogicalExpression —
239+
// both follow the same operator-to-method-call transformation pattern.
240+
function transformBinaryOrLogical(node, state, ancestors) {
241+
if (ancestors.some(a => nodeIsUniform(a) || nodeIsUniformCallbackFn(a, state.uniformCallbackNames))) {
242+
return;
243+
}
244+
const unsafeTypes = ['Literal', 'ArrayExpression', 'Identifier'];
245+
if (unsafeTypes.includes(node.left.type)) {
246+
node.left = {
247+
type: 'CallExpression',
248+
callee: {
249+
type: 'Identifier',
250+
name: '__p5.strandsNode',
251+
},
252+
arguments: [node.left]
253+
};
254+
}
255+
node.type = 'CallExpression';
256+
node.callee = {
257+
type: 'MemberExpression',
258+
object: node.left,
259+
property: {
260+
type: 'Identifier',
261+
name: replaceBinaryOperator(node.operator),
262+
},
263+
};
264+
node.arguments = [node.right];
265+
}
266+
238267
const ASTCallbacks = {
239268
UnaryExpression(node, state, ancestors) {
240269
if (ancestors.some(a => nodeIsUniform(a) || nodeIsUniformCallbackFn(a, state.uniformCallbackNames))) {
@@ -509,72 +538,10 @@ const ASTCallbacks = {
509538
}
510539
}
511540
},
512-
BinaryExpression(node, state, ancestors) {
513-
// Don't convert uniform default values to node methods, as
514-
// they should be evaluated at runtime, not compiled.
515-
if (ancestors.some(a => nodeIsUniform(a) || nodeIsUniformCallbackFn(a, state.uniformCallbackNames))) {
516-
return;
517-
}
518-
// If the left hand side of an expression is one of these types,
519-
// we should construct a node from it.
520-
const unsafeTypes = ['Literal', 'ArrayExpression', 'Identifier'];
521-
if (unsafeTypes.includes(node.left.type)) {
522-
const leftReplacementNode = {
523-
type: 'CallExpression',
524-
callee: {
525-
type: 'Identifier',
526-
name: '__p5.strandsNode',
527-
},
528-
arguments: [node.left]
529-
}
530-
node.left = leftReplacementNode;
531-
}
532-
// Replace the binary operator with a call expression
533-
// in other words a call to BaseNode.mult(), .div() etc.
534-
node.type = 'CallExpression';
535-
node.callee = {
536-
type: 'MemberExpression',
537-
object: node.left,
538-
property: {
539-
type: 'Identifier',
540-
name: replaceBinaryOperator(node.operator),
541-
},
542-
};
543-
node.arguments = [node.right];
544-
},
545-
LogicalExpression(node, state, ancestors) {
546-
// Don't convert uniform default values to node methods, as
547-
// they should be evaluated at runtime, not compiled.
548-
if (ancestors.some(a => nodeIsUniform(a) || nodeIsUniformCallbackFn(a, state.uniformCallbackNames))) {
549-
return;
550-
}
551-
// If the left hand side of an expression is one of these types,
552-
// we should construct a node from it.
553-
const unsafeTypes = ['Literal', 'ArrayExpression', 'Identifier'];
554-
if (unsafeTypes.includes(node.left.type)) {
555-
const leftReplacementNode = {
556-
type: 'CallExpression',
557-
callee: {
558-
type: 'Identifier',
559-
name: '__p5.strandsNode',
560-
},
561-
arguments: [node.left]
562-
}
563-
node.left = leftReplacementNode;
564-
}
565-
// Replace the logical operator with a call expression
566-
// in other words a call to BaseNode.or(), .and() etc.
567-
node.type = 'CallExpression';
568-
node.callee = {
569-
type: 'MemberExpression',
570-
object: node.left,
571-
property: {
572-
type: 'Identifier',
573-
name: replaceBinaryOperator(node.operator),
574-
},
575-
};
576-
node.arguments = [node.right];
577-
},
541+
BinaryExpression: transformBinaryOrLogical,
542+
LogicalExpression: transformBinaryOrLogical,
543+
544+
578545
ConditionalExpression(node, state, ancestors) {
579546
if (ancestors.some(a => nodeIsUniform(a) || nodeIsUniformCallbackFn(a, state.uniformCallbackNames))) {
580547
return;

0 commit comments

Comments
 (0)