Java Code Examples for org.mozilla.javascript.ScriptableObject#getProperty()

The following examples show how to use org.mozilla.javascript.ScriptableObject#getProperty() . 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: Bug482203Test.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testJavaApi() 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();
        cx.executeScriptWithContinuations(script, scope);
        int counter = 0;
        for(;;)
        {
            Object cont = ScriptableObject.getProperty(scope, "c");
            if(cont == null)
            {
                break;
            }
            counter++;
            cx.resumeContinuation(cont, scope, null);
        }
        assertEquals(counter, 5);
        assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result"));
    } finally {
    	Context.exit();
    }
}
 
Example 2
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 3
Source File: EcmaScriptDataModel.java    From JVoiceXML with GNU Lesser General Public License v2.1 6 votes vote down vote up
private <T> T readArray(final String arrayName, final int position,
        final Scope scope, final Scriptable start, final Class<T> type)
        throws SemanticError {
    if (start == null) {
        throw new SemanticError("no scope '" + scope
                + "' present to read '" + arrayName + "'");
    }
    final Scriptable targetScope = getFullScope(topmostScope, arrayName);
    if (targetScope == null) {
        throw new SemanticError("'" + arrayName + "' not found");
    }
    if (!ScriptableObject.hasProperty(targetScope, position)) {
        throw new SemanticError("'" + arrayName + "' has no position "
                + position);
    }
    final Object value = ScriptableObject
            .getProperty(targetScope, position);
    if (value == getUndefinedValue()) {
        return null;
    }
    @SuppressWarnings("unchecked")
    final T t = (T) Context.jsToJava(value, type);
    return t;
}
 
Example 4
Source File: EcmaScriptDataModel.java    From JVoiceXML with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Retrieves the scope identified by the variable's full name.
 * @param scope the scope to start searching
 * @param name the fully qualified name of a variable
 * @return found scope, {@code null} if no such scope exists
 * @since 0.7.7
 */
private Scriptable getScope(final Scriptable scope, final String name) {
    int dotPos = name.indexOf('.');
    if (dotPos >= 0) {
        final String prefix = name.substring(0, dotPos);
        final String suffix = name.substring(dotPos + 1);
        final Object value;
        if (ScriptableObject.hasProperty(scope, prefix)) {
            value = ScriptableObject.getProperty(scope, prefix);
        } else {
            return null;
        }
        if (value instanceof Scriptable) {
            final Scriptable subscope = (Scriptable) value;
            return getScope(subscope, suffix);
        } else {
            return null;
        }
    } else {
        return scope;
    }
}
 
Example 5
Source File: Bug482203Test.java    From rhino-android with Apache License 2.0 6 votes vote down vote up
public void testJavaApi() throws Exception {
    Context cx = RhinoAndroidHelper.prepareContext();
    try {
        cx.setOptimizationLevel(-1);
        Script script = cx.compileString(TestUtils.readAsset("Bug482203.js"),
                "", 1, null);
        Scriptable scope = cx.initStandardObjects();
        cx.executeScriptWithContinuations(script, scope);
        int counter = 0;
        for(;;)
        {
            Object cont = ScriptableObject.getProperty(scope, "c");
            if(cont == null)
            {
                break;
            }
            counter++;
            cx.resumeContinuation(cont, scope, null);
        }
        assertEquals(counter, 5);
        assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result"));
    } finally {
    	Context.exit();
    }
}
 
Example 6
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 7
Source File: JsDebugFrame.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private VMVariable[] getVariablesImpl( Context cx )
{
	List vars = new ArrayList( );

	if ( thisObj != null )
	{
		vars.add( new JsVariable( thisObj, "this" ) ); //$NON-NLS-1$
	}

	if ( scope != null && script != null )
	{
		for ( int i = 0; i < script.getParamAndVarCount( ); i++ )
		{
			String name = script.getParamOrVarName( i );

			Object val = ScriptableObject.getProperty( scope, name );

			if ( JsValue.isValidJsValue( val ) )
			{
				vars.add( new JsVariable( val, name ) );
			}
		}
	}

	if ( vars.size( ) == 0 )
	{
		return NO_VARS;
	}

	Collections.sort( vars );

	return (VMVariable[]) vars.toArray( new VMVariable[vars.size( )] );
}
 
Example 8
Source File: EcmaScriptDataModel.java    From JVoiceXML with GNU Lesser General Public License v2.1 5 votes vote down vote up
private <T extends Object> T readVariable(String variableName, Scope scope,
        final Scriptable start, final Class<T> type) throws SemanticError {
    if (start == null) {
        throw new SemanticError("no scope '" + scope
                + "' present to read '" + variableName + "'");
    }
    final Scriptable subcope = getScope(topmostScope, variableName);
    if (subcope == null) {
        throw new SemanticError("'" + variableName + "' not found");
    }
    final String property;
    int dotPos = variableName.lastIndexOf('.');
    if (dotPos >= 0) {
        property = variableName.substring(dotPos + 1);
    } else {
        property = variableName;
    }
    if (!ScriptableObject.hasProperty(subcope, property)) {
        throw new SemanticError("'" + variableName + "' not found");
    }
    final Object value = ScriptableObject.getProperty(subcope, property);
    if (value == getUndefinedValue()) {
        return null;
    }
    @SuppressWarnings("unchecked")
    final T t = (T) Context.jsToJava(value, type);
    return t;
}
 
Example 9
Source File: CaliperObjectBenchmark.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
@Benchmark
@SuppressWarnings("unused")
void deleteFields(int count)
{
    Function create = (Function)ScriptableObject.getProperty(scope, "createObject");
    Object o = create.call(cx, scope, null, new Object[]{1, strings, ints});
    Function delete = (Function)ScriptableObject.getProperty(scope, "deleteObject");
    delete.call(cx, scope, null, new Object[] {count, o, strings, ints});
}
 
Example 10
Source File: CaliperObjectBenchmark.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
@Benchmark
@SuppressWarnings("unused")
void ownKeysFields(long count)
{
    Function create = (Function)ScriptableObject.getProperty(scope, "createObject");
    Object o = create.call(cx, scope, null, new Object[]{1, strings, ints});
    Function iterate = (Function)ScriptableObject.getProperty(scope, "iterateOwnKeysObject");
    iterate.call(cx, scope, null, new Object[] {count, o});
}
 
Example 11
Source File: CaliperObjectBenchmark.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
@Benchmark
@SuppressWarnings("unused")
void iterateFields(long count)
{
    Function create = (Function)ScriptableObject.getProperty(scope, "createObject");
    Object o = create.call(cx, scope, null, new Object[]{1, strings, ints});
    Function iterate = (Function)ScriptableObject.getProperty(scope, "iterateObject");
    iterate.call(cx, scope, null, new Object[] {count, o});
}
 
Example 12
Source File: CaliperObjectBenchmark.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
@Benchmark
@SuppressWarnings("unused")
void accessFields(int count)
{
    Function create = (Function)ScriptableObject.getProperty(scope, "createObject");
    Object o = create.call(cx, scope, null, new Object[]{1, strings, ints});
    Function access = (Function)ScriptableObject.getProperty(scope, "accessObject");
    access.call(cx, scope, null, new Object[] {count, o, strings, ints});
}
 
Example 13
Source File: CaliperObjectBenchmark.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
@Benchmark
@SuppressWarnings("unused")
void createFields(int count)
{
    Function create = (Function)ScriptableObject.getProperty(scope, "createObject");
    create.call(cx, scope, null, new Object[]{count, strings, ints});
}
 
Example 14
Source File: PrimitiveTypeScopeResolutionTest.java    From rhino-android with Apache License 2.0 4 votes vote down vote up
public Object readPropFoo(final Scriptable s) {
    return ScriptableObject.getProperty(s, "foo");
}
 
Example 15
Source File: EcmaScriptDataModel.java    From JVoiceXML with GNU Lesser General Public License v2.1 4 votes vote down vote up
private int updateArray(final String variableName, final int position,
        final Object newValue, final Scope scope, final Scriptable start) {
    if (start == null) {
        return ERROR_SCOPE_NOT_FOUND;
    }
    final Scriptable subscope = getAndCreateScope(start, variableName);
    final String property;
    int dotPos = variableName.lastIndexOf('.');
    if (dotPos >= 0) {
        property = variableName.substring(dotPos + 1);
    } else {
        property = variableName;
    }
    if (!ScriptableObject.hasProperty(subscope, property)) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("'" + variableName + "' not found");
        }
        return ERROR_VARIABLE_NOT_FOUND;
    }
    final Object jsValue = Context.javaToJS(newValue, subscope);
    final NativeArray array = (NativeArray) ScriptableObject.getProperty(
            subscope, property);
    if (!ScriptableObject.hasProperty(array, position)) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("'" + variableName + "' has no position "
                    + position);
        }
        return ERROR_VARIABLE_NOT_FOUND;
    }
    ScriptableObject.putProperty(array, position, jsValue);
    if (LOGGER.isDebugEnabled()) {
        final String json = toString(jsValue);
        if (scope == null) {
            LOGGER.debug("set '" + variableName + "[" + position
                    + "]' to '" + json + "'");
        } else {
            LOGGER.debug("set '" + variableName + "[" + position
                    + "]' to '" + json + "' in scope '" + scope + "'");
        }
    }
    return 0;
}
 
Example 16
Source File: PrimitiveTypeScopeResolutionTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public Object readPropFoo(final Scriptable s) {
    return ScriptableObject.getProperty(s, "foo");
}
 
Example 17
Source File: JsRuntime.java    From manifold with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unused")
public static Object getStaticProp( ScriptableObject scope, String className, String prop )
{
  return ScriptableObject.getProperty( (Scriptable)scope.get( className, scope ), prop );
}
 
Example 18
Source File: JsValue.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
private VMVariable[] getMembersImpl( Context cx )
{
	if ( reservedValueType != null )
	{
		return NO_CHILD;
	}

	Object valObj = value;

	if ( value instanceof NativeJavaObject )
	{
		valObj = ( (NativeJavaObject) value ).unwrap( );
	}

	if ( valObj == null || valObj.getClass( ).isPrimitive( ) || isPrimitive )
	{
		return NO_CHILD;
	}

	List children = new ArrayList( );

	if ( valObj.getClass( ).isArray( ) )
	{
		int len = Array.getLength( valObj );

		boolean primitive = valObj.getClass( )
				.getComponentType( )
				.isPrimitive( );

		for ( int i = 0; i < len; i++ )
		{
			Object aobj = Array.get( valObj, i );

			if ( isValidJsValue( aobj ) )
			{
				children.add( new JsVariable( aobj, "[" //$NON-NLS-1$
						+ children.size( )
						+ "]", primitive ) ); //$NON-NLS-1$
			}
		}
	}
	else if ( valObj instanceof Scriptable )
	{
		Object[] ids;

		if ( valObj instanceof DebuggableObject )
		{
			ids = ( (DebuggableObject) valObj ).getAllIds( );
		}
		else
		{
			ids = ( (Scriptable) valObj ).getIds( );
		}

		if ( ids == null || ids.length == 0 )
		{
			return NO_CHILD;
		}

		for ( int i = 0; i < ids.length; i++ )
		{
			if ( ids[i] instanceof String )
			{
				Object val = ScriptableObject.getProperty( (Scriptable) valObj,
						(String) ids[i] );

				if ( val instanceof NativeJavaObject )
				{
					val = ( (NativeJavaObject) val ).unwrap( );
				}

				if ( isValidJsValue( val ) )
				{
					children.add( new JsVariable( val, (String) ids[i] ) );
				}
			}
		}
	}
	else
	{
		// refelct native java objects
		reflectMembers( valObj, children );
	}

	if ( children.size( ) == 0 )
	{
		return NO_CHILD;
	}

	Collections.sort( children );

	return (VMVariable[]) children.toArray( new VMVariable[children.size( )] );
}
 
Example 19
Source File: JsRuntime.java    From manifold with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unused")
public static Object getProp( ScriptableObject scope, String prop )
{
  return ScriptableObject.getProperty( scope, prop );
}