Skip to content

Commit e6c7b48

Browse files
committed
Add an SJEP-based script language
For the moment, this language is _for testing only_. Later, if it becomes robust enough, we can move it into SJC.
1 parent 7e39ffe commit e6c7b48

File tree

3 files changed

+302
-0
lines changed

3 files changed

+302
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* #%L
3+
* SciJava UI components for Java Swing.
4+
* %%
5+
* Copyright (C) 2010 - 2016 Board of Regents of the University of
6+
* Wisconsin-Madison.
7+
* %%
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright notice,
12+
* this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright notice,
14+
* this list of conditions and the following disclaimer in the documentation
15+
* and/or other materials provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
21+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
* POSSIBILITY OF SUCH DAMAGE.
28+
* #L%
29+
*/
30+
31+
package org.scijava.script.sjep;
32+
33+
import java.lang.reflect.Field;
34+
import java.util.Collection;
35+
import java.util.Map;
36+
import java.util.Set;
37+
38+
import javax.script.Bindings;
39+
40+
import org.scijava.sjep.eval.AbstractEvaluator;
41+
import org.scijava.sjep.eval.Evaluator;
42+
43+
/**
44+
* Script bindings for the {@link SJEPScriptEngine}.
45+
* <p>
46+
* Some operations are not yet implemented!
47+
* </p>
48+
*
49+
* @author Curtis Rueden
50+
*/
51+
public class SJEPBindings implements Bindings {
52+
53+
private final Evaluator e;
54+
55+
public SJEPBindings(final Evaluator e) {
56+
this.e = e;
57+
}
58+
59+
// -- Map methods --
60+
61+
@Override
62+
public int size() {
63+
return vars().size();
64+
}
65+
66+
@Override
67+
public boolean isEmpty() {
68+
return vars().isEmpty();
69+
}
70+
71+
@Override
72+
public boolean containsValue(final Object value) {
73+
return vars().containsValue(value);
74+
}
75+
76+
@Override
77+
public void clear() {
78+
vars().clear();
79+
}
80+
81+
@Override
82+
public Set<String> keySet() {
83+
return vars().keySet();
84+
}
85+
86+
@Override
87+
public Collection<Object> values() {
88+
return vars().values();
89+
}
90+
91+
@Override
92+
public Set<java.util.Map.Entry<String, Object>> entrySet() {
93+
return vars().entrySet();
94+
}
95+
96+
@Override
97+
public Object put(final String name, final Object value) {
98+
return vars().put(name, value);
99+
}
100+
101+
@Override
102+
public void putAll(final Map<? extends String, ? extends Object> toMerge) {
103+
vars().putAll(toMerge);
104+
}
105+
106+
@Override
107+
public boolean containsKey(final Object key) {
108+
return vars().containsKey(key);
109+
}
110+
111+
@Override
112+
public Object get(final Object key) {
113+
return vars().get(key);
114+
}
115+
116+
@Override
117+
public Object remove(final Object key) {
118+
return vars().remove(key);
119+
}
120+
121+
// -- Helper methods --
122+
123+
/** HACK: Extracts the internal vars map from the evaluator, if present. */
124+
private Map<String, Object> vars() {
125+
// TODO: Use public Evaluator API for this, once it exists.
126+
if (!(e instanceof AbstractEvaluator)) {
127+
throw new UnsupportedOperationException("Unimplemented");
128+
}
129+
try {
130+
final Field varsField = AbstractEvaluator.class.getDeclaredField("vars");
131+
varsField.setAccessible(true);
132+
@SuppressWarnings("unchecked")
133+
final Map<String, Object> vars = (Map<String, Object>) varsField.get(e);
134+
return vars;
135+
}
136+
catch (final NoSuchFieldException exc) {
137+
throw new IllegalStateException(exc);
138+
}
139+
catch (final IllegalAccessException exc) {
140+
throw new IllegalStateException(exc);
141+
}
142+
}
143+
144+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* #%L
3+
* SciJava UI components for Java Swing.
4+
* %%
5+
* Copyright (C) 2010 - 2016 Board of Regents of the University of
6+
* Wisconsin-Madison.
7+
* %%
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright notice,
12+
* this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright notice,
14+
* this list of conditions and the following disclaimer in the documentation
15+
* and/or other materials provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
21+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
* POSSIBILITY OF SUCH DAMAGE.
28+
* #L%
29+
*/
30+
31+
package org.scijava.script.sjep;
32+
33+
import java.io.IOException;
34+
import java.io.Reader;
35+
36+
import javax.script.ScriptException;
37+
38+
import org.scijava.script.AbstractScriptEngine;
39+
import org.scijava.sjep.eval.DefaultEvaluator;
40+
import org.scijava.sjep.eval.Evaluator;
41+
42+
/**
43+
* Script engine for the {@link SJEPScriptLanguage}.
44+
*
45+
* @author Curtis Rueden
46+
*/
47+
public class SJEPScriptEngine extends AbstractScriptEngine {
48+
49+
private final Evaluator e;
50+
51+
public SJEPScriptEngine() {
52+
this(new DefaultEvaluator());
53+
engineScopeBindings = new SJEPBindings(e);
54+
}
55+
56+
public SJEPScriptEngine(final Evaluator e) {
57+
this.e = e;
58+
}
59+
60+
// -- ScriptEngine methods --
61+
62+
@Override
63+
public Object eval(final String script) throws ScriptException {
64+
try {
65+
return e.value(e.evaluate(script.trim()));
66+
}
67+
catch (final IllegalArgumentException exc) {
68+
// NB: Standardize script evaluation exceptions to the checked type.
69+
throw new ScriptException(exc);
70+
}
71+
}
72+
73+
@Override
74+
public Object eval(final Reader reader) throws ScriptException {
75+
try {
76+
return eval(readString(reader));
77+
}
78+
catch (final IOException exc) {
79+
throw new ScriptException(exc);
80+
}
81+
}
82+
83+
// -- Helper methods --
84+
85+
private String readString(final Reader reader) throws IOException {
86+
final char[] buf = new char[8192];
87+
final StringBuilder sb = new StringBuilder();
88+
int r;
89+
while ((r = reader.read(buf, 0, buf.length)) != -1) {
90+
sb.append(buf, 0, r);
91+
}
92+
reader.close();
93+
return sb.toString();
94+
}
95+
96+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* #%L
3+
* SciJava UI components for Java Swing.
4+
* %%
5+
* Copyright (C) 2010 - 2016 Board of Regents of the University of
6+
* Wisconsin-Madison.
7+
* %%
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright notice,
12+
* this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright notice,
14+
* this list of conditions and the following disclaimer in the documentation
15+
* and/or other materials provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
21+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
* POSSIBILITY OF SUCH DAMAGE.
28+
* #L%
29+
*/
30+
31+
package org.scijava.script.sjep;
32+
33+
import java.util.Collections;
34+
import java.util.List;
35+
36+
import javax.script.ScriptEngine;
37+
38+
import org.scijava.plugin.Plugin;
39+
import org.scijava.script.AbstractScriptLanguage;
40+
import org.scijava.script.ScriptLanguage;
41+
42+
/**
43+
* Simple script language built on the {@link org.scijava.sjep.eval} package.
44+
*
45+
* @author Curtis Rueden
46+
*/
47+
@Plugin(type = ScriptLanguage.class)
48+
public class SJEPScriptLanguage extends AbstractScriptLanguage {
49+
50+
// -- ScriptEngineFactory methods --
51+
52+
@Override
53+
public List<String> getExtensions() {
54+
return Collections.singletonList("sjep");
55+
}
56+
57+
@Override
58+
public ScriptEngine getScriptEngine() {
59+
return new SJEPScriptEngine();
60+
}
61+
62+
}

0 commit comments

Comments
 (0)