Implement Object.{entries, values} built-in methods (#3993)

Object.keys also updated to follow the ES11 standard.

JerryScript-DCO-1.0-Signed-off-by: Roland Takacs rtakacs@inf.u-szeged.hu
This commit is contained in:
Roland Takacs
2020-07-14 15:59:40 +02:00
committed by GitHub
parent 9dbbcab49d
commit 8fdbc6a85b
9 changed files with 470 additions and 9 deletions
+12
View File
@@ -357,6 +357,18 @@ typedef enum
* the fast access mode array back to normal array */
} ecma_list_properties_options_t;
/**
* Enumerable property name listing options.
*/
typedef enum
{
ECMA_ENUMERABLE_PROPERTY_KEYS, /**< List only property names */
ECMA_ENUMERABLE_PROPERTY_VALUES, /**< List only property values */
ECMA_ENUMERABLE_PROPERTY_ENTRIES, /**< List both propery names and values */
ECMA_ENUMERABLE_PROPERTY__COUNT /**< Number of enumerable property listing types */
} ecma_enumerable_property_names_options_t;
/**
* List enumerable properties and include the prototype chain.
*/
@@ -227,11 +227,10 @@ ecma_builtin_helper_get_to_locale_string_at_index (ecma_object_t *obj_p, /**< th
} /* ecma_builtin_helper_get_to_locale_string_at_index */
/**
* The Object.keys and Object.getOwnPropertyNames routine's common part.
* The Object's 'getOwnPropertyNames' routine.
*
* See also:
* ECMA-262 v5, 15.2.3.4 steps 2-5
* ECMA-262 v5, 15.2.3.14 steps 3-6
*
* @return ecma value - Array of property names.
* Returned value must be freed with ecma_free_value.
@@ -14,6 +14,7 @@
*/
#include "ecma-alloc.h"
#include "ecma-array-object.h"
#include "ecma-builtin-helpers.h"
#include "ecma-builtins.h"
#include "ecma-conversion.h"
@@ -58,6 +59,8 @@ enum
ECMA_OBJECT_ROUTINE_GET_OWN_PROPERTY_SYMBOLS,
ECMA_OBJECT_ROUTINE_GET_PROTOTYPE_OF,
ECMA_OBJECT_ROUTINE_KEYS,
ECMA_OBJECT_ROUTINE_VALUES,
ECMA_OBJECT_ROUTINE_ENTRIES,
/* These should be in this order. */
ECMA_OBJECT_ROUTINE_FREEZE,
@@ -695,19 +698,35 @@ ecma_builtin_object_object_is_extensible (ecma_object_t *obj_p) /**< routine's a
} /* ecma_builtin_object_object_is_extensible */
/**
* The Object object's 'keys' routine
* Common implementation of the Object object's 'keys', 'values', 'entries' routines
*
* See also:
* ECMA-262 v5, 15.2.3.14
* ECMA-262 v11, 19.1.2.17
* ECMA-262 v11, 19.1.2.22
* ECMA-262 v11, 19.1.2.5
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_object_object_keys (ecma_object_t *obj_p) /**< routine's argument */
ecma_builtin_object_object_keys_values_helper (ecma_object_t *obj_p, /**< routine's first argument */
ecma_enumerable_property_names_options_t option) /**< listing option */
{
return ecma_builtin_helper_object_get_properties (obj_p, ECMA_LIST_ENUMERABLE);
} /* ecma_builtin_object_object_keys */
/* 2. */
ecma_collection_t *props_p = ecma_op_object_get_enumerable_property_names (obj_p, option);
if (props_p == NULL)
{
return ECMA_VALUE_ERROR;
}
ecma_value_t *names_buffer_p = props_p->buffer_p;
/* 3. */
ecma_value_t array_value = ecma_op_create_array_object (names_buffer_p, props_p->item_count, false);
ecma_collection_free (props_p);
return array_value;
} /* ecma_builtin_object_object_keys_values_helper */
/**
* The Object object's 'getOwnPropertyDescriptor' routine
@@ -1228,7 +1247,7 @@ ecma_builtin_object_dispatch_routine (uint16_t builtin_routine_id, /**< built-in
JERRY_ASSERT (builtin_routine_id == ECMA_OBJECT_ROUTINE_DEFINE_PROPERTIES);
return ecma_builtin_object_object_define_properties (obj_p, arg2);
}
else if (builtin_routine_id <= ECMA_OBJECT_ROUTINE_KEYS)
else if (builtin_routine_id <= ECMA_OBJECT_ROUTINE_ENTRIES)
{
#if ENABLED (JERRY_ESNEXT)
ecma_value_t object = ecma_op_to_object (arg1);
@@ -1266,10 +1285,16 @@ ecma_builtin_object_dispatch_routine (uint16_t builtin_routine_id, /**< built-in
result = ecma_builtin_object_object_get_own_property_symbols (obj_p);
break;
}
case ECMA_OBJECT_ROUTINE_ENTRIES:
case ECMA_OBJECT_ROUTINE_VALUES:
#endif /* ENABLED (JERRY_ESNEXT) */
case ECMA_OBJECT_ROUTINE_KEYS:
{
result = ecma_builtin_object_object_keys (obj_p);
JERRY_ASSERT (builtin_routine_id - ECMA_OBJECT_ROUTINE_KEYS < ECMA_ENUMERABLE_PROPERTY__COUNT);
const int option = builtin_routine_id - ECMA_OBJECT_ROUTINE_KEYS;
result = ecma_builtin_object_object_keys_values_helper (obj_p,
(ecma_enumerable_property_names_options_t) option);
break;
}
case ECMA_OBJECT_ROUTINE_GET_OWN_PROPERTY_DESCRIPTOR:
@@ -53,6 +53,10 @@ ROUTINE (LIT_MAGIC_STRING_PREVENT_EXTENSIONS_UL, ECMA_OBJECT_ROUTINE_PREVENT_EXT
ROUTINE (LIT_MAGIC_STRING_IS_SEALED_UL, ECMA_OBJECT_ROUTINE_IS_SEALED, 1, 1)
ROUTINE (LIT_MAGIC_STRING_IS_FROZEN_UL, ECMA_OBJECT_ROUTINE_IS_FROZEN, 1, 1)
ROUTINE (LIT_MAGIC_STRING_IS_EXTENSIBLE, ECMA_OBJECT_ROUTINE_IS_EXTENSIBLE, 1, 1)
#if ENABLED (JERRY_ESNEXT)
ROUTINE (LIT_MAGIC_STRING_ENTRIES, ECMA_OBJECT_ROUTINE_ENTRIES, 1, 1)
ROUTINE (LIT_MAGIC_STRING_VALUES, ECMA_OBJECT_ROUTINE_VALUES, 1, 1)
#endif /* ENABLED (JERRY_ESNEXT) */
ROUTINE (LIT_MAGIC_STRING_KEYS, ECMA_OBJECT_ROUTINE_KEYS, 1, 1)
ROUTINE (LIT_MAGIC_STRING_GET_OWN_PROPERTY_DESCRIPTOR_UL, ECMA_OBJECT_ROUTINE_GET_OWN_PROPERTY_DESCRIPTOR, 2, 2)
#if ENABLED (JERRY_ESNEXT)
+112
View File
@@ -1983,6 +1983,118 @@ ecma_op_object_is_prototype_of (ecma_object_t *base_p, /**< base object */
} while (true);
} /* ecma_op_object_is_prototype_of */
/**
* Object's EnumerableOwnPropertyNames operation
*
* See also:
* ECMA-262 v11, 7.3.23
*
* @return NULL - if operation fails
* collection of property names / values / name-value pairs - otherwise
*/
ecma_collection_t *
ecma_op_object_get_enumerable_property_names (ecma_object_t *obj_p, /**< routine's first argument */
ecma_enumerable_property_names_options_t option) /**< listing option */
{
/* 2. */
ecma_collection_t *prop_names_p = ecma_op_object_get_property_names (obj_p, ECMA_LIST_NO_OPTS);
#if ENABLED (JERRY_BUILTIN_PROXY)
if (JERRY_UNLIKELY (prop_names_p == NULL))
{
return prop_names_p;
}
#endif /* ENABLED (JERRY_BUILTIN_PROXY) */
ecma_value_t *names_buffer_p = prop_names_p->buffer_p;
/* 3. */
ecma_collection_t *properties_p = ecma_new_collection ();
/* 4. */
for (uint32_t i = 0; i < prop_names_p->item_count; i++)
{
/* 4.a */
JERRY_ASSERT (ecma_is_value_string (names_buffer_p[i]));
ecma_string_t *key_p = ecma_get_string_from_value (names_buffer_p[i]);
/* 4.a.i */
ecma_property_descriptor_t prop_desc;
ecma_value_t status;
#if ENABLED (JERRY_BUILTIN_PROXY)
if (ECMA_OBJECT_IS_PROXY (obj_p))
{
status = ecma_proxy_object_get_own_property_descriptor (obj_p, key_p, &prop_desc);
}
else
#endif /* ENABLED (JERRY_BUILTIN_PROXY) */
{
status = ecma_op_object_get_own_property_descriptor (obj_p, key_p, &prop_desc);
}
if (ECMA_IS_VALUE_ERROR (status))
{
ecma_collection_free (prop_names_p);
ecma_collection_free (properties_p);
return NULL;
}
/* 4.a.ii */
if ((prop_desc.flags & ECMA_PROP_IS_ENUMERABLE) != 0)
{
/* 4.a.ii.1 */
if (option == ECMA_ENUMERABLE_PROPERTY_KEYS)
{
ecma_collection_push_back (properties_p, ecma_copy_value (names_buffer_p[i]));
}
else
{
/* 4.a.ii.2.a */
ecma_value_t value = ecma_op_object_get (obj_p, key_p);
if (ECMA_IS_VALUE_ERROR (value))
{
ecma_collection_free (prop_names_p);
ecma_collection_free (properties_p);
return NULL;
}
/* 4.a.ii.2.b */
if (option == ECMA_ENUMERABLE_PROPERTY_VALUES)
{
ecma_collection_push_back (properties_p, value);
}
else
{
/* 4.a.ii.2.c.i */
JERRY_ASSERT (option == ECMA_ENUMERABLE_PROPERTY_ENTRIES);
/* 4.a.ii.2.c.ii */
ecma_object_t *entry_p = ecma_op_new_fast_array_object (2);
ecma_fast_array_set_property (entry_p, 0, names_buffer_p[i]);
ecma_fast_array_set_property (entry_p, 1, value);
ecma_free_value (value);
/* 4.a.ii.2.c.iii */
ecma_collection_push_back (properties_p, ecma_make_object_value (entry_p));
}
}
}
if (ecma_is_value_true (status))
{
ecma_free_property_descriptor (&prop_desc);
}
}
ecma_collection_free (prop_names_p);
return properties_p;
} /* ecma_op_object_get_enumerable_property_names */
/**
* Get collection of property names
*
@@ -68,6 +68,8 @@ ecma_value_t ecma_op_object_get_own_property_descriptor (ecma_object_t *object_p
ecma_value_t ecma_op_object_has_instance (ecma_object_t *obj_p, ecma_value_t value);
ecma_value_t ecma_op_object_is_prototype_of (ecma_object_t *base_p, ecma_object_t *target_p);
ecma_collection_t * ecma_op_object_get_property_names (ecma_object_t *obj_p, uint32_t opts);
ecma_collection_t * ecma_op_object_get_enumerable_property_names (ecma_object_t *obj_p,
ecma_enumerable_property_names_options_t option);
lit_magic_string_id_t ecma_object_get_class_name (ecma_object_t *obj_p);
bool ecma_object_class_is (ecma_object_t *object_p, uint32_t class_id);