Search in sources :

Example 6 with Scriptable

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

the class NativeStringTest method assertEvaluates.

private void assertEvaluates(final Object expected, final String source) {
    final ContextAction action = new ContextAction() {

        public Object run(Context cx) {
            final Scriptable scope = cx.initStandardObjects();
            final Object rep = cx.evaluateString(scope, source, "test.js", 0, null);
            assertEquals(expected, rep);
            return null;
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) Scriptable(org.mozilla.javascript.Scriptable)

Example 7 with Scriptable

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

the class ObserveInstructionCountTest method baseCase.

private void baseCase(int optimizationLevel, String source) {
    ContextFactory factory = new MyFactory();
    Context cx = factory.enterContext();
    cx.setOptimizationLevel(optimizationLevel);
    assertTrue(cx instanceof MyContext);
    try {
        Scriptable globalScope = cx.initStandardObjects();
        cx.evaluateString(globalScope, source, "test source", 1, null);
        fail();
    } catch (QuotaExceeded e) {
    // expected
    } catch (RuntimeException e) {
        fail(e.toString());
    } finally {
        Context.exit();
    }
}
Also used : ContextFactory(org.mozilla.javascript.ContextFactory) Context(org.mozilla.javascript.Context) Scriptable(org.mozilla.javascript.Scriptable)

Example 8 with Scriptable

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

the class PrimitiveTypeScopeResolutionTest method functionObjectPrimitiveToObject.

/**
   * Test that FunctionObject use the right top scope to convert a primitive 
   * to an object
   */
@Test
public void functionObjectPrimitiveToObject() throws Exception {
    final String scriptScope2 = "function f() {\n" + "String.prototype.foo = 'from 2'; \n" + "var s2 = 's2';\n" + "var s2Foo = s2.foo;\n" + "var s2FooReadByFunction = myObject.readPropFoo(s2);\n" + "if (s2Foo != s2FooReadByFunction)\n" + "throw 's2 got: ' + s2FooReadByFunction;\n" + "}";
    // define object with custom method
    final MyObject myObject = new MyObject();
    final String[] functionNames = { "readPropFoo" };
    myObject.defineFunctionProperties(functionNames, MyObject.class, ScriptableObject.EMPTY);
    final String scriptScope1 = "String.prototype.foo = 'from 1'; scope2.f()";
    final ContextAction action = new ContextAction() {

        public Object run(final Context cx) {
            final Scriptable scope1 = cx.initStandardObjects(new MySimpleScriptableObject("scope1"));
            final Scriptable scope2 = cx.initStandardObjects(new MySimpleScriptableObject("scope2"));
            scope2.put("myObject", scope2, myObject);
            cx.evaluateString(scope2, scriptScope2, "source2", 1, null);
            scope1.put("scope2", scope1, scope2);
            return cx.evaluateString(scope1, scriptScope1, "source1", 1, null);
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(org.mozilla.javascript.Context) ContextAction(org.mozilla.javascript.ContextAction) Scriptable(org.mozilla.javascript.Scriptable) Test(org.junit.Test)

Example 9 with Scriptable

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

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

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