Esempio n. 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;
 }
Esempio n. 2
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. 3
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);
  }
 @Override
 public void put(String name, Scriptable start, Object value) {
   int info = findInstanceIdInfo(name);
   if (info != 0) {
     if (start == this && isSealed()) {
       throw Context.reportRuntimeError1("msg.modify.sealed", name);
     }
     int attr = (info >>> 16);
     if ((attr & READONLY) == 0) {
       if (start == this) {
         int id = (info & 0xFFFF);
         setInstanceIdValue(id, value);
       } else {
         start.put(name, start, value);
       }
     }
     return;
   }
   if (prototypeValues != null) {
     int id = prototypeValues.findId(name);
     if (id != 0) {
       if (start == this && isSealed()) {
         throw Context.reportRuntimeError1("msg.modify.sealed", name);
       }
       prototypeValues.set(id, start, value);
       return;
     }
   }
   super.put(name, start, value);
 }
 final void set(int id, Scriptable start, Object value) {
   if (value == NOT_FOUND) throw new IllegalArgumentException();
   ensureId(id);
   int attr = attributeArray[id - 1];
   if ((attr & READONLY) == 0) {
     if (start == obj) {
       if (value == null) {
         value = UniqueTag.NULL_VALUE;
       }
       int valueSlot = (id - 1) * SLOT_SPAN + VALUE_SLOT;
       synchronized (this) {
         valueArray[valueSlot] = value;
       }
     } else {
       int nameSlot = (id - 1) * SLOT_SPAN + NAME_SLOT;
       String name = (String) valueArray[nameSlot];
       start.put(name, start, value);
     }
   }
 }
Esempio n. 6
0
  /**
   * Creates and returns a new Image object that is a rendering of this Image object. The input
   * object defines the rendering parameters of the new Image object. Imagemagick's convert program
   * is used to create a scaled bounding box of this Image with the given dimensions.
   *
   * @param {Object} input A JavaScript object specifying the rendering parameters. For example,
   *     <code> {maxWidth:200, maxHeight:100} </code>
   * @returns {Image} The rendered Image object
   * @throws Exception
   */
  public ImageObject jsFunction_render(Object input) throws Exception {
    if (input == null || !(input instanceof Scriptable)) {
      throw new RuntimeException("The first argument to render() must be a scriptable object.");
    }

    Scriptable s = (Scriptable) input;
    int maxWidth = toInt(s.get("maxWidth", s));
    int maxHeight = toInt(s.get("maxHeight", s));

    int cropWidth = toInt(s.get("cropWidth", s));
    int cropHeight = toInt(s.get("cropHeight", s));
    int cropXOffset = toInt(s.get("cropXOffset", s));
    int cropYOffset = toInt(s.get("cropYOffset", s));

    int currentWidth = (int) node.getInteger(WIDTH);
    int currentHeight = (int) node.getInteger(HEIGHT);

    String aname = null;
    if (maxWidth > 0 && maxHeight > 0 && currentWidth > 0 && currentHeight > 0) {
      int[] dims = this.computeResizedDimensions(maxWidth, maxHeight, currentWidth, currentHeight);
      aname = dims[0] + "x" + dims[1];
      maxWidth = dims[0];
      maxHeight = dims[1];
    } else if (cropWidth > 0 && cropHeight > 0) {
      aname = cropWidth + "x" + cropHeight + "_" + cropXOffset + "x" + cropYOffset;
    } else {
      throw new RuntimeException("render(), invalid parameter set.");
    }

    Object o = this.jsFunction_get(aname);
    if (o instanceof ImageObject) {
      return (ImageObject) o;
    }

    try {
      synchronized (this) {
        while (this.convertOps.contains(aname)) {
          this.wait();
        }
        this.convertOps.add(aname);
      }

      o = ((axiom.objectmodel.db.Node) this.node).getChildElement(aname, true);
      if (o instanceof Node) {
        return (ImageObject) Context.toObject(o, this.core.global);
      }

      ImageObject computedImg = null;
      String[] paths = getPaths();
      String imgPath = paths[0];
      String tmpPath = paths[1];
      String fileName = node.getString(FileObject.FILE_NAME);

      try {
        File tmpFile = new File(tmpPath);
        int[] dims = null;
        if (maxWidth > 0 && maxHeight > 0) {
          dims =
              this.resize(
                  maxWidth,
                  maxHeight,
                  (int) this.node.getInteger(WIDTH),
                  (int) this.node.getInteger(HEIGHT),
                  imgPath,
                  tmpPath,
                  true);
        } else {
          dims = this.crop(cropWidth, cropHeight, cropXOffset, cropYOffset, imgPath, tmpPath);
        }

        if (dims == null) {
          throw new Exception("ImageObject.render(), resizing the image failed.");
        }

        final String protoname = "Image";
        INode node =
            new axiom.objectmodel.db.Node(protoname, protoname, core.app.getWrappedNodeManager());
        computedImg = new ImageObject("Image", core, node, core.getPrototype(protoname), true);

        node.setString(FileObject.FILE_NAME, fileName);
        node.setString(FileObject.ACCESSNAME, FileObjectCtor.generateAccessName(fileName));
        node.setString(FileObject.CONTENT_TYPE, this.node.getString(FileObject.CONTENT_TYPE));
        node.setJavaObject(FileObject.SELF, computedImg);
        node.setInteger(ImageObject.WIDTH, dims[0]);
        node.setInteger(ImageObject.HEIGHT, dims[1]);
        node.setString(FileObject.RENDERED_CONTENT, "true");

        node.setInteger(FileObject.FILE_SIZE, tmpFile.length());
        computedImg.tmpPath = tmpPath;
      } catch (Exception ex) {
        throw new RuntimeException(
            "ImageObject.jsfunction_bound(): Could not write the image to temporary storage, "
                + ex.getMessage());
      }

      if (computedImg != null) {
        this.jsFunction_addThumbnail(computedImg, null);
        return computedImg;
      }
    } finally {
      synchronized (this) {
        this.convertOps.remove(aname);
        this.notifyAll();
      }
    }

    return null;
  }
Esempio n. 7
0
  /**
   * Evaluates the RhinoFile result. If the result is an instance of javax.javascript.Scriptable,
   * assume it is a JavaScript Array of TestCase objects, as described in RhinoDrv.java. For each
   * test case in the array, add an element to the RhinoFile's test case vector. If all test cases
   * passed, set the RhinoFile's passed value to true; else set its passed value to false.
   *
   * <p>If the result is not a Scriptable object, the test failed. Set the the RhinoFile's exception
   * property to the string value of the result. However, negative tests, which should have a
   * "-n.js" extension, are expected to fail.
   */
  public boolean parseResult() {
    FlattenedObject fo = null;

    if (result instanceof Scriptable) {
      fo = new FlattenedObject((Scriptable) result);

      try {
        file.totalCases = ((Number) fo.getProperty("length")).intValue();
        for (int i = 0; i < file.totalCases; i++) {
          Scriptable tc = (Scriptable) ((Scriptable) result).get(i, (Scriptable) result);

          TestCase rt =
              new TestCase(
                  getString(tc.get("passed", tc)),
                  getString(tc.get("name", tc)),
                  getString(tc.get("description", tc)),
                  getString(tc.get("expect", tc)),
                  getString(tc.get("actual", tc)),
                  getString(tc.get("reason", tc)));

          file.bugnumber =
              (getString(tc.get("bugnumber", tc))).startsWith("com.netscape.javascript")
                  ? file.bugnumber
                  : getString(tc.get("bugnumber", tc));

          file.caseVector.addElement(rt);
          if (rt.passed.equals("false")) {
            this.file.passed = false;
            this.suite.passed = false;
          }
        }

        if (file.totalCases == 0) {
          if (file.name.endsWith("-n.js")) {
            this.file.passed = true;
          } else {
            this.file.reason = "File contains no testcases. " + this.file.reason;
            this.file.passed = false;
            this.suite.passed = false;
          }
        }

      } catch (Exception e) {
        this.file.exception =
            "Got a Scriptable result, but failed "
                + "parsing its arguments.  Exception: "
                + e.toString()
                + " Flattened Object is: "
                + fo.toString();

        this.file.passed = false;
        this.suite.passed = false;

        return false;
      }
    } else {
      // if it's not a scriptable object, test failed.  set the file's
      // exception to the string value of whatever result we did get.
      this.file.exception = result.toString();

      // if the file's name ends in "-n", the test expected an error.

      if (file.name.endsWith("-n.js")) {
        this.file.passed = true;
      } else {
        this.file.passed = false;
        this.suite.passed = false;
        return false;
      }
    }

    return true;
  }