Skip to content

Commit 90aaf66

Browse files
committed
Moved Nashorn into its own package. Now it can mangle javascript reserved keywords into valid keywords.
1 parent 64fbd09 commit 90aaf66

File tree

4 files changed

+99
-42
lines changed

4 files changed

+99
-42
lines changed

src/main/java/com/diffplug/jscriptbox/JScriptBox.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static String checkValidIdentifier(String name) {
6464
}
6565

6666
/** Checks that the given name is a valid identifier. */
67-
static boolean isValidIdentifier(String name) {
67+
public static boolean isValidIdentifier(String name) {
6868
return name.length() > 0 &&
6969
Character.isJavaIdentifierStart(name.codePointAt(0)) &&
7070
name.codePoints().skip(1).allMatch(Character::isJavaIdentifierPart);

src/main/java/com/diffplug/jscriptbox/Language.java

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717

1818
import java.util.Map;
1919

20-
import javax.script.ScriptContext;
2120
import javax.script.ScriptEngine;
22-
import javax.script.ScriptEngineManager;
2321
import javax.script.ScriptException;
2422

2523
/** Interface which converts the result of a {@link JScriptBox} into a {@link ScriptEngine}. */
@@ -30,29 +28,4 @@ public interface Language {
3028
* @throws ScriptException
3129
*/
3230
ScriptEngine initializeEngine(Map<String, Object> names) throws ScriptException;
33-
34-
/** Language implementation for javascript using the nashorn engine. */
35-
public static Language javascript() {
36-
return map -> {
37-
ScriptEngine jsEngine = new ScriptEngineManager().getEngineByName("nashorn");
38-
ScriptContext context = jsEngine.getContext();
39-
40-
String mapName = "nashornScriptBoxMap";
41-
context.setAttribute(mapName, map, ScriptContext.ENGINE_SCOPE);
42-
43-
StringBuilder builder = new StringBuilder();
44-
map.entrySet().forEach(entry -> {
45-
builder.append("var ");
46-
builder.append(entry.getKey());
47-
builder.append("=");
48-
builder.append(mapName);
49-
builder.append(".");
50-
builder.append(entry.getKey());
51-
builder.append(";\n");
52-
});
53-
builder.append("delete " + mapName + ";\n");
54-
jsEngine.eval(builder.toString());
55-
return jsEngine;
56-
};
57-
}
5831
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2015 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.jscriptbox.javascript;
17+
18+
import java.util.Arrays;
19+
import java.util.HashSet;
20+
import java.util.Set;
21+
22+
import javax.script.ScriptContext;
23+
import javax.script.ScriptEngine;
24+
import javax.script.ScriptEngineManager;
25+
26+
import com.diffplug.jscriptbox.Language;
27+
28+
public class Nashorn {
29+
/** Language implementation for javascript using the nashorn engine. */
30+
public static Language language() {
31+
return map -> {
32+
ScriptEngine jsEngine = new ScriptEngineManager().getEngineByName("nashorn");
33+
ScriptContext context = jsEngine.getContext();
34+
35+
String mapName = "nashornScriptBoxMap";
36+
context.setAttribute(mapName, map, ScriptContext.ENGINE_SCOPE);
37+
38+
StringBuilder builder = new StringBuilder();
39+
map.entrySet().forEach(entry -> {
40+
builder.append("var ");
41+
builder.append(normalize(entry.getKey()));
42+
builder.append("=");
43+
builder.append(mapName);
44+
builder.append(".get('");
45+
builder.append(entry.getKey());
46+
builder.append("');\n");
47+
});
48+
builder.append("delete " + mapName + ";\n");
49+
jsEngine.eval(builder.toString());
50+
return jsEngine;
51+
};
52+
}
53+
54+
private static String normalize(String input) {
55+
if (restrictedWords.contains(input)) {
56+
return "_" + input;
57+
} else {
58+
return input;
59+
}
60+
}
61+
62+
private static final Set<String> restrictedWords = new HashSet<>(Arrays.asList(
63+
// JavaScript Reserved Words
64+
"abstract", "arguments", "boolean", "break", "byte",
65+
"case", "catch", "char", "class", "const",
66+
"continue", "debugger", "default", "delete", "do",
67+
"double", "else", "enum", "eval", "export",
68+
"extends", "false", "final", "finally", "float",
69+
"for", "function", "goto", "if", "implements",
70+
"import", "in", "instanceof", "int", "interface",
71+
"let", "long", "native", "new", "null",
72+
"package", "private", "protected", "public", "return",
73+
"short", "static", "super", "switch", "synchronized",
74+
"this", "throw", "throws", "transient", "true",
75+
"try", "typeof", "var", "void", "volatile",
76+
"while", "with", "yield",
77+
// JavaScript Objects, Properties, and Methods
78+
"Array", "Date", "eval", "function", "hasOwnProperty",
79+
"Infinity", "isFinite", "isNaN", "isPrototypeOf", "length",
80+
"Math", "NaN", "name", "Number", "Object",
81+
"prototype", "String", "toString", "undefined", "valueOf",
82+
// Java Reserved Words
83+
"getClass", "java", "JavaArray", "javaClass", "JavaObject", "JavaPackage"));
84+
}

src/test/java/com/diffplug/scriptbox/JScriptBoxNashornTest.java renamed to src/test/java/com/diffplug/scriptbox/javascript/JScriptBoxNashornTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.diffplug.scriptbox;
16+
package com.diffplug.scriptbox.javascript;
1717

1818
import java.util.concurrent.atomic.AtomicBoolean;
1919
import java.util.concurrent.atomic.AtomicReference;
@@ -25,20 +25,20 @@
2525
import org.junit.Test;
2626

2727
import com.diffplug.jscriptbox.JScriptBox;
28-
import com.diffplug.jscriptbox.Language;
28+
import com.diffplug.jscriptbox.javascript.Nashorn;
2929

3030
public class JScriptBoxNashornTest {
3131
@Test
3232
public void testBasicExpressions() throws ScriptException {
33-
ScriptEngine engine = JScriptBox.create().build(Language.javascript());
33+
ScriptEngine engine = JScriptBox.create().build(Nashorn.language());
3434
Assert.assertEquals("abc", engine.eval("'abc'"));
3535
Assert.assertEquals(123, engine.eval("123"));
3636
Assert.assertEquals(123.5, engine.eval("123.5"));
3737
}
3838

3939
@Test
4040
public void testBasicScript() throws ScriptException {
41-
ScriptEngine engine = JScriptBox.create().build(Language.javascript());
41+
ScriptEngine engine = JScriptBox.create().build(Nashorn.language());
4242
engine.eval("var txt = 'abc';" +
4343
"var int = 123;" +
4444
"var float = 123.5;");
@@ -55,7 +55,7 @@ public void testVoid0() throws ScriptException {
5555
AtomicBoolean wasRun = new AtomicBoolean(false);
5656
ScriptEngine engine = JScriptBox.create()
5757
.set("void0").toVoid0(() -> wasRun.set(true))
58-
.build(Language.javascript());
58+
.build(Nashorn.language());
5959
engine.eval("void0()");
6060
Assert.assertEquals(true, wasRun.get());
6161
}
@@ -65,7 +65,7 @@ public void testVoid1() throws ScriptException {
6565
AtomicReference<String> arg1 = new AtomicReference<>();
6666
ScriptEngine engine = JScriptBox.create()
6767
.set("void1").toVoid1(arg1::set)
68-
.build(Language.javascript());
68+
.build(Nashorn.language());
6969
engine.eval("void1('it lives!')");
7070
Assert.assertEquals("it lives!", arg1.get());
7171
}
@@ -79,7 +79,7 @@ public void testVoid2() throws ScriptException {
7979
arg1.set(a);
8080
arg2.set(b);
8181
})
82-
.build(Language.javascript());
82+
.build(Nashorn.language());
8383
engine.eval("void2('a', 'b')");
8484
Assert.assertEquals("a", arg1.get());
8585
Assert.assertEquals("b", arg2.get());
@@ -96,7 +96,7 @@ public void testVoid3() throws ScriptException {
9696
arg2.set(b);
9797
arg3.set(c);
9898
})
99-
.build(Language.javascript());
99+
.build(Nashorn.language());
100100
engine.eval("void3('a', 'b', 'c')");
101101
Assert.assertEquals("a", arg1.get());
102102
Assert.assertEquals("b", arg2.get());
@@ -116,7 +116,7 @@ public void testVoid4() throws ScriptException {
116116
arg3.set(c);
117117
arg4.set(d);
118118
})
119-
.build(Language.javascript());
119+
.build(Nashorn.language());
120120
engine.eval("void4('a', 'b', 'c', 'd')");
121121
Assert.assertEquals("a", arg1.get());
122122
Assert.assertEquals("b", arg2.get());
@@ -131,15 +131,15 @@ public void testVoid4() throws ScriptException {
131131
public void testFunc0() throws ScriptException {
132132
ScriptEngine engine = JScriptBox.create()
133133
.set("func0").toFunc0(() -> "wassup")
134-
.build(Language.javascript());
134+
.build(Nashorn.language());
135135
Assert.assertEquals("wassup", engine.eval("func0()"));
136136
}
137137

138138
@Test
139139
public void testFunc1() throws ScriptException {
140140
ScriptEngine engine = JScriptBox.create()
141141
.set("func1").toFunc1(a -> a)
142-
.build(Language.javascript());
142+
.build(Nashorn.language());
143143
Assert.assertEquals("identity", engine.eval("func1('identity')"));
144144
Assert.assertEquals(4, engine.eval("func1(4)"));
145145
Assert.assertEquals(4.5, engine.eval("func1(4.5)"));
@@ -149,23 +149,23 @@ public void testFunc1() throws ScriptException {
149149
public void testFunc2() throws ScriptException {
150150
ScriptEngine engine = JScriptBox.create()
151151
.set("func2").toFunc2((String a, String b) -> a + b)
152-
.build(Language.javascript());
152+
.build(Nashorn.language());
153153
Assert.assertEquals("ab", engine.eval("func2('a', 'b')"));
154154
}
155155

156156
@Test
157157
public void testFunc3() throws ScriptException {
158158
ScriptEngine engine = JScriptBox.create()
159159
.set("func3").toFunc3((String a, String b, String c) -> a + b + c)
160-
.build(Language.javascript());
160+
.build(Nashorn.language());
161161
Assert.assertEquals("abc", engine.eval("func3('a', 'b', 'c')"));
162162
}
163163

164164
@Test
165165
public void testFunc4() throws ScriptException {
166166
ScriptEngine engine = JScriptBox.create()
167167
.set("func4").toFunc4((String a, String b, String c, String d) -> a + b + c + d)
168-
.build(Language.javascript());
168+
.build(Nashorn.language());
169169
Assert.assertEquals("abcd", engine.eval("func4('a', 'b', 'c', 'd')"));
170170
}
171171
}

0 commit comments

Comments
 (0)