From c81e24298f6a00b0093169f107894f4ffadaaa7a Mon Sep 17 00:00:00 2001 From: Ben Sherman Date: Thu, 21 Nov 2024 14:27:58 -0600 Subject: [PATCH] Fix detection of built-in variables Signed-off-by: Ben Sherman --- .../main/java/script/control/VariableScopeVisitor.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/modules/compiler/src/main/java/script/control/VariableScopeVisitor.java b/modules/compiler/src/main/java/script/control/VariableScopeVisitor.java index 8f3ad8d..2d3ad11 100644 --- a/modules/compiler/src/main/java/script/control/VariableScopeVisitor.java +++ b/modules/compiler/src/main/java/script/control/VariableScopeVisitor.java @@ -512,7 +512,7 @@ else if( node instanceof VariableExpression ve ) { private void declareAssignedVariable(VariableExpression ve) { var variable = findVariableDeclaration(ve.getName(), ve); if( variable != null ) { - if( variable instanceof PropertyNode pn && pn.getNodeMetaData("access.method") != null ) + if( isBuiltinVariable(variable) ) addError("Built-in variable cannot be re-assigned", ve); else checkExternalWriteInClosure(ve, variable); @@ -530,6 +530,12 @@ else if( currentDefinition instanceof ProcessNode || currentDefinition instanceo } } + private boolean isBuiltinVariable(Variable variable) { + return variable instanceof PropertyNode pn + && pn.getNodeMetaData("access.method") instanceof MethodNode mn + && findAnnotation(mn, Constant.class).isPresent(); + } + private void visitMutatedVariable(Expression node) { VariableExpression target = null; while( true ) { @@ -550,7 +556,7 @@ else if( node instanceof BinaryExpression be && be.getOperation().getType() == T if( target == null ) return; var variable = findVariableDeclaration(target.getName(), target); - if( variable instanceof PropertyNode pn && pn.getNodeMetaData("access.method") != null ) { + if( isBuiltinVariable(variable) ) { if( "params".equals(variable.getName()) ) sourceUnit.addWarning("Params should be declared at the top-level (i.e. outside the workflow)", target); // TODO: re-enable after workflow.onComplete bug is fixed