Implement Object.getOwnPropertyNames and Object.keys.
JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
#include "ecma-builtin-helpers.h"
|
||||
|
||||
#include "ecma-array-object.h"
|
||||
#include "ecma-builtins.h"
|
||||
#include "ecma-conversion.h"
|
||||
#include "ecma-function-object.h"
|
||||
@@ -187,6 +188,88 @@ ecma_builtin_helper_get_to_locale_string_at_index (ecma_object_t *obj_p, /** < t
|
||||
return ret_value;
|
||||
} /* ecma_builtin_helper_get_to_locale_string_at_index */
|
||||
|
||||
|
||||
/**
|
||||
* The Object.keys and Object.getOwnPropertyName routine's common part.
|
||||
*
|
||||
* See also:
|
||||
* ECMA-262 v5, 15.2.3.4 steps 2-5
|
||||
* ECMA-262 v5, 15.2.3.14 steps 3-6
|
||||
*
|
||||
* @return completion value - Array of property names.
|
||||
* Returned value must be freed with ecma_free_completion_value.
|
||||
*/
|
||||
ecma_completion_value_t
|
||||
ecma_builtin_helper_object_get_properties (ecma_object_t *obj_p, /** < object */
|
||||
bool only_enumerable_properties) /** < list enumerable properties? */
|
||||
{
|
||||
JERRY_ASSERT (obj_p != NULL);
|
||||
|
||||
ecma_completion_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
|
||||
JERRY_ASSERT (ecma_is_completion_value_normal (new_array));
|
||||
ecma_object_t *new_array_p = ecma_get_object_from_completion_value (new_array);
|
||||
|
||||
uint32_t index = 0;
|
||||
|
||||
for (ecma_property_t *property_p = ecma_get_property_list (obj_p);
|
||||
property_p != NULL;
|
||||
property_p = ECMA_GET_POINTER (ecma_property_t, property_p->next_property_p), index++)
|
||||
{
|
||||
ecma_string_t *property_name_p;
|
||||
|
||||
if (property_p->type == ECMA_PROPERTY_NAMEDDATA)
|
||||
{
|
||||
property_name_p = ECMA_GET_NON_NULL_POINTER (ecma_string_t,
|
||||
property_p->u.named_data_property.name_p);
|
||||
}
|
||||
else if (property_p->type == ECMA_PROPERTY_NAMEDACCESSOR)
|
||||
{
|
||||
property_name_p = ECMA_GET_NON_NULL_POINTER (ecma_string_t,
|
||||
property_p->u.named_accessor_property.name_p);
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (only_enumerable_properties && !ecma_is_property_enumerable (property_p))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
JERRY_ASSERT (property_name_p != NULL);
|
||||
|
||||
ecma_string_t *index_string_p = ecma_new_ecma_string_from_uint32 (index);
|
||||
|
||||
ecma_property_descriptor_t item_prop_desc = ecma_make_empty_property_descriptor ();
|
||||
{
|
||||
item_prop_desc.is_value_defined = true;
|
||||
item_prop_desc.value = ecma_make_string_value (property_name_p);
|
||||
|
||||
item_prop_desc.is_writable_defined = true;
|
||||
item_prop_desc.is_writable = true;
|
||||
|
||||
item_prop_desc.is_enumerable_defined = true;
|
||||
item_prop_desc.is_enumerable = true;
|
||||
|
||||
item_prop_desc.is_configurable_defined = true;
|
||||
item_prop_desc.is_configurable = true;
|
||||
}
|
||||
|
||||
ecma_completion_value_t completion = ecma_op_object_define_own_property (new_array_p,
|
||||
index_string_p,
|
||||
&item_prop_desc,
|
||||
false);
|
||||
|
||||
JERRY_ASSERT (ecma_is_completion_value_normal_true (completion));
|
||||
|
||||
ecma_free_completion_value (completion);
|
||||
ecma_deref_ecma_string (index_string_p);
|
||||
}
|
||||
|
||||
return new_array;
|
||||
} /* ecma_builtin_helper_object_get_properties */
|
||||
|
||||
/**
|
||||
* @}
|
||||
* @}
|
||||
|
||||
@@ -28,6 +28,8 @@
|
||||
|
||||
extern ecma_completion_value_t ecma_builtin_helper_object_to_string (const ecma_value_t this_arg);
|
||||
extern ecma_completion_value_t ecma_builtin_helper_get_to_locale_string_at_index (ecma_object_t *obj_p, uint32_t index);
|
||||
extern ecma_completion_value_t ecma_builtin_helper_object_get_properties (ecma_object_t *obj,
|
||||
bool only_enumerable_properties);
|
||||
|
||||
/**
|
||||
* @}
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
|
||||
#include "ecma-alloc.h"
|
||||
#include "ecma-array-object.h"
|
||||
#include "ecma-builtin-helpers.h"
|
||||
#include "ecma-builtins.h"
|
||||
#include "ecma-conversion.h"
|
||||
#include "ecma-exceptions.h"
|
||||
@@ -128,10 +130,24 @@ ecma_builtin_object_object_get_prototype_of (ecma_value_t this_arg, /**< 'this'
|
||||
* Returned value must be freed with ecma_free_completion_value.
|
||||
*/
|
||||
static ecma_completion_value_t
|
||||
ecma_builtin_object_object_get_own_property_names (ecma_value_t this_arg, /**< 'this' argument */
|
||||
ecma_builtin_object_object_get_own_property_names (ecma_value_t this_arg __attr_unused___, /**< 'this' argument */
|
||||
ecma_value_t arg) /**< routine's argument */
|
||||
{
|
||||
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
|
||||
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
|
||||
|
||||
if (!ecma_is_value_object (arg))
|
||||
{
|
||||
/* 1. */
|
||||
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
|
||||
}
|
||||
else
|
||||
{
|
||||
ecma_object_t *obj_p = ecma_get_object_from_value (arg);
|
||||
/* 2-5. */
|
||||
ret_value = ecma_builtin_helper_object_get_properties (obj_p, false);
|
||||
}
|
||||
|
||||
return ret_value;
|
||||
} /* ecma_builtin_object_object_get_own_property_names */
|
||||
|
||||
/**
|
||||
@@ -240,10 +256,24 @@ ecma_builtin_object_object_is_extensible (ecma_value_t this_arg, /**< 'this' arg
|
||||
* Returned value must be freed with ecma_free_completion_value.
|
||||
*/
|
||||
static ecma_completion_value_t
|
||||
ecma_builtin_object_object_keys (ecma_value_t this_arg, /**< 'this' argument */
|
||||
ecma_builtin_object_object_keys (ecma_value_t this_arg __attr_unused___, /**< 'this' argument */
|
||||
ecma_value_t arg) /**< routine's argument */
|
||||
{
|
||||
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
|
||||
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
|
||||
|
||||
if (!ecma_is_value_object (arg))
|
||||
{
|
||||
/* 1. */
|
||||
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
|
||||
}
|
||||
else
|
||||
{
|
||||
ecma_object_t *obj_p = ecma_get_object_from_value (arg);
|
||||
/* 3-6. */
|
||||
ret_value = ecma_builtin_helper_object_get_properties (obj_p, true);
|
||||
}
|
||||
|
||||
return ret_value;
|
||||
} /* ecma_builtin_object_object_keys */
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user