示例#1
0
 /**
  * Take an object from the scripting layer and convert it to an int.
  *
  * @param obj
  * @return int
  */
 private int toInt(Object obj) {
   int result = 0;
   if (obj instanceof String) {
     result = Integer.parseInt((String) obj);
   } else if (obj instanceof Number) {
     result = ((Number) obj).intValue();
   } else if (obj instanceof Scriptable) {
     Scriptable sobj = (Scriptable) obj;
     if (sobj.getClassName().equals("Number")) {
       result = (int) ScriptRuntime.toNumber(sobj);
     }
   }
   return result;
 }
示例#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);
  }