Implement WeakMap and WeakSet (#3328)
JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
This commit is contained in:
committed by
Robert Fancsik
parent
830011c033
commit
bd0cb33172
@@ -26,17 +26,17 @@
|
||||
#include "ecma-property-hashmap.h"
|
||||
#include "ecma-objects.h"
|
||||
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_MAP) || ENABLED (JERRY_ES2015_BUILTIN_SET)
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_CONTAINER)
|
||||
|
||||
/** \addtogroup ecma ECMA
|
||||
* @{
|
||||
*
|
||||
* \addtogroup \addtogroup ecmamaphelpers ECMA builtin map/set helper functions
|
||||
* \addtogroup \addtogroup ecmamaphelpers ECMA builtin Map/Set helper functions
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Handle calling [[Construct]] of built-in map/set like objects
|
||||
* Handle calling [[Construct]] of built-in Map/Set like objects
|
||||
*
|
||||
* @return ecma value
|
||||
*/
|
||||
@@ -47,179 +47,216 @@ ecma_op_container_create (const ecma_value_t *arguments_list_p, /**< arguments l
|
||||
ecma_builtin_id_t proto_id) /**< prototype builtin id */
|
||||
{
|
||||
JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
|
||||
JERRY_ASSERT (lit_id == LIT_MAGIC_STRING_MAP_UL
|
||||
|| lit_id == LIT_MAGIC_STRING_SET_UL
|
||||
|| lit_id == LIT_MAGIC_STRING_WEAKMAP_UL
|
||||
|| lit_id == LIT_MAGIC_STRING_WEAKSET_UL);
|
||||
|
||||
ecma_object_t *internal_object_p = ecma_create_object (NULL, 0, ECMA_OBJECT_TYPE_GENERAL);
|
||||
ecma_object_t *internal_object_p = ecma_create_object (NULL,
|
||||
sizeof (ecma_extended_object_t),
|
||||
ECMA_OBJECT_TYPE_CLASS);
|
||||
|
||||
ecma_extended_object_t *container_p = (ecma_extended_object_t *) internal_object_p;
|
||||
container_p->u.class_prop.class_id = LIT_INTERNAL_MAGIC_STRING_CONTAINER;
|
||||
container_p->u.class_prop.extra_info = ECMA_CONTAINER_FLAGS_EMPTY;
|
||||
container_p->u.class_prop.u.length = 0;
|
||||
|
||||
if (lit_id == LIT_MAGIC_STRING_WEAKMAP_UL || lit_id == LIT_MAGIC_STRING_WEAKSET_UL)
|
||||
{
|
||||
container_p->u.class_prop.extra_info |= ECMA_CONTAINER_FLAGS_WEAK;
|
||||
}
|
||||
|
||||
ecma_object_t *object_p = ecma_create_object (ecma_builtin_get (proto_id),
|
||||
sizeof (ecma_map_object_t),
|
||||
sizeof (ecma_extended_object_t),
|
||||
ECMA_OBJECT_TYPE_CLASS);
|
||||
|
||||
ecma_map_object_t *map_obj_p = (ecma_map_object_t *) object_p;
|
||||
map_obj_p->header.u.class_prop.class_id = (uint16_t) lit_id;
|
||||
map_obj_p->header.u.class_prop.u.value = ecma_make_object_value (internal_object_p);
|
||||
map_obj_p->size = 0;
|
||||
ecma_extended_object_t *map_obj_p = (ecma_extended_object_t *) object_p;
|
||||
map_obj_p->u.class_prop.class_id = (uint16_t) lit_id;
|
||||
ECMA_SET_INTERNAL_VALUE_POINTER (map_obj_p->u.class_prop.u.value, container_p);
|
||||
|
||||
ecma_deref_object (internal_object_p);
|
||||
|
||||
ecma_value_t set_value = ecma_make_object_value (object_p);
|
||||
ecma_value_t result = set_value;
|
||||
|
||||
#if ENABLED (JERRY_ES2015)
|
||||
if (arguments_list_len == 0)
|
||||
{
|
||||
return set_value;
|
||||
return result;
|
||||
}
|
||||
|
||||
ecma_value_t iterable = arguments_list_p[0];
|
||||
|
||||
if (ecma_is_value_undefined (iterable) || ecma_is_value_null (iterable))
|
||||
{
|
||||
return set_value;
|
||||
return result;
|
||||
}
|
||||
|
||||
ecma_value_t iter = ecma_op_get_iterator (iterable, ECMA_VALUE_EMPTY);
|
||||
|
||||
if (ECMA_IS_VALUE_ERROR (iter))
|
||||
lit_magic_string_id_t adder_string_id;
|
||||
if (lit_id == LIT_MAGIC_STRING_MAP_UL || lit_id == LIT_MAGIC_STRING_WEAKMAP_UL)
|
||||
{
|
||||
ecma_deref_object (object_p);
|
||||
return iter;
|
||||
adder_string_id = LIT_MAGIC_STRING_SET;
|
||||
}
|
||||
else
|
||||
{
|
||||
adder_string_id = LIT_MAGIC_STRING_ADD;
|
||||
}
|
||||
|
||||
result = ecma_op_object_get_by_magic_id (object_p, adder_string_id);
|
||||
if (ECMA_IS_VALUE_ERROR (result))
|
||||
{
|
||||
goto cleanup_object;
|
||||
}
|
||||
|
||||
if (!ecma_op_is_callable (result))
|
||||
{
|
||||
ecma_free_value (result);
|
||||
result = ecma_raise_type_error (ECMA_ERR_MSG ("add/set function is not callable."));
|
||||
goto cleanup_object;
|
||||
}
|
||||
|
||||
ecma_object_t *adder_func_p = ecma_get_object_from_value (result);
|
||||
|
||||
result = ecma_op_get_iterator (iterable, ECMA_VALUE_EMPTY);
|
||||
|
||||
if (ECMA_IS_VALUE_ERROR (result))
|
||||
{
|
||||
goto cleanup_adder;
|
||||
}
|
||||
|
||||
const ecma_value_t iter = result;
|
||||
|
||||
while (true)
|
||||
{
|
||||
ecma_value_t next = ecma_op_iterator_step (iter);
|
||||
result = ecma_op_iterator_step (iter);
|
||||
|
||||
if (ECMA_IS_VALUE_ERROR (next))
|
||||
if (ECMA_IS_VALUE_ERROR (result))
|
||||
{
|
||||
ecma_free_value (iter);
|
||||
ecma_deref_object (object_p);
|
||||
return next;
|
||||
goto cleanup_iter;
|
||||
}
|
||||
|
||||
if (ecma_is_value_false (next))
|
||||
if (ecma_is_value_false (result))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
ecma_value_t next_value = ecma_op_iterator_value (next);
|
||||
|
||||
if (ECMA_IS_VALUE_ERROR (next_value))
|
||||
{
|
||||
ecma_free_value (next);
|
||||
ecma_free_value (iter);
|
||||
ecma_deref_object (object_p);
|
||||
return next_value;
|
||||
}
|
||||
|
||||
ecma_value_t result;
|
||||
if (lit_id == LIT_MAGIC_STRING_SET_UL)
|
||||
{
|
||||
result = ecma_op_container_set (set_value, next_value, next_value, lit_id);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!ecma_is_value_object (next_value))
|
||||
{
|
||||
// TODO close the iterator when generator function will be supported
|
||||
ecma_free_value (next_value);
|
||||
ecma_free_value (next);
|
||||
ecma_free_value (iter);
|
||||
ecma_deref_object (object_p);
|
||||
return ecma_raise_type_error (ECMA_ERR_MSG ("Iterator value is not an object."));
|
||||
}
|
||||
|
||||
ecma_object_t *next_object_p = ecma_get_object_from_value (next_value);
|
||||
|
||||
ecma_value_t key = ecma_op_object_get_by_uint32_index (next_object_p, 0);
|
||||
|
||||
if (ECMA_IS_VALUE_ERROR (key))
|
||||
{
|
||||
// TODO close the iterator when generator function will be supported
|
||||
ecma_free_value (next_value);
|
||||
ecma_free_value (next);
|
||||
ecma_free_value (iter);
|
||||
ecma_deref_object (object_p);
|
||||
return key;
|
||||
}
|
||||
|
||||
ecma_value_t value = ecma_op_object_get_by_uint32_index (next_object_p, 1);
|
||||
|
||||
if (ECMA_IS_VALUE_ERROR (value))
|
||||
{
|
||||
// TODO close the iterator when generator function will be supported
|
||||
ecma_free_value (next_value);
|
||||
ecma_free_value (key);
|
||||
ecma_free_value (next);
|
||||
ecma_free_value (iter);
|
||||
ecma_deref_object (object_p);
|
||||
return value;
|
||||
}
|
||||
|
||||
result = ecma_op_container_set (set_value, key, value, lit_id);
|
||||
|
||||
ecma_free_value (key);
|
||||
ecma_free_value (value);
|
||||
}
|
||||
|
||||
ecma_free_value (next_value);
|
||||
const ecma_value_t next = result;
|
||||
result = ecma_op_iterator_value (next);
|
||||
ecma_free_value (next);
|
||||
|
||||
if (ECMA_IS_VALUE_ERROR (result))
|
||||
{
|
||||
goto cleanup_iter;
|
||||
}
|
||||
|
||||
if (lit_id == LIT_MAGIC_STRING_SET_UL || lit_id == LIT_MAGIC_STRING_WEAKSET_UL)
|
||||
{
|
||||
const ecma_value_t value = result;
|
||||
|
||||
ecma_value_t arguments[] = { value };
|
||||
result = ecma_op_function_call (adder_func_p, set_value, arguments, 1);
|
||||
|
||||
ecma_free_value (value);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!ecma_is_value_object (result))
|
||||
{
|
||||
// TODO close the iterator when generator function will be supported
|
||||
ecma_free_value (result);
|
||||
result = ecma_raise_type_error (ECMA_ERR_MSG ("Iterator value is not an object."));
|
||||
goto cleanup_iter;
|
||||
}
|
||||
|
||||
ecma_object_t *next_object_p = ecma_get_object_from_value (result);
|
||||
|
||||
result = ecma_op_object_get_by_uint32_index (next_object_p, 0);
|
||||
|
||||
if (ECMA_IS_VALUE_ERROR (result))
|
||||
{
|
||||
// TODO close the iterator when generator function will be supported
|
||||
ecma_deref_object (next_object_p);
|
||||
goto cleanup_iter;
|
||||
}
|
||||
|
||||
const ecma_value_t key = result;
|
||||
|
||||
result = ecma_op_object_get_by_uint32_index (next_object_p, 1);
|
||||
|
||||
if (ECMA_IS_VALUE_ERROR (result))
|
||||
{
|
||||
// TODO close the iterator when generator function will be supported
|
||||
ecma_deref_object (next_object_p);
|
||||
ecma_free_value (key);
|
||||
goto cleanup_iter;
|
||||
}
|
||||
|
||||
const ecma_value_t value = result;
|
||||
ecma_value_t arguments[] = { key, value };
|
||||
result = ecma_op_function_call (adder_func_p, set_value, arguments, 2);
|
||||
|
||||
ecma_free_value (key);
|
||||
ecma_free_value (value);
|
||||
ecma_deref_object (next_object_p);
|
||||
}
|
||||
|
||||
|
||||
if (ECMA_IS_VALUE_ERROR (result))
|
||||
{
|
||||
// TODO close the iterator when generator function will be supported
|
||||
ecma_free_value (iter);
|
||||
ecma_deref_object (object_p);
|
||||
return result;
|
||||
goto cleanup_iter;
|
||||
}
|
||||
|
||||
ecma_free_value (result);
|
||||
}
|
||||
|
||||
ecma_free_value (iter);
|
||||
ecma_deref_object (adder_func_p);
|
||||
return ecma_make_object_value (object_p);
|
||||
|
||||
cleanup_iter:
|
||||
ecma_free_value (iter);
|
||||
cleanup_adder:
|
||||
ecma_deref_object (adder_func_p);
|
||||
cleanup_object:
|
||||
ecma_deref_object (object_p);
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
|
||||
return set_value;
|
||||
return result;
|
||||
} /* ecma_op_container_create */
|
||||
|
||||
/**
|
||||
* Get map/set object pointer
|
||||
* Get Map/Set object pointer
|
||||
*
|
||||
* Note:
|
||||
* If the function returns with NULL, the error object has
|
||||
* already set, and the caller must return with ECMA_VALUE_ERROR
|
||||
*
|
||||
* @return pointer to the map/set if this_arg is a valid map/set object
|
||||
* @return pointer to the Map/Set if this_arg is a valid Map/Set object
|
||||
* NULL otherwise
|
||||
*/
|
||||
static ecma_map_object_t *
|
||||
static ecma_extended_object_t *
|
||||
ecma_op_container_get_object (ecma_value_t this_arg, /**< this argument */
|
||||
lit_magic_string_id_t lit_id) /**< internal class id */
|
||||
{
|
||||
if (ecma_is_value_object (this_arg))
|
||||
{
|
||||
ecma_map_object_t *map_object_p = (ecma_map_object_t *) ecma_get_object_from_value (this_arg);
|
||||
ecma_extended_object_t *map_object_p = (ecma_extended_object_t *) ecma_get_object_from_value (this_arg);
|
||||
|
||||
if (ecma_get_object_type (&map_object_p->header.object) == ECMA_OBJECT_TYPE_CLASS
|
||||
&& map_object_p->header.u.class_prop.class_id == lit_id)
|
||||
if (ecma_get_object_type ((ecma_object_t *) map_object_p) == ECMA_OBJECT_TYPE_CLASS
|
||||
&& map_object_p->u.class_prop.class_id == lit_id)
|
||||
{
|
||||
return map_object_p;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef JERRY_ENABLE_ERROR_MESSAGES
|
||||
char *msg_p = "Expected a Map object.";
|
||||
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_SET)
|
||||
if (lit_id == LIT_MAGIC_STRING_SET_UL)
|
||||
{
|
||||
msg_p = "Expected a Set object.";
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_SET) */
|
||||
|
||||
ecma_raise_type_error (ECMA_ERR_MSG (msg_p));
|
||||
#else /* !JERRY_ENABLE_ERROR_MESSAGES */
|
||||
#if ENABLED (JERRY_ERROR_MESSAGES)
|
||||
ecma_raise_standard_error_with_format (ECMA_ERROR_TYPE,
|
||||
"Expected a % object.",
|
||||
ecma_make_string_value (ecma_get_magic_string (lit_id)));
|
||||
#else /* !ENABLED (JERRY_ERROR_MESSAGES) */
|
||||
ecma_raise_type_error (NULL);
|
||||
#endif /* JERRY_ENABLE_ERROR_MESSAGES */
|
||||
#endif /* ENABLED (JERRY_ERROR_MESSAGES) */
|
||||
|
||||
return NULL;
|
||||
} /* ecma_op_container_get_object */
|
||||
@@ -242,37 +279,6 @@ ecma_op_container_to_key (ecma_value_t key_arg) /**< key argument */
|
||||
return prop_name_p;
|
||||
}
|
||||
|
||||
if (ecma_is_value_object (key_arg))
|
||||
{
|
||||
ecma_object_t *obj_p = ecma_get_object_from_value (key_arg);
|
||||
ecma_string_t *key_string_p = ecma_get_magic_string (LIT_INTERNAL_MAGIC_STRING_MAP_KEY);
|
||||
|
||||
if (ecma_op_object_is_fast_array (obj_p))
|
||||
{
|
||||
ecma_fast_array_convert_to_normal (obj_p);
|
||||
}
|
||||
|
||||
ecma_property_t *property_p = ecma_find_named_property (obj_p, key_string_p);
|
||||
ecma_string_t *object_key_string;
|
||||
|
||||
if (property_p == NULL)
|
||||
{
|
||||
object_key_string = ecma_new_map_key_string (key_arg);
|
||||
ecma_property_value_t *value_p = ecma_create_named_data_property (obj_p,
|
||||
key_string_p,
|
||||
ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE,
|
||||
NULL);
|
||||
value_p->value = ecma_make_string_value (object_key_string);
|
||||
}
|
||||
else
|
||||
{
|
||||
object_key_string = ecma_get_string_from_value (ECMA_PROPERTY_VALUE_PTR (property_p)->value);
|
||||
}
|
||||
|
||||
ecma_ref_ecma_string (object_key_string);
|
||||
return object_key_string;
|
||||
}
|
||||
|
||||
if (ecma_is_value_integer_number (key_arg))
|
||||
{
|
||||
ecma_integer_value_t integer = ecma_get_integer_from_value (key_arg);
|
||||
@@ -287,51 +293,62 @@ ecma_op_container_to_key (ecma_value_t key_arg) /**< key argument */
|
||||
} /* ecma_op_container_to_key */
|
||||
|
||||
/**
|
||||
* Returns with the size of the map/set object.
|
||||
* Returns with the size of the Map/Set object.
|
||||
*
|
||||
* @return size of the map/set object as ecma-value.
|
||||
* @return size of the Map/Set object as ecma-value.
|
||||
*/
|
||||
ecma_value_t
|
||||
ecma_op_container_size (ecma_value_t this_arg, /**< this argument */
|
||||
lit_magic_string_id_t lit_id) /**< internal class id */
|
||||
{
|
||||
ecma_map_object_t *map_object_p = ecma_op_container_get_object (this_arg, lit_id);
|
||||
ecma_extended_object_t *map_object_p = ecma_op_container_get_object (this_arg, lit_id);
|
||||
|
||||
if (map_object_p == NULL)
|
||||
{
|
||||
return ECMA_VALUE_ERROR;
|
||||
}
|
||||
|
||||
return ecma_make_uint32_value (map_object_p->size);
|
||||
ecma_extended_object_t *container_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_extended_object_t,
|
||||
map_object_p->u.class_prop.u.value);
|
||||
return ecma_make_uint32_value (container_p->u.class_prop.u.length);
|
||||
} /* ecma_op_container_size */
|
||||
|
||||
/**
|
||||
* The generic map prototype object's 'get' routine
|
||||
* The generic Map/WeakMap prototype object's 'get' routine
|
||||
*
|
||||
* @return ecma value
|
||||
* Returned value must be freed with ecma_free_value.
|
||||
*/
|
||||
ecma_value_t
|
||||
ecma_op_container_get (ecma_value_t this_arg, /**< this argument */
|
||||
ecma_value_t key_arg) /**< key argument */
|
||||
ecma_value_t key_arg, /**< key argument */
|
||||
lit_magic_string_id_t lit_id) /**< internal class id */
|
||||
{
|
||||
ecma_map_object_t *map_object_p = ecma_op_container_get_object (this_arg, LIT_MAGIC_STRING_MAP_UL);
|
||||
ecma_extended_object_t *map_object_p = ecma_op_container_get_object (this_arg, lit_id);
|
||||
|
||||
if (map_object_p == NULL)
|
||||
{
|
||||
return ECMA_VALUE_ERROR;
|
||||
}
|
||||
|
||||
if (map_object_p->size == 0)
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_WEAKMAP)
|
||||
if (lit_id == LIT_MAGIC_STRING_WEAKMAP_UL && !ecma_is_value_object (key_arg))
|
||||
{
|
||||
return ECMA_VALUE_UNDEFINED;
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_WEAKMAP) */
|
||||
|
||||
ecma_extended_object_t *container_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_extended_object_t,
|
||||
map_object_p->u.class_prop.u.value);
|
||||
|
||||
if (container_p->u.class_prop.u.length == 0)
|
||||
{
|
||||
return ECMA_VALUE_UNDEFINED;
|
||||
}
|
||||
|
||||
ecma_object_t *internal_obj_p = ecma_get_object_from_value (map_object_p->header.u.class_prop.u.value);
|
||||
|
||||
ecma_string_t *prop_name_p = ecma_op_container_to_key (key_arg);
|
||||
|
||||
ecma_property_t *property_p = ecma_find_named_property (internal_obj_p, prop_name_p);
|
||||
ecma_property_t *property_p = ecma_find_named_property ((ecma_object_t *) container_p, prop_name_p);
|
||||
|
||||
ecma_deref_ecma_string (prop_name_p);
|
||||
|
||||
@@ -344,7 +361,7 @@ ecma_op_container_get (ecma_value_t this_arg, /**< this argument */
|
||||
} /* ecma_op_container_get */
|
||||
|
||||
/**
|
||||
* The generic map/set prototype object's 'has' routine
|
||||
* The generic Map/Set prototype object's 'has' routine
|
||||
*
|
||||
* @return ecma value
|
||||
* Returned value must be freed with ecma_free_value.
|
||||
@@ -354,23 +371,32 @@ ecma_op_container_has (ecma_value_t this_arg, /**< this argument */
|
||||
ecma_value_t key_arg, /**< key argument */
|
||||
lit_magic_string_id_t lit_id) /**< internal class id */
|
||||
{
|
||||
ecma_map_object_t *map_object_p = ecma_op_container_get_object (this_arg, lit_id);
|
||||
ecma_extended_object_t *map_object_p = ecma_op_container_get_object (this_arg, lit_id);
|
||||
|
||||
if (map_object_p == NULL)
|
||||
{
|
||||
return ECMA_VALUE_ERROR;
|
||||
}
|
||||
|
||||
if (map_object_p->size == 0)
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_WEAKMAP) || ENABLED (JERRY_ES2015_BUILTIN_WEAKSET)
|
||||
if ((lit_id == LIT_MAGIC_STRING_WEAKMAP_UL || lit_id == LIT_MAGIC_STRING_WEAKSET_UL)
|
||||
&& !ecma_is_value_object (key_arg))
|
||||
{
|
||||
return ECMA_VALUE_FALSE;
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_WEAKMAP) || ENABLED (JERRY_ES2015_BUILTIN_WEAKSET) */
|
||||
|
||||
ecma_extended_object_t *container_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_extended_object_t,
|
||||
map_object_p->u.class_prop.u.value);
|
||||
|
||||
if (container_p->u.class_prop.u.length == 0)
|
||||
{
|
||||
return ECMA_VALUE_FALSE;
|
||||
}
|
||||
|
||||
ecma_object_t *internal_obj_p = ecma_get_object_from_value (map_object_p->header.u.class_prop.u.value);
|
||||
|
||||
ecma_string_t *prop_name_p = ecma_op_container_to_key (key_arg);
|
||||
|
||||
ecma_property_t *property_p = ecma_find_named_property (internal_obj_p, prop_name_p);
|
||||
ecma_property_t *property_p = ecma_find_named_property ((ecma_object_t *) container_p, prop_name_p);
|
||||
|
||||
ecma_deref_ecma_string (prop_name_p);
|
||||
|
||||
@@ -379,7 +405,7 @@ ecma_op_container_has (ecma_value_t this_arg, /**< this argument */
|
||||
} /* ecma_op_container_has */
|
||||
|
||||
/**
|
||||
* The generic map prototype object's 'set' and set prototype object's 'add' routine
|
||||
* The generic Map prototype object's 'set' and Set prototype object's 'add' routine
|
||||
*
|
||||
* @return ecma value
|
||||
* Returned value must be freed with ecma_free_value.
|
||||
@@ -390,44 +416,114 @@ ecma_op_container_set (ecma_value_t this_arg, /**< this argument */
|
||||
ecma_value_t value_arg, /**< value argument */
|
||||
lit_magic_string_id_t lit_id) /**< internal class id */
|
||||
{
|
||||
ecma_map_object_t *map_object_p = ecma_op_container_get_object (this_arg, lit_id);
|
||||
ecma_extended_object_t *map_object_p = ecma_op_container_get_object (this_arg, lit_id);
|
||||
|
||||
if (map_object_p == NULL)
|
||||
{
|
||||
return ECMA_VALUE_ERROR;
|
||||
}
|
||||
|
||||
ecma_object_t *internal_obj_p = ecma_get_object_from_value (map_object_p->header.u.class_prop.u.value);
|
||||
ecma_extended_object_t *container_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_extended_object_t,
|
||||
map_object_p->u.class_prop.u.value);
|
||||
|
||||
ecma_string_t *prop_name_p = ecma_op_container_to_key (key_arg);
|
||||
|
||||
ecma_property_t *property_p = ecma_find_named_property (internal_obj_p, prop_name_p);
|
||||
ecma_property_t *property_p = ecma_find_named_property ((ecma_object_t *) container_p, prop_name_p);
|
||||
|
||||
if (property_p == NULL)
|
||||
{
|
||||
ecma_property_value_t *value_p = ecma_create_named_data_property (internal_obj_p,
|
||||
ecma_property_value_t *value_p = ecma_create_named_data_property ((ecma_object_t *) container_p,
|
||||
prop_name_p,
|
||||
ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE,
|
||||
NULL);
|
||||
value_p->value = ecma_copy_value_if_not_object (value_arg);
|
||||
map_object_p->size++;
|
||||
container_p->u.class_prop.u.length++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ecma_is_value_empty (ECMA_PROPERTY_VALUE_PTR (property_p)->value))
|
||||
{
|
||||
map_object_p->size++;
|
||||
container_p->u.class_prop.u.length++;
|
||||
}
|
||||
ecma_named_data_property_assign_value (internal_obj_p, ECMA_PROPERTY_VALUE_PTR (property_p), value_arg);
|
||||
|
||||
ecma_named_data_property_assign_value ((ecma_object_t *) container_p,
|
||||
ECMA_PROPERTY_VALUE_PTR (property_p),
|
||||
value_arg);
|
||||
}
|
||||
|
||||
ecma_deref_ecma_string (prop_name_p);
|
||||
ecma_ref_object ((ecma_object_t *) &map_object_p->header);
|
||||
ecma_ref_object ((ecma_object_t *) map_object_p);
|
||||
return this_arg;
|
||||
} /* ecma_op_container_set */
|
||||
|
||||
/**
|
||||
* The generic map/set prototype object's 'forEach' routine
|
||||
* The generic WeakMap prototype object's 'set' and WeakSet prototype object's 'add' routine
|
||||
*
|
||||
* @return ecma value
|
||||
* Returned value must be freed with ecma_free_value.
|
||||
*/
|
||||
ecma_value_t
|
||||
ecma_op_container_set_weak (ecma_value_t this_arg, /**< this argument */
|
||||
ecma_value_t key_arg, /**< key argument */
|
||||
ecma_value_t value_arg, /**< value argument */
|
||||
lit_magic_string_id_t lit_id) /**< internal class id */
|
||||
{
|
||||
ecma_extended_object_t *map_object_p = ecma_op_container_get_object (this_arg, lit_id);
|
||||
|
||||
if (map_object_p == NULL)
|
||||
{
|
||||
return ECMA_VALUE_ERROR;
|
||||
}
|
||||
|
||||
if (!ecma_is_value_object (key_arg))
|
||||
{
|
||||
return ecma_raise_type_error (ECMA_ERR_MSG ("Key must be an object"));
|
||||
}
|
||||
|
||||
/* This will increment the refcount on map_object_p */
|
||||
ecma_op_container_set (this_arg, key_arg, value_arg, lit_id);
|
||||
|
||||
ecma_object_t *obj_p = ecma_get_object_from_value (key_arg);
|
||||
ecma_string_t *weak_refs_string_p = ecma_get_magic_string (LIT_INTERNAL_MAGIC_STRING_WEAK_REFS);
|
||||
|
||||
ecma_property_t *property_p = ecma_find_named_property (obj_p, weak_refs_string_p);
|
||||
ecma_collection_t *refs_p;
|
||||
|
||||
if (property_p == NULL)
|
||||
{
|
||||
ecma_property_value_t *value_p = ecma_create_named_data_property (obj_p,
|
||||
weak_refs_string_p,
|
||||
ECMA_PROPERTY_CONFIGURABLE_WRITABLE,
|
||||
&property_p);
|
||||
ECMA_CONVERT_DATA_PROPERTY_TO_INTERNAL_PROPERTY (property_p);
|
||||
refs_p = ecma_new_collection ();
|
||||
ECMA_SET_INTERNAL_VALUE_POINTER (value_p->value, refs_p);
|
||||
}
|
||||
else
|
||||
{
|
||||
refs_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_collection_t, (ECMA_PROPERTY_VALUE_PTR (property_p)->value));
|
||||
}
|
||||
|
||||
ecma_object_t *container_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t,
|
||||
map_object_p->u.class_prop.u.value);
|
||||
|
||||
const ecma_value_t container_value = ecma_make_object_value (container_p);
|
||||
|
||||
for (uint32_t i = 0; i < refs_p->item_count; i++)
|
||||
{
|
||||
if (ecma_is_value_empty (refs_p->buffer_p[i]))
|
||||
{
|
||||
refs_p->buffer_p[i] = container_value;
|
||||
return this_arg;
|
||||
}
|
||||
}
|
||||
|
||||
ecma_collection_push_back (refs_p, container_value);
|
||||
return this_arg;
|
||||
} /* ecma_op_container_set_weak */
|
||||
|
||||
/**
|
||||
* The generic Map/Set prototype object's 'forEach' routine
|
||||
*
|
||||
* @return ecma value
|
||||
* Returned value must be freed with ecma_free_value.
|
||||
@@ -439,7 +535,7 @@ ecma_op_container_foreach (ecma_value_t this_arg, /**< this argument */
|
||||
* invoke predicate */
|
||||
lit_magic_string_id_t lit_id) /**< internal class id */
|
||||
{
|
||||
ecma_map_object_t *map_object_p = ecma_op_container_get_object (this_arg, lit_id);
|
||||
ecma_extended_object_t *map_object_p = ecma_op_container_get_object (this_arg, lit_id);
|
||||
|
||||
if (map_object_p == NULL)
|
||||
{
|
||||
@@ -454,20 +550,21 @@ ecma_op_container_foreach (ecma_value_t this_arg, /**< this argument */
|
||||
JERRY_ASSERT (ecma_is_value_object (predicate));
|
||||
ecma_object_t *func_object_p = ecma_get_object_from_value (predicate);
|
||||
|
||||
ecma_object_t *internal_obj_p = ecma_get_object_from_value (map_object_p->header.u.class_prop.u.value);
|
||||
ecma_object_t *container_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t,
|
||||
map_object_p->u.class_prop.u.value);
|
||||
|
||||
ecma_collection_t *props_p = ecma_op_object_get_property_names (internal_obj_p, ECMA_LIST_NO_OPTS);
|
||||
ecma_collection_t *props_p = ecma_op_object_get_property_names (container_p, ECMA_LIST_NO_OPTS);
|
||||
|
||||
ecma_value_t *buffer_p = props_p->buffer_p;
|
||||
|
||||
ecma_value_t ret_value = ECMA_VALUE_UNDEFINED;
|
||||
|
||||
ecma_ref_object (internal_obj_p);
|
||||
ecma_ref_object (container_p);
|
||||
|
||||
for (uint32_t i = 0; i < props_p->item_count; i++)
|
||||
{
|
||||
ecma_string_t *prop_name_p = ecma_get_prop_name_from_value (buffer_p[i]);
|
||||
ecma_property_t *property_p = ecma_find_named_property (internal_obj_p, prop_name_p);
|
||||
ecma_property_t *property_p = ecma_find_named_property (container_p, prop_name_p);
|
||||
JERRY_ASSERT (property_p != NULL);
|
||||
|
||||
if (ecma_is_value_empty (ECMA_PROPERTY_VALUE_PTR (property_p)->value))
|
||||
@@ -514,7 +611,7 @@ ecma_op_container_foreach (ecma_value_t this_arg, /**< this argument */
|
||||
ecma_free_value (call_value);
|
||||
}
|
||||
|
||||
ecma_deref_object (internal_obj_p);
|
||||
ecma_deref_object (container_p);
|
||||
ecma_collection_free (props_p);
|
||||
|
||||
return ret_value;
|
||||
@@ -530,16 +627,23 @@ ecma_value_t
|
||||
ecma_op_container_clear (ecma_value_t this_arg, /**< this argument */
|
||||
lit_magic_string_id_t lit_id) /**< internal class id */
|
||||
{
|
||||
ecma_map_object_t *map_object_p = ecma_op_container_get_object (this_arg, lit_id);
|
||||
ecma_extended_object_t *map_object_p = ecma_op_container_get_object (this_arg, lit_id);
|
||||
|
||||
if (map_object_p == NULL)
|
||||
{
|
||||
return ECMA_VALUE_ERROR;
|
||||
}
|
||||
|
||||
ecma_object_t *internal_object_p = ecma_create_object (NULL, 0, ECMA_OBJECT_TYPE_GENERAL);
|
||||
map_object_p->header.u.class_prop.u.value = ecma_make_object_value (internal_object_p);
|
||||
map_object_p->size = 0;
|
||||
ecma_object_t *internal_object_p = ecma_create_object (NULL,
|
||||
sizeof (ecma_extended_object_t),
|
||||
ECMA_OBJECT_TYPE_CLASS);
|
||||
|
||||
ecma_extended_object_t *container_p = (ecma_extended_object_t *) internal_object_p;
|
||||
container_p->u.class_prop.class_id = LIT_INTERNAL_MAGIC_STRING_CONTAINER;
|
||||
container_p->u.class_prop.extra_info = ECMA_CONTAINER_FLAGS_EMPTY;
|
||||
container_p->u.class_prop.u.length = 0;
|
||||
|
||||
ECMA_SET_INTERNAL_VALUE_POINTER (map_object_p->u.class_prop.u.value, internal_object_p);
|
||||
|
||||
ecma_deref_object (internal_object_p);
|
||||
|
||||
@@ -547,7 +651,7 @@ ecma_op_container_clear (ecma_value_t this_arg, /**< this argument */
|
||||
} /* ecma_op_container_clear */
|
||||
|
||||
/**
|
||||
* The generic map/set prototype object's 'delete' routine
|
||||
* The generic Map/Set prototype object's 'delete' routine
|
||||
*
|
||||
* @return ecma value
|
||||
* Returned value must be freed with ecma_free_value.
|
||||
@@ -557,18 +661,19 @@ ecma_op_container_delete (ecma_value_t this_arg, /**< this argument */
|
||||
ecma_value_t key_arg, /**< key argument */
|
||||
lit_magic_string_id_t lit_id) /**< internal class id */
|
||||
{
|
||||
ecma_map_object_t *map_object_p = ecma_op_container_get_object (this_arg, lit_id);
|
||||
ecma_extended_object_t *map_object_p = ecma_op_container_get_object (this_arg, lit_id);
|
||||
|
||||
if (map_object_p == NULL)
|
||||
{
|
||||
return ECMA_VALUE_ERROR;
|
||||
}
|
||||
|
||||
ecma_object_t *internal_obj_p = ecma_get_object_from_value (map_object_p->header.u.class_prop.u.value);
|
||||
ecma_extended_object_t *container_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_extended_object_t,
|
||||
map_object_p->u.class_prop.u.value);
|
||||
|
||||
ecma_string_t *prop_name_p = ecma_op_container_to_key (key_arg);
|
||||
|
||||
ecma_property_t *property_p = ecma_find_named_property (internal_obj_p, prop_name_p);
|
||||
ecma_property_t *property_p = ecma_find_named_property ((ecma_object_t *) container_p, prop_name_p);
|
||||
|
||||
ecma_deref_ecma_string (prop_name_p);
|
||||
|
||||
@@ -577,12 +682,107 @@ ecma_op_container_delete (ecma_value_t this_arg, /**< this argument */
|
||||
return ECMA_VALUE_FALSE;
|
||||
}
|
||||
|
||||
ecma_named_data_property_assign_value (internal_obj_p, ECMA_PROPERTY_VALUE_PTR (property_p), ECMA_VALUE_EMPTY);
|
||||
map_object_p->size--;
|
||||
|
||||
ecma_named_data_property_assign_value ((ecma_object_t *) container_p,
|
||||
ECMA_PROPERTY_VALUE_PTR (property_p),
|
||||
ECMA_VALUE_EMPTY);
|
||||
container_p->u.class_prop.u.length--;
|
||||
|
||||
return ECMA_VALUE_TRUE;
|
||||
} /* ecma_op_container_delete */
|
||||
|
||||
/**
|
||||
* The generic WeakMap/WeakSet prototype object's 'delete' routine
|
||||
*
|
||||
* @return ecma value
|
||||
* Returned value must be freed with ecma_free_value.
|
||||
*/
|
||||
ecma_value_t
|
||||
ecma_op_container_delete_weak (ecma_value_t this_arg, /**< this argument */
|
||||
ecma_value_t key_arg, /**< key argument */
|
||||
lit_magic_string_id_t lit_id) /**< internal class id */
|
||||
{
|
||||
ecma_extended_object_t *map_object_p = ecma_op_container_get_object (this_arg, lit_id);
|
||||
|
||||
if (map_object_p == NULL)
|
||||
{
|
||||
return ECMA_VALUE_ERROR;
|
||||
}
|
||||
|
||||
if (!ecma_is_value_object (key_arg))
|
||||
{
|
||||
return ECMA_VALUE_FALSE;
|
||||
}
|
||||
|
||||
ecma_extended_object_t *container_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_extended_object_t,
|
||||
map_object_p->u.class_prop.u.value);
|
||||
|
||||
ecma_string_t *prop_name_p = ecma_op_container_to_key (key_arg);
|
||||
|
||||
ecma_property_t *property_p = ecma_find_named_property ((ecma_object_t *) container_p, prop_name_p);
|
||||
|
||||
ecma_deref_ecma_string (prop_name_p);
|
||||
|
||||
if (property_p == NULL)
|
||||
{
|
||||
return ECMA_VALUE_FALSE;
|
||||
}
|
||||
|
||||
ecma_delete_property ((ecma_object_t *) container_p, ECMA_PROPERTY_VALUE_PTR (property_p));
|
||||
|
||||
ecma_object_t *internal_obj_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t,
|
||||
map_object_p->u.class_prop.u.value);
|
||||
ecma_object_t *key_object_p = ecma_get_object_from_value (key_arg);
|
||||
ecma_op_container_unref_weak (key_object_p, ecma_make_object_value (internal_obj_p));
|
||||
|
||||
return ECMA_VALUE_TRUE;
|
||||
} /* ecma_op_container_delete_weak */
|
||||
|
||||
/**
|
||||
* Helper function to remove a weak reference to an object.
|
||||
*
|
||||
* @return ecma value
|
||||
* Returned value must be freed with ecma_free_value.
|
||||
*/
|
||||
void
|
||||
ecma_op_container_unref_weak (ecma_object_t *object_p, /**< this argument */
|
||||
ecma_value_t ref_holder) /**< key argument */
|
||||
{
|
||||
ecma_string_t *weak_refs_string_p = ecma_get_magic_string (LIT_INTERNAL_MAGIC_STRING_WEAK_REFS);
|
||||
|
||||
ecma_property_t *property_p = ecma_find_named_property (object_p, weak_refs_string_p);
|
||||
JERRY_ASSERT (property_p != NULL);
|
||||
|
||||
ecma_collection_t *refs_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_collection_t,
|
||||
ECMA_PROPERTY_VALUE_PTR (property_p)->value);
|
||||
for (uint32_t i = 0; i < refs_p->item_count; i++)
|
||||
{
|
||||
if (refs_p->buffer_p[i] == ref_holder)
|
||||
{
|
||||
refs_p->buffer_p[i] = ECMA_VALUE_EMPTY;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} /* ecma_op_container_unref_weak */
|
||||
|
||||
/**
|
||||
* Helper function to remove a key/value pair from a weak container object
|
||||
*/
|
||||
void
|
||||
ecma_op_container_remove_weak_entry (ecma_object_t *container_p, /**< internal container object */
|
||||
ecma_value_t key_arg) /**< key */
|
||||
{
|
||||
ecma_string_t *prop_name_p = ecma_new_map_key_string (key_arg);
|
||||
|
||||
ecma_property_t *property_p = ecma_find_named_property (container_p, prop_name_p);
|
||||
JERRY_ASSERT (property_p != NULL);
|
||||
|
||||
ecma_deref_ecma_string (prop_name_p);
|
||||
|
||||
ecma_delete_property (container_p, ECMA_PROPERTY_VALUE_PTR (property_p));
|
||||
((ecma_extended_object_t *) container_p)->u.class_prop.u.length--;
|
||||
} /* ecma_op_container_remove_weak_entry */
|
||||
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_ITERATOR)
|
||||
|
||||
/**
|
||||
@@ -595,7 +795,7 @@ ecma_op_container_delete (ecma_value_t this_arg, /**< this argument */
|
||||
* Note:
|
||||
* Returned value must be freed with ecma_free_value.
|
||||
*
|
||||
* @return set/map iterator object, if success
|
||||
* @return Map/Set iterator object, if success
|
||||
* error - otherwise
|
||||
*/
|
||||
ecma_value_t
|
||||
@@ -606,7 +806,7 @@ ecma_op_container_create_iterator (ecma_value_t this_arg, /**< this argument */
|
||||
ecma_builtin_id_t proto_id, /**< prototype builtin id */
|
||||
ecma_pseudo_array_type_t iterator_type) /**< type of the iterator */
|
||||
{
|
||||
ecma_map_object_t *map_object_p = ecma_op_container_get_object (this_arg, lit_id);
|
||||
ecma_extended_object_t *map_object_p = ecma_op_container_get_object (this_arg, lit_id);
|
||||
|
||||
if (map_object_p == NULL)
|
||||
{
|
||||
@@ -657,9 +857,10 @@ ecma_op_container_iterator_next (ecma_value_t this_val, /**< this argument */
|
||||
return ecma_create_iter_result_object (ECMA_VALUE_UNDEFINED, ECMA_VALUE_TRUE);
|
||||
}
|
||||
|
||||
ecma_map_object_t *map_object_p = (ecma_map_object_t *) (ecma_get_object_from_value (iterated_value));
|
||||
ecma_extended_object_t *map_object_p = (ecma_extended_object_t *) (ecma_get_object_from_value (iterated_value));
|
||||
|
||||
ecma_object_t *internal_obj_p = ecma_get_object_from_value (map_object_p->header.u.class_prop.u.value);
|
||||
ecma_object_t *internal_obj_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t,
|
||||
map_object_p->u.class_prop.u.value);
|
||||
ecma_collection_t *props_p = ecma_op_object_get_property_names (internal_obj_p, ECMA_LIST_NO_OPTS);
|
||||
|
||||
uint32_t length = props_p->item_count;
|
||||
@@ -783,4 +984,4 @@ ecma_op_container_iterator_next (ecma_value_t this_val, /**< this argument */
|
||||
* @}
|
||||
*/
|
||||
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_MAP) || ENABLED (JERRY_ES2015_BUILTIN_SET) */
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_CONTAINER) */
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
#include "ecma-globals.h"
|
||||
#include "ecma-builtins.h"
|
||||
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_MAP) || ENABLED (JERRY_ES2015_BUILTIN_SET)
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_CONTAINER)
|
||||
|
||||
/** \addtogroup ecma ECMA
|
||||
* @{
|
||||
@@ -31,15 +31,20 @@
|
||||
ecma_value_t ecma_op_container_create (const ecma_value_t *arguments_list_p, ecma_length_t arguments_list_len,
|
||||
lit_magic_string_id_t lit_id, ecma_builtin_id_t proto_id);
|
||||
ecma_value_t ecma_op_container_size (ecma_value_t this_arg, lit_magic_string_id_t lit_id);
|
||||
ecma_value_t ecma_op_container_get (ecma_value_t this_arg, ecma_value_t key_arg);
|
||||
ecma_value_t ecma_op_container_get (ecma_value_t this_arg, ecma_value_t key_arg, lit_magic_string_id_t lit_id);
|
||||
ecma_value_t ecma_op_container_foreach (ecma_value_t this_arg, ecma_value_t predicate, ecma_value_t predicate_this_arg,
|
||||
lit_magic_string_id_t lit_id);
|
||||
ecma_value_t ecma_op_container_has (ecma_value_t this_arg, ecma_value_t key_arg, lit_magic_string_id_t lit_id);
|
||||
ecma_value_t ecma_op_container_set (ecma_value_t this_arg, ecma_value_t key_arg, ecma_value_t value_arg,
|
||||
lit_magic_string_id_t lit_id);
|
||||
ecma_value_t ecma_op_container_set_weak (ecma_value_t this_arg, ecma_value_t key_arg, ecma_value_t value_arg,
|
||||
lit_magic_string_id_t lit_id);
|
||||
|
||||
ecma_value_t ecma_op_container_clear (ecma_value_t this_arg, lit_magic_string_id_t lit_id);
|
||||
ecma_value_t ecma_op_container_delete (ecma_value_t this_arg, ecma_value_t key_arg, lit_magic_string_id_t lit_id);
|
||||
ecma_value_t ecma_op_container_delete_weak (ecma_value_t this_arg, ecma_value_t key_arg, lit_magic_string_id_t lit_id);
|
||||
void ecma_op_container_unref_weak (ecma_object_t *object_p, ecma_value_t ref_holder);
|
||||
void ecma_op_container_remove_weak_entry (ecma_object_t *container_p, ecma_value_t key_arg);
|
||||
ecma_value_t ecma_op_container_create_iterator (ecma_value_t this_arg, uint8_t type, lit_magic_string_id_t lit_id,
|
||||
ecma_builtin_id_t proto_id, ecma_pseudo_array_type_t iterator_type);
|
||||
ecma_value_t ecma_op_container_iterator_next (ecma_value_t this_val, ecma_pseudo_array_type_t iterator_type);
|
||||
@@ -49,6 +54,6 @@ ecma_value_t ecma_op_container_iterator_next (ecma_value_t this_val, ecma_pseudo
|
||||
* @}
|
||||
*/
|
||||
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_MAP) || ENABLED (JERRY_ES2015_BUILTIN_SET) */
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_CONTAINER) */
|
||||
|
||||
#endif /* !ECMA_CONTAINER_OBJECT_H */
|
||||
|
||||
Reference in New Issue
Block a user