@Override
  public E remove(int index) {
    Property property = new Property(this, String.valueOf(index));

    E oldContent = get(index);

    internal.remove(index);

    if (oldContent instanceof IObject) {
      ((IObject) oldContent).getParentsLinksToThis().remove(property);
    }

    for (; index < size(); index++) {
      E content = get(index);
      if (content instanceof IObject) {
        Property oldProperty = new Property(this, String.valueOf(index + 1));
        Property newProperty = new Property(this, String.valueOf(index));
        int linkIndex = ((IObject) content).getParentsLinksToThis().indexOf(oldProperty);
        ((IObject) content).getParentsLinksToThis().set(linkIndex, newProperty);
      }
    }

    if (propagate) propagateChange(new Property(this, ""));

    return oldContent;
  }
  /**
   * Create a new instance of NativeNumber
   *
   * @param owningEnvironment The environment in which this native number was created
   */
  public JSNumberConstructor(Environment owningEnvironment) {
    super(owningEnvironment);

    int fileIndex = FileContextManager.BUILT_IN_FILE_INDEX;
    int offset = Range.Empty.getStartingOffset();

    // get global
    IScope global = owningEnvironment.getGlobal();

    // put self into environment
    global.putPropertyValue(
        "Number", this, fileIndex, Property.DONT_DELETE | Property.DONT_ENUM); // $NON-NLS-1$

    // set Number.[[prototype]]
    IObject function = global.getPropertyValue("Function", fileIndex, offset); // $NON-NLS-1$
    IObject functionPrototype =
        function.getPropertyValue("prototype", fileIndex, offset); // $NON-NLS-1$
    this.setPrototype(functionPrototype);

    // create public prototype object
    IObject prototype = owningEnvironment.createObject(fileIndex, Range.Empty);

    // store our public prototype
    this.putPropertyValue("prototype", prototype, fileIndex); // $NON-NLS-1$
  }
Example #3
0
  @Override
  public String id() {
    if (id == null) {
      id = "";

      IObject container = getContainer();

      if (container != null) {
        id = container.id();
        id = id + (id.isEmpty() ? "" : ".") + getIndex();
      }
    }

    return id;
  }
 @Override
 protected void setLocal(String propertyName, Object content) {
   if (propertyName.matches("^\\d+$")) {
     internal.set(Integer.parseInt(propertyName), (E) content);
   } else {
     super.setLocal(propertyName, content);
   }
 }
  public static void main(String[] args) throws UnsupportedEncodingException {
    IObject memObj = new MemObject();
    memObj.set("key1", Types.INT, 33);
    System.out.println("key1=" + memObj.as("key1", Types.INT));

    JsonObject jsonObject = new JsonObject();
    jsonObject.set("welt", Types.INT, 44);
    System.out.println("key1(json)=" + jsonObject.as("welt", Types.INT));

    JsonObject innerObj = new JsonObject();
    innerObj.set("innerKey", Types.INT, 32);

    JsonArray jsa = new JsonArray();
    jsa.add(Types.STRING, "Ich bin ein String im Array");
    jsa.add(Types.INT, 666);
    jsa.add(Types.OBJECT, innerObj);
    jsonObject.set("keyZumArray", Types.ARRAY, jsa);

    final byte[] binarySrc =
        (new String("Ich bin binäre daten, encodiert in UTF-8")).getBytes("UTF-8");
    final IBinaryType binary = new JsonBinary();
    binary.write(
        new IBinaryWriter() {
          @Override
          public void write(OutputStream writer) {
            try {
              writer.write(binarySrc);
            } catch (IOException e) {
              e.printStackTrace(); // To change body of catch statement use File | Settings |
              // File Templates.
            }
          }
        });
    jsonObject.set("binaryKey", Types.BINARY, binary);

    System.out.println("json=" + jsonObject.toJsonString());

    final Optional<IBinaryType> binOutput = jsonObject.as("binaryKey", Types.BINARY);
    binOutput
        .get()
        .read(
            new IBinaryReader() {
              @Override
              public void read(InputStream reader) {}
            });
  }
  @Override
  public void add(int index, E element) {
    Property property = new Property(this, String.valueOf(index));

    internal.add(index, element);

    if (element instanceof IObject) {
      ((IObject) element).getParentsLinksToThis().add(property);
    }

    for (index = index + 1; index < size(); index++) {
      E content = get(index);
      if (content instanceof IObject) {
        Property oldProperty = new Property(this, String.valueOf(index - 1));
        Property newProperty = new Property(this, String.valueOf(index));
        int linkIndex = ((IObject) content).getParentsLinksToThis().indexOf(oldProperty);
        ((IObject) content).getParentsLinksToThis().set(linkIndex, newProperty);
      }
    }

    if (propagate) propagateChange(new Property(this, ""));
  }
Example #7
0
  @Override
  public guid key() {
    if (key == null) {
      String ownerName = owner != null ? owner.name() : null;
      String name = name();

      if (name == null) throw new UnsupportedOperationException();

      String value = (ownerName != null ? ownerName + "." : "") + name;
      key = new guid(UUID.nameUUIDFromBytes(value.getBytes()));
    }

    return key;
  }
  @Override
  public boolean add(E e) {
    Property property = new Property(this, String.valueOf(size()));

    boolean ret = internal.add(e);

    if (ret) {
      if (e instanceof IObject) {
        ((IObject) e).getParentsLinksToThis().add(property);
      }

      if (propagate) propagateChange(property);
    }

    return ret;
  }
  /** Initialize the properties on this object */
  public void initializeProperties() {
    Environment environment = this.owningEnvironment;
    int attributes = Property.DONT_DELETE | Property.DONT_ENUM;
    int fileIndex = FileContextManager.BUILT_IN_FILE_INDEX;
    int offset = Range.Empty.getStartingOffset();

    // add properties to Number
    this.putPropertyValue(
        "length",
        environment.createNumber(fileIndex, Range.Empty),
        fileIndex,
        attributes); //$NON-NLS-1$
    this.putPropertyValue(
        "MAX_VALUE",
        environment.createNumber(fileIndex, Range.Empty),
        fileIndex,
        attributes); //$NON-NLS-1$
    this.putPropertyValue(
        "MIN_VALUE",
        environment.createNumber(fileIndex, Range.Empty),
        fileIndex,
        attributes); //$NON-NLS-1$
    this.putPropertyValue(
        "NaN",
        environment.createNumber(fileIndex, Range.Empty),
        fileIndex,
        attributes); //$NON-NLS-1$
    this.putPropertyValue(
        "NEGATIVE_INFINITY",
        environment.createNumber(fileIndex, Range.Empty),
        fileIndex,
        attributes); //$NON-NLS-1$
    this.putPropertyValue(
        "POSITIVE_INFINITY",
        environment.createNumber(fileIndex, Range.Empty),
        fileIndex,
        attributes); //$NON-NLS-1$

    // add properties to Number.prototype
    IObject prototype = this.getPropertyValue("prototype", fileIndex, offset); // $NON-NLS-1$

    prototype.putPropertyValue("constructor", this, fileIndex, attributes); // $NON-NLS-1$
    prototype.putPropertyValue(
        "toString",
        environment.createFunction(fileIndex, Range.Empty),
        fileIndex,
        attributes); //$NON-NLS-1$
    prototype.putPropertyValue(
        "toLocaleString",
        environment.createFunction(fileIndex, Range.Empty),
        fileIndex,
        attributes); //$NON-NLS-1$
    prototype.putPropertyValue(
        "valueOf",
        environment.createFunction(fileIndex, Range.Empty),
        fileIndex,
        attributes); //$NON-NLS-1$
    prototype.putPropertyValue(
        "toFixed",
        environment.createFunction(fileIndex, Range.Empty),
        fileIndex,
        attributes); //$NON-NLS-1$
    prototype.putPropertyValue(
        "toExponential",
        environment.createFunction(fileIndex, Range.Empty),
        fileIndex,
        attributes); //$NON-NLS-1$
    prototype.putPropertyValue(
        "toPrecision",
        environment.createFunction(fileIndex, Range.Empty),
        fileIndex,
        attributes); //$NON-NLS-1$
  }