Script { public Object run(final String functionReference, final Object... values) { Context context = enterContext(); try { Scriptable object = this.localScope; Iterator<String> parts = Splitter.on('.').split(functionReference).iterator(); while (parts.hasNext()) { String property = parts.next(); if (parts.hasNext()) { object = (Scriptable) ScriptableObject.getProperty(object, property); } else { return unwrap(ScriptableObject.callMethod(context, object, property, jsValues(values))); } } throw new IllegalArgumentException("functionName is empty"); } finally { exitContext(); } } Script(final String name, final String source);  Script(final String name,
                final String source,
                final LoadPath loadPath,
                final String... globalFiles);  Script(final String name,
                final String source,
                final ScriptCompilation scriptCompilation,
                final LoadPath loadPath,
                final String... globalFiles); static Context enterContext(); static void exitContext(); Scriptable getScope(); synchronized Object load(final String scriptName); String read(final String filename); Object run(final String functionReference, final Object... values); Object call(final Object object, final String property, final Object... values); @ReferencedByJavascript
  static final Cache<ValueName, Object> STATIC_VALUES; }
@Test public void functionsShouldReturnJavaNativeTypes() throws ScriptException, LoadError { Script s = new Script("test", "function add(a, b) { return a + b }"); Assert.assertEquals(106.0, s.run("add", 6, 100)); Assert.assertEquals("ab", s.run("add", "a", "b")); }
@Test public void functionReferenceChainShouldWork() throws ScriptException, LoadError { Script s = new Script("test", "x = {a: {b: {c: function(a, b) { return a + b } } } }"); Assert.assertEquals(5.0, s.run("x.a.b.c", 2, 3)); }
@Test public void concurrentRunsShouldNotEffectOneAnother() throws ScriptException, InterruptedException, LoadError { final Script s = new Script("test", "function add(a, b) { return a + b }"); final Random random = new Random(); TestUtils.runConcurrent(10, new Runnable() { @Override public void run() { double a = random.nextDouble(); Integer b = random.nextInt(100); Assert.assertEquals(a + b, s.run("add", a, b)); } }); }
@Test public void loadViaGlobalFilesImportsModuleSymbol() throws Exception { final Script s = new Script("test", "function tester() { return test.me(); }", LoadPaths.defaultPath(), "hegemon/test"); Assert.assertEquals("here", s.run("tester")); }
@Test public void loadViaInternalFunctionReturnsModule() throws ScriptException, LoadError { final Script s = new Script("test", "let test = core.load('hegemon/test'); function tester() { return test.me(); }"); Assert.assertEquals("here", s.run("tester")); }
@Test public void loadViaInternalFunctionDoesntPolluteGlobals() throws Exception { try { final Script s = new Script( "test", "let testImport = core.load('hegemon/testImport'); function tester() { return FOO_BAR; }"); s.run("tester"); Assert.fail(); } catch (EcmaError e) { if (!e.getMessage().startsWith("ReferenceError: \"FOO_BAR\"")) { throw e; } } }