Esempio n. 1
0
  /**
   * Return new {@link Scriptable} instance using the default constructor for the class of the
   * underlying Java method. Return null to indicate that the call method should be used to create
   * new objects.
   */
  public Scriptable createObject(Context cx, Scriptable scope) {
    if (member.isCtor() || (parmsLength == VARARGS_CTOR)) {
      return null;
    }
    Scriptable result;
    try {
      result = (Scriptable) member.getDeclaringClass().newInstance();
    } catch (Exception ex) {
      throw Context.throwAsScriptRuntimeEx(ex);
    }

    result.setPrototype(getClassPrototype());
    result.setParentScope(getParentScope());
    return result;
  }
Esempio n. 2
0
  /**
   * Define this function as a JavaScript constructor.
   *
   * <p>Sets up the "prototype" and "constructor" properties. Also calls setParent and setPrototype
   * with appropriate values. Then adds the function object as a property of the given scope, using
   * <code>prototype.getClassName()</code> as the name of the property.
   *
   * @param scope the scope in which to define the constructor (typically the global object)
   * @param prototype the prototype object
   * @see gov.nbcs.rp.common.js.javascript.Scriptable#setParentScope
   * @see gov.nbcs.rp.common.js.javascript.Scriptable#setPrototype
   * @see gov.nbcs.rp.common.js.javascript.Scriptable#getClassName
   */
  public void addAsConstructor(Scriptable scope, Scriptable prototype) {
    ScriptRuntime.setFunctionProtoAndParent(this, scope);
    setImmunePrototypeProperty(prototype);

    prototype.setParentScope(this);

    final int attr =
        ScriptableObject.DONTENUM | ScriptableObject.PERMANENT | ScriptableObject.READONLY;
    defineProperty(prototype, "constructor", this, attr);

    String name = prototype.getClassName();
    defineProperty(scope, name, this, ScriptableObject.DONTENUM);

    setParentScope(scope);
  }