Skip to content

Commit 9f1c1fd

Browse files
committed
Implement Object.getOwnPropertyNames.
JerryScript-DCO-1.0-Signed-off-by: Peter Gal [email protected]
1 parent aaeec7f commit 9f1c1fd

File tree

2 files changed

+152
-2
lines changed

2 files changed

+152
-2
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-object.cpp

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include "ecma-alloc.h"
18+
#include "ecma-array-object.h"
1819
#include "ecma-builtins.h"
1920
#include "ecma-conversion.h"
2021
#include "ecma-exceptions.h"
@@ -128,10 +129,86 @@ ecma_builtin_object_object_get_prototype_of (ecma_value_t this_arg, /**< 'this'
128129
* Returned value must be freed with ecma_free_completion_value.
129130
*/
130131
static ecma_completion_value_t
131-
ecma_builtin_object_object_get_own_property_names (ecma_value_t this_arg, /**< 'this' argument */
132+
ecma_builtin_object_object_get_own_property_names (ecma_value_t this_arg __attr_unused___, /**< 'this' argument */
132133
ecma_value_t arg) /**< routine's argument */
133134
{
134-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
135+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
136+
137+
if (!ecma_is_value_object (arg))
138+
{
139+
/* 1. */
140+
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
141+
}
142+
else
143+
{
144+
ecma_object_t *obj_p = ecma_get_object_from_value (arg);
145+
146+
/* 2. */
147+
ecma_completion_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
148+
ecma_object_t *new_array_p = ecma_get_object_from_completion_value (new_array);
149+
150+
/* 3. */
151+
uint32_t n = 0;
152+
153+
/* 4. */
154+
for (ecma_property_t *property_p = ecma_get_property_list (obj_p);
155+
property_p != NULL;
156+
property_p = ECMA_GET_POINTER (ecma_property_t, property_p->next_property_p), n++)
157+
{
158+
ecma_string_t *property_name_p;
159+
160+
/* 4.a. */
161+
if (property_p->type == ECMA_PROPERTY_NAMEDDATA)
162+
{
163+
property_name_p = ECMA_GET_NON_NULL_POINTER (ecma_string_t,
164+
property_p->u.named_data_property.name_p);
165+
}
166+
else if (property_p->type == ECMA_PROPERTY_NAMEDACCESSOR)
167+
{
168+
property_name_p = ECMA_GET_NON_NULL_POINTER (ecma_string_t,
169+
property_p->u.named_accessor_property.name_p);
170+
}
171+
else
172+
{
173+
continue;
174+
}
175+
176+
JERRY_ASSERT (property_name_p != NULL);
177+
178+
/* 4.b */
179+
ecma_string_t *index_string_p = ecma_new_ecma_string_from_uint32 (n);
180+
181+
ecma_property_descriptor_t item_prop_desc = ecma_make_empty_property_descriptor ();
182+
{
183+
item_prop_desc.is_value_defined = true;
184+
item_prop_desc.value = ecma_make_string_value (property_name_p);
185+
186+
item_prop_desc.is_writable_defined = true;
187+
item_prop_desc.is_writable = true;
188+
189+
item_prop_desc.is_enumerable_defined = true;
190+
item_prop_desc.is_enumerable = true;
191+
192+
item_prop_desc.is_configurable_defined = true;
193+
item_prop_desc.is_configurable = true;
194+
}
195+
196+
ecma_completion_value_t completion = ecma_op_object_define_own_property (new_array_p,
197+
index_string_p,
198+
&item_prop_desc,
199+
false);
200+
201+
JERRY_ASSERT (ecma_is_completion_value_normal_true (completion)
202+
|| ecma_is_completion_value_normal_false (completion));
203+
204+
ecma_deref_ecma_string (index_string_p);
205+
}
206+
207+
/* 5. */
208+
ret_value = new_array;
209+
}
210+
211+
return ret_value;
135212
} /* ecma_builtin_object_object_get_own_property_names */
136213

137214
/**
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2015 Samsung Electronics Co., Ltd.
2+
// Copyright 2015 University of Szeged.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
// Test array
17+
var arr = ['a', 'b', 'c'];
18+
var props = Object.getOwnPropertyNames(arr);
19+
// props should contain: 0,1,2,length and the order is not defined!
20+
assert (props.indexOf("0") !== -1);
21+
assert (props.indexOf("1") !== -1);
22+
assert (props.indexOf("2") !== -1);
23+
assert (props.indexOf("length") !== -1);
24+
assert (props.length === 4);
25+
26+
// Test object
27+
var obj = {key1: 'a', key3: 'b', key2: 'c', key4: 'c', key5: ''};
28+
props = Object.getOwnPropertyNames(obj);
29+
// props should contain: key1,key2,key3,key4,key5 and the order is not defined!
30+
assert (props.indexOf("key1") !== -1);
31+
assert (props.indexOf("key2") !== -1);
32+
assert (props.indexOf("key3") !== -1);
33+
assert (props.indexOf("key4") !== -1);
34+
assert (props.indexOf("key5") !== -1);
35+
assert (props.length === 5);
36+
37+
var obj2 = {};
38+
Object.defineProperties(obj2, {
39+
key_one: {enumerable: true, value: 'one'},
40+
key_two: {enumerable: false, value: 'two'},
41+
});
42+
43+
props = Object.getOwnPropertyNames(obj2);
44+
// props should contain: key_one,key_two and the order is not defined!
45+
assert (props.indexOf("key_one") !== -1);
46+
assert (props.indexOf("key_two") !== -1);
47+
assert (props.length === 2);
48+
49+
// Test prototype chain
50+
function Parent() {}
51+
Parent.prototype.inheritedMethod = function() {};
52+
53+
function Child() {
54+
this.prop = 5;
55+
this.method = function() {};
56+
}
57+
Child.prototype = new Parent;
58+
Child.prototype.prototypeMethod = function() {};
59+
60+
props = Object.getOwnPropertyNames (new Child());
61+
// props should contain: prop,method and the order is not defined!
62+
assert (props.indexOf("prop") !== -1);
63+
assert (props.indexOf("method") !== -1);
64+
65+
assert (props.length === 2);
66+
67+
// Test non-object argument
68+
try {
69+
Object.getOwnPrototypeNames("hello");
70+
assert (false);
71+
} catch (e) {
72+
assert (e instanceof TypeError);
73+
}

0 commit comments

Comments
 (0)