Skip to content

Commit 40d358b

Browse files
committed
Create ecma_builtin_helper_object_get_properties function.
The Object.keys and Object.getOwnPropertyNames have a common logic with just a minor difference, so to avoid code duplication we have created the ecma_builtin_helper_object_get_properties function. JerryScript-DCO-1.0-Signed-off-by: Peter Gal [email protected]
1 parent 3ec0f9b commit 40d358b

File tree

3 files changed

+89
-129
lines changed

3 files changed

+89
-129
lines changed

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "ecma-builtin-helpers.h"
1818

19+
#include "ecma-array-object.h"
1920
#include "ecma-builtins.h"
2021
#include "ecma-conversion.h"
2122
#include "ecma-function-object.h"
@@ -189,6 +190,87 @@ ecma_builtin_helper_get_to_locale_string_at_index (ecma_object_t *obj_p, /** < t
189190
return ret_value;
190191
} /* ecma_builtin_helper_get_to_locale_string_at_index */
191192

193+
194+
/**
195+
* The Object.keys and Object.getOwnPropertyName routine's common part.
196+
*
197+
* See also:
198+
* ECMA-262 v5, 15.2.3.4 steps 2-5
199+
* ECMA-262 v5, 15.2.3.14 steps 3-6
200+
*
201+
* @return completion value - Array of property names.
202+
* Returned value must be freed with ecma_free_completion_value.
203+
*/
204+
ecma_completion_value_t
205+
ecma_builtin_helper_object_get_properties (ecma_object_t *obj_p, /** < object */
206+
bool only_enumerable_properties) /** < list enumerable properties? */
207+
{
208+
JERRY_ASSERT (obj_p != NULL);
209+
210+
ecma_completion_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
211+
ecma_object_t *new_array_p = ecma_get_object_from_completion_value (new_array);
212+
213+
uint32_t index = 0;
214+
215+
for (ecma_property_t *property_p = ecma_get_property_list (obj_p);
216+
property_p != NULL;
217+
property_p = ECMA_GET_POINTER (ecma_property_t, property_p->next_property_p), index++)
218+
{
219+
ecma_string_t *property_name_p;
220+
221+
if (property_p->type == ECMA_PROPERTY_NAMEDDATA)
222+
{
223+
property_name_p = ECMA_GET_NON_NULL_POINTER (ecma_string_t,
224+
property_p->u.named_data_property.name_p);
225+
}
226+
else if (property_p->type == ECMA_PROPERTY_NAMEDACCESSOR)
227+
{
228+
property_name_p = ECMA_GET_NON_NULL_POINTER (ecma_string_t,
229+
property_p->u.named_accessor_property.name_p);
230+
}
231+
else
232+
{
233+
continue;
234+
}
235+
236+
if (only_enumerable_properties && !ecma_is_property_enumerable (property_p))
237+
{
238+
continue;
239+
}
240+
241+
JERRY_ASSERT (property_name_p != NULL);
242+
243+
ecma_string_t *index_string_p = ecma_new_ecma_string_from_uint32 (index);
244+
245+
ecma_property_descriptor_t item_prop_desc = ecma_make_empty_property_descriptor ();
246+
{
247+
item_prop_desc.is_value_defined = true;
248+
item_prop_desc.value = ecma_make_string_value (property_name_p);
249+
250+
item_prop_desc.is_writable_defined = true;
251+
item_prop_desc.is_writable = true;
252+
253+
item_prop_desc.is_enumerable_defined = true;
254+
item_prop_desc.is_enumerable = true;
255+
256+
item_prop_desc.is_configurable_defined = true;
257+
item_prop_desc.is_configurable = true;
258+
}
259+
260+
ecma_completion_value_t completion = ecma_op_object_define_own_property (new_array_p,
261+
index_string_p,
262+
&item_prop_desc,
263+
false);
264+
265+
JERRY_ASSERT (ecma_is_completion_value_normal_true (completion)
266+
|| ecma_is_completion_value_normal_false (completion));
267+
268+
ecma_deref_ecma_string (index_string_p);
269+
}
270+
271+
return new_array;
272+
} /* ecma_builtin_helper_object_get_properties */
273+
192274
/**
193275
* @}
194276
* @}

jerry-core/ecma/builtin-objects/ecma-builtin-helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
extern ecma_completion_value_t ecma_builtin_helper_object_to_string (const ecma_value_t this_arg);
3030
extern ecma_completion_value_t ecma_builtin_helper_get_to_locale_string_at_index (ecma_object_t *obj_p, uint32_t index);
31+
extern ecma_completion_value_t ecma_builtin_helper_object_get_properties (ecma_object_t *obj,
32+
bool only_enumerable_properties);
3133

3234
/**
3335
* @}

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

Lines changed: 5 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "ecma-alloc.h"
1818
#include "ecma-array-object.h"
19+
#include "ecma-builtin-helpers.h"
1920
#include "ecma-builtins.h"
2021
#include "ecma-conversion.h"
2122
#include "ecma-exceptions.h"
@@ -142,70 +143,8 @@ ecma_builtin_object_object_get_own_property_names (ecma_value_t this_arg __attr_
142143
else
143144
{
144145
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;
146+
/* 2-5. */
147+
ret_value = ecma_builtin_helper_object_get_properties (obj_p, false);
209148
}
210149

211150
return ret_value;
@@ -330,71 +269,8 @@ ecma_builtin_object_object_keys (ecma_value_t this_arg __attr_unused___, /**< 't
330269
else
331270
{
332271
ecma_object_t *obj_p = ecma_get_object_from_value (arg);
333-
334-
/* 2. */
335-
ecma_completion_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
336-
ecma_object_t *new_array_p = ecma_get_object_from_completion_value (new_array);
337-
338-
/* 4. */
339-
uint32_t index = 0;
340-
341-
/* 5. */
342-
for (ecma_property_t *property_p = ecma_get_property_list (obj_p);
343-
property_p != NULL;
344-
property_p = ECMA_GET_POINTER (ecma_property_t, property_p->next_property_p), index++)
345-
{
346-
if (property_p->type == ECMA_PROPERTY_INTERNAL
347-
|| !ecma_is_property_enumerable (property_p))
348-
{
349-
continue;
350-
}
351-
352-
ecma_string_t *property_name_p;
353-
354-
if (property_p->type == ECMA_PROPERTY_NAMEDDATA)
355-
{
356-
property_name_p = ECMA_GET_NON_NULL_POINTER (ecma_string_t,
357-
property_p->u.named_data_property.name_p);
358-
}
359-
else if (property_p->type == ECMA_PROPERTY_NAMEDACCESSOR)
360-
{
361-
property_name_p = ECMA_GET_NON_NULL_POINTER (ecma_string_t,
362-
property_p->u.named_accessor_property.name_p);
363-
}
364-
365-
JERRY_ASSERT (property_name_p != NULL);
366-
367-
/* 5.a */
368-
ecma_string_t *index_string_p = ecma_new_ecma_string_from_uint32 (index);
369-
370-
ecma_property_descriptor_t item_prop_desc = ecma_make_empty_property_descriptor ();
371-
{
372-
item_prop_desc.is_value_defined = true;
373-
item_prop_desc.value = ecma_make_string_value (property_name_p);
374-
375-
item_prop_desc.is_writable_defined = true;
376-
item_prop_desc.is_writable = true;
377-
378-
item_prop_desc.is_enumerable_defined = true;
379-
item_prop_desc.is_enumerable = true;
380-
381-
item_prop_desc.is_configurable_defined = true;
382-
item_prop_desc.is_configurable = true;
383-
}
384-
385-
ecma_completion_value_t completion = ecma_op_object_define_own_property (new_array_p,
386-
index_string_p,
387-
&item_prop_desc,
388-
false);
389-
390-
JERRY_ASSERT (ecma_is_completion_value_normal_true (completion)
391-
|| ecma_is_completion_value_normal_false (completion));
392-
393-
ecma_deref_ecma_string (index_string_p);
394-
}
395-
396-
/* 6. */
397-
ret_value = new_array;
272+
/* 3-6. */
273+
ret_value = ecma_builtin_helper_object_get_properties (obj_p, true);
398274
}
399275

400276
return ret_value;

0 commit comments

Comments
 (0)