Search in sources :

Example 11 with Context

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

the class TypeOfTest method test0.

/**
	 * ECMA 11.4.3 says that typeof on host object is Implementation-dependent
	 */
public void test0() throws Exception {
    final Function f = new BaseFunction() {

        @Override
        public Object call(Context _cx, Scriptable _scope, Scriptable _thisObj, Object[] _args) {
            return _args[0].getClass().getName();
        }
    };
    final ContextAction action = new ContextAction() {

        public Object run(final Context context) {
            final Scriptable scope = context.initStandardObjects();
            scope.put("myObj", scope, f);
            return context.evaluateString(scope, "typeof myObj", "test script", 1, null);
        }
    };
    doTest("function", action);
}
Also used : Context(org.mozilla.javascript.Context) BaseFunction(org.mozilla.javascript.BaseFunction) Function(org.mozilla.javascript.Function) BaseFunction(org.mozilla.javascript.BaseFunction) ContextAction(org.mozilla.javascript.ContextAction) Scriptable(org.mozilla.javascript.Scriptable)

Example 12 with Context

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

the class TypeOfTest method testCustomizeTypeOf.

private void testCustomizeTypeOf(final String expected, final Scriptable obj) {
    final ContextAction action = new ContextAction() {

        public Object run(final Context context) {
            final Scriptable scope = context.initStandardObjects();
            scope.put("myObj", scope, obj);
            return context.evaluateString(scope, "typeof myObj", "test script", 1, null);
        }
    };
    doTest(expected, action);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) Scriptable(org.mozilla.javascript.Scriptable)

Example 13 with Context

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

the class TypeOfTest method doTest.

private void doTest(String expected, final String script) {
    final ContextAction action = new ContextAction() {

        public Object run(final Context context) {
            final Scriptable scope = context.initStandardObjects();
            return context.evaluateString(scope, script, "test script", 1, null);
        }
    };
    doTest(expected, action);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) Scriptable(org.mozilla.javascript.Scriptable)

Example 14 with Context

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

the class TypeOfTest method doTest.

private void doTest(final int optimizationLevel, final String expected, final ContextAction action) {
    Object o = new ContextFactory().call(new ContextAction() {

        public Object run(final Context context) {
            context.setOptimizationLevel(optimizationLevel);
            return Context.toString(action.run(context));
        }
    });
    assertEquals(expected, o);
}
Also used : ContextFactory(org.mozilla.javascript.ContextFactory) Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 15 with Context

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

the class Utils method executeScript.

/**
	 * Execute the provided script in a fresh context as "myScript.js".
	 * @param script the script code
	 */
static void executeScript(final String script, final int optimizationLevel) {
    final ContextAction action = new ContextAction() {

        public Object run(Context cx) {
            final Scriptable scope = cx.initStandardObjects();
            return cx.evaluateString(scope, script, "myScript.js", 1, null);
        }
    };
    Utils.runWithOptimizationLevel(action, optimizationLevel);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) 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