Exemplo n.º 1
0
 private static Object doCall(
     Context cx,
     Scriptable scope,
     Scriptable thisObj,
     Function f,
     Object[] args,
     long argsToWrap) {
   // Wrap the rest of objects
   for (int i = 0; i != args.length; ++i) {
     if (0 != (argsToWrap & (1 << i))) {
       Object arg = args[i];
       if (!(arg instanceof Scriptable)) {
         args[i] = cx.getWrapFactory().wrap(cx, scope, arg, null);
       }
     }
   }
   return f.call(cx, scope, thisObj, args);
 }
Exemplo n.º 2
0
  /**
   * Performs conversions on argument types if needed and invokes the underlying Java method or
   * constructor.
   *
   * <p>Implements Function.call.
   *
   * @see gov.nbcs.rp.common.js.javascript.Function#call( Context, Scriptable, Scriptable, Object[])
   */
  public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
    Object result;
    boolean checkMethodResult = false;

    if (parmsLength < 0) {
      if (parmsLength == VARARGS_METHOD) {
        Object[] invokeArgs = {cx, thisObj, args, this};
        result = member.invoke(null, invokeArgs);
        checkMethodResult = true;
      } else {
        boolean inNewExpr = (thisObj == null);
        Boolean b = inNewExpr ? Boolean.TRUE : Boolean.FALSE;
        Object[] invokeArgs = {cx, args, this, b};
        result =
            (member.isCtor()) ? member.newInstance(invokeArgs) : member.invoke(null, invokeArgs);
      }

    } else {
      if (!isStatic) {
        Class clazz = member.getDeclaringClass();
        if (!clazz.isInstance(thisObj)) {
          boolean compatible = false;
          if (thisObj == scope) {
            Scriptable parentScope = getParentScope();
            if (scope != parentScope) {
              // Call with dynamic scope for standalone function,
              // use parentScope as thisObj
              compatible = clazz.isInstance(parentScope);
              if (compatible) {
                thisObj = parentScope;
              }
            }
          }
          if (!compatible) {
            // Couldn't find an object to call this on.
            throw ScriptRuntime.typeError1("msg.incompat.call", functionName);
          }
        }
      }

      Object[] invokeArgs;
      if (parmsLength == args.length) {
        // Do not allocate new argument array if java arguments are
        // the same as the original js ones.
        invokeArgs = args;
        for (int i = 0; i != parmsLength; ++i) {
          Object arg = args[i];
          Object converted = convertArg(cx, scope, arg, typeTags[i]);
          if (arg != converted) {
            if (invokeArgs == args) {
              invokeArgs = (Object[]) args.clone();
            }
            invokeArgs[i] = converted;
          }
        }
      } else if (parmsLength == 0) {
        invokeArgs = ScriptRuntime.emptyArgs;
      } else {
        invokeArgs = new Object[parmsLength];
        for (int i = 0; i != parmsLength; ++i) {
          Object arg = (i < args.length) ? args[i] : Undefined.instance;
          invokeArgs[i] = convertArg(cx, scope, arg, typeTags[i]);
        }
      }

      if (member.isMethod()) {
        result = member.invoke(thisObj, invokeArgs);
        checkMethodResult = true;
      } else {
        result = member.newInstance(invokeArgs);
      }
    }

    if (checkMethodResult) {
      if (hasVoidReturn) {
        result = Undefined.instance;
      } else if (returnTypeTag == JAVA_UNSUPPORTED_TYPE) {
        result = cx.getWrapFactory().wrap(cx, scope, result, null);
      }
      // XXX: the code assumes that if returnTypeTag == JAVA_OBJECT_TYPE
      // then the Java method did a proper job of converting the
      // result to JS primitive or Scriptable to avoid
      // potentially costly Context.javaToJS call.
    }

    return result;
  }
  /**
   * @deprecated Use {@link Context#getWrapFactory()} together with calling {@link
   *     WrapFactory#wrap(Context, Scriptable, Object, Class)}
   */
  public static Object wrap(Scriptable scope, Object obj, Class<?> staticType) {

    Context cx = Context.getContext();
    return cx.getWrapFactory().wrap(cx, scope, obj, staticType);
  }