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; } }
/** * @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); } }
public Object run(Context cx) { if (modulePath != null || mainModule != null) { require = global.installRequire(cx, modulePath, sandboxed); } if (type == PROCESS_FILES) { processFiles(cx, args); } else if (type == EVAL_INLINE_SCRIPT) { Script script = loadScriptFromSource(cx, scriptText, "<command>", 1, null); if (script != null) { evaluateScript(script, cx, getGlobal()); } } else { throw Kit.codeBug(); } return null; }
public static Script loadScriptFromSource(Context cx, String scriptSource, String path, int lineno, Object securityDomain) { try { return cx.compileString(scriptSource, path, lineno, securityDomain); } catch (EvaluatorException ee) { // Already printed message. exitCode = EXITCODE_RUNTIME_ERROR; } 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()); exitCode = EXITCODE_RUNTIME_ERROR; Context.reportError(msg); } return null; }
public static Object evaluateScript(Script script, Context cx, Scriptable scope) { try { return script.exec(cx, scope); } 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()); exitCode = EXITCODE_RUNTIME_ERROR; Context.reportError(msg); } return Context.getUndefinedValue(); }
public void testJsApi() throws Exception { Context cx = Context.enter(); cx.setOptimizationLevel(-1); Script script = cx.compileReader(new InputStreamReader( Bug482203.class.getResourceAsStream("conttest.js")), "", 1, null); Scriptable scope = cx.initStandardObjects(); script.exec(cx, scope); for(;;) { Object cont = ScriptableObject.getProperty(scope, "c"); if(cont == null) { break; } ((Callable)cont).call(cx, scope, scope, new Object[] { null }); } }
public void testJavaApi() throws Exception { Context cx = Context.enter(); try { cx.setOptimizationLevel(-1); Script script = cx.compileReader(new InputStreamReader( Bug482203.class.getResourceAsStream("conttest.js")), "", 1, null); Scriptable scope = cx.initStandardObjects(); cx.executeScriptWithContinuations(script, scope); for(;;) { Object cont = ScriptableObject.getProperty(scope, "c"); if(cont == null) { break; } cx.resumeContinuation(cont, scope, null); } } finally { Context.exit(); } }
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(); } }
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(); } }
/** * 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(); } }
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(); } }
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(); } }
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(); } }
@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); }
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; }
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; }
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(); Script script = scriptCache.get(expression); if (script == null) { ctx.setOptimizationLevel(9); script = ctx.compileString(expression, "<cmd>", 1, null); scriptCache.put(expression, script); } 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)); } return script.exec(ctx, scope); } catch (final EvaluatorException ex) { throw new ExpressionEvaluationException("Evaluating JavaScript expression failed: " + expression, ex); } finally { Context.exit(); } }
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(); } }
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(); } }
public void execute( ExecutionContext executionContext ) throws ParsingException, ExecutionException { Context context = adapter.enterContext( executionContext ); try { ScriptableObject scope = adapter.getScope( executable, executionContext, context, startLineNumber ); Script script = this.script; if( script != null ) script.exec( context, scope ); else context.evaluateString( scope, sourceCode, executable.getDocumentName(), startLineNumber, null ); } catch( Exception x ) { throw RhinoAdapter.createExecutionException( executable, x ); } finally { Context.exit(); } }
@Override public CompiledScript compile(Reader script) throws ScriptException { CompiledScript compiledScript = null; Context cx = enterContext(); try { String filename = getFilenameFromReader(script); Script rhinoScript = cx.compileReader(script, filename, 1, null); compiledScript = new CompiledJavaScript(this, rhinoScript); } catch (Exception e) { throwWrappedScriptException(e); } finally { exitContext(); } return compiledScript; }
@SuppressWarnings("deprecation") public CompiledScript compile(java.io.Reader script) throws ScriptException { CompiledScript ret = null; Context cx = enterContext(); try { String fileName = (String) get(ScriptEngine.FILENAME); if (fileName == null) { fileName = "<Unknown Source>"; } Scriptable scope = getRuntimeScope(context); Script scr = cx.compileReader(scope, script, fileName, 1, null); ret = new RhinoCompiledScript(this, scr); } catch (Exception e) { throw new ScriptException(e); } finally { Context.exit(); } return ret; }