org.mozilla.javascript.BaseFunction Java Examples

The following examples show how to use org.mozilla.javascript.BaseFunction. 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: 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 #2
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 #3
Source File: StethoInitializer.java    From droidconat-2016 with Apache License 2.0 5 votes vote down vote up
private InspectorModulesProvider createWebkitModulesProvider() {
    return () -> new Stetho.DefaultInspectorModulesBuilder(context).runtimeRepl(
            new JsRuntimeReplFactoryBuilder(context)
                    .addFunction("activity", new BaseFunction() {
                        @Override
                        public Object call(org.mozilla.javascript.Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                            return activityProvider.getCurrentActivity();
                        }
                    }).build()
    ).finish();
}
 
Example #4
Source File: ReportDesign.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
private void initFunctions( )
{
	Method[] tmpMethods = this.getClass( ).getDeclaredMethods( );
	HashMap<String, Method> methods = new LinkedHashMap<String, Method>( );
	for ( int i = 0; i < tmpMethods.length; i++ )
	{
		Method tmpMethod = tmpMethods[i];
		String methodName = tmpMethod.getName( );
		// must handle special case with long parameter or polymiorphism
		if ( "getReportElementByID".equals( methodName ) //$NON-NLS-1$
				|| "setUserProperty".equals( methodName ) ) //$NON-NLS-1$
			continue;
		if ( ( tmpMethod.getModifiers( ) & Modifier.PUBLIC ) != 0 )
			methods.put( methodName, tmpMethod );
	}

	Context.enter( );
	try
	{
		for ( final Entry<String, Method> entry : methods.entrySet( ) )
		{
			this.defineProperty( entry.getKey( ), new BaseFunction( ) {

				private static final long serialVersionUID = 1L;

				public Object call( Context cx, Scriptable scope,
						Scriptable thisObj, Object[] args )
				{
					Object[] convertedArgs = JavascriptEvalUtil
							.convertToJavaObjects( args );
					try
					{
						Method method = entry.getValue( );
						return method.invoke( ReportDesign.this,
								convertedArgs );
					}
					catch ( Exception e )
					{
						throw new WrappedException( e );
					}
				}

			}, DONTENUM );
		}
	}
	finally
	{
		Context.exit( );
	}

	this.defineProperty( "getReportElementByID", //$NON-NLS-1$
			new Function_getReportElementByID( ), DONTENUM );
	this.defineProperty( "setUserProperty", //$NON-NLS-1$
			new Function_setUserProperty( ), DONTENUM );
}