Skip to content

Commit 9039fe8

Browse files
committed
Fix which env bind uses
1 parent eda1f13 commit 9039fe8

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/main/java/com/laytonsmith/core/Script.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,8 @@ private static Mixed evalLoop(EvalStack stack, Mixed lastResult, boolean hasResu
568568
// First visit: resolve function or procedure
569569
if(frame.getFunction() == null && !frame.hasFlowFunction()) {
570570
if(cfunc.hasProcedure()) {
571-
Procedure p = gEnv.GetProcs().get(data.val());
571+
Procedure p = frame.getEnv().getEnv(GlobalEnv.class)
572+
.GetProcs().get(data.val());
572573
if(p == null) {
573574
throw new CREInvalidProcedureException(
574575
"Unknown procedure \"" + data.val() + "\"", data.getTarget());

src/test/java/com/laytonsmith/testing/ProcedureTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import static com.laytonsmith.testing.StaticTest.SRun;
55

66
import com.laytonsmith.core.environments.GlobalEnv;
7+
import com.laytonsmith.core.events.BindableEvent;
8+
import com.laytonsmith.core.events.Driver;
9+
import com.laytonsmith.core.events.EventUtils;
710
import com.laytonsmith.core.exceptions.CRE.CRECastException;
811
import com.laytonsmith.core.exceptions.CRE.CREStackOverflowError;
912
import com.laytonsmith.core.exceptions.StackTraceManager;
@@ -13,6 +16,7 @@
1316
import org.junit.Test;
1417

1518
import static org.junit.Assert.fail;
19+
import static org.mockito.Mockito.atLeastOnce;
1620
import static org.mockito.Mockito.mock;
1721
import static org.mockito.Mockito.times;
1822
import static org.mockito.Mockito.verify;
@@ -161,4 +165,37 @@ public void testCustomCallDepthLimit() throws Exception {
161165
}
162166
}
163167

168+
@Test
169+
public void testProcVisibleInsideClosure() throws Exception {
170+
SRun("proc _test() { msg('hello') } closure() { _test() }()", fakePlayer);
171+
verify(fakePlayer).sendMessage("hello");
172+
}
173+
174+
@Test
175+
public void testProcVisibleInClosureExecutedFromBind() throws Exception {
176+
try {
177+
SRun("@callbacks = array()"
178+
+ "\nbind('cmdline_test_event', null, null, @event, @callbacks) {"
179+
+ "\n foreach(@cb in @callbacks) {"
180+
+ "\n execute(@cb)"
181+
+ "\n }"
182+
+ "\n}"
183+
+ "\nproc _test() { msg('hello') }"
184+
+ "\n@callbacks[] = closure() {"
185+
+ "\n _test()"
186+
+ "\n}", fakePlayer);
187+
188+
EventUtils.TriggerListener(Driver.EXTENSION, "cmdline_test_event", new BindableEvent() {
189+
@Override
190+
public Object _GetObject() {
191+
return new Object();
192+
}
193+
});
194+
195+
verify(fakePlayer, atLeastOnce()).sendMessage("hello");
196+
} finally {
197+
EventUtils.UnregisterAll();
198+
}
199+
}
200+
164201
}

0 commit comments

Comments
 (0)