Skip to content

Commit

Permalink
Add ScriptStackFrame API class with inline and error trapping support.
Browse files Browse the repository at this point in the history
Move and rewrite EvalStackFrame as ScriptStackFrame API.
Add ability to trap errors and filter allowed commands.
Add flags to eval command for --inline, --trap-errors and --allowed-commands.
Add tests for script execution.
  • Loading branch information
neilcsmith-net committed Apr 22, 2024
1 parent 926a226 commit 88b70df
Show file tree
Hide file tree
Showing 12 changed files with 852 additions and 362 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<version>3.12.1</version>
<configuration>
<release>17</release>
<release>21</release>
</configuration>
</plugin>
<plugin>
Expand Down
14 changes: 12 additions & 2 deletions praxiscore-script/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@

<dependencies>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.praxislive.core.Lookup;
import org.praxislive.core.types.PError;
import org.praxislive.script.commands.CoreCommandsInstaller;
import org.praxislive.script.commands.ScriptCmds;

import static java.lang.System.Logger.Level;

Expand All @@ -42,36 +41,27 @@ class ScriptExecutor {

private static final System.Logger log = System.getLogger(ScriptExecutor.class.getName());

private List<StackFrame> stack;
private Queue<Call> queue;
private Env env;
private Command evaluator;
private Map<String, Command> commandMap;
private Namespace rootNS;
private final List<StackFrame> stack;
private final Queue<Call> queue;
private final Env env;
private final Map<String, Command> commandMap;
private final Namespace rootNS;

public ScriptExecutor(Env env, boolean inline) {
this.env = env;
ScriptExecutor(Env context, final ComponentAddress ctxt) {
this.env = context;
stack = new LinkedList<>();
queue = new LinkedList<>();
if (inline) {
evaluator = ScriptCmds.INLINE_EVAL;
} else {
evaluator = ScriptCmds.EVAL;
}
commandMap = buildCommandMap();
rootNS = new NS();
buildCommandMap();
}

public ScriptExecutor(Env context, final ComponentAddress ctxt) {
this(context, true);
rootNS.addVariable(Env.CONTEXT, new ConstantImpl(ctxt));
}

private void buildCommandMap() {
commandMap = new HashMap<>();
private Map<String, Command> buildCommandMap() {
Map<String, Command> map = new HashMap<>();
CommandInstaller installer = new CoreCommandsInstaller();
installer.install(commandMap);
Lookup.SYSTEM.findAll(CommandInstaller.class).forEach(cmds -> cmds.install(commandMap));
installer.install(map);
Lookup.SYSTEM.findAll(CommandInstaller.class).forEach(cmds -> cmds.install(map));
return map;
}

public void queueEvalCall(Call call) {
Expand Down Expand Up @@ -150,7 +140,11 @@ private void checkAndStartEval() {
Call call = queue.peek();
var args = call.args();
try {
stack.add(0, evaluator.createStackFrame(rootNS, args));
var script = args.get(0).toString();
var stackFrame = ScriptStackFrame.forScript(rootNS, script)
.inline()
.build();
stack.add(0, stackFrame);
processStack();
break;
} catch (Exception ex) {
Expand Down
Loading

0 comments on commit 88b70df

Please sign in to comment.