diff --git a/src/main/java/carpet/script/Expression.java b/src/main/java/carpet/script/Expression.java index e9201c4e..1fdbe740 100644 --- a/src/main/java/carpet/script/Expression.java +++ b/src/main/java/carpet/script/Expression.java @@ -710,6 +710,21 @@ public class Expression implements Cloneable throw new ExpressionException(expr, token, "Problems in allocating global function "+name); } + Map contextValues = new HashMap<>(); + for (String global : globals) + { + LazyValue lv = context.getVariable(global); + if (lv == null) + { + Value zero = Value.ZERO.reboundedTo(global); + contextValues.put(global, (cc, tt) -> zero); + } + else + { + contextValues.put(global, lv); + } + } + context.host.globalFunctions.put(name, new UserDefinedFunction(arguments, function_context, token) { @Override @@ -724,19 +739,7 @@ public class Expression implements Cloneable } Context newFrame = c.recreate(); - for (String global : globals) - { - LazyValue lv = c.getVariable(global); - if (lv == null) - { - Value zero = Value.ZERO.reboundedTo(global); - newFrame.setVariable(global, (cc, tt) -> zero); - } - else - { - newFrame.setVariable(global, lv); - } - } + contextValues.forEach(newFrame::setVariable); for (int i=0; i