Search in sources :

Example 31 with Context

use of org.mozilla.javascript.Context in project hackpad by dropbox.

the class DecompileTest method newObject0Arg.

/**
	 * 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);
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) ContextAction(org.mozilla.javascript.ContextAction) Test(org.junit.Test)

Example 32 with Context

use of org.mozilla.javascript.Context in project cucumber-jvm by cucumber.

the class HTMLFormatterTest method writes_valid_report_js.

@Test
public void writes_valid_report_js() throws IOException {
    URL reportJs = new URL(outputDir, "report.js");
    Context cx = Context.enter();
    Global scope = new Global(cx);
    try {
        cx.evaluateReader(scope, new InputStreamReader(reportJs.openStream(), "UTF-8"), reportJs.getFile(), 1, null);
        fail("Should have failed");
    } catch (EcmaError expected) {
        assertTrue(expected.getMessage().startsWith("ReferenceError: \"document\" is not defined."));
    }
}
Also used : Context(org.mozilla.javascript.Context) EcmaError(org.mozilla.javascript.EcmaError) InputStreamReader(java.io.InputStreamReader) URL(java.net.URL) Global(org.mozilla.javascript.tools.shell.Global) Test(org.junit.Test)

Example 33 with Context

use of org.mozilla.javascript.Context in project OpenAM by OpenRock.

the class RhinoScriptEngine method eval.

/**
     * {@inheritDoc}
     */
@Override
public Object eval(final Reader reader, final ScriptContext scriptContext) throws ScriptException {
    Reject.ifNull(reader, scriptContext);
    Object result = null;
    final Context context = factory.getContext();
    try {
        final Scriptable scope = getScope(context, scriptContext);
        final String filename = getFilename(scriptContext);
        result = context.evaluateReader(scope, reader, filename, 1, null);
    } catch (RhinoException ex) {
        throw convertException(ex);
    } catch (IOException ex) {
        throw new ScriptException(ex);
    } finally {
        factory.releaseContext(context);
    }
    return result;
}
Also used : Context(org.mozilla.javascript.Context) ScriptContext(javax.script.ScriptContext) ScriptException(javax.script.ScriptException) ScriptableObject(org.mozilla.javascript.ScriptableObject) RhinoException(org.mozilla.javascript.RhinoException) IOException(java.io.IOException) Scriptable(org.mozilla.javascript.Scriptable)

Example 34 with Context

use of org.mozilla.javascript.Context in project OpenAM by OpenRock.

the class RhinoScriptEngine method evalCompiled.

/**
     * Evaluates a pre-compiled script against this script engine. This should only be called by
     * {@link org.forgerock.openam.scripting.factories.RhinoCompiledScript#eval(javax.script.ScriptContext)}.
     *
     * @param compiledScript The compiled script. Must not be null.
     * @param scriptContext The JSR 223 script context. Must not be null.
     * @return the result of evaluating the compiled script.
     * @throws ScriptException if an error occurs during script execution.
     */
Object evalCompiled(final Script compiledScript, final ScriptContext scriptContext) throws ScriptException {
    Reject.ifNull(compiledScript, scriptContext);
    Object result = null;
    final Context context = factory.getContext();
    try {
        final Scriptable scope = getScope(context, scriptContext);
        result = compiledScript.exec(context, scope);
    } catch (RhinoException ex) {
        throw convertException(ex);
    } finally {
        factory.releaseContext(context);
    }
    return result;
}
Also used : Context(org.mozilla.javascript.Context) ScriptContext(javax.script.ScriptContext) ScriptableObject(org.mozilla.javascript.ScriptableObject) RhinoException(org.mozilla.javascript.RhinoException) Scriptable(org.mozilla.javascript.Scriptable)

Example 35 with Context

use of org.mozilla.javascript.Context in project sling by apache.

the class RhinoJavaScriptEngineFactory method activate.

// ---------- SCR integration
@Activate
protected void activate(final ComponentContext context, final RhinoJavaScriptEngineFactoryConfiguration configuration) {
    Dictionary<?, ?> props = context.getProperties();
    boolean debugging = getProperty("org.apache.sling.scripting.javascript.debug", props, context.getBundleContext(), false);
    optimizationLevel = readOptimizationLevel(configuration);
    // setup the wrap factory
    wrapFactory = new SlingWrapFactory();
    // initialize the Rhino Context Factory
    SlingContextFactory.setup(this);
    Context cx = Context.enter();
    setEngineName(getEngineName() + " (" + cx.getImplementationVersion() + ")");
    languageVersion = String.valueOf(cx.getLanguageVersion());
    Context.exit();
    setExtensions(ECMA_SCRIPT_EXTENSION, ESP_SCRIPT_EXTENSION);
    setMimeTypes("text/javascript", "application/ecmascript", "application/javascript");
    setNames("javascript", ECMA_SCRIPT_EXTENSION, ESP_SCRIPT_EXTENSION);
    final ContextFactory contextFactory = ContextFactory.getGlobal();
    if (contextFactory instanceof SlingContextFactory) {
        ((SlingContextFactory) contextFactory).setDebugging(debugging);
    }
    // set the dynamic class loader as the application class loader
    final DynamicClassLoaderManager dclm = this.dynamicClassLoaderManager;
    if (dclm != null) {
        contextFactory.initApplicationClassLoader(dynamicClassLoaderManager.getDynamicClassLoader());
    }
    log.info("Activated with optimization level {}", optimizationLevel);
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) Context(org.mozilla.javascript.Context) BundleContext(org.osgi.framework.BundleContext) SlingContextFactory(org.apache.sling.scripting.javascript.helper.SlingContextFactory) ContextFactory(org.mozilla.javascript.ContextFactory) SlingContextFactory(org.apache.sling.scripting.javascript.helper.SlingContextFactory) SlingWrapFactory(org.apache.sling.scripting.javascript.helper.SlingWrapFactory) DynamicClassLoaderManager(org.apache.sling.commons.classloader.DynamicClassLoaderManager) Activate(org.osgi.service.component.annotations.Activate)

Aggregations

Context (org.mozilla.javascript.Context)153 Scriptable (org.mozilla.javascript.Scriptable)69 ScriptableObject (org.mozilla.javascript.ScriptableObject)63 ContextAction (org.mozilla.javascript.ContextAction)23 ContextFactory (org.mozilla.javascript.ContextFactory)22 Script (org.mozilla.javascript.Script)17 IOException (java.io.IOException)14 Function (org.mozilla.javascript.Function)13 ScriptContext (javax.script.ScriptContext)10 Test (org.junit.Test)8 RhinoException (org.mozilla.javascript.RhinoException)8 ArrayList (java.util.ArrayList)7 ContinuationPending (org.mozilla.javascript.ContinuationPending)7 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)7 InputStreamReader (java.io.InputStreamReader)6 Date (java.util.Date)6 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)6 JavaScriptException (org.mozilla.javascript.JavaScriptException)6 HashSet (java.util.HashSet)5 List (java.util.List)5