Add Map, Set, WeakMap, WeakSet basic functionality to the API (#3502)
JerryScript-DCO-1.0-Signed-off-by: Daniel Balla dballa@inf.u-szeged.hu
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
#include "ecma-builtin-helpers.h"
|
||||
#include "ecma-builtins.h"
|
||||
#include "ecma-comparison.h"
|
||||
#include "ecma-container-object.h"
|
||||
#include "ecma-dataview-object.h"
|
||||
#include "ecma-exceptions.h"
|
||||
#include "ecma-eval.h"
|
||||
@@ -1001,6 +1002,18 @@ jerry_is_feature_enabled (const jerry_feature_t feature) /**< feature to check *
|
||||
#if ENABLED (JERRY_LOGGING)
|
||||
|| feature == JERRY_FEATURE_LOGGING
|
||||
#endif /* ENABLED (JERRY_LOGGING) */
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_MAP)
|
||||
|| feature == JERRY_FEATURE_MAP
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_MAP) */
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_SET)
|
||||
|| feature == JERRY_FEATURE_SET
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_SET) */
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_WEAKMAP)
|
||||
|| feature == JERRY_FEATURE_WEAKMAP
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_WEAKMAP) */
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_WEAKSET)
|
||||
|| feature == JERRY_FEATURE_WEAKSET
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_WEAKSET) */
|
||||
);
|
||||
} /* jerry_is_feature_enabled */
|
||||
|
||||
@@ -4376,6 +4389,141 @@ jerry_json_stringify (const jerry_value_t object_to_stringify) /**< a jerry_obje
|
||||
#endif /* ENABLED (JERRY_BUILTIN_JSON) */
|
||||
} /* jerry_json_stringify */
|
||||
|
||||
/**
|
||||
* Create a container type specified in jerry_container_type_t.
|
||||
* The container can be created with a list of arguments, which will be passed to the container constructor to be
|
||||
* inserted to the container.
|
||||
*
|
||||
* Note:
|
||||
* The returned value must be freed with jerry_release_value
|
||||
* @return jerry_value_t representing a container with the given type.
|
||||
*/
|
||||
jerry_value_t
|
||||
jerry_create_container (jerry_container_type_t container_type, /**< Type of the container */
|
||||
const jerry_value_t *arguments_list_p, /**< arguments list */
|
||||
jerry_length_t arguments_list_len) /**< Length of arguments list */
|
||||
{
|
||||
jerry_assert_api_available ();
|
||||
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_CONTAINER)
|
||||
for (jerry_length_t i = 0; i < arguments_list_len; i++)
|
||||
{
|
||||
if (ecma_is_value_error_reference (arguments_list_p[i]))
|
||||
{
|
||||
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (error_value_msg_p)));
|
||||
}
|
||||
}
|
||||
|
||||
switch (container_type)
|
||||
{
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_MAP)
|
||||
case JERRY_CONTAINER_TYPE_MAP:
|
||||
{
|
||||
return ecma_op_container_create (arguments_list_p,
|
||||
arguments_list_len,
|
||||
LIT_MAGIC_STRING_MAP_UL,
|
||||
ECMA_BUILTIN_ID_MAP_PROTOTYPE);
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_MAP) */
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_SET)
|
||||
case JERRY_CONTAINER_TYPE_SET:
|
||||
{
|
||||
return ecma_op_container_create (arguments_list_p,
|
||||
arguments_list_len,
|
||||
LIT_MAGIC_STRING_SET_UL,
|
||||
ECMA_BUILTIN_ID_SET_PROTOTYPE);
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_SET) */
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_WEAKMAP)
|
||||
case JERRY_CONTAINER_TYPE_WEAKMAP:
|
||||
{
|
||||
return ecma_op_container_create (arguments_list_p,
|
||||
arguments_list_len,
|
||||
LIT_MAGIC_STRING_WEAKMAP_UL,
|
||||
ECMA_BUILTIN_ID_WEAKMAP_PROTOTYPE);
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_WEAKMAP) */
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_WEAKSET)
|
||||
case JERRY_CONTAINER_TYPE_WEAKSET:
|
||||
{
|
||||
return ecma_op_container_create (arguments_list_p,
|
||||
arguments_list_len,
|
||||
LIT_MAGIC_STRING_WEAKSET_UL,
|
||||
ECMA_BUILTIN_ID_WEAKSET_PROTOTYPE);
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_WEAKSET) */
|
||||
default:
|
||||
{
|
||||
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("Invalid container type.")));
|
||||
}
|
||||
}
|
||||
#else /* !ENABLED (JERRY_ES2015_BUILTIN_CONTAINER) */
|
||||
JERRY_UNUSED (arguments_list_p);
|
||||
JERRY_UNUSED (arguments_list_len);
|
||||
JERRY_UNUSED (container_type);
|
||||
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("Containers are disabled.")));
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_CONTAINER) */
|
||||
} /* jerry_create_container */
|
||||
|
||||
/**
|
||||
* Get the type of the given container object.
|
||||
*
|
||||
* @return Corresponding type to the given container object.
|
||||
*/
|
||||
jerry_container_type_t
|
||||
jerry_get_container_type (const jerry_value_t value) /**< the container object */
|
||||
{
|
||||
jerry_assert_api_available ();
|
||||
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_CONTAINER)
|
||||
if (ecma_is_value_object (value))
|
||||
{
|
||||
ecma_object_t *obj_p = ecma_get_object_from_value (value);
|
||||
|
||||
if (ecma_get_object_type (obj_p) == ECMA_OBJECT_TYPE_CLASS)
|
||||
{
|
||||
uint16_t type = ((ecma_extended_object_t *) obj_p)->u.class_prop.class_id;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_MAP)
|
||||
case LIT_MAGIC_STRING_MAP_UL:
|
||||
{
|
||||
return JERRY_CONTAINER_TYPE_MAP;
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_MAP) */
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_SET)
|
||||
case LIT_MAGIC_STRING_SET_UL:
|
||||
{
|
||||
return JERRY_CONTAINER_TYPE_SET;
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_SET) */
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_WEAKMAP)
|
||||
case LIT_MAGIC_STRING_WEAKMAP_UL:
|
||||
{
|
||||
return JERRY_CONTAINER_TYPE_WEAKMAP;
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_WEAKMAP) */
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_WEAKSET)
|
||||
case LIT_MAGIC_STRING_WEAKSET_UL:
|
||||
{
|
||||
return JERRY_CONTAINER_TYPE_WEAKSET;
|
||||
}
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_WEAKSET) */
|
||||
default:
|
||||
{
|
||||
return JERRY_CONTAINER_TYPE_INVALID;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#else /* !ENABLED (JERRY_ES2015_BUILTIN_CONTAINER) */
|
||||
JERRY_UNUSED (value);
|
||||
#endif /* ENABLED (JERRY_ES2015_BUILTIN_CONTAINER) */
|
||||
return JERRY_CONTAINER_TYPE_INVALID;
|
||||
} /* jerry_get_container_type */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
@@ -100,6 +100,10 @@ typedef enum
|
||||
JERRY_FEATURE_SYMBOL, /**< symbol support */
|
||||
JERRY_FEATURE_DATAVIEW, /**< DataView support */
|
||||
JERRY_FEATURE_PROXY, /**< Proxy support */
|
||||
JERRY_FEATURE_MAP, /**< Map support */
|
||||
JERRY_FEATURE_SET, /**< Set support */
|
||||
JERRY_FEATURE_WEAKMAP, /**< WeakMap support */
|
||||
JERRY_FEATURE_WEAKSET, /**< WeakSet support */
|
||||
JERRY_FEATURE__COUNT /**< number of features. NOTE: must be at the end of the list */
|
||||
} jerry_feature_t;
|
||||
|
||||
@@ -669,6 +673,18 @@ typedef enum
|
||||
JERRY_TYPEDARRAY_FLOAT64,
|
||||
} jerry_typedarray_type_t;
|
||||
|
||||
/**
|
||||
* Container types.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
JERRY_CONTAINER_TYPE_INVALID = 0, /**< Invalid container */
|
||||
JERRY_CONTAINER_TYPE_MAP, /**< Map type */
|
||||
JERRY_CONTAINER_TYPE_SET, /**< Set type */
|
||||
JERRY_CONTAINER_TYPE_WEAKMAP, /**< WeakMap type */
|
||||
JERRY_CONTAINER_TYPE_WEAKSET, /**< WeakSet type */
|
||||
} jerry_container_type_t;
|
||||
|
||||
bool jerry_value_is_typedarray (jerry_value_t value);
|
||||
jerry_value_t jerry_create_typedarray (jerry_typedarray_type_t type_name, jerry_length_t length);
|
||||
jerry_value_t jerry_create_typedarray_for_arraybuffer_sz (jerry_typedarray_type_t type_name,
|
||||
@@ -684,6 +700,10 @@ jerry_value_t jerry_get_typedarray_buffer (jerry_value_t value,
|
||||
jerry_length_t *byte_length);
|
||||
jerry_value_t jerry_json_parse (const jerry_char_t *string_p, jerry_size_t string_size);
|
||||
jerry_value_t jerry_json_stringify (const jerry_value_t object_to_stringify);
|
||||
jerry_value_t jerry_create_container (jerry_container_type_t container_type,
|
||||
const jerry_value_t *arguments_list_p,
|
||||
jerry_length_t arguments_list_len);
|
||||
jerry_container_type_t jerry_get_container_type (const jerry_value_t value);
|
||||
|
||||
/**
|
||||
* @}
|
||||
|
||||
Reference in New Issue
Block a user