org.mozilla.javascript.Function Java Examples

The following examples show how to use org.mozilla.javascript.Function. 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: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static String resolveIP( Context actualContext, Scriptable actualObject, Object[] ArgList,
                                Function FunctionContext ) {
  String sRC;
  if ( ArgList.length == 2 ) {
    try {
      InetAddress address = InetAddress.getByName( Context.toString( ArgList[ 0 ] ) );
      if ( Context.toString( ArgList[ 1 ] ).equals( "IP" ) ) {
        sRC = address.getHostName();
      } else {
        sRC = address.getHostAddress();
      }
      if ( sRC.equals( Context.toString( ArgList[ 0 ] ) ) ) {
        sRC = "-";
      }
    } catch ( Exception e ) {
      sRC = "-";
    }
  } else {
    throw Context.reportRuntimeError( "The function call resolveIP requires 2 arguments." );
  }

  return sRC;
}
 
Example #2
Source File: JavascriptTemplate.java    From manifold with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unused")
public static String renderToStringImpl( ScriptableObject scope, JSTNode templateNode, Object... args )
{
  try
  {
    //make argument list including the raw string list
    Object[] argsWithStrings = Arrays.copyOf( args, args.length + 1 );

    List rawStrings = templateNode.getChildren( RawStringNode.class )
      .stream()
      .map( node -> node.genCode() )
      .collect( Collectors.toList() );

    argsWithStrings[argsWithStrings.length - 1] = rawStrings;

    Function renderToString = (Function)scope.get( "renderToString", scope );
    return (String)renderToString.call( Context.getCurrentContext(), scope, scope, argsWithStrings );
  }
  catch( Exception e )
  {
    throw new RuntimeException( e );
  }
}
 
Example #3
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static Object getNextWorkingDay( Context actualContext, Scriptable actualObject, Object[] ArgList,
                                        Function FunctionContext ) {
  // (Date dIn){
  if ( ArgList.length == 1 ) {
    try {
      if ( isNull( ArgList[ 0 ] ) ) {
        return null;
      } else if ( isUndefined( ArgList[ 0 ] ) ) {
        return Context.getUndefinedValue();
      }
      Date dIn = (Date) Context.jsToJava( ArgList[ 0 ], Date.class );
      Calendar startDate = Calendar.getInstance();
      startDate.setTime( dIn );
      startDate.add( Calendar.DATE, 1 );
      while ( startDate.get( Calendar.DAY_OF_WEEK ) == Calendar.SATURDAY
        || startDate.get( Calendar.DAY_OF_WEEK ) == Calendar.SUNDAY ) {
        startDate.add( Calendar.DATE, 1 );
      }
      return startDate.getTime();
    } catch ( Exception e ) {
      throw Context.reportRuntimeError( e.toString() );
    }
  } else {
    throw Context.reportRuntimeError( "The function call getNextWorkingDay requires 1 argument." );
  }
}
 
Example #4
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static Object isWorkingDay( Context actualContext, Scriptable actualObject, Object[] ArgList,
                                   Function FunctionContext ) {
  if ( ArgList.length == 1 ) {
    try {
      if ( isNull( ArgList[ 0 ] ) ) {
        return null;
      } else if ( isUndefined( ArgList[ 0 ] ) ) {
        return Context.getUndefinedValue();
      } else {
        Date dIn = (Date) Context.jsToJava( ArgList[ 0 ], Date.class );
        Calendar startDate = Calendar.getInstance();
        startDate.setTime( dIn );
        if ( startDate.get( Calendar.DAY_OF_WEEK ) != Calendar.SATURDAY
          && startDate.get( Calendar.DAY_OF_WEEK ) != Calendar.SUNDAY ) {
          return Boolean.TRUE;
        }
        return Boolean.FALSE;
      }
    } catch ( Exception e ) {
      return null;
    }
  } else {
    throw Context.reportRuntimeError( "The function call isWorkingDay requires 1 argument." );
  }
}
 
Example #5
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static Object floor( Context actualContext, Scriptable actualObject, Object[] ArgList,
                            Function FunctionContext ) {
  if ( ArgList.length == 1 ) {
    try {
      if ( isNull( ArgList[ 0 ] ) ) {
        return new Double( Double.NaN );
      } else if ( isUndefined( ArgList[ 0 ] ) ) {
        return Context.getUndefinedValue();
      } else {
        return new Double( Math.floor( Context.toNumber( ArgList[ 0 ] ) ) );
      }
    } catch ( Exception e ) {
      return null;
      // throw Context.reportRuntimeError(e.toString());
    }
  } else {
    throw Context.reportRuntimeError( "The function call floor requires 1 argument." );
  }
}
 
Example #6
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static Object ceil( Context actualContext, Scriptable actualObject, Object[] ArgList,
                           Function FunctionContext ) {
  if ( ArgList.length == 1 ) {
    try {
      if ( isNull( ArgList[ 0 ] ) ) {
        return Double.NaN;
      } else if ( isUndefined( ArgList[ 0 ] ) ) {
        return Context.getUndefinedValue();
      } else {
        return Math.ceil( Context.toNumber( ArgList[ 0 ] ) );
      }
    } catch ( Exception e ) {
      return null;
    }
  } else {
    throw Context.reportRuntimeError( "The function call ceil requires 1 argument." );
  }
}
 
Example #7
Source File: SAMLSSORelyingPartyObject.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Get requested URI for relay state. And relay state value is removed, as relay state is unique and onetime value.
 *
 * @param cx
 * @param thisObj
 * @param args
 * @param funObj
 * @return
 * @throws ScriptException
 */
public static String jsFunction_getRelayStateProperty(Context cx, Scriptable thisObj,
                                                      Object[] args,
                                                      Function funObj)
        throws ScriptException {
    int argLength = args.length;
    if (argLength != 1 || !(args[0] instanceof String)) {
        throw new ScriptException("Invalid argument. Relay state value is missing.");
    }
    String requestedURI = relayStateMap.get((String) args[0]);
    if (log.isDebugEnabled()) {
        log.debug("Requested URI:" + relayStateMap.get((String) args[0]));
    }
    relayStateMap.remove((String) args[0]);
    return requestedURI;

}
 
Example #8
Source File: JSEngine.java    From NewFastFrame with Apache License 2.0 6 votes vote down vote up
/**
 * 执行JS
 */
public String runScript(String functionParams, String methodName) {
    Context rhino = Context.enter();
    rhino.setOptimizationLevel(-1);
    try {
        Scriptable scope = rhino.initStandardObjects();

        ScriptableObject.putProperty(scope, "javaContext", Context.javaToJS(this, scope));
        ScriptableObject.putProperty(scope, "javaLoader", Context.javaToJS(JSEngine.class.getClassLoader(), scope));
        rhino.evaluateString(scope, sb.toString(), JSEngine.class.getName(), 1, null);
        Function function = (Function) scope.get(methodName, scope);
        Object result = function.call(rhino, scope, scope, new Object[]{functionParams});
        if (result instanceof String) {
            return (String) result;
        } else if (result instanceof NativeJavaObject) {
            return (String) ((NativeJavaObject) result).getDefaultValue(String.class);
        } else if (result instanceof NativeObject) {
            return (String) ((NativeObject) result).getDefaultValue(String.class);
        }
        return result.toString();//(String) function.call(rhino, scope, scope, functionParams);
    } finally {
        Context.exit();
    }
}
 
Example #9
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static String removeCRLF( Context actualContext, Scriptable actualObject, Object[] ArgList,
                                 Function FunctionContext ) {
  if ( ArgList.length == 1 ) {
    try {
      if ( isNull( ArgList[ 0 ] ) ) {
        return null;
      } else if ( isUndefined( ArgList[ 0 ] ) ) {
        return (String) Context.getUndefinedValue();
      }

      return Const.removeCRLF( Context.toString( ArgList[ 0 ] ) );
    } catch ( Exception e ) {
      throw Context.reportRuntimeError( "The function call removeCRLF is not valid" );
    }
  } else {
    throw Context.reportRuntimeError( "The function call removeCRLF is not valid" );
  }
}
 
Example #10
Source File: ContinuationsApiTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testFunctionWithContinuations() {
    Context cx = Context.enter();
    try {
        cx.setOptimizationLevel(-1); // must use interpreter mode
        cx.evaluateString(globalScope,
                "function f(a) { return myObject.f(a); }",
                "function test source", 1, null);
        Function f = (Function) globalScope.get("f", globalScope);
        Object[] args = { 7 };
        cx.callFunctionWithContinuations(f, globalScope, args);
        fail("Should throw ContinuationPending");
    } catch (ContinuationPending pending) {
        Object applicationState = pending.getApplicationState();
        assertEquals(7, ((Number)applicationState).intValue());
        int saved = (Integer) applicationState;
        Object result = cx.resumeContinuation(pending.getContinuation(), globalScope, saved + 1);
        assertEquals(8, ((Number)result).intValue());
    } finally {
        Context.exit();
    }
}
 
Example #11
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static Object year( Context actualContext, Scriptable actualObject, Object[] ArgList,
                           Function FunctionContext ) {
  try {
    if ( ArgList.length == 1 ) {
      if ( isNull( ArgList[ 0 ] ) ) {
        return new Double( Double.NaN );
      } else if ( isUndefined( ArgList[ 0 ] ) ) {
        return Context.getUndefinedValue();
      }
      Date dArg1 = (Date) Context.jsToJava( ArgList[ 0 ], Date.class );
      Calendar cal = Calendar.getInstance();
      cal.setTime( dArg1 );
      return new Double( cal.get( Calendar.YEAR ) );
    } else {
      throw Context.reportRuntimeError( "The function call year requires 1 argument." );
    }
  } catch ( Exception e ) {
    throw Context.reportRuntimeError( e.toString() );
  }
}
 
Example #12
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static Object month( Context actualContext, Scriptable actualObject, Object[] ArgList,
                            Function FunctionContext ) {
  try {
    if ( ArgList.length == 1 ) {
      if ( isNull( ArgList[ 0 ] ) ) {
        return new Double( Double.NaN );
      } else if ( isUndefined( ArgList[ 0 ] ) ) {
        return Context.getUndefinedValue();
      }
      Date dArg1 = (Date) Context.jsToJava( ArgList[ 0 ], Date.class );
      Calendar cal = Calendar.getInstance();
      cal.setTime( dArg1 );
      return new Double( cal.get( Calendar.MONTH ) );
    } else {
      throw Context.reportRuntimeError( "The function call month requires 1 argument." );
    }
  } catch ( Exception e ) {
    throw Context.reportRuntimeError( e.toString() );
  }

}
 
Example #13
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static Object week( Context actualContext, Scriptable actualObject, Object[] ArgList,
                           Function FunctionContext ) {
  try {
    if ( ArgList.length == 1 ) {
      if ( isNull( ArgList[ 0 ] ) ) {
        return new Double( Double.NaN );
      } else if ( isUndefined( ArgList[ 0 ] ) ) {
        return Context.getUndefinedValue();
      }
      Date dArg1 = (Date) Context.jsToJava( ArgList[ 0 ], Date.class );
      Calendar cal = Calendar.getInstance();
      cal.setTime( dArg1 );
      return new Double( cal.get( Calendar.WEEK_OF_YEAR ) );
    } else {
      throw Context.reportRuntimeError( "The function call week requires 1 argument." );
    }
  } catch ( Exception e ) {
    throw Context.reportRuntimeError( e.toString() );
  }
}
 
Example #14
Source File: CipherManager.java    From YTPlayer with GNU General Public License v3.0 6 votes vote down vote up
private static String RhinoEngine(String s) {
	Context rhino = Context.enter();
	rhino.setOptimizationLevel(-1);
	try {
		Scriptable scope = rhino.initStandardObjects();
		rhino.evaluateString(scope, cachedDechiperFunction, "JavaScript", 1, null);
		Object obj = scope.get("decipher", scope);

		if (obj instanceof Function) {
			Function jsFunction = (Function) obj;
			Object jsResult = jsFunction.call(rhino, scope, scope, new Object[]{s});
			String result = Context.toString(jsResult);
			return result ;
		}
	}
	finally {
		Context.exit();
	}
	return s;
}
 
Example #15
Source File: SAMLSSORelyingPartyObject.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * @param cx
 * @param args      - args[0]-issuerId, this issuer need to be registered in Identity server.
 * @param ctorObj
 * @param inNewExpr
 * @return
 * @throws ScriptException
 */
public static Scriptable jsConstructor(Context cx, Object[] args, Function ctorObj,
                                       boolean inNewExpr)
        throws Exception {
    int argLength = args.length;
    if (argLength != 1 || !(args[0] instanceof String)) {
        throw new ScriptException("Invalid arguments!, IssuerId is missing in parameters.");
    }

    SAMLSSORelyingPartyObject relyingPartyObject = ssoRelyingPartyMap.get((String) args[0]);
    if (relyingPartyObject == null) {
        synchronized (SAMLSSORelyingPartyObject.class) {
            if (relyingPartyObject == null) {
                relyingPartyObject = new SAMLSSORelyingPartyObject();
                relyingPartyObject.setSSOProperty(SSOConstants.ISSUER_ID, (String) args[0]);
                ssoRelyingPartyMap.put((String) args[0], relyingPartyObject);
            }
        }
    }
    return relyingPartyObject;
}
 
Example #16
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static Object[] createRowCopy( Context actualContext, Scriptable actualObject, Object[] ArgList,
                                      Function FunctionContext ) {
  if ( ArgList.length == 1 ) {
    try {
      int newSize = (int) Math.round( Context.toNumber( ArgList[ 0 ] ) );

      Object scmO = actualObject.get( "row", actualObject );
      Object[] row = (Object[]) Context.jsToJava( scmO, ( new Object[] {} ).getClass() );

      return RowDataUtil.createResizedCopy( row, newSize );
    } catch ( Exception e ) {
      throw Context.reportRuntimeError( "Unable to create a row copy: " + Const.CR + e.toString() );
    }
  } else {
    throw Context
      .reportRuntimeError( "The function call createRowCopy requires a single arguments : the new size of the row" );
  }
}
 
Example #17
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static Object isDate( Context actualContext, Scriptable actualObject, Object[] ArgList,
                             Function FunctionContext ) {

  if ( ArgList.length == 1 ) {
    try {
      if ( isNull( ArgList[ 0 ] ) ) {
        return null;
      } else if ( isUndefined( ArgList[ 0 ] ) ) {
        return Context.getUndefinedValue();
      }
      /* java.util.Date d = (java.util.Date) */
      Context.jsToJava( ArgList[ 0 ], Date.class );
      return Boolean.TRUE;
    } catch ( Exception e ) {
      return Boolean.FALSE;
    }
  } else {
    throw Context.reportRuntimeError( "The function call isDate requires 1 argument." );
  }
}
 
Example #18
Source File: DcEngineContext.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * UserScript実行.
 * @param source ユーザースクリプトソース
 * @throws IOException IO例外
 * @throws DcEngineException DcEngineException
 */
private Object evalUserScript(final String source, JSGIRequest dcReq) throws DcEngineException {
    cx.evaluateString(scope, "fn_jsgi = " + source, null, 1, null);

    Object fObj = scope.get("fn_jsgi", scope);
    Object result = null;
    if (!(fObj instanceof Function)) {
        log.warn("fn_jsgi not found");
        throw new DcEngineException("Server Error", DcEngineException.STATUSCODE_SERVER_ERROR);
    }

    Object[] functionArgs = {dcReq.getRequestObject() };
    Function f = (Function) fObj;
    result = f.call(cx, scope, scope, functionArgs);
    return result;
}
 
Example #19
Source File: APIStoreHostObjectTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetHTTPsURL() throws APIManagementException {
    Context cx = Mockito.mock(Context.class);
    Scriptable thisObj = Mockito.mock(Scriptable.class);
    Function funObj = Mockito.mock(Function.class);
    Object[] args = new Object[1];
    PowerMockito.mockStatic(CarbonUtils.class);
    PowerMockito.mockStatic(HostObjectUtils.class);
    ServerConfiguration serverConfiguration = Mockito.mock(ServerConfiguration.class);
    PowerMockito.when(CarbonUtils.getServerConfiguration()).thenReturn(serverConfiguration);
    PowerMockito.when(HostObjectUtils.getBackendPort("https")).thenReturn("9443");

    //when hostName is not set and when hostName is set in system property
    System.setProperty("carbon.local.ip", "127.0.0.1");
    Assert.assertEquals("https://127.0.0.1:9443", APIStoreHostObject.jsFunction_getHTTPsURL(cx, thisObj, args, funObj));

    //when hostName is not set and when hostName is set in carbon.xml
    Mockito.when(serverConfiguration.getFirstProperty("HostName")).thenReturn("localhost");
    Assert.assertEquals("https://localhost:9443", APIStoreHostObject.jsFunction_getHTTPsURL(cx, thisObj, args, funObj));

    //when hostName is set to a valid host
    args[0] = "https://localhost:9443/store/";
    Assert.assertEquals("https://localhost:9443", APIStoreHostObject.jsFunction_getHTTPsURL(cx, thisObj, args, funObj));

}
 
Example #20
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static String upper( Context actualContext, Scriptable actualObject, Object[] ArgList,
                            Function FunctionContext ) {
  String sRC = "";
  if ( ArgList.length == 1 ) {
    try {
      if ( isNull( ArgList[ 0 ] ) ) {
        return null;
      } else if ( isUndefined( ArgList[ 0 ] ) ) {
        return (String) Context.getUndefinedValue();
      }
      sRC = Context.toString( ArgList[ 0 ] );
      sRC = sRC.toUpperCase();
    } catch ( Exception e ) {
      throw Context.reportRuntimeError( "The function call upper is not valid : " + e.getMessage() );
    }
  } else {
    throw Context.reportRuntimeError( "The function call upper requires 1 argument." );
  }
  return sRC;
}
 
Example #21
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static Object isRegExp( Context actualContext, Scriptable actualObject, Object[] ArgList,
                               Function FunctionContext ) {

  if ( ArgList.length >= 2 ) {
    if ( isNull( ArgList, new int[] { 0, 1 } ) ) {
      return null;
    } else if ( isUndefined( ArgList, new int[] { 0, 1 } ) ) {
      return Context.getUndefinedValue();
    }
    String strToMatch = Context.toString( ArgList[ 0 ] );
    for ( int i = 1; i < ArgList.length; i++ ) {
      Pattern p = Pattern.compile( Context.toString( ArgList[ i ] ) );
      Matcher m = p.matcher( strToMatch );
      if ( m.matches() ) {
        return new Double( i );
      }
    }
  }
  return new Double( -1 );
}
 
Example #22
Source File: SAMLSSORelyingPartyObject.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Invalidate current browser authenticated session based on session id.
 * Session will be invalidated after user log out request get succeeded.
 *
 * @param cx
 * @param thisObj
 * @param args
 * @param funObj
 * @throws Exception
 */
public static void jsFunction_invalidateSessionBySessionId(Context cx, Scriptable thisObj,
                                                           Object[] args,
                                                           Function funObj)
        throws Exception {
    int argLength = args.length;
    if (argLength != 1 || !(args[0] instanceof String)) {
        throw new ScriptException("Invalid argument. Session id is missing.");
    }
    if (log.isDebugEnabled()) {
        log.debug("jsFunction_invalidateSessionBySessionId===================Invalidating the authenticated session ");
    }
    SAMLSSORelyingPartyObject relyingPartyObject = (SAMLSSORelyingPartyObject) thisObj;
    String sessionId = (String) args[0];
    String sessionIndex = relyingPartyObject.getSessionIndex(sessionId);
    if (sessionIndex != null) {
        relyingPartyObject.handleLogout(sessionIndex);
    } else {
        relyingPartyObject.handleLogoutBySessionId(sessionId);
    }
}
 
Example #23
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static void touch( Context actualContext, Scriptable actualObject, Object[] ArgList,
                          Function FunctionContext ) {
  try {
    if ( ArgList.length == 1 && !isNull( ArgList[ 0 ] ) && !isUndefined( ArgList[ 0 ] ) ) {
      File file = new File( Context.toString( ArgList[ 0 ] ) );
      boolean success = file.createNewFile();
      if ( !success ) {
        file.setLastModified( System.currentTimeMillis() );
      }
    } else {
      throw Context.reportRuntimeError( "The function call touch requires 1 valid argument." );
    }
  } catch ( Exception e ) {
    throw Context.reportRuntimeError( e.toString() );
  }
}
 
Example #24
Source File: SAMLSSORelyingPartyObject.java    From carbon-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Invalidate current browser authenticated session based on session id.
 * Session will be invalidated after user log out request get succeeded.
 *
 * @param cx
 * @param thisObj
 * @param args
 * @param funObj
 * @throws Exception
 */
public static void jsFunction_invalidateSessionBySessionId(Context cx, Scriptable thisObj,
                                                           Object[] args,
                                                           Function funObj)
        throws Exception {
    int argLength = args.length;
    if (argLength != 1 || !(args[0] instanceof String)) {
        throw new ScriptException("Invalid argument. Session id is missing.");
    }
    SAMLSSORelyingPartyObject relyingPartyObject = (SAMLSSORelyingPartyObject) thisObj;

    relyingPartyObject.invalidateSessionBySessionId((String) args[0]);
    // this is to invalidate relying party object after user log out. To release memory allocations.
    invalidateRelyingPartyObject(relyingPartyObject.getSSOProperty(SSOConstants.ISSUER_ID));

}
 
Example #25
Source File: A6Util.java    From Huochexing12306 with Apache License 2.0 5 votes vote down vote up
/**
 * 执行JS
 * 
 * @param js js代码
 * @param functionName js方法名称
 * @param functionParams js方法参数
 * @return
 */
public static String runScript(Context context, String js, String functionName, Object[] functionParams) {
	org.mozilla.javascript.Context rhino = org.mozilla.javascript.Context.enter();
	rhino.setOptimizationLevel(-1);
	try {
		Scriptable scope = rhino.initStandardObjects();

		ScriptableObject.putProperty(scope, "javaContext", org.mozilla.javascript.Context.javaToJS(context, scope));
		ScriptableObject.putProperty(scope, "javaLoader", org.mozilla.javascript.Context.javaToJS(context.getClass().getClassLoader(), scope));

		rhino.evaluateString(scope, js, context.getClass().getSimpleName(), 1, null);

		Function function = (Function) scope.get(functionName, scope);

		Object result = function.call(rhino, scope, scope, functionParams);
		if (result instanceof String) {
			return (String) result;
		} else if (result instanceof NativeJavaObject) {
			return (String) ((NativeJavaObject) result).getDefaultValue(String.class);
		} else if (result instanceof NativeObject) {
			return (String) ((NativeObject) result).getDefaultValue(String.class);
		}
		return result.toString();//(String) function.call(rhino, scope, scope, functionParams);
	} finally {
		org.mozilla.javascript.Context.exit();
	}
}
 
Example #26
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 5 votes vote down vote up
public static String initCap( Context actualContext, Scriptable actualObject, Object[] ArgList,
                              Function FunctionContext ) {
  if ( ArgList.length == 1 ) {
    return ValueDataUtil.initCap(null,  Context.toString( ArgList[ 0 ] ) );
  } else {
    throw Context.reportRuntimeError( "The function call initCap requires 1 argument." );

  }
}
 
Example #27
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 5 votes vote down vote up
public static void LoadScriptFromTab( Context actualContext, Scriptable actualObject, Object[] ArgList,
                                      Function FunctionContext ) {
  try {
    for ( int i = 0; i < ArgList.length; i++ ) { // don't worry about "undefined" arguments
      String strToLoad = Context.toString( ArgList[ i ] );
      String strScript = actualObject.get( strToLoad, actualObject ).toString();
      actualContext.evaluateString( actualObject, strScript, "_" + strToLoad + "_", 0, null );
    }
  } catch ( Exception e ) {
    // TODO: DON'T EAT EXCEPTION
    // System.out.println(e.toString());
  }
}
 
Example #28
Source File: TestUtils.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
public static void loadAsset(Context cx, Scriptable thisObj,
                             Object[] args, Function funObj)
{
    for (Object arg : args) {
        String file = Context.toString(arg);
        Global.load(cx, thisObj, new Object[]{copyFromAssets(file)}, funObj);
    }
}
 
Example #29
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 5 votes vote down vote up
public static String getDigitsOnly( Context actualContext, Scriptable actualObject, Object[] ArgList,
                                    Function FunctionContext ) {
  if ( ArgList.length == 1 ) {
    return Const.getDigitsOnly( Context.toString( ArgList[ 0 ] ) );
  } else {
    throw Context.reportRuntimeError( "The function call getDigitsOnly requires 1 argument." );

  }
}
 
Example #30
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});
}