Skip to content

Commit 329e9cc

Browse files
committed
Make optional to specify the env id in some procs
1 parent cca1f92 commit 329e9cc

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ Remove reference of environment by id.
3434
### void save\_env(id):
3535
Save reference of environment by id.
3636

37-
### void x\_replace\_procedure(id, proc, replacement):
38-
Swaps one procedure for another (or given closure) in a saved environment.
37+
### void x\_replace\_procedure([id], proc, replacement):
38+
Swaps one procedure for another (or given closure) in a saved environment (or current if two arguments passed).
3939

4040
### void x_add_procedure(id, proc, [closure])
4141
Add procedure (or given closure as procedure named by procName) in a saved environment.
4242

43-
### boolean x_remove_procedure(id, proc)
44-
Remove procedure from a saved environment.
43+
### boolean x_remove_procedure([id], proc)
44+
Remove procedure from a saved environment (or current if two arguments passed).
4545

4646
***
4747

src/main/java/me/anatoliy57/chunit/functions/Environments.java

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package me.anatoliy57.chunit.functions;
22

33
import com.laytonsmith.annotations.api;
4-
import com.laytonsmith.core.ArgumentValidation;
5-
import com.laytonsmith.core.Globals;
64
import com.laytonsmith.core.MSVersion;
75
import com.laytonsmith.core.Procedure;
86
import com.laytonsmith.core.constructs.*;
@@ -14,7 +12,6 @@
1412
import com.laytonsmith.core.exceptions.CRE.CREThrowable;
1513
import com.laytonsmith.core.exceptions.ConfigRuntimeException;
1614
import com.laytonsmith.core.functions.AbstractFunction;
17-
import com.laytonsmith.core.functions.ArrayHandling;
1815
import com.laytonsmith.core.natives.interfaces.Mixed;
1916
import 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

Comments
 (0)