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     static PropertyMap getInitialMap() {
  59         return $nasgenmap$;
  60     }
  61 
  62     GenericPropertyDescriptor(final boolean configurable, final boolean enumerable, final Global global) {
  63         super(global.getObjectPrototype(), getInitialMap());
  64         this.configurable = configurable;
  65         this.enumerable   = enumerable;
  66     }
  67 
  68     @Override
  69     public boolean isConfigurable() {
  70         return JSType.toBoolean(configurable);
  71     }
  72 
  73     @Override
  74     public boolean isEnumerable() {
  75         return JSType.toBoolean(enumerable);
  76     }
  77 
  78     @Override
  79     public boolean isWritable() {
  80         // Not applicable for this. But simplifies flag calculations.
  81         return false;
  82     }
  83 
  84     @Override
  85     public Object getValue() {
  86         throw new UnsupportedOperationException("value");
  87     }
  88 
  89     @Override
  90     public ScriptFunction getGetter() {
  91         throw new UnsupportedOperationException("get");
  92     }
  93 
  94     @Override
  95     public ScriptFunction getSetter() {
  96         throw new UnsupportedOperationException("set");
  97     }
  98 
  99     @Override
 100     public void setConfigurable(final boolean flag) {
 101         this.configurable = flag;
 102     }
 103 
 104     @Override
 105     public void setEnumerable(final boolean flag) {
 106         this.enumerable = flag;
 107     }
 108 
 109     @Override
 110     public void setWritable(final boolean flag) {
 111         throw new UnsupportedOperationException("set writable");
 112     }
 113 
 114     @Override
 115     public void setValue(final Object value) {
 116         throw new UnsupportedOperationException("set value");
 117     }
 118 
 119     @Override
 120     public void setGetter(final Object getter) {
 121         throw new UnsupportedOperationException("set getter");
 122     }
 123 
 124     @Override
 125     public void setSetter(final Object setter) {
 126         throw new UnsupportedOperationException("set setter");
 127     }
 128 
 129     @Override
 130     public PropertyDescriptor fillFrom(final ScriptObject sobj) {
 131         if (sobj.has(CONFIGURABLE)) {
 132             this.configurable = JSType.toBoolean(sobj.get(CONFIGURABLE));
 133         } else {
 134             delete(CONFIGURABLE, false);
 135         }
 136 
 137         if (sobj.has(ENUMERABLE)) {
 138             this.enumerable = JSType.toBoolean(sobj.get(ENUMERABLE));
 139         } else {
 140             delete(ENUMERABLE, false);
 141         }
 142 
 143         return this;
 144     }
 145 
 146     @Override
 147     public int type() {
 148         return GENERIC;
 149     }
 150 
 151     @Override
 152     public boolean hasAndEquals(final PropertyDescriptor other) {
 153         if (has(CONFIGURABLE) && other.has(CONFIGURABLE)) {
 154             if (isConfigurable() != other.isConfigurable()) {
 155                 return false;
 156             }
 157         }
 158 
 159         if (has(ENUMERABLE) && other.has(ENUMERABLE)) {
 160             if (isEnumerable() != other.isEnumerable()) {
 161                 return false;
 162             }
 163         }
 164 
 165         return true;
 166     }
 167 
 168     @Override
 169     public boolean equals(final Object obj) {
 170         if (this == obj) {
 171             return true;
 172         }
 173         if (!(obj instanceof GenericPropertyDescriptor)) {
 174             return false;
 175         }
 176 
 177         final GenericPropertyDescriptor other = (GenericPropertyDescriptor)obj;
 178         return ScriptRuntime.sameValue(configurable, other.configurable) &&
 179                ScriptRuntime.sameValue(enumerable, other.enumerable);
 180     }
 181 
 182     @Override
 183     public int hashCode() {
 184         int hash = 7;
 185         hash = 97 * hash + Objects.hashCode(this.configurable);
 186         hash = 97 * hash + Objects.hashCode(this.enumerable);
 187         return hash;
 188     }
 189 }