Java 类org.mozilla.javascript.Script 实例源码

项目:https-github.com-hyb1996-NoRootScriptDroid    文件:Main.java   
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;
    }
}
项目:alfresco-repository    文件:RhinoScriptProcessor.java   
/**
 * @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);
    }
}
项目:Auto.js    文件:Main.java   
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;
    }
}
项目:whackpad    文件:Main.java   
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;
}
项目:whackpad    文件:Main.java   
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;
}
项目:whackpad    文件:Main.java   
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();
}
项目:whackpad    文件:Bug482203.java   
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 });
    }
}
项目:whackpad    文件:Bug482203.java   
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();
    }
}
项目:whackpad    文件:ContinuationsApiTest.java   
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();
    }
}
项目:whackpad    文件:ContinuationsApiTest.java   
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();
    }
}
项目:whackpad    文件:ContinuationsApiTest.java   
/**
 * 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();
    }
}
项目:whackpad    文件:Bug421071Test.java   
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();
    }
}
项目:rhino-android    文件:ContinuationsApiTest.java   
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();
    }
}
项目:rhino-android    文件:ContinuationsApiTest.java   
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();
    }
}
项目:rhino-android    文件:ContinuationsApiTest.java   
/**
 * 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();
    }
}
项目:rhino-android    文件:Bug482203Test.java   
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();
    }
}
项目:rhino-android    文件:Bug482203Test.java   
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();
    }
}
项目:rhino-android    文件:Bug685403Test.java   
@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);
}
项目:rhino-android    文件:Bug421071Test.java   
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();
    }
}
项目:jasperreports    文件:JavaScriptEvaluatorScope.java   
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;
}
项目:jasperreports    文件:JavaScriptEvaluatorScope.java   
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;
}
项目:oval    文件:ExpressionLanguageJavaScriptImpl.java   
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();
    }
}
项目:code404    文件:Main.java   
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;
    }
}
项目:code404    文件:ContinuationsApiTest.java   
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();
    }
}
项目:code404    文件:ContinuationsApiTest.java   
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();
    }
}
项目:code404    文件:ContinuationsApiTest.java   
/**
 * 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();
    }
}
项目:code404    文件:Bug482203Test.java   
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();
    }
}
项目:code404    文件:Bug482203Test.java   
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();
    }
}
项目:code404    文件:Bug421071Test.java   
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();
    }
}
项目:scripturian    文件:RhinoProgram.java   
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();
    }
}
项目:jsen-js    文件:JavaScriptEngine.java   
@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;
}
项目:ef-orm    文件:RhinoScriptEngine.java   
@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;
   }
项目:community-edition-old    文件:RhinoScriptProcessor.java   
/**
 * @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);
    }
}
项目:S3F    文件:Main.java   
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;
    }
}
项目:rhino-jscover    文件:Main.java   
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;
    }
}
项目:ScriptBox    文件:WindowJavaScriptEngine.java   
@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;
}
项目:Rhino-Prov-Mod    文件:Bug482203Test.java   
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();
    }
}
项目:rhino-jscover    文件:ContinuationsApiTest.java   
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();
    }
}
项目:Rhino-Prov-Mod    文件:Bug421071Test.java   
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();
    }
}
项目:rhino-jscover    文件:ContinuationsApiTest.java   
/**
 * 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();
    }
}