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
use libc::c_void;
use chakracore_sys::*;
use context::ContextGuard;
use error::*;
use util::jstry;
use Property;
use super::{Value, Array};
type BeforeCollectCallback = Fn(&Value);
pub struct Object(JsValueRef);
impl Object {
pub fn new(_guard: &ContextGuard) -> Self {
let mut value = JsValueRef::new();
unsafe {
jsassert!(JsCreateObject(&mut value));
Self::from_raw(value)
}
}
pub fn set(&self, _guard: &ContextGuard, key: &Property, value: &Value) {
jsassert!(unsafe { JsSetProperty(self.as_raw(), key.as_raw(), value.as_raw(), false) });
}
pub fn set_index(&self, guard: &ContextGuard, index: u32, value: &Value) {
let index = super::Number::new(guard, index as i32);
jsassert!(unsafe { JsSetIndexedProperty(self.as_raw(), index.as_raw(), value.as_raw()) });
}
pub fn get(&self, _guard: &ContextGuard, key: &Property) -> Value {
let mut result = JsValueRef::new();
unsafe {
jsassert!(JsGetProperty(self.as_raw(), key.as_raw(), &mut result));
Value::from_raw(result)
}
}
pub fn get_index(&self, guard: &ContextGuard, index: u32) -> Value {
let index = super::Number::new(guard, index as i32);
let mut result = JsValueRef::new();
unsafe {
jsassert!(JsGetIndexedProperty(self.as_raw(), index.as_raw(), &mut result));
Value::from_raw(result)
}
}
pub fn delete(&self, _guard: &ContextGuard, key: &Property) -> bool {
let mut result = JsValueRef::new();
unsafe {
jsassert!(JsDeleteProperty(self.as_raw(), key.as_raw(), false, &mut result));
super::Boolean::from_raw(result).value()
}
}
pub fn delete_index(&self, guard: &ContextGuard, index: u32) {
let index = super::Number::new(guard, index as i32);
jsassert!(unsafe { JsDeleteIndexedProperty(self.as_raw(), index.as_raw()) });
}
pub fn has(&self, _guard: &ContextGuard, key: &Property) -> bool {
let mut result = false;
jsassert!(unsafe { JsHasProperty(self.as_raw(), key.as_raw(), &mut result) });
result
}
pub fn has_index(&self, guard: &ContextGuard, index: u32) -> bool {
let mut result = false;
let index = super::Number::new(guard, index as i32);
jsassert!(unsafe { JsHasIndexedProperty(self.as_raw(), index.as_raw(), &mut result) });
result
}
pub fn define_property(&self, _guard: &ContextGuard, key: &Property, desc: &Object) -> bool {
let mut result = false;
jsassert!(unsafe {
JsDefineProperty(self.as_raw(), key.as_raw(), desc.as_raw(), &mut result)
});
result
}
pub fn set_prototype(&self, _guard: &ContextGuard, prototype: &Value) -> Result<()> {
unsafe { jstry(JsSetPrototype(self.as_raw(), prototype.as_raw())) }
}
pub fn get_prototype(&self, _guard: &ContextGuard) -> Value {
let mut prototype = JsValueRef::new();
unsafe {
jsassert!(JsGetPrototype(self.as_raw(), &mut prototype));
Value::from_raw(prototype)
}
}
pub fn get_own_property_names(&self, _guard: &ContextGuard) -> Array {
let mut properties = JsValueRef::new();
unsafe {
jsassert!(JsGetOwnPropertyNames(self.as_raw(), &mut properties));
Array::from_raw(properties)
}
}
pub fn prevent_extension(&self) {
jsassert!(unsafe { JsPreventExtension(self.as_raw()) });
}
pub fn is_extensible(&self) -> bool {
let mut result = false;
jsassert!(unsafe { JsGetExtensionAllowed(self.as_raw(), &mut result) });
result
}
pub unsafe fn set_collect_callback(&self, callback: Box<BeforeCollectCallback>) {
let wrapper = Box::new(callback);
let api = JsSetObjectBeforeCollectCallback;
jsassert!(api(self.as_raw(),
Box::into_raw(wrapper) as *mut _,
Some(Self::collect)));
}
pub fn is_same(value: &Value) -> bool {
match value.get_type() {
JsValueType::Object |
JsValueType::Function |
JsValueType::Error |
JsValueType::Array |
JsValueType::ArrayBuffer |
JsValueType::TypedArray |
JsValueType::DataView => true,
_ => false,
}
}
unsafe extern "system" fn collect(value: JsValueRef, data: *mut c_void) {
let wrapper: Box<Box<BeforeCollectCallback>> = Box::from_raw(data as *mut _);
wrapper(&Value::from_raw(value));
}
}
reference!(Object);
inherit!(Object, Value);
#[cfg(test)]
mod tests {
use {test, value, Property};
#[test]
fn properties() {
test::run_with_context(|guard| {
let object = value::Object::new(guard);
let prop_foo = Property::new(guard, "foo");
let prop_bar = Property::new(guard, "bar");
object.set(guard, &prop_foo, &value::Number::new(guard, 10));
object.set(guard, &prop_bar, &value::null(guard));
assert_eq!(object.get(guard, &prop_foo).to_integer(guard), 10);
assert!(object.get(guard, &prop_bar).is_null());
let properties = object.get_own_property_names(guard)
.iter(guard)
.map(|val| val.to_string(guard))
.collect::<Vec<_>>();
assert_eq!(properties, ["foo", "bar"]);
assert!(object.has(guard, &prop_foo));
object.delete(guard, &prop_foo);
assert!(!object.has(guard, &prop_foo));
});
}
}