Skip to content

Commit b82b49b

Browse files
committed
1 parent e4bc0dc commit b82b49b

File tree

3 files changed

+241
-0
lines changed

3 files changed

+241
-0
lines changed

LICENSE.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Copyright (c) 2008 - 2014, Board of Regents of the University of
2+
Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
3+
Institute of Molecular Cell Biology and Genetics.
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without modification,
7+
are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
2. Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
POSSIBILITY OF SUCH DAMAGE.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* #%L
3+
* JSR-223-compliant JavaScript scripting language plugin.
4+
* %%
5+
* Copyright (C) 2008 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.plugins.scripting.javascript;
33+
34+
import java.lang.reflect.InvocationTargetException;
35+
36+
import javax.script.ScriptEngine;
37+
38+
import org.scijava.log.LogService;
39+
import org.scijava.plugin.Parameter;
40+
import org.scijava.plugin.Plugin;
41+
import org.scijava.script.AdaptedScriptLanguage;
42+
import org.scijava.script.ScriptLanguage;
43+
import org.scijava.util.ClassUtils;
44+
45+
/**
46+
* An adapter of the JavaScript interpreter to ImageJ's scripting interfaces.
47+
*
48+
* @author Curtis Rueden
49+
* @see ScriptEngine
50+
*/
51+
@Plugin(type = ScriptLanguage.class)
52+
public class JavaScriptScriptLanguage extends AdaptedScriptLanguage {
53+
54+
@Parameter
55+
private LogService log;
56+
57+
public JavaScriptScriptLanguage() {
58+
super("javascript");
59+
}
60+
61+
@Override
62+
public Object decode(final Object object) {
63+
// NB: JavaScript objects come out of the engine wrapped as
64+
// JavaScript-specific objects (e.g., NativeJavaObject), which must be
65+
// unwrapped. Unfortunately, we don't necessarily have direct compile-time
66+
// access to the JavaScript Wrapper interface implemented by the
67+
// NativeJavaObject wrapper. But we can access it via reflection. It is
68+
// typically org.mozilla.javascript.Wrapper, except for Oracle's shaded
69+
// implementation, which is sun.org.mozilla.javascript.internal.Wrapper.
70+
// Either way, the package will match that of the wrapped object itself.
71+
if (object == null) return null;
72+
final Class<?> objectClass = object.getClass();
73+
final String packageName = objectClass.getPackage().getName();
74+
final Class<?> wrapperClass =
75+
ClassUtils.loadClass(packageName + ".Wrapper");
76+
if (wrapperClass == null || !wrapperClass.isAssignableFrom(objectClass)) {
77+
return object;
78+
}
79+
try {
80+
return wrapperClass.getMethod("unwrap").invoke(object);
81+
}
82+
catch (final IllegalArgumentException exc) {
83+
log.warn(exc);
84+
}
85+
catch (final IllegalAccessException exc) {
86+
log.warn(exc);
87+
}
88+
catch (final InvocationTargetException exc) {
89+
log.warn(exc);
90+
}
91+
catch (final NoSuchMethodException exc) {
92+
log.warn(exc);
93+
}
94+
return null;
95+
}
96+
97+
@Override
98+
public String getLanguageName() {
99+
// NB: Must override, or else the name is "ECMAScript".
100+
return "JavaScript";
101+
}
102+
103+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* #%L
3+
* JSR-223-compliant JavaScript scripting language plugin.
4+
* %%
5+
* Copyright (C) 2008 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.plugins.scripting.javascript;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertNull;
36+
import static org.junit.Assert.assertTrue;
37+
38+
import java.io.IOException;
39+
import java.util.concurrent.ExecutionException;
40+
41+
import javax.script.Bindings;
42+
import javax.script.ScriptContext;
43+
import javax.script.ScriptEngine;
44+
import javax.script.ScriptException;
45+
46+
import org.junit.Test;
47+
import org.scijava.Context;
48+
import org.scijava.script.ScriptLanguage;
49+
import org.scijava.script.ScriptModule;
50+
import org.scijava.script.ScriptService;
51+
52+
/**
53+
* JavaScript unit tests.
54+
*
55+
* @author Johannes Schindelin
56+
* @author Curtis Rueden
57+
*/
58+
public class JavaScriptTest {
59+
60+
@Test
61+
public void testBasic() throws InterruptedException, ExecutionException,
62+
IOException, ScriptException
63+
{
64+
final Context context = new Context(ScriptService.class);
65+
final ScriptService scriptService = context.getService(ScriptService.class);
66+
final String script = "$x = 1 + 2;";
67+
// NB: Some JVMs return Integer, others Double. Let's be careful here.
68+
final ScriptModule m = scriptService.run("add.js", script, true).get();
69+
final Number result = (Number) m.getReturnValue();
70+
assertEquals(3.0, result.doubleValue(), 0.0);
71+
}
72+
73+
@Test
74+
public void testLocals() throws ScriptException {
75+
final Context context = new Context(ScriptService.class);
76+
final ScriptService scriptService = context.getService(ScriptService.class);
77+
78+
final ScriptLanguage language = scriptService.getLanguageByExtension("js");
79+
final ScriptEngine engine = language.getScriptEngine();
80+
assertTrue(engine.getClass().getName().endsWith(".RhinoScriptEngine"));
81+
engine.put("$hello", 17);
82+
assertEquals("17", engine.eval("$hello").toString());
83+
assertEquals("17", engine.get("$hello").toString());
84+
85+
final Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
86+
bindings.clear();
87+
assertNull(engine.get("$hello"));
88+
}
89+
90+
@Test
91+
public void testParameters() throws InterruptedException, ExecutionException,
92+
IOException, ScriptException
93+
{
94+
final Context context = new Context(ScriptService.class);
95+
final ScriptService scriptService = context.getService(ScriptService.class);
96+
97+
final String script = "" + //
98+
"// @ScriptService ss\n" + //
99+
"// @OUTPUT String language\n" + //
100+
"language = ss.getLanguageByName('JavaScript').getLanguageName()\n";
101+
final ScriptModule m = scriptService.run("hello.js", script, true).get();
102+
103+
final Object actual = m.getOutput("language");
104+
final String expected =
105+
scriptService.getLanguageByName("JavaScript").getLanguageName();
106+
assertEquals(expected, actual);
107+
108+
final Object result = m.getReturnValue();
109+
assertEquals(expected, result);
110+
}
111+
112+
}

0 commit comments

Comments
 (0)