Skip to content
Permalink
Browse files
Fix iclosure retaining unused cloned variable list.
Also adds an iclosure test.
  • Loading branch information
PseudoKnight committed May 19, 2020
1 parent bd424bf commit debbde0511d5778660cf4a97f8b76d1cc27a45d4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
@@ -47,12 +47,8 @@ public void execute(Mixed... values)
try {
Environment environment;
synchronized(this) {
boolean prev = env.getEnv(GlobalEnv.class).getCloneVars();
env.getEnv(GlobalEnv.class).setCloneVars(false);
environment = env.clone();
env.getEnv(GlobalEnv.class).setCloneVars(prev);
}
environment.getEnv(GlobalEnv.class).setCloneVars(true);
if(values != null) {
for(int i = 0; i < names.length; i++) {
String name = names[i];
@@ -1901,7 +1901,7 @@ public Mixed execs(Target t, Environment env, Script parent, ParseTree... nodes)
try {
myEnv = env.clone();
} catch (CloneNotSupportedException ex) {
myEnv = env;
throw new RuntimeException(ex);
}
for(int i = 0; i < nodes.length - 1; i++) {
ParseTree node = nodes[i];
@@ -2024,7 +2024,7 @@ public Mixed execs(Target t, Environment env, Script parent, ParseTree... nodes)
try {
myEnv = env.clone();
} catch (CloneNotSupportedException ex) {
myEnv = env;
throw new RuntimeException(ex);
}
for(int i = 0; i < nodes.length - 1; i++) {
ParseTree node = nodes[i];
@@ -2047,8 +2047,10 @@ public Mixed execs(Target t, Environment env, Script parent, ParseTree... nodes)
Logger.getLogger(DataHandling.class.getName()).log(Level.SEVERE, null, ex);
}
}
CIClosure closure = new CIClosure(nodes[nodes.length - 1], myEnv, returnType, names, defaults, types, t);
return closure;
// Now that iclosure is done with the current variable list, it can be removed from the cloned environment.
// This ensures it's not unintentionally retaining values in memory cloned from the original scope.
myEnv.getEnv(GlobalEnv.class).SetVarList(null);
return new CIClosure(nodes[nodes.length - 1], myEnv, returnType, names, defaults, types, t);
}

@Override
@@ -2908,9 +2910,6 @@ public Integer[] numArgs() {

@Override
public Mixed exec(Target t, Environment env, Mixed... args) throws CancelCommandException, ConfigRuntimeException {
for(int i = 0; i < args.length; i++) {
args[i].val();
}
return CVoid.VOID;
}

@@ -316,6 +316,14 @@ public void testClosure13() throws Exception {
+ "@s();", fakePlayer);
}

@Test(timeout = 10000)
public void testIClosure1() throws Exception {
SRun("@s = 'string';\n"
+ "@c = iclosure(@var){msg(reflect_pull('varlist'))};\n"
+ "execute(@c);", fakePlayer);
verify(fakePlayer).sendMessage("{@var, @arguments}");
}

@Test
public void testToRadix() throws Exception {
assertEquals("f", SRun("to_radix(15, 16)", null));

0 comments on commit debbde0

Please sign in to comment.