Search in sources :

Example 21 with Context

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

the class ApplyOnPrimitiveNumberTest method testIt.

public void testIt() {
    final String script = "var fn = function() { return this; }\n" + "fn.apply(1)";
    final ContextAction action = new ContextAction() {

        public Object run(final Context _cx) {
            final ScriptableObject scope = _cx.initStandardObjects();
            final Object result = _cx.evaluateString(scope, script, "test script", 0, null);
            assertEquals("object", ScriptRuntime.typeof(result));
            assertEquals("1", Context.toString(result));
            return null;
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 22 with Context

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

the class ArrayConcatTest method testArrayConcat.

public void testArrayConcat() {
    final String script = "var a = ['a0', 'a1'];\n" + "a[3] = 'a3';\n" + "var b = ['b1', 'b2'];\n" + "b.concat(a)";
    final ContextAction action = new ContextAction() {

        public Object run(final Context _cx) {
            final ScriptableObject scope = _cx.initStandardObjects();
            final Object result = _cx.evaluateString(scope, script, "test script", 0, null);
            assertEquals("b1,b2,a0,a1,,a3", Context.toString(result));
            return null;
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 23 with Context

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

the class Bug421071Test method createGlobalScope.

private TopLevelScope createGlobalScope() {
    factory = new DynamicScopeContextFactory();
    Context context = factory.enterContext();
    // noinspection deprecation
    TopLevelScope globalScope = new TopLevelScope(context);
    Context.exit();
    return globalScope;
}
Also used : Context(org.mozilla.javascript.Context)

Example 24 with Context

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

the class Bug466207Test method setUp.

@SuppressWarnings("unchecked")
@Override
protected void setUp() {
    // set up a reference map
    reference = new ArrayList<Object>();
    reference.add("a");
    reference.add(Boolean.TRUE);
    reference.add(new HashMap<Object, Object>());
    reference.add(new Integer(42));
    reference.add("a");
    // get a js object as map
    Context context = Context.enter();
    ScriptableObject scope = context.initStandardObjects();
    list = (List<Object>) context.evaluateString(scope, "(['a', true, new java.util.HashMap(), 42, 'a']);", "testsrc", 1, null);
    Context.exit();
}
Also used : Context(org.mozilla.javascript.Context) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 25 with Context

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

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