| 1 |
From f195514ffee1b759ba088883732e77b026b3a694 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Gregory Brail <[email protected]> |
| 3 |
Date: Fri, 5 Jun 2020 14:38:28 -0700 |
| 4 |
Subject: [PATCH] Implement standard Java ScriptEngine |
| 5 |
|
| 6 |
This is not based on the now-removed JDK code but instead does a |
| 7 |
few things in a more modern way. See the comments for supported |
| 8 |
parameters (you can set language and optimization level via |
| 9 |
properties) and built-in functions (only print is supported right now.) |
| 10 |
|
| 11 |
This is built into a separate JAR called "rhino-engine" because |
| 12 |
otherwise, including it in any Java 8 JDK would break scripts that |
| 13 |
are expecting to see Nashorn instead. |
| 14 |
--- |
| 15 |
.circleci/config.yml | 2 +- |
| 16 |
build.gradle | 86 ++++- |
| 17 |
.../services/javax.script.ScriptEngineFactory | 1 + |
| 18 |
.../javascript/engine/BindingsObject.java | 60 +++ |
| 19 |
.../mozilla/javascript/engine/Builtins.java | 59 +++ |
| 20 |
.../engine/RhinoCompiledScript.java | 33 ++ |
| 21 |
.../engine/RhinoInvocationHandler.java | 25 ++ |
| 22 |
.../javascript/engine/RhinoScriptEngine.java | 363 ++++++++++++++++++ |
| 23 |
.../engine/RhinoScriptEngineFactory.java | 140 +++++++ |
| 24 |
.../tests/scriptengine/BuiltinsTest.java | 54 +++ |
| 25 |
.../tests/scriptengine/FactoryTest.java | 56 +++ |
| 26 |
.../tests/scriptengine/InvocableTest.java | 158 ++++++++ |
| 27 |
.../tests/scriptengine/ScriptEngineTest.java | 276 +++++++++++++ |
| 28 |
13 files changed, 1305 insertions(+), 8 deletions(-) |
| 29 |
create mode 100644 src/META-INF/services/javax.script.ScriptEngineFactory |
| 30 |
create mode 100644 src/org/mozilla/javascript/engine/BindingsObject.java |
| 31 |
create mode 100644 src/org/mozilla/javascript/engine/Builtins.java |
| 32 |
create mode 100644 src/org/mozilla/javascript/engine/RhinoCompiledScript.java |
| 33 |
create mode 100644 src/org/mozilla/javascript/engine/RhinoInvocationHandler.java |
| 34 |
create mode 100644 src/org/mozilla/javascript/engine/RhinoScriptEngine.java |
| 35 |
create mode 100644 src/org/mozilla/javascript/engine/RhinoScriptEngineFactory.java |
| 36 |
create mode 100644 testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java |
| 37 |
create mode 100644 testsrc/org/mozilla/javascript/tests/scriptengine/FactoryTest.java |
| 38 |
create mode 100644 testsrc/org/mozilla/javascript/tests/scriptengine/InvocableTest.java |
| 39 |
create mode 100644 testsrc/org/mozilla/javascript/tests/scriptengine/ScriptEngineTest.java |
| 40 |
|
| 41 |
--- /dev/null |
| 42 |
+++ b/src/META-INF/services/javax.script.ScriptEngineFactory |
| 43 |
@@ -0,0 +1 @@ |
| 44 |
+org.mozilla.javascript.engine.RhinoScriptEngineFactory |
| 45 |
--- /dev/null |
| 46 |
+++ b/src/org/mozilla/javascript/engine/BindingsObject.java |
| 47 |
@@ -0,0 +1,60 @@ |
| 48 |
+/* This Source Code Form is subject to the terms of the Mozilla Public |
| 49 |
+ * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 50 |
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 51 |
+ |
| 52 |
+package org.mozilla.javascript.engine; |
| 53 |
+ |
| 54 |
+import org.mozilla.javascript.Context; |
| 55 |
+import org.mozilla.javascript.Scriptable; |
| 56 |
+import org.mozilla.javascript.ScriptableObject; |
| 57 |
+import javax.script.Bindings; |
| 58 |
+ |
| 59 |
+/** |
| 60 |
+ * This class makes the Bindings object into a Scriptable. That way, we can query and modify |
| 61 |
+ * the contents of the Bindings on demand. |
| 62 |
+ */ |
| 63 |
+public class BindingsObject |
| 64 |
+ extends ScriptableObject { |
| 65 |
+ private final Bindings bindings; |
| 66 |
+ |
| 67 |
+ BindingsObject(Bindings bindings) { |
| 68 |
+ if (bindings == null) { |
| 69 |
+ throw new IllegalArgumentException("Bindings must not be null"); |
| 70 |
+ } |
| 71 |
+ this.bindings = bindings; |
| 72 |
+ } |
| 73 |
+ |
| 74 |
+ @Override |
| 75 |
+ public String getClassName() { |
| 76 |
+ return "BindingsObject"; |
| 77 |
+ } |
| 78 |
+ |
| 79 |
+ @Override |
| 80 |
+ public Object get(String name, Scriptable start) { |
| 81 |
+ Object ret = bindings.get(name); |
| 82 |
+ if (ret == null) { |
| 83 |
+ return Scriptable.NOT_FOUND; |
| 84 |
+ } |
| 85 |
+ return Context.jsToJava(ret, Object.class); |
| 86 |
+ } |
| 87 |
+ |
| 88 |
+ @Override |
| 89 |
+ public void put(String name, Scriptable start, Object value) { |
| 90 |
+ bindings.put(name, Context.javaToJS(value, start)); |
| 91 |
+ } |
| 92 |
+ |
| 93 |
+ @Override |
| 94 |
+ public void delete(String name) { |
| 95 |
+ bindings.remove(name); |
| 96 |
+ } |
| 97 |
+ |
| 98 |
+ @Override |
| 99 |
+ public boolean has(String name, Scriptable start) { |
| 100 |
+ return bindings.containsKey(name); |
| 101 |
+ } |
| 102 |
+ |
| 103 |
+ @Override |
| 104 |
+ public Object[] getIds() { |
| 105 |
+ return bindings.keySet().toArray(); |
| 106 |
+ } |
| 107 |
+} |
| 108 |
--- /dev/null |
| 109 |
+++ b/src/org/mozilla/javascript/engine/Builtins.java |
| 110 |
@@ -0,0 +1,59 @@ |
| 111 |
+/* This Source Code Form is subject to the terms of the Mozilla Public |
| 112 |
+ * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 113 |
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 114 |
+ |
| 115 |
+package org.mozilla.javascript.engine; |
| 116 |
+ |
| 117 |
+import java.io.IOException; |
| 118 |
+import java.io.OutputStreamWriter; |
| 119 |
+import java.io.Writer; |
| 120 |
+import javax.script.ScriptContext; |
| 121 |
+import org.mozilla.javascript.Context; |
| 122 |
+import org.mozilla.javascript.Function; |
| 123 |
+import org.mozilla.javascript.ScriptRuntime; |
| 124 |
+import org.mozilla.javascript.Scriptable; |
| 125 |
+import org.mozilla.javascript.ScriptableObject; |
| 126 |
+ |
| 127 |
+/** |
| 128 |
+ * <p> |
| 129 |
+ * This class defines the following built-in functions for the RhinoScriptEngine. |
| 130 |
+ * </p> |
| 131 |
+ * <ul> |
| 132 |
+ * <li>print(arg, arg, ...): Write each argument, concatenated to the ScriptEngine's |
| 133 |
+ * "standard output" as a string.</li> |
| 134 |
+ * </ul> |
| 135 |
+ */ |
| 136 |
+public class Builtins { |
| 137 |
+ |
| 138 |
+ static final Object BUILTIN_KEY = new Object(); |
| 139 |
+ |
| 140 |
+ private Writer stdout; |
| 141 |
+ |
| 142 |
+ void register(Context cx, ScriptableObject scope, ScriptContext sc) { |
| 143 |
+ if (sc.getWriter() == null) { |
| 144 |
+ stdout = new OutputStreamWriter(System.out); |
| 145 |
+ } else { |
| 146 |
+ stdout = sc.getWriter(); |
| 147 |
+ } |
| 148 |
+ |
| 149 |
+ scope.defineFunctionProperties(new String[]{"print"}, |
| 150 |
+ Builtins.class, |
| 151 |
+ ScriptableObject.PERMANENT | ScriptableObject.DONTENUM); |
| 152 |
+ } |
| 153 |
+ |
| 154 |
+ public static void print(Context cx, Scriptable thisObj, Object[] args, Function f) |
| 155 |
+ throws IOException { |
| 156 |
+ Builtins self = getSelf(thisObj); |
| 157 |
+ for (Object arg : args) { |
| 158 |
+ self.stdout.write(ScriptRuntime.toString(arg)); |
| 159 |
+ } |
| 160 |
+ self.stdout.write('\n'); |
| 161 |
+ } |
| 162 |
+ |
| 163 |
+ private static Builtins getSelf(Scriptable scope) { |
| 164 |
+ // Since this class is invoked as a set of anonymous functions, "this" |
| 165 |
+ // in JavaScript does not point to "this" in Java. We set a key on the |
| 166 |
+ // top-level scope to address this. |
| 167 |
+ return (Builtins) ScriptableObject.getTopScopeValue(scope, BUILTIN_KEY); |
| 168 |
+ } |
| 169 |
+} |
| 170 |
--- /dev/null |
| 171 |
+++ b/src/org/mozilla/javascript/engine/RhinoCompiledScript.java |
| 172 |
@@ -0,0 +1,33 @@ |
| 173 |
+/* This Source Code Form is subject to the terms of the Mozilla Public |
| 174 |
+ * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 175 |
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 176 |
+ |
| 177 |
+package org.mozilla.javascript.engine; |
| 178 |
+ |
| 179 |
+import javax.script.CompiledScript; |
| 180 |
+import javax.script.ScriptContext; |
| 181 |
+import javax.script.ScriptEngine; |
| 182 |
+import javax.script.ScriptException; |
| 183 |
+import org.mozilla.javascript.Script; |
| 184 |
+ |
| 185 |
+public class RhinoCompiledScript |
| 186 |
+ extends CompiledScript { |
| 187 |
+ |
| 188 |
+ private final RhinoScriptEngine engine; |
| 189 |
+ private final Script script; |
| 190 |
+ |
| 191 |
+ RhinoCompiledScript(RhinoScriptEngine engine, Script script) { |
| 192 |
+ this.engine = engine; |
| 193 |
+ this.script = script; |
| 194 |
+ } |
| 195 |
+ |
| 196 |
+ @Override |
| 197 |
+ public Object eval(ScriptContext context) throws ScriptException { |
| 198 |
+ return engine.eval(script, context); |
| 199 |
+ } |
| 200 |
+ |
| 201 |
+ @Override |
| 202 |
+ public ScriptEngine getEngine() { |
| 203 |
+ return engine; |
| 204 |
+ } |
| 205 |
+} |
| 206 |
--- /dev/null |
| 207 |
+++ b/src/org/mozilla/javascript/engine/RhinoInvocationHandler.java |
| 208 |
@@ -0,0 +1,25 @@ |
| 209 |
+/* This Source Code Form is subject to the terms of the Mozilla Public |
| 210 |
+ * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 211 |
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 212 |
+ |
| 213 |
+package org.mozilla.javascript.engine; |
| 214 |
+ |
| 215 |
+import java.lang.reflect.InvocationHandler; |
| 216 |
+import java.lang.reflect.Method; |
| 217 |
+ |
| 218 |
+public class RhinoInvocationHandler |
| 219 |
+ implements InvocationHandler { |
| 220 |
+ |
| 221 |
+ private final Object thiz; |
| 222 |
+ private final RhinoScriptEngine engine; |
| 223 |
+ |
| 224 |
+ RhinoInvocationHandler(RhinoScriptEngine engine, Object thiz) { |
| 225 |
+ this.engine = engine; |
| 226 |
+ this.thiz = thiz; |
| 227 |
+ } |
| 228 |
+ |
| 229 |
+ @Override |
| 230 |
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
| 231 |
+ return engine.invokeMethodRaw(thiz, method.getName(), method.getReturnType(), args); |
| 232 |
+ } |
| 233 |
+} |
| 234 |
--- /dev/null |
| 235 |
+++ b/src/org/mozilla/javascript/engine/RhinoScriptEngine.java |
| 236 |
@@ -0,0 +1,355 @@ |
| 237 |
+/* This Source Code Form is subject to the terms of the Mozilla Public |
| 238 |
+ * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 239 |
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 240 |
+ |
| 241 |
+package org.mozilla.javascript.engine; |
| 242 |
+ |
| 243 |
+import java.io.IOException; |
| 244 |
+import java.io.Reader; |
| 245 |
+import java.lang.reflect.Method; |
| 246 |
+import java.lang.reflect.Proxy; |
| 247 |
+import javax.script.AbstractScriptEngine; |
| 248 |
+import javax.script.Bindings; |
| 249 |
+import javax.script.Compilable; |
| 250 |
+import javax.script.CompiledScript; |
| 251 |
+import javax.script.Invocable; |
| 252 |
+import javax.script.ScriptContext; |
| 253 |
+import javax.script.ScriptEngine; |
| 254 |
+import javax.script.ScriptEngineFactory; |
| 255 |
+import javax.script.ScriptException; |
| 256 |
+import javax.script.SimpleBindings; |
| 257 |
+import org.mozilla.javascript.Callable; |
| 258 |
+import org.mozilla.javascript.Context; |
| 259 |
+import org.mozilla.javascript.ContextFactory; |
| 260 |
+import org.mozilla.javascript.RhinoException; |
| 261 |
+import org.mozilla.javascript.Script; |
| 262 |
+import org.mozilla.javascript.Scriptable; |
| 263 |
+import org.mozilla.javascript.ScriptableObject; |
| 264 |
+ |
| 265 |
+/** |
| 266 |
+ * <p> |
| 267 |
+ * This is the implementation of the standard ScriptEngine interface for Rhino. |
| 268 |
+ * </p> |
| 269 |
+ * <p> |
| 270 |
+ * An instance of the Rhino ScriptEngine is fully self-contained. Bindings at the GLOBAL_SCOPE may |
| 271 |
+ * be set, but there is nothing special about them -- if both global and ENGINE_SCOPE bindings are |
| 272 |
+ * set then the "engine" bindings override the global ones. |
| 273 |
+ * </p> |
| 274 |
+ * <p> |
| 275 |
+ * The Rhino engine is not thread safe. Rhino does no synchronization of ScriptEngine instances and |
| 276 |
+ * no synchronization of Bindings instances. It is up to the caller to ensure that the ScriptEngine |
| 277 |
+ * and all its Bindings are used by a single thread at a time. |
| 278 |
+ * </p> |
| 279 |
+ * <p> |
| 280 |
+ * The Rhino script engine includes some top-level built-in functions. See the Builtins class for |
| 281 |
+ * more documentation. |
| 282 |
+ * </p> |
| 283 |
+ * <p> |
| 284 |
+ * The engine supports a few configuration parameters that may be set at the "engine scope". Both |
| 285 |
+ * are numbers that may be set to a String or Number object. |
| 286 |
+ * </p> |
| 287 |
+ * <ul> |
| 288 |
+ * <li>javax.script.language_version: The version of the JavaScript language supported, |
| 289 |
+ * which is an integer defined in the Context class. The default is the latest "ES6" |
| 290 |
+ * version, defined as 200.</li> |
| 291 |
+ * <li>org.mozilla.javascript.optimization_level: The level of optimization Rhino performs |
| 292 |
+ * on the generated bytecode. Default is 9, which is the most. Set to -1 to use interpreted |
| 293 |
+ * mode.</li> |
| 294 |
+ * </ul> |
| 295 |
+ */ |
| 296 |
+public class RhinoScriptEngine |
| 297 |
+ extends AbstractScriptEngine |
| 298 |
+ implements Compilable, Invocable { |
| 299 |
+ |
| 300 |
+ /** |
| 301 |
+ * Reserved key for the Rhino optimization level. Default is "9," for optimized and compiled code. |
| 302 |
+ * Set this to "-1" to run Rhino in interpreted mode -- this is much much slower but the only |
| 303 |
+ * option on platforms like Android that don't support class files. |
| 304 |
+ */ |
| 305 |
+ public static final String OPTIMIZATION_LEVEL = "org.mozilla.javascript.optimization_level"; |
| 306 |
+ |
| 307 |
+ static final int DEFAULT_LANGUAGE_VERSION = Context.VERSION_ES6; |
| 308 |
+ private static final int DEFAULT_OPT = 9; |
| 309 |
+ private static final boolean DEFAULT_DEBUG = true; |
| 310 |
+ private static final String DEFAULT_FILENAME = "eval"; |
| 311 |
+ |
| 312 |
+ private static final CtxFactory ctxFactory = new CtxFactory(); |
| 313 |
+ |
| 314 |
+ private final RhinoScriptEngineFactory factory; |
| 315 |
+ private final Builtins builtins; |
| 316 |
+ private ScriptableObject topLevelScope = null; |
| 317 |
+ |
| 318 |
+ RhinoScriptEngine(RhinoScriptEngineFactory factory) { |
| 319 |
+ this.factory = factory; |
| 320 |
+ this.builtins = new Builtins(); |
| 321 |
+ } |
| 322 |
+ |
| 323 |
+ private Scriptable initScope(Context cx, ScriptContext sc) throws ScriptException { |
| 324 |
+ configureContext(cx); |
| 325 |
+ |
| 326 |
+ if (topLevelScope == null) { |
| 327 |
+ topLevelScope = cx.initStandardObjects(); |
| 328 |
+ // We need to stash this away so that the built in functions can find |
| 329 |
+ // this engine's specific stuff that they need to work. |
| 330 |
+ topLevelScope.associateValue(Builtins.BUILTIN_KEY, builtins); |
| 331 |
+ builtins.register(cx, topLevelScope, sc); |
| 332 |
+ } |
| 333 |
+ |
| 334 |
+ Scriptable engineScope = new BindingsObject( |
| 335 |
+ sc.getBindings(ScriptContext.ENGINE_SCOPE)); |
| 336 |
+ engineScope.setParentScope(null); |
| 337 |
+ engineScope.setPrototype(topLevelScope); |
| 338 |
+ |
| 339 |
+ if (sc.getBindings(ScriptContext.GLOBAL_SCOPE) != null) { |
| 340 |
+ Scriptable globalScope = new BindingsObject( |
| 341 |
+ sc.getBindings(ScriptContext.GLOBAL_SCOPE)); |
| 342 |
+ globalScope.setParentScope(null); |
| 343 |
+ globalScope.setPrototype(topLevelScope); |
| 344 |
+ engineScope.setPrototype(globalScope); |
| 345 |
+ } |
| 346 |
+ |
| 347 |
+ return engineScope; |
| 348 |
+ } |
| 349 |
+ |
| 350 |
+ @Override |
| 351 |
+ public Object eval(String script, ScriptContext context) throws ScriptException { |
| 352 |
+ Context cx = ctxFactory.enterContext(); |
| 353 |
+ try { |
| 354 |
+ Scriptable scope = initScope(cx, context); |
| 355 |
+ Object ret = cx.evaluateString(scope, script, getFilename(), 0, null); |
| 356 |
+ return Context.jsToJava(ret, Object.class); |
| 357 |
+ } catch (RhinoException re) { |
| 358 |
+ throw new ScriptException(re.getMessage(), re.sourceName(), re.lineNumber(), |
| 359 |
+ re.columnNumber()); |
| 360 |
+ } finally { |
| 361 |
+ Context.exit(); |
| 362 |
+ } |
| 363 |
+ } |
| 364 |
+ |
| 365 |
+ @Override |
| 366 |
+ public Object eval(Reader reader, ScriptContext context) throws ScriptException { |
| 367 |
+ Context cx = ctxFactory.enterContext(); |
| 368 |
+ try { |
| 369 |
+ Scriptable scope = initScope(cx, context); |
| 370 |
+ Object ret = cx.evaluateReader(scope, reader, getFilename(), 0, null); |
| 371 |
+ return Context.jsToJava(ret, Object.class); |
| 372 |
+ } catch (RhinoException re) { |
| 373 |
+ throw new ScriptException(re.getMessage(), re.sourceName(), re.lineNumber(), |
| 374 |
+ re.columnNumber()); |
| 375 |
+ } catch (IOException ioe) { |
| 376 |
+ throw new ScriptException(ioe); |
| 377 |
+ } finally { |
| 378 |
+ Context.exit(); |
| 379 |
+ } |
| 380 |
+ } |
| 381 |
+ |
| 382 |
+ @Override |
| 383 |
+ public CompiledScript compile(String script) throws ScriptException { |
| 384 |
+ Context cx = ctxFactory.enterContext(); |
| 385 |
+ try { |
| 386 |
+ configureContext(cx); |
| 387 |
+ Script s = |
| 388 |
+ cx.compileString(script, getFilename(), 1, null); |
| 389 |
+ return new RhinoCompiledScript(this, s); |
| 390 |
+ } catch (RhinoException re) { |
| 391 |
+ throw new ScriptException(re.getMessage(), re.sourceName(), re.lineNumber(), |
| 392 |
+ re.columnNumber()); |
| 393 |
+ } finally { |
| 394 |
+ Context.exit(); |
| 395 |
+ } |
| 396 |
+ } |
| 397 |
+ |
| 398 |
+ @Override |
| 399 |
+ public CompiledScript compile(Reader script) throws ScriptException { |
| 400 |
+ Context cx = ctxFactory.enterContext(); |
| 401 |
+ try { |
| 402 |
+ configureContext(cx); |
| 403 |
+ Script s = |
| 404 |
+ cx.compileReader(script, getFilename(), 1, null); |
| 405 |
+ return new RhinoCompiledScript(this, s); |
| 406 |
+ } catch (RhinoException re) { |
| 407 |
+ throw new ScriptException(re.getMessage(), re.sourceName(), re.lineNumber(), |
| 408 |
+ re.columnNumber()); |
| 409 |
+ } catch (IOException ioe) { |
| 410 |
+ throw new ScriptException(ioe); |
| 411 |
+ } finally { |
| 412 |
+ Context.exit(); |
| 413 |
+ } |
| 414 |
+ } |
| 415 |
+ |
| 416 |
+ Object eval(Script script, ScriptContext sc) throws ScriptException { |
| 417 |
+ Context cx = ctxFactory.enterContext(); |
| 418 |
+ try { |
| 419 |
+ Scriptable scope = initScope(cx, sc); |
| 420 |
+ Object ret = script.exec(cx, scope); |
| 421 |
+ return Context.jsToJava(ret, Object.class); |
| 422 |
+ } catch (RhinoException re) { |
| 423 |
+ throw new ScriptException(re.getMessage(), re.sourceName(), re.lineNumber(), |
| 424 |
+ re.columnNumber()); |
| 425 |
+ } finally { |
| 426 |
+ Context.exit(); |
| 427 |
+ } |
| 428 |
+ } |
| 429 |
+ |
| 430 |
+ @Override |
| 431 |
+ public Object invokeFunction(String name, Object... args) |
| 432 |
+ throws ScriptException, NoSuchMethodException { |
| 433 |
+ return invokeMethod(null, name, args); |
| 434 |
+ } |
| 435 |
+ |
| 436 |
+ @Override |
| 437 |
+ public Object invokeMethod(Object thiz, String name, Object... args) |
| 438 |
+ throws ScriptException, NoSuchMethodException { |
| 439 |
+ return invokeMethodRaw(thiz, name, Object.class, args); |
| 440 |
+ } |
| 441 |
+ |
| 442 |
+ Object invokeMethodRaw(Object thiz, String name, Class<?> returnType, Object... args) |
| 443 |
+ throws ScriptException, NoSuchMethodException { |
| 444 |
+ Context cx = ctxFactory.enterContext(); |
| 445 |
+ try { |
| 446 |
+ Scriptable scope = initScope(cx, context); |
| 447 |
+ |
| 448 |
+ Scriptable localThis; |
| 449 |
+ if (thiz == null) { |
| 450 |
+ localThis = scope; |
| 451 |
+ } else { |
| 452 |
+ localThis = Context.toObject(thiz, scope); |
| 453 |
+ } |
| 454 |
+ |
| 455 |
+ Object f = ScriptableObject.getProperty(localThis, name); |
| 456 |
+ if (f == Scriptable.NOT_FOUND) { |
| 457 |
+ throw new NoSuchMethodException(name); |
| 458 |
+ } |
| 459 |
+ if (!(f instanceof Callable)) { |
| 460 |
+ throw new ScriptException("\"" + name + "\" is not a function"); |
| 461 |
+ } |
| 462 |
+ Callable func = (Callable) f; |
| 463 |
+ |
| 464 |
+ if (args != null) { |
| 465 |
+ for (int i = 0; i < args.length; i++) { |
| 466 |
+ args[i] = Context.javaToJS(args[i], scope); |
| 467 |
+ } |
| 468 |
+ } |
| 469 |
+ |
| 470 |
+ Object ret = func.call(cx, scope, localThis, args); |
| 471 |
+ if (returnType == Void.TYPE) { |
| 472 |
+ return null; |
| 473 |
+ } |
| 474 |
+ return Context.jsToJava(ret, returnType); |
| 475 |
+ |
| 476 |
+ } catch (RhinoException re) { |
| 477 |
+ throw new ScriptException(re.getMessage(), re.sourceName(), re.lineNumber(), |
| 478 |
+ re.columnNumber()); |
| 479 |
+ } finally { |
| 480 |
+ Context.exit(); |
| 481 |
+ } |
| 482 |
+ } |
| 483 |
+ |
| 484 |
+ @Override |
| 485 |
+ public <T> T getInterface(Class<T> clasz) { |
| 486 |
+ if ((clasz == null) || !clasz.isInterface()) { |
| 487 |
+ throw new IllegalArgumentException("Not an interface"); |
| 488 |
+ } |
| 489 |
+ Context cx = ctxFactory.enterContext(); |
| 490 |
+ try { |
| 491 |
+ Scriptable scope = initScope(cx, context); |
| 492 |
+ if (methodsMissing(scope, clasz)) { |
| 493 |
+ return null; |
| 494 |
+ } |
| 495 |
+ } catch (ScriptException se) { |
| 496 |
+ return null; |
| 497 |
+ } finally { |
| 498 |
+ Context.exit(); |
| 499 |
+ } |
| 500 |
+ return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), |
| 501 |
+ new Class<?>[]{clasz}, new RhinoInvocationHandler(this, null)); |
| 502 |
+ } |
| 503 |
+ |
| 504 |
+ @Override |
| 505 |
+ public <T> T getInterface(Object thiz, Class<T> clasz) { |
| 506 |
+ if ((clasz == null) || !clasz.isInterface()) { |
| 507 |
+ throw new IllegalArgumentException("Not an interface"); |
| 508 |
+ } |
| 509 |
+ Context cx = ctxFactory.enterContext(); |
| 510 |
+ try { |
| 511 |
+ Scriptable scope = initScope(cx, context); |
| 512 |
+ Scriptable thisObj = Context.toObject(thiz, scope); |
| 513 |
+ if (methodsMissing(thisObj, clasz)) { |
| 514 |
+ return null; |
| 515 |
+ } |
| 516 |
+ } catch (ScriptException se) { |
| 517 |
+ return null; |
| 518 |
+ } finally { |
| 519 |
+ Context.exit(); |
| 520 |
+ } |
| 521 |
+ return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), |
| 522 |
+ new Class<?>[]{clasz}, new RhinoInvocationHandler(this, thiz)); |
| 523 |
+ } |
| 524 |
+ |
| 525 |
+ @Override |
| 526 |
+ public Bindings createBindings() { |
| 527 |
+ return new SimpleBindings(); |
| 528 |
+ } |
| 529 |
+ |
| 530 |
+ @Override |
| 531 |
+ public ScriptEngineFactory getFactory() { |
| 532 |
+ return factory; |
| 533 |
+ } |
| 534 |
+ |
| 535 |
+ private void configureContext(Context cx) throws ScriptException { |
| 536 |
+ Object lv = get(ScriptEngine.LANGUAGE_VERSION); |
| 537 |
+ if (lv != null) { |
| 538 |
+ cx.setLanguageVersion(parseInteger(lv)); |
| 539 |
+ } |
| 540 |
+ Object ol = get(OPTIMIZATION_LEVEL); |
| 541 |
+ if (ol != null) { |
| 542 |
+ cx.setOptimizationLevel(parseInteger(ol)); |
| 543 |
+ } |
| 544 |
+ } |
| 545 |
+ |
| 546 |
+ private int parseInteger(Object v) throws ScriptException { |
| 547 |
+ if (v instanceof String) { |
| 548 |
+ try { |
| 549 |
+ return Integer.parseInt((String) v); |
| 550 |
+ } catch (NumberFormatException nfe) { |
| 551 |
+ throw new ScriptException("Invalid number " + v); |
| 552 |
+ } |
| 553 |
+ } else if (v instanceof Integer) { |
| 554 |
+ return (Integer) v; |
| 555 |
+ } else { |
| 556 |
+ throw new ScriptException("Value must be a string or number"); |
| 557 |
+ } |
| 558 |
+ } |
| 559 |
+ |
| 560 |
+ private String getFilename() { |
| 561 |
+ Object fn = get(ScriptEngine.FILENAME); |
| 562 |
+ if (fn instanceof String) { |
| 563 |
+ return (String) fn; |
| 564 |
+ } |
| 565 |
+ return DEFAULT_FILENAME; |
| 566 |
+ } |
| 567 |
+ |
| 568 |
+ private boolean methodsMissing(Scriptable scope, Class<?> clasz) { |
| 569 |
+ for (Method m : clasz.getMethods()) { |
| 570 |
+ if (m.getDeclaringClass() == Object.class) { |
| 571 |
+ continue; |
| 572 |
+ } |
| 573 |
+ Object methodObj = ScriptableObject.getProperty(scope, m.getName()); |
| 574 |
+ if (!(methodObj instanceof Callable)) { |
| 575 |
+ return true; |
| 576 |
+ } |
| 577 |
+ } |
| 578 |
+ return false; |
| 579 |
+ } |
| 580 |
+ |
| 581 |
+ private static final class CtxFactory |
| 582 |
+ extends ContextFactory { |
| 583 |
+ |
| 584 |
+ @Override |
| 585 |
+ protected void onContextCreated(Context cx) { |
| 586 |
+ cx.setLanguageVersion(Context.VERSION_ES6); |
| 587 |
+ cx.setOptimizationLevel(DEFAULT_OPT); |
| 588 |
+ cx.setGeneratingDebug(DEFAULT_DEBUG); |
| 589 |
+ } |
| 590 |
+ } |
| 591 |
+} |
| 592 |
--- /dev/null |
| 593 |
+++ b/src/org/mozilla/javascript/engine/RhinoScriptEngineFactory.java |
| 594 |
@@ -0,0 +1,140 @@ |
| 595 |
+/* This Source Code Form is subject to the terms of the Mozilla Public |
| 596 |
+ * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 597 |
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 598 |
+ |
| 599 |
+package org.mozilla.javascript.engine; |
| 600 |
+ |
| 601 |
+import java.util.Arrays; |
| 602 |
+import java.util.Collections; |
| 603 |
+import java.util.List; |
| 604 |
+import javax.script.ScriptEngine; |
| 605 |
+import javax.script.ScriptEngineFactory; |
| 606 |
+import org.mozilla.javascript.Context; |
| 607 |
+ |
| 608 |
+/** |
| 609 |
+ * <p> |
| 610 |
+ * This is an implementation of the standard Java "ScriptEngine" for Rhino. If the Rhino engine |
| 611 |
+ * (typically in the form of the "rhino-engine" JAR) is in the classpath, then this script |
| 612 |
+ * engine will be activated. |
| 613 |
+ * </p> |
| 614 |
+ * <p> |
| 615 |
+ * See the list of constants in this class for the list of language names, file extensions, and |
| 616 |
+ * MIME types that this engine supports. This list is essentially the same as the list supported |
| 617 |
+ * in the Nashorn script engine that was included in Java 8. |
| 618 |
+ * </p> |
| 619 |
+ * <p> |
| 620 |
+ * Since this engine and Nashorn support the same language and file extensions, then unless |
| 621 |
+ * you are sure you are running in an environment that has Nashorn, the best way to get this |
| 622 |
+ * engine is to call ScriptEngine.getEngineByName("rhino") to ask for Rhino directly. |
| 623 |
+ * </p> |
| 624 |
+ */ |
| 625 |
+public class RhinoScriptEngineFactory |
| 626 |
+ implements ScriptEngineFactory { |
| 627 |
+ |
| 628 |
+ public static final String NAME = "rhino"; |
| 629 |
+ public static final String LANGUAGE = "javascript"; |
| 630 |
+ public static final List<String> NAMES = |
| 631 |
+ Arrays.asList("rhino", "Rhino", "javascript", "JavaScript"); |
| 632 |
+ public static final List<String> EXTENSIONS = |
| 633 |
+ Collections.singletonList("js"); |
| 634 |
+ public static final List<String> MIME_TYPES = |
| 635 |
+ Arrays.asList("application/javascript", "application/ecmascript", |
| 636 |
+ "text/javascript", "text/ecmascript"); |
| 637 |
+ public static final String LANGUAGE_VERSION = |
| 638 |
+ String.valueOf(RhinoScriptEngine.DEFAULT_LANGUAGE_VERSION); |
| 639 |
+ |
| 640 |
+ @Override |
| 641 |
+ public String getEngineName() { |
| 642 |
+ return NAME; |
| 643 |
+ } |
| 644 |
+ |
| 645 |
+ @Override |
| 646 |
+ public String getEngineVersion() { |
| 647 |
+ Context cx = Context.enter(); |
| 648 |
+ try { |
| 649 |
+ String v = cx.getImplementationVersion(); |
| 650 |
+ return (v == null ? "unknown" : v); |
| 651 |
+ } finally { |
| 652 |
+ Context.exit(); |
| 653 |
+ } |
| 654 |
+ } |
| 655 |
+ |
| 656 |
+ @Override |
| 657 |
+ public List<String> getExtensions() { |
| 658 |
+ return EXTENSIONS; |
| 659 |
+ } |
| 660 |
+ |
| 661 |
+ @Override |
| 662 |
+ public List<String> getMimeTypes() { |
| 663 |
+ return MIME_TYPES; |
| 664 |
+ } |
| 665 |
+ |
| 666 |
+ @Override |
| 667 |
+ public List<String> getNames() { |
| 668 |
+ return NAMES; |
| 669 |
+ } |
| 670 |
+ |
| 671 |
+ @Override |
| 672 |
+ public String getLanguageName() { |
| 673 |
+ return LANGUAGE; |
| 674 |
+ } |
| 675 |
+ |
| 676 |
+ @Override |
| 677 |
+ public String getLanguageVersion() { |
| 678 |
+ return LANGUAGE_VERSION; |
| 679 |
+ } |
| 680 |
+ |
| 681 |
+ @Override |
| 682 |
+ public Object getParameter(String key) { |
| 683 |
+ switch (key) { |
| 684 |
+ case ScriptEngine.ENGINE: |
| 685 |
+ return getEngineName(); |
| 686 |
+ case ScriptEngine.ENGINE_VERSION: |
| 687 |
+ return getEngineVersion(); |
| 688 |
+ case ScriptEngine.LANGUAGE: |
| 689 |
+ return getLanguageName(); |
| 690 |
+ case ScriptEngine.LANGUAGE_VERSION: |
| 691 |
+ return getLanguageVersion(); |
| 692 |
+ case ScriptEngine.NAME: |
| 693 |
+ return NAME; |
| 694 |
+ case "THREADING": |
| 695 |
+ // Engines are explicitly not thread-safe |
| 696 |
+ return null; |
| 697 |
+ default: |
| 698 |
+ return null; |
| 699 |
+ } |
| 700 |
+ } |
| 701 |
+ |
| 702 |
+ @Override |
| 703 |
+ public String getMethodCallSyntax(String obj, String m, String... args) { |
| 704 |
+ StringBuilder sb = new StringBuilder(); |
| 705 |
+ sb.append(obj).append('.').append(m).append('('); |
| 706 |
+ for (int i = 0; i < args.length; i++) { |
| 707 |
+ if (i > 0) { |
| 708 |
+ sb.append(','); |
| 709 |
+ } |
| 710 |
+ sb.append(args[i]); |
| 711 |
+ } |
| 712 |
+ sb.append(");"); |
| 713 |
+ return sb.toString(); |
| 714 |
+ } |
| 715 |
+ |
| 716 |
+ @Override |
| 717 |
+ public String getOutputStatement(String toDisplay) { |
| 718 |
+ return "print('" + toDisplay + "');"; |
| 719 |
+ } |
| 720 |
+ |
| 721 |
+ @Override |
| 722 |
+ public String getProgram(String... statements) { |
| 723 |
+ StringBuilder sb = new StringBuilder(); |
| 724 |
+ for (String stmt : statements) { |
| 725 |
+ sb.append(stmt).append(";\n"); |
| 726 |
+ } |
| 727 |
+ return sb.toString(); |
| 728 |
+ } |
| 729 |
+ |
| 730 |
+ @Override |
| 731 |
+ public ScriptEngine getScriptEngine() { |
| 732 |
+ return new RhinoScriptEngine(this); |
| 733 |
+ } |
| 734 |
+} |
| 735 |
--- /dev/null |
| 736 |
+++ b/testsrc/org/mozilla/javascript/tests/scriptengine/BuiltinsTest.java |
| 737 |
@@ -0,0 +1,54 @@ |
| 738 |
+package org.mozilla.javascript.tests.scriptengine; |
| 739 |
+ |
| 740 |
+import java.io.StringWriter; |
| 741 |
+import javax.script.ScriptContext; |
| 742 |
+import javax.script.ScriptEngine; |
| 743 |
+import javax.script.ScriptEngineManager; |
| 744 |
+import javax.script.ScriptException; |
| 745 |
+import javax.script.SimpleScriptContext; |
| 746 |
+import org.junit.Before; |
| 747 |
+import org.junit.BeforeClass; |
| 748 |
+import org.junit.Test; |
| 749 |
+import org.mozilla.javascript.engine.RhinoScriptEngineFactory; |
| 750 |
+ |
| 751 |
+import static org.junit.Assert.*; |
| 752 |
+ |
| 753 |
+public class BuiltinsTest { |
| 754 |
+ |
| 755 |
+ private static ScriptEngineManager manager; |
| 756 |
+ |
| 757 |
+ private ScriptEngine engine; |
| 758 |
+ |
| 759 |
+ @BeforeClass |
| 760 |
+ public static void init() { |
| 761 |
+ manager = new ScriptEngineManager(); |
| 762 |
+ manager.registerEngineName("rhino", new RhinoScriptEngineFactory()); |
| 763 |
+ } |
| 764 |
+ |
| 765 |
+ @Before |
| 766 |
+ public void setup() { |
| 767 |
+ engine = manager.getEngineByName("rhino"); |
| 768 |
+ } |
| 769 |
+ |
| 770 |
+ @Test |
| 771 |
+ public void testPrintStdout() throws ScriptException { |
| 772 |
+ engine.eval("print('Hello, World!');"); |
| 773 |
+ } |
| 774 |
+ |
| 775 |
+ @Test |
| 776 |
+ public void testPrintWriter() throws ScriptException { |
| 777 |
+ StringWriter sw = new StringWriter(); |
| 778 |
+ ScriptContext sc = new SimpleScriptContext(); |
| 779 |
+ sc.setWriter(sw); |
| 780 |
+ engine.eval("print('one', 2, true);", sc); |
| 781 |
+ assertEquals(sw.toString(), "one2true\n"); |
| 782 |
+ } |
| 783 |
+ |
| 784 |
+ @Test |
| 785 |
+ public void testPrintWriterGeneric() throws ScriptException { |
| 786 |
+ StringWriter sw = new StringWriter(); |
| 787 |
+ engine.getContext().setWriter(sw); |
| 788 |
+ engine.eval(engine.getFactory().getOutputStatement("Display This!")); |
| 789 |
+ assertEquals(sw.toString(), "Display This!\n"); |
| 790 |
+ } |
| 791 |
+} |
| 792 |
--- /dev/null |
| 793 |
+++ b/testsrc/org/mozilla/javascript/tests/scriptengine/FactoryTest.java |
| 794 |
@@ -0,0 +1,56 @@ |
| 795 |
+package org.mozilla.javascript.tests.scriptengine; |
| 796 |
+ |
| 797 |
+import javax.script.ScriptEngine; |
| 798 |
+import javax.script.ScriptEngineFactory; |
| 799 |
+import javax.script.ScriptEngineManager; |
| 800 |
+import org.junit.Test; |
| 801 |
+import org.mozilla.javascript.engine.RhinoScriptEngine; |
| 802 |
+import org.mozilla.javascript.engine.RhinoScriptEngineFactory; |
| 803 |
+ |
| 804 |
+import static org.junit.Assert.*; |
| 805 |
+ |
| 806 |
+/* |
| 807 |
+ * A series of tests that depend on us having our engine registered with the |
| 808 |
+ * ScriptEngineManager by default. |
| 809 |
+ */ |
| 810 |
+public class FactoryTest { |
| 811 |
+ |
| 812 |
+ @Test |
| 813 |
+ public void findRhinoFactory() { |
| 814 |
+ ScriptEngineManager manager = new ScriptEngineManager(); |
| 815 |
+ for (ScriptEngineFactory factory : manager.getEngineFactories()) { |
| 816 |
+ if (factory instanceof RhinoScriptEngineFactory) { |
| 817 |
+ assertEquals("rhino", factory.getEngineName()); |
| 818 |
+ assertEquals("rhino", factory.getParameter(ScriptEngine.ENGINE)); |
| 819 |
+ assertEquals("rhino", factory.getParameter(ScriptEngine.NAME)); |
| 820 |
+ // This could be "unknown" if we're not running from a regular JAR |
| 821 |
+ assertFalse(factory.getEngineVersion().isEmpty()); |
| 822 |
+ assertEquals("javascript", factory.getLanguageName()); |
| 823 |
+ assertEquals("javascript", factory.getParameter(ScriptEngine.LANGUAGE)); |
| 824 |
+ assertEquals("200", factory.getLanguageVersion()); |
| 825 |
+ assertEquals("200", factory.getParameter(ScriptEngine.LANGUAGE_VERSION)); |
| 826 |
+ assertNull(factory.getParameter("THREADING")); |
| 827 |
+ assertTrue(factory.getExtensions().contains("js")); |
| 828 |
+ assertTrue(factory.getMimeTypes().contains("application/javascript")); |
| 829 |
+ assertTrue(factory.getMimeTypes().contains("application/ecmascript")); |
| 830 |
+ assertTrue(factory.getMimeTypes().contains("text/javascript")); |
| 831 |
+ assertTrue(factory.getMimeTypes().contains("text/ecmascript")); |
| 832 |
+ assertTrue(factory.getNames().contains("rhino")); |
| 833 |
+ assertTrue(factory.getNames().contains("Rhino")); |
| 834 |
+ assertTrue(factory.getNames().contains("javascript")); |
| 835 |
+ assertTrue(factory.getNames().contains("JavaScript")); |
| 836 |
+ return; |
| 837 |
+ } |
| 838 |
+ } |
| 839 |
+ fail("Expected to find Rhino script engine"); |
| 840 |
+ } |
| 841 |
+ |
| 842 |
+ @Test |
| 843 |
+ public void testRhinoFactory() { |
| 844 |
+ // This will always uniquely return our engine. |
| 845 |
+ // In Java 8, other ways to find it may return Nashorn. |
| 846 |
+ ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino"); |
| 847 |
+ assertTrue(engine instanceof RhinoScriptEngine); |
| 848 |
+ |
| 849 |
+ } |
| 850 |
+} |
| 851 |
--- /dev/null |
| 852 |
+++ b/testsrc/org/mozilla/javascript/tests/scriptengine/InvocableTest.java |
| 853 |
@@ -0,0 +1,158 @@ |
| 854 |
+package org.mozilla.javascript.tests.scriptengine; |
| 855 |
+ |
| 856 |
+import java.io.FileNotFoundException; |
| 857 |
+import java.io.FileReader; |
| 858 |
+import javax.script.Invocable; |
| 859 |
+import javax.script.ScriptEngine; |
| 860 |
+import javax.script.ScriptEngineManager; |
| 861 |
+import javax.script.ScriptException; |
| 862 |
+import org.junit.Before; |
| 863 |
+import org.junit.BeforeClass; |
| 864 |
+import org.junit.Test; |
| 865 |
+import org.mozilla.javascript.engine.RhinoScriptEngineFactory; |
| 866 |
+ |
| 867 |
+import static org.junit.Assert.*; |
| 868 |
+ |
| 869 |
+public class InvocableTest { |
| 870 |
+ |
| 871 |
+ private static ScriptEngineManager manager; |
| 872 |
+ |
| 873 |
+ private ScriptEngine engine; |
| 874 |
+ private Invocable iEngine; |
| 875 |
+ |
| 876 |
+ @BeforeClass |
| 877 |
+ public static void init() { |
| 878 |
+ manager = new ScriptEngineManager(); |
| 879 |
+ manager.registerEngineName("rhino", new RhinoScriptEngineFactory()); |
| 880 |
+ } |
| 881 |
+ |
| 882 |
+ @Before |
| 883 |
+ public void setup() { |
| 884 |
+ engine = manager.getEngineByName("rhino"); |
| 885 |
+ iEngine = (Invocable) engine; |
| 886 |
+ } |
| 887 |
+ |
| 888 |
+ @Test |
| 889 |
+ public void invokeFunctionTest() throws ScriptException, NoSuchMethodException { |
| 890 |
+ engine.eval("function foo(a, b) { return a + b; }"); |
| 891 |
+ Object result = iEngine.invokeFunction("foo", 2, 2); |
| 892 |
+ assertEquals(result, 4L); |
| 893 |
+ } |
| 894 |
+ |
| 895 |
+ @Test |
| 896 |
+ public void invokeScriptFunctionTest() throws ScriptException, NoSuchMethodException { |
| 897 |
+ Object scriptObj = engine.eval("let o = {};\n" |
| 898 |
+ + "o.test = function(x) { return x + 2; }\n" |
| 899 |
+ + "o;"); |
| 900 |
+ assertEquals(4L, iEngine.invokeMethod(scriptObj, "test", 2)); |
| 901 |
+ } |
| 902 |
+ |
| 903 |
+ @Test |
| 904 |
+ public void invokeGenericFunctionTest() throws ScriptException, NoSuchMethodException { |
| 905 |
+ engine.eval("let o = {};\n" |
| 906 |
+ + "o.test = function(x) { return x + 2; }\n"); |
| 907 |
+ Object result = engine.eval(engine.getFactory().getMethodCallSyntax("o", "test", "1")); |
| 908 |
+ assertEquals(3L, result); |
| 909 |
+ } |
| 910 |
+ |
| 911 |
+ @Test |
| 912 |
+ public void invokeGenericFunctionTest2() throws ScriptException, NoSuchMethodException { |
| 913 |
+ engine.eval("let o = {};\n" |
| 914 |
+ + "o.test = function(x, y) { return x + y; }\n"); |
| 915 |
+ Object result = engine.eval(engine.getFactory().getMethodCallSyntax("o", "test", "1", "7")); |
| 916 |
+ assertEquals(8L, result); |
| 917 |
+ } |
| 918 |
+ |
| 919 |
+ @Test |
| 920 |
+ public void invokeMethodTest() |
| 921 |
+ throws ScriptException, NoSuchMethodException, FileNotFoundException { |
| 922 |
+ engine.eval(new FileReader("testsrc/assert.js")); |
| 923 |
+ engine.eval("function FooObj() { this.x = 0; }\n" |
| 924 |
+ + "FooObj.prototype.set = function(a, b) { this.x = a + b; }"); |
| 925 |
+ engine.eval("let f = new FooObj();\n" |
| 926 |
+ + "assertEquals(f.x, 0);\n" |
| 927 |
+ + "f.set(2, 2);\n" |
| 928 |
+ + "assertEquals(f.x, 4);"); |
| 929 |
+ |
| 930 |
+ Object fooObj = engine.eval("let y = new FooObj(); y"); |
| 931 |
+ assertNotNull(fooObj); |
| 932 |
+ iEngine.invokeMethod(fooObj, "set", 3, 3); |
| 933 |
+ Object result = engine.eval("y.x"); |
| 934 |
+ assertEquals(result, 6L); |
| 935 |
+ } |
| 936 |
+ |
| 937 |
+ @Test |
| 938 |
+ public void interfaceFunctionTest() |
| 939 |
+ throws ScriptException, FileNotFoundException { |
| 940 |
+ engine.eval(new FileReader("testsrc/assert.js")); |
| 941 |
+ engine.eval("var foo = 'initialized';\n" |
| 942 |
+ + "function setFoo(v) { foo = v; }\n" |
| 943 |
+ + "function getFoo() { return foo; }\n" |
| 944 |
+ + "function addItUp(a, b) { return a + b; }"); |
| 945 |
+ I tester = iEngine.getInterface(I.class); |
| 946 |
+ assertEquals(tester.getFoo(), "initialized"); |
| 947 |
+ tester.setFoo("tested"); |
| 948 |
+ assertEquals(tester.getFoo(), "tested"); |
| 949 |
+ assertEquals(tester.addItUp(100, 1), 101); |
| 950 |
+ } |
| 951 |
+ |
| 952 |
+ @Test |
| 953 |
+ public void interfaceMethodTest() |
| 954 |
+ throws ScriptException, FileNotFoundException { |
| 955 |
+ engine.eval(new FileReader("testsrc/assert.js")); |
| 956 |
+ Object foo = engine.eval("function Foo() { this.foo = 'initialized' }\n" |
| 957 |
+ + "Foo.prototype.setFoo = function(v) { this.foo = v; };\n" |
| 958 |
+ + "Foo.prototype.getFoo = function() { return this.foo; };\n" |
| 959 |
+ + "Foo.prototype.addItUp = function(a, b) { return a + b; };\n" |
| 960 |
+ + "new Foo();"); |
| 961 |
+ I tester = iEngine.getInterface(foo, I.class); |
| 962 |
+ assertEquals(tester.getFoo(), "initialized"); |
| 963 |
+ tester.setFoo("tested"); |
| 964 |
+ assertEquals(tester.getFoo(), "tested"); |
| 965 |
+ assertEquals(tester.addItUp(100, 1), 101); |
| 966 |
+ } |
| 967 |
+ |
| 968 |
+ @Test |
| 969 |
+ public void interfaceFunctionMissingTest() { |
| 970 |
+ I tester = iEngine.getInterface(I.class); |
| 971 |
+ assertNull(tester); |
| 972 |
+ } |
| 973 |
+ |
| 974 |
+ @Test |
| 975 |
+ public void interfaceMethodMissingTest() |
| 976 |
+ throws ScriptException { |
| 977 |
+ // Functions defined, but not on the right object |
| 978 |
+ Object foo = engine.eval("var foo = 'initialized';\n" |
| 979 |
+ + "function setFoo(v) { foo = v; }\n" |
| 980 |
+ + "function getFoo() { return foo; }\n" |
| 981 |
+ + "function addItUp(a, b) { return a + b; }\n" |
| 982 |
+ + "function Foo() {}\n" |
| 983 |
+ + "new Foo();"); |
| 984 |
+ I tester = iEngine.getInterface(foo, I.class); |
| 985 |
+ assertNull(tester); |
| 986 |
+ } |
| 987 |
+ |
| 988 |
+ @Test |
| 989 |
+ public void invokeNotFoundTest() { |
| 990 |
+ assertThrows(NoSuchMethodException.class, () -> { |
| 991 |
+ iEngine.invokeFunction("foo", 2, 2); |
| 992 |
+ }); |
| 993 |
+ } |
| 994 |
+ |
| 995 |
+ @Test |
| 996 |
+ public void invokeNotFunctionTest() { |
| 997 |
+ assertThrows(ScriptException.class, () -> { |
| 998 |
+ engine.eval("foo = 'bar';"); |
| 999 |
+ iEngine.invokeFunction("foo", 2, 2); |
| 1000 |
+ }); |
| 1001 |
+ } |
| 1002 |
+ |
| 1003 |
+ interface I { |
| 1004 |
+ |
| 1005 |
+ void setFoo(String v); |
| 1006 |
+ |
| 1007 |
+ String getFoo(); |
| 1008 |
+ |
| 1009 |
+ int addItUp(int a, int b); |
| 1010 |
+ } |
| 1011 |
+} |
| 1012 |
--- /dev/null |
| 1013 |
+++ b/testsrc/org/mozilla/javascript/tests/scriptengine/ScriptEngineTest.java |
| 1014 |
@@ -0,0 +1,276 @@ |
| 1015 |
+package org.mozilla.javascript.tests.scriptengine; |
| 1016 |
+ |
| 1017 |
+import java.io.File; |
| 1018 |
+import java.io.FileReader; |
| 1019 |
+import java.io.IOException; |
| 1020 |
+import java.io.StringReader; |
| 1021 |
+import javax.script.Bindings; |
| 1022 |
+import javax.script.Compilable; |
| 1023 |
+import javax.script.CompiledScript; |
| 1024 |
+import javax.script.ScriptContext; |
| 1025 |
+import javax.script.ScriptEngine; |
| 1026 |
+import javax.script.ScriptEngineFactory; |
| 1027 |
+import javax.script.ScriptEngineManager; |
| 1028 |
+import javax.script.ScriptException; |
| 1029 |
+import javax.script.SimpleBindings; |
| 1030 |
+import javax.script.SimpleScriptContext; |
| 1031 |
+import org.junit.Before; |
| 1032 |
+import org.junit.BeforeClass; |
| 1033 |
+import org.junit.Test; |
| 1034 |
+import org.mozilla.javascript.engine.RhinoScriptEngine; |
| 1035 |
+import org.mozilla.javascript.engine.RhinoScriptEngineFactory; |
| 1036 |
+ |
| 1037 |
+import static org.junit.Assert.*; |
| 1038 |
+ |
| 1039 |
+public class ScriptEngineTest { |
| 1040 |
+ |
| 1041 |
+ private static ScriptEngineManager manager; |
| 1042 |
+ private ScriptEngine engine; |
| 1043 |
+ private Compilable cEngine; |
| 1044 |
+ |
| 1045 |
+ @BeforeClass |
| 1046 |
+ public static void initManager() { |
| 1047 |
+ manager = new ScriptEngineManager(); |
| 1048 |
+ manager.registerEngineName("rhino", new RhinoScriptEngineFactory()); |
| 1049 |
+ } |
| 1050 |
+ |
| 1051 |
+ @Before |
| 1052 |
+ public void init() { |
| 1053 |
+ engine = manager.getEngineByName("rhino"); |
| 1054 |
+ cEngine = (Compilable) engine; |
| 1055 |
+ } |
| 1056 |
+ |
| 1057 |
+ @Test |
| 1058 |
+ public void testHello() throws ScriptException { |
| 1059 |
+ Object result = engine.eval("'Hello, World!';"); |
| 1060 |
+ assertEquals(result, "Hello, World!"); |
| 1061 |
+ } |
| 1062 |
+ |
| 1063 |
+ @Test |
| 1064 |
+ public void testHelloInterpreted() throws ScriptException { |
| 1065 |
+ engine.put(RhinoScriptEngine.OPTIMIZATION_LEVEL, -1); |
| 1066 |
+ Object result = engine.eval("'Hello, World!';"); |
| 1067 |
+ assertEquals(result, "Hello, World!"); |
| 1068 |
+ } |
| 1069 |
+ |
| 1070 |
+ |
| 1071 |
+ @Test |
| 1072 |
+ public void testHelloReader() throws ScriptException { |
| 1073 |
+ String src = "1 + 1;"; |
| 1074 |
+ StringReader sr = new StringReader(src); |
| 1075 |
+ Object result = engine.eval(sr); |
| 1076 |
+ assertEquals(result, 2L); |
| 1077 |
+ } |
| 1078 |
+ |
| 1079 |
+ @Test |
| 1080 |
+ public void testGenericStatements() throws ScriptException { |
| 1081 |
+ Object result = engine.eval(engine.getFactory().getProgram( |
| 1082 |
+ "let x = 1;", |
| 1083 |
+ "let y = 2", |
| 1084 |
+ "x + y" |
| 1085 |
+ )); |
| 1086 |
+ assertEquals(3L, result); |
| 1087 |
+ } |
| 1088 |
+ |
| 1089 |
+ @Test |
| 1090 |
+ public void testThrows() { |
| 1091 |
+ assertThrows(ScriptException.class, () -> { |
| 1092 |
+ engine.eval("throw 'This is an error'"); |
| 1093 |
+ }); |
| 1094 |
+ } |
| 1095 |
+ |
| 1096 |
+ @Test |
| 1097 |
+ public void testEngineBindings() throws IOException, ScriptException { |
| 1098 |
+ engine.put("string", "Hello"); |
| 1099 |
+ engine.put("integer", 123); |
| 1100 |
+ engine.put("a", "a"); |
| 1101 |
+ engine.put("b", "b"); |
| 1102 |
+ engine.put("c", "c"); |
| 1103 |
+ |
| 1104 |
+ // Ensure that stuff we just stuck in bindings made it to a global |
| 1105 |
+ engine.eval(new FileReader("testsrc/assert.js")); |
| 1106 |
+ engine.eval("assertEquals(string, 'Hello');\n" |
| 1107 |
+ + "assertEquals(integer, 123);\n" |
| 1108 |
+ + "string = 'Goodbye';\n" |
| 1109 |
+ + "assertEquals(string, 'Goodbye');"); |
| 1110 |
+ assertEquals(engine.get("string"), "Goodbye"); |
| 1111 |
+ |
| 1112 |
+ // Make sure we can delete |
| 1113 |
+ engine.getBindings(ScriptContext.ENGINE_SCOPE).remove("string"); |
| 1114 |
+ // This will throw because string is undefined |
| 1115 |
+ assertThrows(ScriptException.class, () -> { |
| 1116 |
+ engine.eval("let failing = string + '123';"); |
| 1117 |
+ }); |
| 1118 |
+ } |
| 1119 |
+ |
| 1120 |
+ @Test |
| 1121 |
+ public void testEngineScope() throws IOException, ScriptException { |
| 1122 |
+ engine.put("string", "Hello"); |
| 1123 |
+ engine.put("integer", 123); |
| 1124 |
+ engine.eval(new FileReader("testsrc/assert.js")); |
| 1125 |
+ engine.eval("assertEquals(string, 'Hello');" |
| 1126 |
+ + "assertEquals(integer, 123);"); |
| 1127 |
+ |
| 1128 |
+ // Additional things added to the context but old stuff still there |
| 1129 |
+ engine.put("second", true); |
| 1130 |
+ engine.put("integer", 99); |
| 1131 |
+ engine.eval("assertEquals(string, 'Hello');" |
| 1132 |
+ + "assertEquals(integer, 99);" |
| 1133 |
+ + "assertTrue(second);"); |
| 1134 |
+ } |
| 1135 |
+ |
| 1136 |
+ @Test |
| 1137 |
+ public void testScopedBindings() throws IOException, ScriptException { |
| 1138 |
+ ScriptContext sc = new SimpleScriptContext(); |
| 1139 |
+ |
| 1140 |
+ // We treat engine and global scope the same -- if the user actually |
| 1141 |
+ // uses both, then engine scope overrides global scope. |
| 1142 |
+ Bindings eb = new SimpleBindings(); |
| 1143 |
+ sc.setBindings(eb, ScriptContext.ENGINE_SCOPE); |
| 1144 |
+ eb.put("engine", Boolean.TRUE); |
| 1145 |
+ eb.put("level", 2); |
| 1146 |
+ |
| 1147 |
+ Bindings gb = new SimpleBindings(); |
| 1148 |
+ sc.setBindings(gb, ScriptContext.GLOBAL_SCOPE); |
| 1149 |
+ gb.put("global", Boolean.TRUE); |
| 1150 |
+ gb.put("level", 0); |
| 1151 |
+ |
| 1152 |
+ engine.eval(new FileReader("testsrc/assert.js"), sc); |
| 1153 |
+ engine.eval("assertTrue(engine);" |
| 1154 |
+ + "assertTrue(global);" |
| 1155 |
+ + "assertEquals(level, 2);", sc); |
| 1156 |
+ } |
| 1157 |
+ |
| 1158 |
+ @Test |
| 1159 |
+ public void testReservedBindings() throws ScriptException { |
| 1160 |
+ engine.put(ScriptEngine.ENGINE, "engine"); |
| 1161 |
+ engine.put(ScriptEngine.ENGINE_VERSION, "123"); |
| 1162 |
+ engine.put(ScriptEngine.LANGUAGE, "foo"); |
| 1163 |
+ engine.put(ScriptEngine.NAME, "nothing"); |
| 1164 |
+ |
| 1165 |
+ // Can't actually test for those invalid property names -- but |
| 1166 |
+ // at least they didn't break the script. |
| 1167 |
+ assertEquals(engine.eval("'success'"), "success"); |
| 1168 |
+ } |
| 1169 |
+ |
| 1170 |
+ @Test |
| 1171 |
+ public void testCompiled() throws ScriptException, IOException { |
| 1172 |
+ CompiledScript asserts = |
| 1173 |
+ cEngine.compile(new FileReader("testsrc/assert.js")); |
| 1174 |
+ CompiledScript tests = |
| 1175 |
+ cEngine.compile("assertEquals(compiled, true);"); |
| 1176 |
+ |
| 1177 |
+ // Fails because asserts have not been loaded |
| 1178 |
+ assertThrows(ScriptException.class, tests::eval); |
| 1179 |
+ |
| 1180 |
+ asserts.eval(); |
| 1181 |
+ // Fails because value has not been set |
| 1182 |
+ assertThrows(ScriptException.class, tests::eval); |
| 1183 |
+ |
| 1184 |
+ engine.put("compiled", Boolean.TRUE); |
| 1185 |
+ tests.eval(); |
| 1186 |
+ } |
| 1187 |
+ |
| 1188 |
+ @Test |
| 1189 |
+ public void testCompiled2() throws ScriptException, IOException { |
| 1190 |
+ CompiledScript asserts = |
| 1191 |
+ cEngine.compile(new FileReader("testsrc/assert.js")); |
| 1192 |
+ CompiledScript init = |
| 1193 |
+ cEngine.compile("value = 0;"); |
| 1194 |
+ CompiledScript tests = |
| 1195 |
+ cEngine.compile("assertEquals(value, expectedValue);" |
| 1196 |
+ + "value += 1;"); |
| 1197 |
+ |
| 1198 |
+ asserts.eval(); |
| 1199 |
+ init.eval(); |
| 1200 |
+ for (int i = 0; i <= 10; i++) { |
| 1201 |
+ engine.put("expectedValue", i); |
| 1202 |
+ tests.eval(); |
| 1203 |
+ } |
| 1204 |
+ } |
| 1205 |
+ |
| 1206 |
+ @Test |
| 1207 |
+ public void testCompiledThrows() throws ScriptException { |
| 1208 |
+ engine.put(ScriptEngine.FILENAME, "throws1.js"); |
| 1209 |
+ CompiledScript throw1 = cEngine.compile("throw 'one';"); |
| 1210 |
+ engine.put(ScriptEngine.FILENAME, "throws2.js"); |
| 1211 |
+ CompiledScript throw2 = cEngine.compile("throw 'two';"); |
| 1212 |
+ |
| 1213 |
+ try { |
| 1214 |
+ throw1.eval(); |
| 1215 |
+ fail("Expected a throw"); |
| 1216 |
+ } catch (ScriptException se) { |
| 1217 |
+ assertTrue(se.getMessage().startsWith("one")); |
| 1218 |
+ assertEquals("throws1.js", se.getFileName()); |
| 1219 |
+ assertEquals(1, se.getLineNumber()); |
| 1220 |
+ } |
| 1221 |
+ |
| 1222 |
+ try { |
| 1223 |
+ throw2.eval(); |
| 1224 |
+ fail("Expected a throw"); |
| 1225 |
+ } catch (ScriptException se) { |
| 1226 |
+ assertTrue(se.getMessage().startsWith("two")); |
| 1227 |
+ assertEquals("throws2.js", se.getFileName()); |
| 1228 |
+ assertEquals(1, se.getLineNumber()); |
| 1229 |
+ } |
| 1230 |
+ } |
| 1231 |
+ |
| 1232 |
+ @Test |
| 1233 |
+ public void testCantCompile() { |
| 1234 |
+ assertThrows(ScriptException.class, () -> { |
| 1235 |
+ cEngine.compile("This is not JavaScript at all!"); |
| 1236 |
+ }); |
| 1237 |
+ } |
| 1238 |
+ |
| 1239 |
+ @Test |
| 1240 |
+ public void testLanguageVersion() throws ScriptException { |
| 1241 |
+ // Default language version is modernish |
| 1242 |
+ ScriptEngine newEngine = manager.getEngineByName("rhino"); |
| 1243 |
+ assertEquals(newEngine.eval("Symbol() == Symbol()"), Boolean.FALSE); |
| 1244 |
+ |
| 1245 |
+ // Older language versions |
| 1246 |
+ ScriptEngine oldEngine = manager.getEngineByName("rhino"); |
| 1247 |
+ oldEngine.put(ScriptEngine.LANGUAGE_VERSION, 120); |
| 1248 |
+ assertThrows(ScriptException.class, () -> { |
| 1249 |
+ oldEngine.eval("Symbol() == Symbol()"); |
| 1250 |
+ }); |
| 1251 |
+ |
| 1252 |
+ // The same with a string |
| 1253 |
+ ScriptEngine olderEngine = manager.getEngineByName("rhino"); |
| 1254 |
+ olderEngine.put(ScriptEngine.LANGUAGE_VERSION, "100"); |
| 1255 |
+ assertThrows(ScriptException.class, () -> { |
| 1256 |
+ olderEngine.eval("Symbol() == Symbol()"); |
| 1257 |
+ }); |
| 1258 |
+ } |
| 1259 |
+ |
| 1260 |
+ @Test |
| 1261 |
+ public void testBadLanguageVersion() { |
| 1262 |
+ assertThrows(ScriptException.class, () -> { |
| 1263 |
+ engine.put(ScriptEngine.LANGUAGE_VERSION, "Not a number"); |
| 1264 |
+ engine.eval("print('Hi!');"); |
| 1265 |
+ }); |
| 1266 |
+ assertThrows(ScriptException.class, () -> { |
| 1267 |
+ engine.put(ScriptEngine.LANGUAGE_VERSION, 3.14); |
| 1268 |
+ engine.eval("print('Hi!');"); |
| 1269 |
+ }); |
| 1270 |
+ } |
| 1271 |
+ |
| 1272 |
+ @Test |
| 1273 |
+ public void testFilename() { |
| 1274 |
+ engine.put(ScriptEngine.FILENAME, "test.js"); |
| 1275 |
+ try { |
| 1276 |
+ engine.eval("throw 'This is an exception';"); |
| 1277 |
+ } catch (ScriptException se) { |
| 1278 |
+ assertEquals(se.getFileName(), "test.js"); |
| 1279 |
+ } |
| 1280 |
+ } |
| 1281 |
+ |
| 1282 |
+ @Test |
| 1283 |
+ public void testJavaObject() throws ScriptException { |
| 1284 |
+ File f = new File("testsrc/assert.js"); |
| 1285 |
+ String absVal = f.getAbsolutePath(); |
| 1286 |
+ engine.put("file", f); |
| 1287 |
+ Object result = engine.eval("file.getAbsolutePath();"); |
| 1288 |
+ assertEquals(absVal, result); |
| 1289 |
+ } |
| 1290 |
+} |
| 1291 |
--- a/build.xml |
| 1292 |
+++ b/build.xml |
| 1293 |
@@ -87,7 +87,9 @@ |
| 1294 |
basedir="${classes}" |
| 1295 |
manifest="src/manifest" |
| 1296 |
compress="${jar-compression}" |
| 1297 |
- /> |
| 1298 |
+ > |
| 1299 |
+ <fileset dir="src" includes="META-INF/**"/> |
| 1300 |
+ </jar> |
| 1301 |
</target> |
| 1302 |
|
| 1303 |
<target name="console" depends="jar"> |