org.mozilla.javascript.Callable Java Examples

The following examples show how to use org.mozilla.javascript.Callable. 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: ScriptableQNameMap.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * @see org.mozilla.javascript.Scriptable#get(java.lang.String, org.mozilla.javascript.Scriptable)
 */
public Object get(String name, Scriptable start)
{
    // get the property from the underlying QName map
    if ("length".equals(name))
    {
        return this.size();
    }
    else if ("hasOwnProperty".equals(name))
    {
        return new Callable()
        {
            @Override
            public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args)
            {
                return (args.length > 0 ? hasOwnProperty(args[0]) : null);
            }
        };
    }
    else
    {
        return get(name);
    }
}
 
Example #2
Source File: ScriptableHashMap.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * @see org.mozilla.javascript.Scriptable#get(java.lang.String, org.mozilla.javascript.Scriptable)
 */
public Object get(String name, Scriptable start)
{
    // get the property from the underlying QName map
    if ("length".equals(name))
    {
        return this.size();
    }
    else if ("hasOwnProperty".equals(name))
    {
        return new Callable()
        {
            @Override
            public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args)
            {
                return (args.length > 0 ? hasOwnProperty(args[0]) : null);
            }
        };
    }
    else
    {
        return get(name);
    }
}
 
Example #3
Source File: Bug482203Test.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
public void testJsApi() throws Exception {
    Context cx = RhinoAndroidHelper.prepareContext();
    try {
        cx.setOptimizationLevel(-1);
        Script script = cx.compileString(TestUtils.readAsset("Bug482203.js"),
                "", 1, null);
        Scriptable scope = cx.initStandardObjects();
        script.exec(cx, scope);
        int counter = 0;
        for(;;)
        {
            Object cont = ScriptableObject.getProperty(scope, "c");
            if(cont == null)
            {
                break;
            }
            counter++;
            ((Callable)cont).call(cx, scope, scope, new Object[] { null });
        }
        assertEquals(counter, 5);
        assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result"));
    } finally {
        Context.exit();
    }
}
 
Example #4
Source File: Bug482203Test.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testJsApi() throws Exception {
    Context cx = Context.enter();
    try {
        cx.setOptimizationLevel(-1);
        Script script = cx.compileReader(new InputStreamReader(
                Bug482203Test.class.getResourceAsStream("Bug482203.js")),
                "", 1, null);
        Scriptable scope = cx.initStandardObjects();
        script.exec(cx, scope);
        int counter = 0;
        for(;;)
        {
            Object cont = ScriptableObject.getProperty(scope, "c");
            if(cont == null)
            {
                break;
            }
            counter++;
            ((Callable)cont).call(cx, scope, scope, new Object[] { null });
        }
        assertEquals(counter, 5);
        assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result"));
    } finally {
        Context.exit();
    }
}
 
Example #5
Source File: SandboxingContextFactory.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected Object doTopCall(final Callable callable, final Context cx, final Scriptable scope,
        final Scriptable thisObj, final Object[] args) {
    final StartTimeAwareContext mcx = (StartTimeAwareContext) cx;
    mcx.startTime = System.currentTimeMillis();

    return super.doTopCall(callable, cx, scope, thisObj, args);
}
 
Example #6
Source File: ObserveInstructionCountTest.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
@Override
protected Object doTopCall(Callable callable,
                           Context cx, Scriptable scope,
                           Scriptable thisObj, Object[] args)
{
    MyContext mcx = (MyContext)cx;
    mcx.quota = 2000;
    return super.doTopCall(callable, cx, scope, thisObj, args);
}
 
Example #7
Source File: DcJsContextFactory.java    From io with Apache License 2.0 5 votes vote down vote up
@Override
protected final Object doTopCall(
        final Callable callable,
        final Context cx,
        final Scriptable scope,
        final Scriptable thisObj,
        final Object[] args) {
    long curTime = System.currentTimeMillis();
    ((DcJsContext) cx).setTimeout(curTime + TIMEOUTVALUE);
    return super.doTopCall(callable, cx, scope, thisObj, args);
}
 
Example #8
Source File: ObserveInstructionCountTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Object doTopCall(Callable callable,
                           Context cx, Scriptable scope,
                           Scriptable thisObj, Object[] args)
{
    MyContext mcx = (MyContext)cx;
    mcx.quota = 2000;
    return super.doTopCall(callable, cx, scope, thisObj, args);
}
 
Example #9
Source File: ScriptableParameter.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
public Object get( String name, Scriptable scope )
{
	ParameterAttribute parameter = getParameterAttribute( name );
	if ( FIELD_VALUE.equals( name ) )
	{
		return parameter.getValue( );
	}
	else if ( FIELD_DISPLAY_TEXT.equals( name ) )
	{
		return parameter.getDisplayText( );
	}
	Object value = parameter.getValue( );
	Scriptable jsValue = Context.toObject( value, scope );
	Scriptable prototype = jsValue.getPrototype( );
	if( prototype != null )
	{
		Object property = jsValue.getPrototype( ).get( name, jsValue );
		if ( property instanceof Callable )
		{
			Callable callable = (Callable) property;
			return new JsValueCallable( callable, jsValue );
		}
		return jsValue.get( name, jsValue );
	}
	else
	{
		return jsValue.get( name, jsValue );
	}
}
 
Example #10
Source File: SecureScriptContextFactory.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
protected Object doTopCall(Callable callable, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
    SecureScriptContext mcx = (SecureScriptContext) cx;
    mcx.setStartTime(System.currentTimeMillis());
    return super.doTopCall(callable, cx, scope, thisObj, args);
}
 
Example #11
Source File: JavaScriptCallableThisDecorator.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
public JavaScriptCallableThisDecorator(Callable decorated,
		Scriptable thisTarget)
{
	this.decorated = decorated;
	this.thisTarget = thisTarget;
}
 
Example #12
Source File: SecureScriptContextFactory.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
protected Object doTopCall(Callable callable, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
    SecureScriptContext mcx = (SecureScriptContext) cx;
    mcx.setStartTime(System.currentTimeMillis());
    return super.doTopCall(callable, cx, scope, thisObj, args);
}
 
Example #13
Source File: ReportParameter.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
public JsValueCallable( Callable callable)
{
	this.impl = callable;
}
 
Example #14
Source File: JsDebugFrame.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
public synchronized VMValue evaluate( String expression )
{
	int currentState = debugger.currentState( );

	if ( currentState == VM_TERMINATED )
	{
		return null;
	}

	JsValue result = null;
	Debugger oldDebugger = cx.getDebugger( );
	Object oldContextData = cx.getDebuggerContextData( );
	int oldLevel = cx.getOptimizationLevel( );

	cx.setDebugger( null, null );
	cx.setOptimizationLevel( -1 );
	cx.setGeneratingDebug( false );

	try
	{
		Callable script = (Callable) cx.compileString( expression,
				EVALUATOR_LITERAL,
				0,
				null );
		Object val = script.call( cx,
				scope,
				thisObj,
				ScriptRuntime.emptyArgs );

		if ( val == Undefined.instance )
		{
			result = new JsValue( UNDEFINED_LITERAL, UNDEFINED_TYPE );
		}
		else
		{
			result = new JsValue( val );
		}
	}
	catch ( Exception ex )
	{
		result = new JsValue( ex.getMessage( ), EXCEPTION_TYPE );
	}
	finally
	{
		cx.setGeneratingDebug( true );
		cx.setOptimizationLevel( oldLevel );
		cx.setDebugger( oldDebugger, oldContextData );
	}

	return result;
}
 
Example #15
Source File: ScriptableParameter.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
public JsValueCallable( Callable callable, Scriptable value)
{
	this.impl = callable;
	this.value = value;
}