org.mozilla.javascript.Script Java Examples
The following examples show how to use
org.mozilla.javascript.Script.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Bug685403Test.java From rhino-android with Apache License 2.0 | 6 votes |
@Test public void test() { String source = "var state = '';"; source += "function A(){state += 'A'}"; source += "function B(){state += 'B'}"; source += "function C(){state += 'C'}"; source += "try { A(); continuation(); B() } finally { C() }"; source += "state"; String[] functions = new String[] { "continuation" }; scope.defineFunctionProperties(functions, Bug685403Test.class, ScriptableObject.DONTENUM); Object state = null; Script script = cx.compileString(source, "", 1, null); try { cx.executeScriptWithContinuations(script, scope); fail("expected ContinuationPending exception"); } catch (ContinuationPending pending) { state = cx.resumeContinuation(pending.getContinuation(), scope, ""); } assertEquals("ABC", state); }
Example #2
Source File: DcEngineContext.java From io with Apache License 2.0 | 6 votes |
/** * JavaScriptファイルを解析し、オブジェクトに登録. * @param name JavaScriptソース名 * @throws IOException IO例外 */ private Object loadJs(final String name) throws IOException { URL path = getClass().getResource("/js-lib/" + name + ".js"); Script jsBuildObject = null; if (engineLibCache.containsKey(path.toString())) { jsBuildObject = engineLibCache.get(path.toString()); } else { FileInputStream fis = new FileInputStream(path.getFile()); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); jsBuildObject = cx.compileReader(isr, path.getPath(), 1, null); engineLibCache.put(path.toString(), jsBuildObject); } if (jsBuildObject == null) { return null; } Object ret = jsBuildObject.exec(cx, scope); log.debug("Load JavaScript from Local Resource : " + path); return ret; }
Example #3
Source File: Main.java From astor with GNU General Public License v2.0 | 6 votes |
static void evalInlineScript(Context cx, String scriptText) { try { Script script = cx.compileString(scriptText, "<command>", 1, null); if (script != null) { script.exec(cx, getShellScope()); } } catch (RhinoException rex) { ToolErrorReporter.reportException( cx.getErrorReporter(), rex); exitCode = EXITCODE_RUNTIME_ERROR; } catch (VirtualMachineError ex) { // Treat StackOverflow and OutOfMemory as runtime errors ex.printStackTrace(); String msg = ToolErrorReporter.getMessage( "msg.uncaughtJSException", ex.toString()); Context.reportError(msg); exitCode = EXITCODE_RUNTIME_ERROR; } }
Example #4
Source File: ContinuationsApiTest.java From rhino-android with Apache License 2.0 | 6 votes |
public void testScriptWithContinuations() { Context cx = Context.enter(); try { cx.setOptimizationLevel(-1); // must use interpreter mode Script script = cx.compileString("myObject.f(3) + 1;", "test source", 1, null); cx.executeScriptWithContinuations(script, globalScope); fail("Should throw ContinuationPending"); } catch (ContinuationPending pending) { Object applicationState = pending.getApplicationState(); assertEquals(new Integer(3), applicationState); int saved = (Integer) applicationState; Object result = cx.resumeContinuation(pending.getContinuation(), globalScope, saved + 1); assertEquals(5, ((Number)result).intValue()); } finally { Context.exit(); } }
Example #5
Source File: ExpressionLanguageJavaScriptImpl.java From oval with Eclipse Public License 2.0 | 6 votes |
@Override public Object evaluate(final String expression, final Map<String, ?> values) throws ExpressionEvaluationException { LOG.debug("Evaluating JavaScript expression: {1}", expression); try { final Context ctx = ContextFactory.getGlobal().enterContext(); final Scriptable scope = ctx.newObject(parentScope); scope.setPrototype(parentScope); scope.setParentScope(null); for (final Entry<String, ?> entry : values.entrySet()) { scope.put(entry.getKey(), scope, Context.javaToJS(entry.getValue(), scope)); } final Script expr = expressionCache.get(expression); return expr.exec(ctx, scope); } catch (final EvaluatorException ex) { throw new ExpressionEvaluationException("Evaluating JavaScript expression failed: " + expression, ex); } finally { Context.exit(); } }
Example #6
Source File: ContinuationsApiTest.java From rhino-android with Apache License 2.0 | 6 votes |
public void testScriptWithNestedContinuations() { Context cx = Context.enter(); try { cx.setOptimizationLevel(-1); // must use interpreter mode Script script = cx.compileString("myObject.g( myObject.f(1) ) + 2;", "test source", 1, null); cx.executeScriptWithContinuations(script, globalScope); fail("Should throw ContinuationPending"); } catch (ContinuationPending pending) { try { Object applicationState = pending.getApplicationState(); assertEquals(new Integer(1), applicationState); int saved = (Integer) applicationState; cx.resumeContinuation(pending.getContinuation(), globalScope, saved + 1); fail("Should throw another ContinuationPending"); } catch (ContinuationPending pending2) { Object applicationState2 = pending2.getApplicationState(); assertEquals(new Integer(4), applicationState2); int saved2 = (Integer) applicationState2; Object result2 = cx.resumeContinuation(pending2.getContinuation(), globalScope, saved2 + 2); assertEquals(8, ((Number)result2).intValue()); } } finally { Context.exit(); } }
Example #7
Source File: RhinoScriptProcessor.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
/** * @see org.alfresco.service.cmr.repository.ScriptProcessor#executeString(java.lang.String, java.util.Map) */ public Object executeString(String source, Map<String, Object> model) { try { // compile the script based on the node content Script script; Context cx = Context.enter(); try { script = cx.compileString(resolveScriptImports(source), "AlfrescoJS", 1, null); } finally { Context.exit(); } return executeScriptImpl(script, model, true, "string script"); } catch (Throwable err) { throw new ScriptException("Failed to execute supplied script: " + err.getMessage(), err); } }
Example #8
Source File: ContinuationsApiTest.java From rhino-android with Apache License 2.0 | 6 votes |
/** * Since a continuation can only capture JavaScript frames and not Java * frames, ensure that Rhino throws an exception when the JavaScript frames * don't reach all the way to the code called by * executeScriptWithContinuations or callFunctionWithContinuations. */ public void testErrorOnEvalCall() { Context cx = Context.enter(); try { cx.setOptimizationLevel(-1); // must use interpreter mode Script script = cx.compileString("eval('myObject.f(3);');", "test source", 1, null); cx.executeScriptWithContinuations(script, globalScope); fail("Should throw IllegalStateException"); } catch (WrappedException we) { Throwable t = we.getWrappedException(); assertTrue(t instanceof IllegalStateException); assertTrue(t.getMessage().startsWith("Cannot capture continuation")); } finally { Context.exit(); } }
Example #9
Source File: JavaScriptEvaluatorScope.java From jasperreports with GNU Lesser General Public License v3.0 | 6 votes |
protected Script getCompiledExpression(String expression) { Script compiledExpression = compiledExpressions.get(expression); if (compiledExpression == null) { if (log.isTraceEnabled()) { log.trace("compiling expression " + expression); } ensureContext(); compiledExpression = context.compileString(expression, "expression", 0, getProtectionDomain()); compiledExpressions.put(expression, compiledExpression); } return compiledExpression; }
Example #10
Source File: ContinuationsApiTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testScriptWithContinuations() { Context cx = Context.enter(); try { cx.setOptimizationLevel(-1); // must use interpreter mode Script script = cx.compileString("myObject.f(3) + 1;", "test source", 1, null); cx.executeScriptWithContinuations(script, globalScope); fail("Should throw ContinuationPending"); } catch (ContinuationPending pending) { Object applicationState = pending.getApplicationState(); assertEquals(new Integer(3), applicationState); int saved = (Integer) applicationState; Object result = cx.resumeContinuation(pending.getContinuation(), globalScope, saved + 1); assertEquals(5, ((Number)result).intValue()); } finally { Context.exit(); } }
Example #11
Source File: JavaScriptEvaluatorScope.java From jasperreports with GNU Lesser General Public License v3.0 | 6 votes |
public Object evaluateExpression(Script expression) { ensureContext(); Object value = expression.exec(context, scope); Object javaValue; // not converting Number objects because the generic conversion call below // always converts to Double if (value == null || value instanceof Number) { javaValue = value; } else { try { javaValue = Context.jsToJava(value, Object.class); } catch (EvaluatorException e) { throw new JRRuntimeException(e); } } return javaValue; }
Example #12
Source File: ContinuationsApiTest.java From astor with GNU General Public License v2.0 | 6 votes |
public void testScriptWithNestedContinuations() { Context cx = Context.enter(); try { cx.setOptimizationLevel(-1); // must use interpreter mode Script script = cx.compileString("myObject.g( myObject.f(1) ) + 2;", "test source", 1, null); cx.executeScriptWithContinuations(script, globalScope); fail("Should throw ContinuationPending"); } catch (ContinuationPending pending) { try { Object applicationState = pending.getApplicationState(); assertEquals(new Integer(1), applicationState); int saved = (Integer) applicationState; cx.resumeContinuation(pending.getContinuation(), globalScope, saved + 1); fail("Should throw another ContinuationPending"); } catch (ContinuationPending pending2) { Object applicationState2 = pending2.getApplicationState(); assertEquals(new Integer(4), applicationState2); int saved2 = (Integer) applicationState2; Object result2 = cx.resumeContinuation(pending2.getContinuation(), globalScope, saved2 + 2); assertEquals(8, ((Number)result2).intValue()); } } finally { Context.exit(); } }
Example #13
Source File: JavaScriptClassLoader.java From jasperreports with GNU Lesser General Public License v3.0 | 6 votes |
public Script createScript(int classIndex, JavaScriptCompiledData compiledData) { CompiledClass compiledClass = compiledData.getCompiledClass(classIndex); Class<? extends Script> scriptClass = loadExpressionClass(compiledClass); try { Script script = scriptClass.getDeclaredConstructor().newInstance(); return script; } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { throw new JRRuntimeException( EXCEPTION_MESSAGE_KEY_INSTANCE_ERROR, new Object[]{compiledClass.getClassName()}, e); } }
Example #14
Source File: Main.java From JsDroidCmd with Mozilla Public License 2.0 | 6 votes |
static void evalInlineScript(Context cx, String scriptText) { try { Script script = cx.compileString(scriptText, "<command>", 1, null); if (script != null) { script.exec(cx, getShellScope()); } } catch (RhinoException rex) { ToolErrorReporter.reportException( cx.getErrorReporter(), rex); exitCode = EXITCODE_RUNTIME_ERROR; } catch (VirtualMachineError ex) { // Treat StackOverflow and OutOfMemory as runtime errors ex.printStackTrace(); String msg = ToolErrorReporter.getMessage( "msg.uncaughtJSException", ex.toString()); Context.reportError(msg); exitCode = EXITCODE_RUNTIME_ERROR; } }
Example #15
Source File: Bug482203Test.java From rhino-android with Apache License 2.0 | 6 votes |
public void testJsApi() throws Exception { Context cx = RhinoAndroidHelper.prepareContext(); try { cx.setOptimizationLevel(-1); Script script = cx.compileString(TestUtils.readAsset("Bug482203.js"), "", 1, null); Scriptable scope = cx.initStandardObjects(); script.exec(cx, scope); int counter = 0; for(;;) { Object cont = ScriptableObject.getProperty(scope, "c"); if(cont == null) { break; } counter++; ((Callable)cont).call(cx, scope, scope, new Object[] { null }); } assertEquals(counter, 5); assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result")); } finally { Context.exit(); } }
Example #16
Source File: Bug482203Test.java From rhino-android with Apache License 2.0 | 6 votes |
public void testJavaApi() throws Exception { Context cx = RhinoAndroidHelper.prepareContext(); try { cx.setOptimizationLevel(-1); Script script = cx.compileString(TestUtils.readAsset("Bug482203.js"), "", 1, null); Scriptable scope = cx.initStandardObjects(); cx.executeScriptWithContinuations(script, scope); int counter = 0; for(;;) { Object cont = ScriptableObject.getProperty(scope, "c"); if(cont == null) { break; } counter++; cx.resumeContinuation(cont, scope, null); } assertEquals(counter, 5); assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result")); } finally { Context.exit(); } }
Example #17
Source File: Bug482203Test.java From astor with GNU General Public License v2.0 | 6 votes |
public void testJsApi() throws Exception { Context cx = Context.enter(); try { cx.setOptimizationLevel(-1); Script script = cx.compileReader(new InputStreamReader( Bug482203Test.class.getResourceAsStream("Bug482203.js")), "", 1, null); Scriptable scope = cx.initStandardObjects(); script.exec(cx, scope); int counter = 0; for(;;) { Object cont = ScriptableObject.getProperty(scope, "c"); if(cont == null) { break; } counter++; ((Callable)cont).call(cx, scope, scope, new Object[] { null }); } assertEquals(counter, 5); assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result")); } finally { Context.exit(); } }
Example #18
Source File: Bug482203Test.java From astor with GNU General Public License v2.0 | 6 votes |
public void testJavaApi() throws Exception { Context cx = Context.enter(); try { cx.setOptimizationLevel(-1); Script script = cx.compileReader(new InputStreamReader( Bug482203Test.class.getResourceAsStream("Bug482203.js")), "", 1, null); Scriptable scope = cx.initStandardObjects(); cx.executeScriptWithContinuations(script, scope); int counter = 0; for(;;) { Object cont = ScriptableObject.getProperty(scope, "c"); if(cont == null) { break; } counter++; cx.resumeContinuation(cont, scope, null); } assertEquals(counter, 5); assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result")); } finally { Context.exit(); } }
Example #19
Source File: ContinuationsApiTest.java From astor with GNU General Public License v2.0 | 6 votes |
/** * Since a continuation can only capture JavaScript frames and not Java * frames, ensure that Rhino throws an exception when the JavaScript frames * don't reach all the way to the code called by * executeScriptWithContinuations or callFunctionWithContinuations. */ public void testErrorOnEvalCall() { Context cx = Context.enter(); try { cx.setOptimizationLevel(-1); // must use interpreter mode Script script = cx.compileString("eval('myObject.f(3);');", "test source", 1, null); cx.executeScriptWithContinuations(script, globalScope); fail("Should throw IllegalStateException"); } catch (WrappedException we) { Throwable t = we.getWrappedException(); assertTrue(t instanceof IllegalStateException); assertTrue(t.getMessage().startsWith("Cannot capture continuation")); } finally { Context.exit(); } }
Example #20
Source File: Bug421071Test.java From rhino-android with Apache License 2.0 | 6 votes |
private Script compileScript() { String scriptSource = "importPackage(java.util);\n" + "var searchmon = 3;\n" + "var searchday = 10;\n" + "var searchyear = 2008;\n" + "var searchwkday = 0;\n" + "\n" + "var myDate = Calendar.getInstance();\n // this is a java.util.Calendar" + "myDate.set(Calendar.MONTH, searchmon);\n" + "myDate.set(Calendar.DATE, searchday);\n" + "myDate.set(Calendar.YEAR, searchyear);\n" + "searchwkday.value = myDate.get(Calendar.DAY_OF_WEEK);"; Script script; Context context = factory.enterContext(); try { script = context.compileString(scriptSource, "testScript", 1, null); return script; } finally { Context.exit(); } }
Example #21
Source File: Bug421071Test.java From astor with GNU General Public License v2.0 | 6 votes |
private Script compileScript() { String scriptSource = "importPackage(java.util);\n" + "var searchmon = 3;\n" + "var searchday = 10;\n" + "var searchyear = 2008;\n" + "var searchwkday = 0;\n" + "\n" + "var myDate = Calendar.getInstance();\n // this is a java.util.Calendar" + "myDate.set(Calendar.MONTH, searchmon);\n" + "myDate.set(Calendar.DATE, searchday);\n" + "myDate.set(Calendar.YEAR, searchyear);\n" + "searchwkday.value = myDate.get(Calendar.DAY_OF_WEEK);"; Script script; Context context = factory.enterContext(); try { script = context.compileString(scriptSource, "testScript", 1, null); return script; } finally { Context.exit(); } }
Example #22
Source File: JavaAcessibilityTest.java From astor with GNU General Public License v2.0 | 5 votes |
private Object runScript(final String scriptSourceText) { return contextFactory.call(new ContextAction() { public Object run(Context context) { Script script = context.compileString(scriptSourceText, "", 1, null); return script.exec(context, global); } }); }
Example #23
Source File: ContinuationsApiTest.java From astor with GNU General Public License v2.0 | 5 votes |
public void testScriptWithMultipleContinuations() { Context cx = Context.enter(); try { cx.setOptimizationLevel(-1); // must use interpreter mode Script script = cx.compileString( "myObject.f(3) + myObject.g(3) + 2;", "test source", 1, null); cx.executeScriptWithContinuations(script, globalScope); fail("Should throw ContinuationPending"); } catch (ContinuationPending pending) { try { Object applicationState = pending.getApplicationState(); assertEquals(new Integer(3), applicationState); int saved = (Integer) applicationState; cx.resumeContinuation(pending.getContinuation(), globalScope, saved + 1); fail("Should throw another ContinuationPending"); } catch (ContinuationPending pending2) { Object applicationState2 = pending2.getApplicationState(); assertEquals(new Integer(6), applicationState2); int saved2 = (Integer) applicationState2; Object result2 = cx.resumeContinuation(pending2.getContinuation(), globalScope, saved2 + 1); assertEquals(13, ((Number)result2).intValue()); } } finally { Context.exit(); } }
Example #24
Source File: DecompileTest.java From astor with GNU General Public License v2.0 | 5 votes |
/** * As of head of trunk on 30.09.09, decompile of "new Date()" returns "new Date" without parentheses. * @see <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=519692">Bug 519692</a> */ @Test public void newObject0Arg() { final String source = "var x = new Date().getTime();"; final ContextAction action = new ContextAction() { public Object run(final Context cx) { final Script script = cx.compileString(source, "my script", 0, null); Assert.assertEquals(source, cx.decompileScript(script, 4).trim()); return null; } }; Utils.runWithAllOptimizationLevels(action); }
Example #25
Source File: GeneratedMethodNameTest.java From astor with GNU General Public License v2.0 | 5 votes |
public void doTest(final String scriptCode) throws Exception { final Context cx = ContextFactory.getGlobal().enterContext(); try { Scriptable topScope = cx.initStandardObjects(); topScope.put("javaNameGetter", topScope, new JavaNameGetter()); Script script = cx.compileString(scriptCode, "myScript", 1, null); script.exec(cx, topScope); } finally { Context.exit(); } }
Example #26
Source File: Bug714204Test.java From astor with GNU General Public License v2.0 | 5 votes |
@Test public void test_assign_this() { StringBuilder sb = new StringBuilder(); sb.append("function F() {\n"); sb.append(" [this.x] = arguments;\n"); sb.append("}\n"); sb.append("var f = new F('a');\n"); sb.append("(f.x == 'a')\n"); Script script = cx.compileString(sb.toString(), "<eval>", 1, null); Object result = script.exec(cx, scope); assertEquals(Boolean.TRUE, result); }
Example #27
Source File: GeneratedClassNameTest.java From astor with GNU General Public License v2.0 | 5 votes |
private void doTest(final String expectedName, final String scriptName) throws Exception { final Script script = (Script) ContextFactory.getGlobal().call( new ContextAction() { public Object run(final Context context) { return context.compileString("var f = 1", scriptName, 1, null); } }); // remove serial number String name = script.getClass().getSimpleName(); assertEquals(expectedName, name.substring(0, name.lastIndexOf('_'))); }
Example #28
Source File: SoftCachingModuleScriptProvider.java From astor with GNU General Public License v2.0 | 5 votes |
CachedModuleScript getCachedModuleScript() { final Script script = get(); if(script == null) { return null; } return new CachedModuleScript(new ModuleScript(script, uri, base), validator); }
Example #29
Source File: JavascriptEvalUtil.java From birt with Eclipse Public License 1.0 | 5 votes |
/** * This method will not convert the data of return value, so it might the * Java data type or that of Java Script. * * @param cx * @param scope * @param scriptText * @param source * @param lineNo * @return the evaluated value * @throws BirtException */ public static Object evaluateRawScript(Context cx, Scriptable scope, String scriptText, String source, int lineNo) throws BirtException { Object result = null; // Use provided context, or get the thread context if none provided boolean enterContext = cx == null; if ( enterContext ) cx = Context.enter(); try { Script compiledScript = getCompiledScript( cx, scope, scriptText, source, lineNo ); result = compiledScript.exec( cx, scope ); } catch ( RhinoException e) { // Note: use the real source and lineNo here. The source and lineNo reported // by e can be wrong, since we may be executing an identical compiled script // from a different source/line throw wrapRhinoException( e, scriptText, source, lineNo ); } finally { if ( enterContext) Context.exit(); } return result; }
Example #30
Source File: JavaScriptCompiledEvaluator.java From jasperreports with GNU Lesser General Public License v3.0 | 5 votes |
protected Object evaluateExpression(int expressionIndex) { int scriptIndex = JavaScriptCompiledData.scriptIndex(expressionIndex); Script script = scripts.get(scriptIndex); if (script == null) { if (log.isTraceEnabled()) { log.trace("creating script for expression index " + expressionIndex + ", script index " + scriptIndex); } JavaScriptClassLoader scriptClassLoader = getScriptClassLoader(unitName); script = scriptClassLoader.createScript(scriptIndex, compiledData); scripts.put(scriptIndex, script); } int expressionId = JavaScriptCompiledData.expressionId(expressionIndex); evaluatorScope.setScopeVariable(EXPRESSION_ID_VAR, expressionId); Object value = evaluatorScope.evaluateExpression(script); if (log.isTraceEnabled()) { log.trace("expression with index " + expressionIndex + ", id " + expressionId + " evaluated to " + value); } return value; }