Search in sources :

Example 16 with Scriptable

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

the class Bug467396Test method testOverloadedVarargs.

public void testOverloadedVarargs() {
    Context cx = ContextFactory.getGlobal().enterContext();
    try {
        Scriptable scope = cx.initStandardObjects();
        Object result = unwrap(cx.evaluateString(scope, "java.lang.reflect.Array.newInstance(java.lang.Object, 1)", "source", 1, null));
        assertTrue(result instanceof Object[]);
        assertEquals(1, ((Object[]) result).length);
        result = unwrap(cx.evaluateString(scope, "java.lang.reflect.Array.newInstance(java.lang.Object, [1])", "source", 1, null));
        assertTrue(result instanceof Object[]);
        assertEquals(1, ((Object[]) result).length);
        result = unwrap(cx.evaluateString(scope, "java.lang.reflect.Array.newInstance(java.lang.Object, [1, 1])", "source", 1, null));
        assertTrue(result instanceof Object[][]);
        assertEquals(1, ((Object[][]) result).length);
        assertEquals(1, ((Object[][]) result)[0].length);
        result = unwrap(cx.evaluateString(scope, "java.lang.reflect.Array.newInstance(java.lang.Object, 1, 1)", "source", 1, null));
        assertTrue(result instanceof Object[][]);
        assertEquals(1, ((Object[][]) result).length);
        assertEquals(1, ((Object[][]) result)[0].length);
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Scriptable(org.mozilla.javascript.Scriptable)

Example 17 with Scriptable

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

the class Bug482203 method testJsApi.

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 });
    }
}
Also used : Context(org.mozilla.javascript.Context) Script(org.mozilla.javascript.Script) InputStreamReader(java.io.InputStreamReader) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) Callable(org.mozilla.javascript.Callable)

Example 18 with Scriptable

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

the class ContextFactoryTest method testCustomContextFactory.

public void testCustomContextFactory() {
    ContextFactory factory = new MyFactory();
    Context cx = factory.enterContext();
    try {
        Scriptable globalScope = cx.initStandardObjects();
    // Test that FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME is enabled
    /* TODO(stevey): fix this functionality in parser
            Object result = cx.evaluateString(globalScope,
                    "var obj = {};" +
                    "function obj.foo() { return 'bar'; }" +
                    "obj.foo();",
                    "test source", 1, null);
            assertEquals("bar", result);
            */
    } catch (RhinoException e) {
        fail(e.toString());
    } finally {
        Context.exit();
    }
}
Also used : ContextFactory(org.mozilla.javascript.ContextFactory) Context(org.mozilla.javascript.Context) RhinoException(org.mozilla.javascript.RhinoException) Scriptable(org.mozilla.javascript.Scriptable)

Example 19 with Scriptable

use of org.mozilla.javascript.Scriptable 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 20 with Scriptable

use of org.mozilla.javascript.Scriptable 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)

Aggregations

Scriptable (org.mozilla.javascript.Scriptable)151 Context (org.mozilla.javascript.Context)67 ScriptableObject (org.mozilla.javascript.ScriptableObject)47 Test (org.junit.Test)17 ContextAction (org.mozilla.javascript.ContextAction)12 Script (org.mozilla.javascript.Script)10 Function (org.mozilla.javascript.Function)9 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)9 ArrayList (java.util.ArrayList)8 NativeObject (org.mozilla.javascript.NativeObject)8 Date (java.util.Date)7 RhinoException (org.mozilla.javascript.RhinoException)7 IOException (java.io.IOException)6 Map (java.util.Map)6 BProgram (il.ac.bgu.cs.bp.bpjs.model.BProgram)5 Notifier (org.apache.cxf.javascript.JavascriptTestUtilities.Notifier)5 KettleException (org.pentaho.di.core.exception.KettleException)5 File (java.io.File)4 InputStreamReader (java.io.InputStreamReader)4 EvaluatorException (org.mozilla.javascript.EvaluatorException)4