1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

use crate::vm::value::Value;
use crate::vm::value::Undefined;
use crate::vm::value::Boolean;
use crate::vm::value::String;
use crate::vm::value::Symbol;
// use builtin::Function;


use std::fmt;
use std::cmp;
use std::hash;
use std::string;
use std::vec::Vec;
use std::default::Default;
use std::collections::VecDeque;
use std::collections::LinkedList;
use std::collections::HashMap;
use std::collections::BTreeMap;
use std::collections::HashSet;
use std::collections::BTreeSet;

// Fundamental Objects
// 19.1 Object Objects
// 19.2 Function Objects
// 19.3 Boolean Objects
// 19.4 Symbol Objects
// 19.5 Error Objects

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
pub enum ObjectKind {
    Normal,   // hasConstructor() == false && isCallable() == false
    Function, // isCallable() == true && hasConstructor() == true || false
    Instance, // hasConstructor() == true
}

#[derive(Debug, Clone)]
pub struct Object {
    pub properties: HashMap<PropertyKey, Property>,
    pub is_frozen: bool,
    pub is_sealed: bool,
    pub is_extensible: bool,
    pub kind: ObjectKind,
    // pub constructor: Option<Function>,
}


#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum PropertyKey {
    String(String),
    Symbol(Symbol),
}

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Property {
    pub descriptor: PropertyDescriptor,
    pub enumerable: bool,
    pub configurable: bool,
}

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum PropertyDescriptor {
    Data(DataPropertyDescriptor),
    Accessor(AccessorPropertyDescriptor),
}

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct DataPropertyDescriptor {
    pub value: Box<Value>, // NOTE: Any ECMAScript Value
    pub writable: bool,
}

#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct AccessorPropertyDescriptor {
    pub get: Box<Value>,  // WARN: Value::Undefined || Value::Object<+ Callable>
    pub set: Box<Value>,  // WARN: Value::Undefined || Value::Object<+ Callable>
}



impl hash::Hash for Object {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        (self as *const Object).hash(state);
    }
}

impl PartialEq for Object {
    fn eq(&self, other: &Object) -> bool {
        (self as *const Object).eq( &(other as *const Object) )
    }
}

impl Eq for Object {}

impl PartialOrd for Object {
    fn partial_cmp(&self, other: &Object) -> Option<cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Object {
    fn cmp(&self, other: &Object) -> cmp::Ordering {
        (self as *const Object).cmp( &(other as *const Object) )
    }
}


impl Object {
    pub fn get(&self) -> Option<Value> {

        unimplemented!()
    }

    pub fn set(&mut self, key: Value) -> bool {
        self.properties.insert(key.into(), Property::default());
        true
    }

    pub fn construct(&self) -> Option<Value> {
        unimplemented!()
    }
}


impl fmt::Display for Object {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[object Object]")
    }
}


impl Default for DataPropertyDescriptor {
    fn default() -> Self {
        DataPropertyDescriptor {
            value: Box::new(Undefined.into()),
            writable: false
        }
    }
}

impl Default for AccessorPropertyDescriptor {
    fn default() -> Self {
        AccessorPropertyDescriptor {
            get: Box::new(Undefined.into()),
            set: Box::new(Undefined.into()),
        }
    }
}

impl Default for PropertyDescriptor {
    fn default() -> Self {
        PropertyDescriptor::Data(DataPropertyDescriptor::default())
    }
}

impl Default for Property {
    fn default() -> Self {
        Property {
            descriptor: PropertyDescriptor::default(),
            enumerable: false,
            configurable: false,
        }
    }
}


impl fmt::Display for PropertyKey {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            PropertyKey::String(ref s) => s.fmt(f),
            PropertyKey::Symbol(ref s) => s.fmt(f),
        }
    }
}

impl fmt::Display for PropertyDescriptor {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            PropertyDescriptor::Data(ref data_prop_descptor) => data_prop_descptor.fmt(f),
            PropertyDescriptor::Accessor(ref accesor_prop_descptor) => accesor_prop_descptor.fmt(f),
        }
        
    }
}

impl fmt::Display for DataPropertyDescriptor {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "value: {}, writable: {}", self.value, self.writable)
    }
}

impl fmt::Display for AccessorPropertyDescriptor {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "get: {}, set: {}", self.get, self.set)
    }
}

impl fmt::Display for Property {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{{ {}, enumerable: {}, configurable: {} }}", self.descriptor, self.enumerable, self.configurable)
    }
}


// impl From<Vec<Value>> for Object {
//     fn from(vec: Vec<Value>) -> Self {
//         unimplemented!()
//     }
// }

// impl From<VecDeque<Value>> for Object {
//     fn from(vec: Vec<Value>) -> Self {
//         unimplemented!()
//     }
// }