Ejemplo n.º 1
0
 /**
  * Utility method to construct type error to indicate incompatible call when converting script
  * thisObj to a particular type is not possible. Possible usage would be to have a private
  * function like realThis:
  *
  * <pre>
  *  private static NativeSomething realThis(Scriptable thisObj,
  *                                          IdFunctionObject f)
  *  {
  *      if (!(thisObj instanceof NativeSomething))
  *          throw incompatibleCallError(f);
  *      return (NativeSomething)thisObj;
  * }
  * </pre>
  *
  * Note that although such function can be implemented universally via
  * java.lang.Class.isInstance(), it would be much more slower.
  *
  * @param f function that is attempting to convert 'this' object.
  * @return Scriptable object suitable for a check by the instanceof operator.
  * @throws RuntimeException if no more instanceof target can be found
  */
 protected static EcmaError incompatibleCallError(IdFunctionObject f) {
   throw ScriptRuntime.typeError1("msg.incompat.call", f.getFunctionName());
 }
Ejemplo 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;
  }