org.mozilla.javascript.ContextAction Java Examples

The following examples show how to use org.mozilla.javascript.ContextAction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PrimitiveTypeScopeResolutionTest.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
private void testWithTwoScopes(final String scriptScope1,
                               final String scriptScope2)
{
	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"));
	        cx.evaluateString(scope2, scriptScope2, "source2", 1, null);

	        scope1.put("scope2", scope1, scope2);

	        return cx.evaluateString(scope1, scriptScope1, "source1", 1,
	                                 null);
		}
	};
	Utils.runWithAllOptimizationLevels(action);
}
 
Example #2
Source File: JsDebugFrame.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
public VMVariable[] getVariables( )
{
	// TODO ensure current context
	return (VMVariable[]) Context.call( new ContextAction( ) {

		public Object run( Context arg0 )
		{
			try
			{
				return getVariablesImpl( arg0 );
			}
			catch ( Exception e )
			{
				StringWriter sw = new StringWriter( );
				e.printStackTrace( new PrintWriter( sw ) );

				return new VMVariable[]{
					new JsVariable( sw.toString( ),
							ERROR_LITERAL,
							EXCEPTION_TYPE )
				};
			}
		}

	} );
}
 
Example #3
Source File: JsValue.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
public VMVariable[] getMembers( )
{
	return (VMVariable[]) Context.call( new ContextAction( ) {

		public Object run( Context arg0 )
		{
			try
			{
				return getMembersImpl( arg0 );
			}
			catch ( Exception e )
			{
				StringWriter sw = new StringWriter( );
				e.printStackTrace( new PrintWriter( sw ) );

				return new VMVariable[]{
					new JsVariable( sw.toString( ),
							ERROR_LITERAL,
							EXCEPTION_TYPE )
				};
			}
		}

	} );
}
 
Example #4
Source File: StackTraceTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void runWithExpectedStackTrace(final String _source, final String _expectedStackTrace)
{
       final ContextAction action = new ContextAction() {
       	public Object run(Context cx) {
       		final Scriptable scope = cx.initStandardObjects();
       		try {
       			cx.evaluateString(scope, _source, "test.js", 0, null);
       		}
       		catch (final JavaScriptException e)
       		{
       			assertEquals(_expectedStackTrace, e.getScriptStackTrace());
       			return null;
       		}
       		throw new RuntimeException("Exception expected!");
       	}
       };
       Utils.runWithOptimizationLevel(action, -1);
}
 
Example #5
Source File: TypeOfTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #6
Source File: Bug496585Test.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void callOverloadedFunction() {
    new ContextFactory().call(new ContextAction() {
        public Object run(Context cx) {
            cx.getWrapFactory().setJavaPrimitiveWrap(false);
            Assert.assertEquals("string[]", cx.evaluateString(
                    cx.initStandardObjects(),
                    "new org.mozilla.javascript.tests.Bug496585Test().method('one', 'two', 'three')",
                    "<test>", 1, null));
            Assert.assertEquals("string+function", cx.evaluateString(
                cx.initStandardObjects(),
                "new org.mozilla.javascript.tests.Bug496585Test().method('one', function() {})",
                "<test>", 1, null));
            return null;
        }
    });
}
 
Example #7
Source File: ArrayConcatTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
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);
  }
 
Example #8
Source File: ApplyOnPrimitiveNumberTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example #9
Source File: DeletePropertyTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * delete should not delete anything in the prototype chain.
 */
@Test
public void testDeletePropInPrototype() throws Exception {
	final String script = "Array.prototype.foo = function() {};\n"
		+ "Array.prototype[1] = function() {};\n"
		+ "var t = [];\n"
		+ "[].foo();\n"
		+ "for (var i in t) delete t[i];\n"
		+ "[].foo();\n"
		+ "[][1]();\n";

	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);
			return null;
		}
	};

	Utils.runWithAllOptimizationLevels(action);
}
 
Example #10
Source File: PrimitiveTypeScopeResolutionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void testWithTwoScopes(final String scriptScope1,
                               final String scriptScope2)
{
	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"));
	        cx.evaluateString(scope2, scriptScope2, "source2", 1, null);

	        scope1.put("scope2", scope1, scope2);

	        return cx.evaluateString(scope1, scriptScope1, "source1", 1,
	                                 null);
		}
	};
	Utils.runWithAllOptimizationLevels(action);
}
 
Example #11
Source File: JSLintValidator.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
private Scriptable optionsAsJavaScriptObject(final Map<String, Object> options)
{
	return (Scriptable) contextFactory.call(new ContextAction()
	{
		public Object run(Context cx)
		{
			Scriptable opts = cx.newObject(scope);
			for (Map.Entry<String, Object> entry : options.entrySet())
			{
				String key = entry.getKey();
				Object value = javaToJS(entry.getValue(), opts);
				opts.put(key, opts, value);
			}
			return opts;
		}
	});
}
 
Example #12
Source File: StackTraceTest.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
private void runWithExpectedStackTrace(final String _source, final String _expectedStackTrace)
{
       final ContextAction action = new ContextAction() {
       	public Object run(Context cx) {
       		final Scriptable scope = cx.initStandardObjects();
       		try {
       			cx.evaluateString(scope, _source, "test.js", 0, null);
       		}
       		catch (final JavaScriptException e)
       		{
       			assertEquals(_expectedStackTrace, e.getScriptStackTrace());
       			return null;
       		}
       		throw new RuntimeException("Exception expected!");
       	}
       };
       Utils.runWithOptimizationLevel(action, -1);
}
 
Example #13
Source File: ArrayConcatTest.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
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);
  }
 
Example #14
Source File: Bug496585Test.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
@Test
public void callOverloadedFunction() {
    new AndroidContextFactory(new File(System.getProperty("java.io.tmpdir", "."), "classes")).call(new ContextAction() {
        public Object run(Context cx) {
            cx.getWrapFactory().setJavaPrimitiveWrap(false);
            Assert.assertEquals("string[]", cx.evaluateString(
                    cx.initStandardObjects(),
                    "new org.mozilla.javascript.tests.Bug496585Test().method('one', 'two', 'three')",
                    "<test>", 1, null));
            Assert.assertEquals("string+function", cx.evaluateString(
                cx.initStandardObjects(),
                "new org.mozilla.javascript.tests.Bug496585Test().method('one', function() {})",
                "<test>", 1, null));
            return null;
        }
    });
}
 
Example #15
Source File: DeletePropertyTest.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
/**
 * delete should not delete anything in the prototype chain.
 */
@Test
public void testDeletePropInPrototype() throws Exception {
	final String script = "Array.prototype.foo = function() {};\n"
		+ "Array.prototype[1] = function() {};\n"
		+ "var t = [];\n"
		+ "[].foo();\n"
		+ "for (var i in t) delete t[i];\n"
		+ "[].foo();\n"
		+ "[][1]();\n";

	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);
			return null;
		}
	};

	Utils.runWithAllOptimizationLevels(action);
}
 
Example #16
Source File: ApplyOnPrimitiveNumberTest.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #17
Source File: TypeOfTest.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #18
Source File: GeneratedClassNameTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void doTest(final String expectedName, final String scriptName)
    throws Exception
{
    final Script script = (Script) ContextFactory.getGlobal().call(
      new ContextAction() {
        public Object run(final Context context) {
          return context.compileString("var f = 1", scriptName, 1, null);
        }
      });

    // remove serial number
    String name = script.getClass().getSimpleName();
    assertEquals(expectedName, name.substring(0, name.lastIndexOf('_')));
}
 
Example #19
Source File: DecompileTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #20
Source File: StrictModeApiTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private Object runScript(final String scriptSourceText) {
  return this.contextFactory.call(new ContextAction() {
    public Object run(Context context) {
        return context.evaluateString(global, scriptSourceText,
                "test source", 1, null);
    }
  });
}
 
Example #21
Source File: DecompileTest.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #22
Source File: FunctionTest.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #23
Source File: TypeOfTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #24
Source File: TypeOfTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #25
Source File: TypeOfTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #26
Source File: StrictModeApiTest.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
private Object runScript(final String scriptSourceText) {
  return this.contextFactory.call(new ContextAction() {
    public Object run(Context context) {
        return context.evaluateString(global, scriptSourceText,
                "test source", 1, null);
    }
  });
}
 
Example #27
Source File: JavaAcessibilityTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private Object runScript(final String scriptSourceText) {
  return contextFactory.call(new ContextAction() {
    public Object run(Context context) {
      Script script = context.compileString(scriptSourceText, "", 1, null);
      return script.exec(context, global);
    }
  });
}
 
Example #28
Source File: NativeStringTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #29
Source File: Utils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Runs the action successively with all available optimization levels
 */
public static void runWithAllOptimizationLevels(final ContextAction action)
{
	runWithOptimizationLevel(action, -1);
	runWithOptimizationLevel(action, 0);
	runWithOptimizationLevel(action, 1);
}
 
Example #30
Source File: Utils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Runs the action successively with all available optimization levels
 */
public static void runWithAllOptimizationLevels(final ContextFactory contextFactory, final ContextAction action)
{
	runWithOptimizationLevel(contextFactory, action, -1);
	runWithOptimizationLevel(contextFactory, action, 0);
	runWithOptimizationLevel(contextFactory, action, 1);
}