11package me .anatoliy57 .chunit .functions ;
22
33import com .laytonsmith .annotations .api ;
4- import com .laytonsmith .core .ArgumentValidation ;
5- import com .laytonsmith .core .Globals ;
64import com .laytonsmith .core .MSVersion ;
75import com .laytonsmith .core .Procedure ;
86import com .laytonsmith .core .constructs .*;
1412import com .laytonsmith .core .exceptions .CRE .CREThrowable ;
1513import com .laytonsmith .core .exceptions .ConfigRuntimeException ;
1614import com .laytonsmith .core .functions .AbstractFunction ;
17- import com .laytonsmith .core .functions .ArrayHandling ;
1815import com .laytonsmith .core .natives .interfaces .Mixed ;
1916import me .anatoliy57 .chunit .core .ProcClosure ;
2017
@@ -126,11 +123,11 @@ public String getName() {
126123 }
127124
128125 public Integer [] numArgs () {
129- return new Integer []{3 };
126+ return new Integer []{2 , 3 };
130127 }
131128
132129 public String docs () {
133- return "void {id , proc, replacement} Swaps one procedure for another (or given closure) in a saved environment." ;
130+ return "void {[id] , proc, replacement} Swaps one procedure for another (or given closure) in a saved environment (or current if two arguments passed) ." ;
134131 }
135132
136133 public Class <? extends CREThrowable >[] thrown () {
@@ -146,15 +143,24 @@ public MSVersion since() {
146143 }
147144
148145 public Mixed exec (Target t , Environment env , Mixed ... args ) throws ConfigRuntimeException {
149- String id = args [0 ].val ();
150- String proc = args [1 ].val ();
151146
152147 Environment savedEnv ;
153- synchronized (environmentMap ) {
154- savedEnv = Optional .ofNullable (environmentMap .get (id )).orElseThrow (() -> {
155- throw new CREIndexOverflowException ("No environment with this id \" " +id +"\" found" , t );
156- });
148+ String proc ;
149+ Mixed replacement ;
150+
151+ if (args .length == 2 ) {
152+ savedEnv = env ;
153+ } else {
154+ String id = args [0 ].val ();
155+ synchronized (environmentMap ) {
156+ savedEnv = Optional .ofNullable (environmentMap .get (id )).orElseThrow (() -> {
157+ throw new CREIndexOverflowException ("No environment with this id \" " + id + "\" found" , t );
158+ });
159+ }
157160 }
161+ proc = args [args .length - 2 ].val ();
162+ replacement = args [args .length - 1 ];
163+
158164 GlobalEnv global = savedEnv .getEnv (GlobalEnv .class );
159165 Map <String , Procedure > procedures = global .GetProcs ();
160166
@@ -163,7 +169,6 @@ public Mixed exec(Target t, Environment env, Mixed... args) throws ConfigRuntime
163169 }
164170
165171 Procedure val ;
166- Mixed replacement = args [2 ];
167172 Map <String , Procedure > currentProcedures = env .getEnv (GlobalEnv .class ).GetProcs ();
168173 if (replacement .isInstanceOf (CClosure .TYPE )) {
169174 val = new ProcClosure (proc , (CClosure ) replacement , t );
@@ -266,11 +271,11 @@ public String getName() {
266271 }
267272
268273 public Integer [] numArgs () {
269- return new Integer []{2 };
274+ return new Integer []{1 , 2 };
270275 }
271276
272277 public String docs () {
273- return "boolean {id , procName} Remove procedure from a saved environment." ;
278+ return "boolean {[id] , procName} Remove procedure from a saved environment (or current if two arguments passed) ." ;
274279 }
275280
276281 public Class <? extends CREThrowable >[] thrown () {
@@ -286,15 +291,20 @@ public MSVersion since() {
286291 }
287292
288293 public Mixed exec (Target t , Environment env , Mixed ... args ) throws ConfigRuntimeException {
289- String id = args [0 ].val ();
290- String proc = args [1 ].val ();
291-
292294 Environment savedEnv ;
293- synchronized (environmentMap ) {
294- savedEnv = Optional .ofNullable (environmentMap .get (id )).orElseThrow (() -> {
295- throw new CREIndexOverflowException ("No environment with this id \" " +id +"\" found" , t );
296- });
295+ String proc = args [args .length - 1 ].val ();
296+
297+ if (args .length == 1 ) {
298+ savedEnv = env ;
299+ } else {
300+ String id = args [0 ].val ();
301+ synchronized (environmentMap ) {
302+ savedEnv = Optional .ofNullable (environmentMap .get (id )).orElseThrow (() -> {
303+ throw new CREIndexOverflowException ("No environment with this id \" " +id +"\" found" , t );
304+ });
305+ }
297306 }
307+
298308 GlobalEnv global = savedEnv .getEnv (GlobalEnv .class );
299309 Map <String , Procedure > procedures = global .GetProcs ();
300310
0 commit comments