Ejemplo n.º 1
0
 public static Object convertArg(Context cx, Scriptable scope, Object arg, int typeTag) {
   switch (typeTag) {
     case JAVA_STRING_TYPE:
       if (arg instanceof String) {
         return arg;
       }
       return ScriptRuntime.toString(arg);
     case JAVA_INT_TYPE:
       if (arg instanceof Integer) {
         return arg;
       }
       return new Integer(ScriptRuntime.toInt32(arg));
     case JAVA_BOOLEAN_TYPE:
       if (arg instanceof Boolean) {
         return arg;
       }
       return ScriptRuntime.toBoolean(arg) ? Boolean.TRUE : Boolean.FALSE;
     case JAVA_DOUBLE_TYPE:
       if (arg instanceof Double) {
         return arg;
       }
       return new Double(ScriptRuntime.toNumber(arg));
     case JAVA_SCRIPTABLE_TYPE:
       if (arg instanceof Scriptable) {
         return arg;
       }
       return ScriptRuntime.toObject(cx, scope, arg);
     case JAVA_OBJECT_TYPE:
       return arg;
     default:
       throw new IllegalArgumentException();
   }
 }
Ejemplo n.º 2
0
 private static ObjToIntMap getObjectFunctionNames(Scriptable obj) {
   Object[] ids = ScriptableObject.getPropertyIds(obj);
   ObjToIntMap map = new ObjToIntMap(ids.length);
   for (int i = 0; i != ids.length; ++i) {
     if (!(ids[i] instanceof String)) continue;
     String id = (String) ids[i];
     Object value = ScriptableObject.getProperty(obj, id);
     if (value instanceof Function) {
       Function f = (Function) value;
       int length = ScriptRuntime.toInt32(ScriptableObject.getProperty(f, "length"));
       if (length < 0) {
         length = 0;
       }
       map.put(id, length);
     }
   }
   return map;
 }