1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.objects;
  27 
  28 import java.util.Objects;
  29 import jdk.nashorn.internal.objects.annotations.Property;
  30 import jdk.nashorn.internal.objects.annotations.ScriptClass;
  31 import jdk.nashorn.internal.runtime.JSType;
  32 import jdk.nashorn.internal.runtime.PropertyDescriptor;
  33 import jdk.nashorn.internal.runtime.PropertyMap;
  34 import jdk.nashorn.internal.runtime.ScriptFunction;
  35 import jdk.nashorn.internal.runtime.ScriptObject;
  36 import jdk.nashorn.internal.runtime.ScriptRuntime;
  37 
  38 /**
  39  * Generic Property descriptor is used to represent attributes an object property
  40  * that is neither a data property descriptor nor an accessor property descriptor.
  41  *
  42  * See ECMA 8.10 The Property Descriptor and Property Identifier Specification Types
  43  *
  44  */
  45 @ScriptClass("GenericPropertyDescriptor")
  46 public final class GenericPropertyDescriptor extends ScriptObject implements PropertyDescriptor {
  47     /** Is the property configurable? */
  48     @Property
  49     public Object configurable;
  50 
  51     /** Is the property writable? */
  52     @Property
  53     public Object enumerable;
  54 
  55     // initialized by nasgen
  56     private static PropertyMap $nasgenmap$;
  57 
  58     GenericPropertyDescriptor(final boolean configurable, final boolean enumerable, final Global global) {
  59         super(global.getObjectPrototype(), $nasgenmap$);
  60         this.configurable = configurable;
  61         this.enumerable   = enumerable;
  62     }
  63 
  64     @Override
  65     public boolean isConfigurable() {
  66         return JSType.toBoolean(configurable);
  67     }
  68 
  69     @Override
  70     public boolean isEnumerable() {
  71         return JSType.toBoolean(enumerable);
  72     }
  73 
  74     @Override
  75     public boolean isWritable() {
  76         // Not applicable for this. But simplifies flag calculations.
  77         return false;
  78     }
  79 
  80     @Override
  81     public Object getValue() {
  82         throw new UnsupportedOperationException("value");
  83     }
  84 
  85     @Override
  86     public ScriptFunction getGetter() {
  87         throw new UnsupportedOperationException("get");
  88     }
  89 
  90     @Override
  91     public ScriptFunction getSetter() {
  92         throw new UnsupportedOperationException("set");
  93     }
  94 
  95     @Override
  96     public void setConfigurable(final boolean flag) {
  97         this.configurable = flag;
  98     }
  99 
 100     @Override
 101     public void setEnumerable(final boolean flag) {
 102         this.enumerable = flag;
 103     }
 104 
 105     @Override
 106     public void setWritable(final boolean flag) {
 107         throw new UnsupportedOperationException("set writable");
 108     }
 109 
 110     @Override
 111     public void setValue(final Object value) {
 112         throw new UnsupportedOperationException("set value");
 113     }
 114 
 115     @Override
 116     public void setGetter(final Object getter) {
 117         throw new UnsupportedOperationException("set getter");
 118     }
 119 
 120     @Override
 121     public void setSetter(final Object setter) {
 122         throw new UnsupportedOperationException("set setter");
 123     }
 124 
 125     @Override
 126     public PropertyDescriptor fillFrom(final ScriptObject sobj) {
 127         if (sobj.has(CONFIGURABLE)) {
 128             this.configurable = JSType.toBoolean(sobj.get(CONFIGURABLE));
 129         } else {
 130             delete(CONFIGURABLE, false);
 131         }
 132 
 133         if (sobj.has(ENUMERABLE)) {
 134             this.enumerable = JSType.toBoolean(sobj.get(ENUMERABLE));
 135         } else {
 136             delete(ENUMERABLE, false);
 137         }
 138 
 139         return this;
 140     }
 141 
 142     @Override
 143     public int type() {
 144         return GENERIC;
 145     }
 146 
 147     @Override
 148     public boolean hasAndEquals(final PropertyDescriptor other) {
 149         if (has(CONFIGURABLE) && other.has(CONFIGURABLE)) {
 150             if (isConfigurable() != other.isConfigurable()) {
 151                 return false;
 152             }
 153         }
 154 
 155         if (has(ENUMERABLE) && other.has(ENUMERABLE)) {
 156             if (isEnumerable() != other.isEnumerable()) {
 157                 return false;
 158             }
 159         }
 160 
 161         return true;
 162     }
 163 
 164     @Override
 165     public boolean equals(final Object obj) {
 166         if (this == obj) {
 167             return true;
 168         }
 169         if (!(obj instanceof GenericPropertyDescriptor)) {
 170             return false;
 171         }
 172 
 173         final GenericPropertyDescriptor other = (GenericPropertyDescriptor)obj;
 174         return ScriptRuntime.sameValue(configurable, other.configurable) &&
 175                ScriptRuntime.sameValue(enumerable, other.enumerable);
 176     }
 177 
 178     @Override
 179     public int hashCode() {
 180         int hash = 7;
 181         hash = 97 * hash + Objects.hashCode(this.configurable);
 182         hash = 97 * hash + Objects.hashCode(this.enumerable);
 183         return hash;
 184     }
 185 }