Skip to content

Commit

Permalink
Refactor Script API.
Browse files Browse the repository at this point in the history
Remove script.impl package and move useful classes into the main script API package.
Add default methods in Namespace to create variables and constants, hiding the implementation types.
Add default method to InlineCommand to create the stack frame and hide the implementation type.
Remove ExecutionException.
Various other tidy up.
  • Loading branch information
neilcsmith-net committed Feb 8, 2024
1 parent ef1f72c commit 82127a4
Show file tree
Hide file tree
Showing 28 changed files with 387 additions and 401 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import org.praxislive.script.Env;
import org.praxislive.script.InlineCommand;
import org.praxislive.script.Namespace;
import org.praxislive.script.impl.AbstractSingleCallFrame;
import org.praxislive.script.AbstractSingleCallFrame;

/**
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2020 Neil C Smith.
* Copyright 2024 Neil C Smith.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3 only, as
Expand Down Expand Up @@ -32,10 +32,9 @@
import org.praxislive.script.Command;
import org.praxislive.script.CommandInstaller;
import org.praxislive.script.Env;
import org.praxislive.script.ExecutionException;
import org.praxislive.script.Namespace;
import org.praxislive.script.StackFrame;
import org.praxislive.script.impl.AbstractSingleCallFrame;
import org.praxislive.script.AbstractSingleCallFrame;

/**
*
Expand All @@ -53,7 +52,7 @@ public void install(Map<String, Command> commands) {
private final static class ConfigurationCommand implements Command {

@Override
public StackFrame createStackFrame(Namespace namespace, List<Value> args) throws ExecutionException {
public StackFrame createStackFrame(Namespace namespace, List<Value> args) throws Exception {
return new AbstractSingleCallFrame(namespace, args) {
@Override
protected Call createCall(Env env, List<Value> args) throws Exception {
Expand Down
4 changes: 2 additions & 2 deletions praxiscore-hub/src/main/java/org/praxislive/hub/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import org.praxislive.core.RootHub;
import org.praxislive.core.services.Service;
import org.praxislive.core.services.Services;
import org.praxislive.script.impl.ScriptServiceImpl;
import org.praxislive.script.DefaultScriptService;

/**
* Support for configuring and running a {@link RootHub}, along with the
Expand Down Expand Up @@ -91,7 +91,7 @@ private Hub(Builder builder) {

private void extractExtensions(Builder builder, List<Root> exts) {
exts.add(new DefaultComponentFactoryService());
exts.add(new ScriptServiceImpl());
exts.add(new DefaultScriptService());
exts.add(new DefaultTaskService());
exts.addAll(builder.extensions);
}
Expand Down
3 changes: 0 additions & 3 deletions praxiscore-script/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@

module org.praxislive.script {

requires java.logging;

requires org.praxislive.core;
requires org.praxislive.base;

exports org.praxislive.script;
exports org.praxislive.script.impl;

uses org.praxislive.script.CommandInstaller;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2020 Neil C Smith.
* Copyright 2024 Neil C Smith.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3 only, as
Expand All @@ -19,18 +19,23 @@
* Please visit https://www.praxislive.org if you need additional information or
* have any questions.
*/
package org.praxislive.script.impl;
package org.praxislive.script;

import java.util.List;
import org.praxislive.core.Call;
import org.praxislive.core.Value;
import org.praxislive.core.types.PError;
import org.praxislive.core.types.PReference;
import org.praxislive.script.Env;
import org.praxislive.script.Namespace;
import org.praxislive.script.StackFrame;

/**
*
* An abstract {@link StackFrame} for commands that need to make a stack frame
* that makes a single call and processes its response.
* <p>
* Subclasses should implement
* {@link #createCall(org.praxislive.script.Env, java.util.List)} to create the
* call when required. Subclasses may additionally override
* {@link #processResult(java.util.List)} if the need to alter the return values
* from the call.
*/
public abstract class AbstractSingleCallFrame implements StackFrame {

Expand Down Expand Up @@ -81,14 +86,20 @@ public final void postResponse(Call response) {
call = null;
result = response.args();
if (response.isReply()) {
result = processResult(result);
state = State.OK;
try {
result = processResult(result);
state = State.OK;
} catch (Exception ex) {
result = List.of(PError.of(ex));
state = State.Error;
}
} else {
state = State.Error;
}
}
}

@Override
public final void postResponse(State state, List<Value> args) {
throw new IllegalStateException();
}
Expand All @@ -101,11 +112,27 @@ public final List<Value> result() {
return result;
}

/**
* Create the Call. The call must use {@link Env#getAddress()} as the from
* address, and require a response.
*
* @param env environment for address, time, etc.
* @param args command arguments
* @return call
* @throws Exception on error
*/
protected abstract Call createCall(Env env, List<Value> args) throws Exception;

/**
* Process the result from the call on a successful response. Unless this
* method is overridden the result of the stack frame will be the result of
* the call.
*
* @param result successful result from call
* @return processed result
*/
protected List<Value> processResult(List<Value> result) {
return result;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public interface Command {
* @param namespace current namespace
* @param args arguments
* @return stack frame to execute command with provided arguments
* @throws ExecutionException if stack frame cannot be created
* @throws Exception if stack frame cannot be created
*/
public StackFrame createStackFrame(Namespace namespace, List<Value> args)
throws ExecutionException;
throws Exception;

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2018 Neil C Smith.
* Copyright 2024 Neil C Smith.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3 only, as
Expand All @@ -19,30 +19,37 @@
* Please visit https://www.praxislive.org if you need additional information or
* have any questions.
*/
package org.praxislive.script;


package org.praxislive.script.impl;

import java.util.Objects;
import org.praxislive.core.Value;
import org.praxislive.script.Variable;

/**
* Default constant implementation used by
* {@link Namespace#createConstant(java.lang.String, org.praxislive.core.Value)}.
*
*
*/
public class ConstantImpl implements Variable {
final class ConstantImpl implements Variable {

Value value;
private final Value value;

public ConstantImpl(Value value) {
this.value = value;
this.value = Objects.requireNonNull(value);
}

@Override
public void setValue(Value value) {
throw new UnsupportedOperationException();
}

@Override
public Value getValue() {
return value;
}

@Override
public String toString() {
return value.toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2019 Neil C Smith.
* Copyright 2024 Neil C Smith.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3 only, as
Expand All @@ -19,13 +19,11 @@
* Please visit https://www.praxislive.org if you need additional information or
* have any questions.
*/
package org.praxislive.script.impl;
package org.praxislive.script;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.praxislive.base.AbstractRoot;
import org.praxislive.core.Call;
import org.praxislive.core.Control;
Expand All @@ -37,22 +35,19 @@
import org.praxislive.core.services.ScriptService;
import org.praxislive.core.services.Service;
import org.praxislive.core.types.PError;
import org.praxislive.script.Env;

/**
*
* A default implementation of {@link ScriptService}.
*/
public class ScriptServiceImpl extends AbstractRoot implements RootHub.ServiceProvider {
public final class DefaultScriptService extends AbstractRoot implements RootHub.ServiceProvider {

private static final Logger LOG = Logger.getLogger(ScriptServiceImpl.class.getName());
private static final System.Logger LOG = System.getLogger(DefaultScriptService.class.getName());

// private ScriptContext context;
// private ScriptExecutor defaultExecutor;
private final Map<String, Control> controls;
private final Map<ControlAddress, ScriptContext> contexts;
private int exID;

public ScriptServiceImpl() {
public DefaultScriptService() {
controls = new HashMap<>();
controls.put(ScriptService.EVAL, new EvalControl());
controls.put(ScriptService.CLEAR, new ClearControl());
Expand Down Expand Up @@ -81,7 +76,6 @@ private ScriptExecutor getExecutor(ControlAddress from) {
exID++;
String id = "_exec_" + exID;
EnvImpl env = new EnvImpl(ControlAddress.of(getAddress(), id));
// ScriptExecutor ex = new ScriptExecutor(env, true);
ScriptExecutor ex = new ScriptExecutor(env, from.component());
controls.put(id, new ScriptControl(ex));
contexts.put(from, new ScriptContext(id, ex));
Expand Down Expand Up @@ -174,12 +168,12 @@ private EnvImpl(ControlAddress address) {

@Override
public Lookup getLookup() {
return ScriptServiceImpl.this.getLookup();
return DefaultScriptService.this.getLookup();
}

@Override
public long getTime() {
return ScriptServiceImpl.this.getExecutionContext().getTime();
return DefaultScriptService.this.getExecutionContext().getTime();
}

@Override
Expand All @@ -197,7 +191,8 @@ private class Router implements PacketRouter {

@Override
public void route(Packet packet) {
LOG.log(Level.FINEST, () -> "Sending Call : ---\n" + packet.toString());
LOG.log(System.Logger.Level.TRACE,
() -> "Sending Call : ---\n" + packet.toString());
getRouter().route(packet);
}

Expand Down

This file was deleted.

Loading

0 comments on commit 82127a4

Please sign in to comment.