Java Code Examples for org.mozilla.javascript.Context#toObject()

The following examples show how to use org.mozilla.javascript.Context#toObject() . 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: JavaScriptEngine.java    From commons-bsf with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize the engine. 
 * Put the manager into the context-manager
 * map hashtable too.
 */
public void initialize(BSFManager mgr, String lang, Vector declaredBeans)
    throws BSFException {
    
    super.initialize(mgr, lang, declaredBeans);

    // Initialize context and global scope object
    try {
        Context cx = Context.enter();
        global = new ImporterTopLevel(cx);
        Scriptable bsf = Context.toObject(new BSFFunctions(mgr, this), global);
        global.put("bsf", global, bsf);

        for(Iterator it = declaredBeans.iterator(); it.hasNext();) {
            declareBean((BSFDeclaredBean) it.next());
        }
    } 
    catch (Throwable t) {

    } 
    finally {
        Context.exit();
    }
}
 
Example 2
Source File: ReportParameter.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
public boolean has( String name, Scriptable scope )
{
	if ( FIELD_VALUE.equals( name ) || FIELD_DISPLAY_TEXT.equals( name ) )
	{
		return true;
	}
	if ( jsStrPrototype == null )
	{
		jsStrPrototype = Context.toObject( "", scope );
		if ( jsStrPrototype != null )
		{
			jsStrPrototype = jsStrPrototype.getPrototype( );
		}
	}
	return jsStrPrototype != null && jsStrPrototype.has( name, scope );
}
 
Example 3
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 4
Source File: JavaScriptUtilsTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
public void jsToBigNumber_NativeJavaObject_BigDecimal() throws Exception {
  Value value = new Value();
  value.setValue( BigDecimal.ONE );
  Scriptable object = Context.toObject( value, scope );
  assertEquals( 1.0, JavaScriptUtils.jsToBigNumber( object, JAVA_OBJECT ).doubleValue(), 1e-6 );
}
 
Example 5
Source File: JavaScriptEngine.java    From commons-bsf with Apache License 2.0 5 votes vote down vote up
public void declareBean(BSFDeclaredBean bean) throws BSFException {
    if ((bean.bean instanceof Number) ||
        (bean.bean instanceof String) ||
        (bean.bean instanceof Boolean)) {
        global.put(bean.name, global, bean.bean);
    } 
    else {
        // Must wrap non-scriptable objects before presenting to Rhino
        Scriptable wrapped = Context.toObject(bean.bean, global);
        global.put(bean.name, global, wrapped);
    }
}
 
Example 6
Source File: ScriptableParameter.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
public boolean has( String name, Scriptable scope )
{
	if ( FIELD_VALUE.equals( name ) || FIELD_DISPLAY_TEXT.equals( name ) )
	{
		return true;
	}
	ParameterAttribute parameter = getParameterAttribute( name );
	Scriptable jsValue = Context.toObject( parameter.getValue( ), scope );
	if ( jsValue != null )
	{
		return jsValue.has( name, scope );
	}
	return false;
}
 
Example 7
Source File: JavaScriptUtilsTest.java    From hop with Apache License 2.0 5 votes vote down vote up
@Test
public void jsToString_NativeJavaObject_Double() throws Exception {
  // TODO: return "1.0" in previous release with org.apache.hop.compatibility.Value
  assertEquals( "1", JavaScriptUtils.jsToString( getDoubleValue(), JAVA_OBJECT ) );

  Scriptable value = Context.toObject( new Double( 1.23 ), scope );
  assertEquals( "1.23", JavaScriptUtils.jsToString( value, JAVA_OBJECT ) );
}
 
Example 8
Source File: JavaScriptUtilsTest.java    From hop with Apache License 2.0 4 votes vote down vote up
@Test
public void jsToBigNumber_NativeJavaObject_BigDecimal() throws Exception {
  Scriptable object = Context.toObject( BigDecimal.ONE, scope );
  assertEquals( 1.0, JavaScriptUtils.jsToBigNumber( object, JAVA_OBJECT ).doubleValue(), 1e-6 );
}
 
Example 9
Source File: JavaScriptUtilsTest.java    From hop with Apache License 2.0 4 votes vote down vote up
@Test
public void jsToBigNumber_NativeNumber() throws Exception {
  Scriptable value = Context.toObject( 1.0, scope );
  BigDecimal number = JavaScriptUtils.jsToBigNumber( value, NATIVE_NUMBER );
  assertEquals( 1.0, number.doubleValue(), 1e-6 );
}
 
Example 10
Source File: JavaScriptUtilsTest.java    From hop with Apache License 2.0 4 votes vote down vote up
private static Scriptable getIntValue() {
  return Context.toObject( new Long( 1 ), scope );
}
 
Example 11
Source File: JavaScriptUtilsTest.java    From hop with Apache License 2.0 4 votes vote down vote up
@Test
public void jsToDate_NativeJavaObject() throws Exception {
  Scriptable value = Context.toObject( new Date( 1 ), scope );
  assertEquals( new Date( 1 ), JavaScriptUtils.jsToDate( value, JAVA_OBJECT ) );
}
 
Example 12
Source File: JavaScriptUtilsTest.java    From hop with Apache License 2.0 4 votes vote down vote up
@Test
public void jsToNumber_NativeNumber() throws Exception {
  Scriptable value = Context.toObject( 1.0, scope );
  Number number = JavaScriptUtils.jsToNumber( value, NATIVE_NUMBER );
  assertEquals( 1.0, number.doubleValue(), 1e-6 );
}
 
Example 13
Source File: JavaScriptUtilsTest.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
private static Scriptable getIntValue() {
  Value value = new Value();
  value.setValue( 1 );
  return Context.toObject( value, scope );
}
 
Example 14
Source File: JavaScriptUtilsTest.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
private static Scriptable getDoubleValue() {
  Value value = new Value();
  value.setValue( 1.0 );
  return Context.toObject( value, scope );
}
 
Example 15
Source File: JavaScriptUtilsTest.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Test
public void jsToNumber_NativeNumber() throws Exception {
  Scriptable value = Context.toObject( 1.0, scope );
  Number number = JavaScriptUtils.jsToNumber( value, NATIVE_NUMBER );
  assertEquals( 1.0, number.doubleValue(), 1e-6 );
}
 
Example 16
Source File: JavaScriptUtilsTest.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Test
public void jsToBigNumber_NativeNumber() throws Exception {
  Scriptable value = Context.toObject( 1.0, scope );
  BigDecimal number = JavaScriptUtils.jsToBigNumber( value, NATIVE_NUMBER );
  assertEquals( 1.0, number.doubleValue(), 1e-6 );
}
 
Example 17
Source File: JavaScriptUtilsTest.java    From hop with Apache License 2.0 4 votes vote down vote up
private static Scriptable getDoubleValue() {
  return Context.toObject( new Double( 1.0 ), scope );
}