Implement the Symbol builtin object (#2601)

This patch contains the base functionalities that the new builtin object requires.

Currently unavailable:
 - print (Symbol('foo')) - this features requires the refactor of the print handler function
 - Several global symbol based builtin routines (follow up patch)

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
Robert Fancsik
2019-01-16 10:09:23 +01:00
committed by Robert Sipka
parent 08c7183ef8
commit 7e3d688e5b
43 changed files with 1896 additions and 212 deletions
+3
View File
@@ -592,6 +592,9 @@ ecma_gc_free_object (ecma_object_t *object_p) /**< object to free */
switch (ext_object_p->u.class_prop.class_id)
{
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
case LIT_MAGIC_STRING_SYMBOL_UL:
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
case LIT_MAGIC_STRING_STRING_UL:
case LIT_MAGIC_STRING_NUMBER_UL:
{
+27
View File
@@ -73,6 +73,7 @@ typedef enum
ECMA_TYPE_STRING = 1, /**< pointer to description of a string */
ECMA_TYPE_FLOAT = 2, /**< pointer to a 64 or 32 bit floating point number */
ECMA_TYPE_OBJECT = 3, /**< pointer to description of an object */
ECMA_TYPE_SYMBOL = 4, /**< pointer to description of a symbol */
ECMA_TYPE_DIRECT_STRING = 5, /**< directly encoded string values */
ECMA_TYPE_ERROR = 7, /**< pointer to description of an error reference (only supported by C API) */
ECMA_TYPE_POINTER = ECMA_TYPE_ERROR, /**< a generic aligned pointer */
@@ -361,6 +362,9 @@ typedef enum
* that are not indices */
ECMA_LIST_ENUMERABLE = (1 << 1), /**< exclude non-enumerable properties */
ECMA_LIST_PROTOTYPE = (1 << 2), /**< list properties from prototype chain */
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
ECMA_LIST_SYMBOLS = (1 << 3), /**< list symbol properties only */
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
} ecma_list_properties_options_t;
/**
@@ -1328,6 +1332,8 @@ typedef enum
stored locally in the string's descriptor */
ECMA_STRING_CONTAINER_MAGIC_STRING_EX, /**< the ecma-string is equal to one of external magic strings */
ECMA_STRING_CONTAINER_SYMBOL, /**< the ecma-string is a symbol */
ECMA_STRING_LITERAL_NUMBER, /**< a literal number which is used solely by the literal storage
* so no string processing function supports this type except
* the ecma_deref_ecma_string function. */
@@ -1398,6 +1404,7 @@ typedef struct
uint32_t magic_string_ex_id; /**< identifier of an external magic string (lit_magic_string_ex_id_t) */
ecma_value_t lit_number; /**< number (see ECMA_STRING_LITERAL_NUMBER) */
uint32_t common_uint32_field; /**< for zeroing and comparison in some cases */
ecma_value_t symbol_descriptor; /**< symbol descriptor string-value */
} u;
} ecma_string_t;
@@ -1535,6 +1542,26 @@ typedef struct
} ecma_extended_typedarray_object_t;
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
/**
* Flag for indicating whether the symbol is a well known symbol
*
* See also: 6.1.5.1
*/
#define ECMA_GLOBAL_SYMBOL_FLAG 0x01
/**
* Bitshift index for indicating whether the symbol is a well known symbol
*
* See also: 6.1.5.1
*/
#define ECMA_GLOBAL_SYMBOL_SHIFT 1
/**
* Bitshift index for the symbol hash property
*/
#define ECMA_SYMBOL_HASH_SHIFT 2
/**
* @}
* @}
@@ -198,6 +198,42 @@ ecma_new_ecma_string_from_magic_string_ex_id (lit_magic_string_ex_id_t id) /**<
return string_desc_p;
} /* ecma_new_ecma_string_from_magic_string_ex_id */
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
/**
* Allocate new ecma-string and fill it with reference to the symbol descriptor
*
* @return pointer to ecma-string descriptor
*/
ecma_string_t *
ecma_new_symbol_from_descriptor_string (ecma_value_t string_desc) /**< ecma-string */
{
JERRY_ASSERT (!ecma_is_value_symbol (string_desc));
ecma_string_t *symbol_p = ecma_alloc_string ();
symbol_p->refs_and_container = ECMA_STRING_REF_ONE | ECMA_STRING_CONTAINER_SYMBOL;
symbol_p->u.symbol_descriptor = string_desc;
symbol_p->hash = (uint16_t) (((uintptr_t) symbol_p) >> ECMA_SYMBOL_HASH_SHIFT);
JERRY_ASSERT ((symbol_p->hash & ECMA_GLOBAL_SYMBOL_FLAG) == 0);
return symbol_p;
} /* ecma_new_symbol_from_descriptor_string */
/**
* Check whether an ecma-string contains an ecma-symbol
*
* @return true - if the ecma-string contains an ecma-symbol
* false - otherwise
*/
bool
ecma_prop_name_is_symbol (ecma_string_t *string_p) /**< ecma-string */
{
JERRY_ASSERT (string_p != NULL);
return (!ECMA_IS_DIRECT_STRING (string_p)
&& ECMA_STRING_GET_CONTAINER (string_p) == ECMA_STRING_CONTAINER_SYMBOL);
} /* ecma_prop_name_is_symbol */
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
/**
* Allocate new ecma-string and fill it with characters from the utf8 string
*
@@ -934,6 +970,13 @@ ecma_deref_ecma_string (ecma_string_t *string_p) /**< ecma-string */
ecma_free_value (string_p->u.lit_number);
break;
}
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
case ECMA_STRING_CONTAINER_SYMBOL:
{
ecma_free_value (string_p->u.symbol_descriptor);
break;
}
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
default:
{
JERRY_ASSERT (ECMA_STRING_GET_CONTAINER (string_p) == ECMA_STRING_CONTAINER_UINT32_IN_DESC
@@ -1773,6 +1816,13 @@ ecma_compare_ecma_strings (const ecma_string_t *string1_p, /**< ecma-string */
return false;
}
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
if (string1_container == ECMA_STRING_CONTAINER_SYMBOL)
{
return false;
}
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
if (string1_container >= ECMA_STRING_CONTAINER_UINT32_IN_DESC)
{
return string1_p->u.common_uint32_field == string2_p->u.common_uint32_field;
+123 -1
View File
@@ -311,6 +311,36 @@ ecma_is_value_string (ecma_value_t value) /**< ecma value */
return ((value & (ECMA_VALUE_TYPE_MASK - 0x4)) == ECMA_TYPE_STRING);
} /* ecma_is_value_string */
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
/**
* Check if the value is symbol.
*
* @return true - if the value contains symbol value,
* false - otherwise
*/
inline bool JERRY_ATTR_CONST JERRY_ATTR_ALWAYS_INLINE
ecma_is_value_symbol (ecma_value_t value) /**< ecma value */
{
return (ecma_get_value_type_field (value) == ECMA_TYPE_SYMBOL);
} /* ecma_is_value_symbol */
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
/**
* Check if the value can be property name.
*
* @return true - if the value can be property name value,
* false - otherwise
*/
inline bool JERRY_ATTR_CONST JERRY_ATTR_ALWAYS_INLINE
ecma_is_value_prop_name (ecma_value_t value) /**< ecma value */
{
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
return ecma_is_value_string (value) || ecma_is_value_symbol (value);
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
return ecma_is_value_string (value);
} /* ecma_is_value_prop_name */
/**
* Check if the value is direct ecma-string.
*
@@ -383,6 +413,7 @@ ecma_check_value_type_is_spec_defined (ecma_value_t value) /**< ecma value */
|| ecma_is_value_boolean (value)
|| ecma_is_value_number (value)
|| ecma_is_value_string (value)
|| ECMA_ASSERT_VALUE_IS_SYMBOL (value)
|| ecma_is_value_object (value));
} /* ecma_check_value_type_is_spec_defined */
@@ -531,6 +562,9 @@ inline ecma_value_t JERRY_ATTR_PURE JERRY_ATTR_ALWAYS_INLINE
ecma_make_string_value (const ecma_string_t *ecma_string_p) /**< string to reference in value */
{
JERRY_ASSERT (ecma_string_p != NULL);
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
JERRY_ASSERT (!ecma_prop_name_is_symbol ((ecma_string_t *) ecma_string_p));
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
if ((((uintptr_t) ecma_string_p) & ECMA_VALUE_TYPE_MASK) != 0)
{
@@ -540,6 +574,42 @@ ecma_make_string_value (const ecma_string_t *ecma_string_p) /**< string to refer
return ecma_pointer_to_ecma_value (ecma_string_p) | ECMA_TYPE_STRING;
} /* ecma_make_string_value */
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
/**
* Symbol value constructor
*
* @return ecma-value representation of the string argument
*/
inline ecma_value_t JERRY_ATTR_PURE JERRY_ATTR_ALWAYS_INLINE
ecma_make_symbol_value (const ecma_string_t *ecma_symbol_p) /**< symbol to reference in value */
{
JERRY_ASSERT (ecma_symbol_p != NULL);
JERRY_ASSERT (ecma_prop_name_is_symbol ((ecma_string_t *) ecma_symbol_p));
return ecma_pointer_to_ecma_value (ecma_symbol_p) | ECMA_TYPE_SYMBOL;
} /* ecma_make_symbol_value */
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
/**
* Property-name value constructor
*
* @return ecma-value representation of a property name argument
*/
inline ecma_value_t JERRY_ATTR_PURE JERRY_ATTR_ALWAYS_INLINE
ecma_make_prop_name_value (const ecma_string_t *ecma_prop_name_p) /**< property name to reference in value */
{
JERRY_ASSERT (ecma_prop_name_p != NULL);
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
if (ecma_prop_name_is_symbol ((ecma_string_t *) ecma_prop_name_p))
{
return ecma_make_symbol_value (ecma_prop_name_p);
}
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
return ecma_make_string_value (ecma_prop_name_p);
} /* ecma_make_prop_name_value */
/**
* String value constructor
*
@@ -660,6 +730,39 @@ ecma_get_string_from_value (ecma_value_t value) /**< ecma value */
return (ecma_string_t *) ecma_get_pointer_from_ecma_value (value);
} /* ecma_get_string_from_value */
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
/**
* Get pointer to ecma-string from ecma value
*
* @return the string pointer
*/
inline ecma_string_t *JERRY_ATTR_PURE JERRY_ATTR_ALWAYS_INLINE
ecma_get_symbol_from_value (ecma_value_t value) /**< ecma value */
{
JERRY_ASSERT (ecma_is_value_symbol (value));
return (ecma_string_t *) ecma_get_pointer_from_ecma_value (value);
} /* ecma_get_symbol_from_value */
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
/**
* Get pointer to a property name from ecma value
*
* @return the string pointer
*/
inline ecma_string_t *JERRY_ATTR_PURE JERRY_ATTR_ALWAYS_INLINE
ecma_get_prop_name_from_value (ecma_value_t value) /**< ecma value */
{
JERRY_ASSERT (ecma_is_value_prop_name (value));
if ((value & ECMA_VALUE_TYPE_MASK) == ECMA_TYPE_DIRECT_STRING)
{
return (ecma_string_t *) (uintptr_t) value;
}
return (ecma_string_t *) ecma_get_pointer_from_ecma_value (value);
} /* ecma_get_prop_name_from_value */
/**
* Get pointer to ecma-object from ecma value
*
@@ -737,6 +840,13 @@ ecma_copy_value (ecma_value_t value) /**< value description */
ecma_ref_ecma_string (ecma_get_string_from_value (value));
return value;
}
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
case ECMA_TYPE_SYMBOL:
{
ecma_ref_ecma_string (ecma_get_symbol_from_value (value));
return value;
}
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
case ECMA_TYPE_OBJECT:
{
ecma_ref_object (ecma_get_object_from_value (value));
@@ -927,7 +1037,13 @@ ecma_free_value (ecma_value_t value) /**< value description */
ecma_deref_ecma_string (string_p);
break;
}
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
case ECMA_TYPE_SYMBOL:
{
ecma_deref_ecma_string (ecma_get_symbol_from_value (value));
break;
}
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
case ECMA_TYPE_OBJECT:
{
ecma_deref_object (ecma_get_object_from_value (value));
@@ -1027,6 +1143,12 @@ ecma_get_typeof_lit_id (ecma_value_t value) /**< input ecma value */
{
ret_value = LIT_MAGIC_STRING_STRING;
}
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
else if (ecma_is_value_symbol (value))
{
ret_value = LIT_MAGIC_STRING_SYMBOL;
}
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
else
{
JERRY_ASSERT (ecma_is_value_object (value));
+28
View File
@@ -137,6 +137,18 @@ typedef enum
*/
#define ECMA_BOOL_TO_BITFIELD(x) ((x) ? 1 : 0)
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
/**
* JERRY_ASSERT compatible macro for checking whether the given ecma-value is symbol
*/
#define ECMA_ASSERT_VALUE_IS_SYMBOL(value) (ecma_is_value_symbol ((value)))
#else /* CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
/**
* JERRY_ASSERT compatible macro for checking whether the given ecma-value is symbol
*/
#define ECMA_ASSERT_VALUE_IS_SYMBOL(value) (false)
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
/* ecma-helpers-value.c */
bool JERRY_ATTR_CONST ecma_is_value_direct (ecma_value_t value);
bool JERRY_ATTR_CONST ecma_is_value_simple (ecma_value_t value);
@@ -154,6 +166,10 @@ bool JERRY_ATTR_CONST ecma_are_values_integer_numbers (ecma_value_t first_value,
bool JERRY_ATTR_CONST ecma_is_value_float_number (ecma_value_t value);
bool JERRY_ATTR_CONST ecma_is_value_number (ecma_value_t value);
bool JERRY_ATTR_CONST ecma_is_value_string (ecma_value_t value);
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
bool JERRY_ATTR_CONST ecma_is_value_symbol (ecma_value_t value);
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
bool JERRY_ATTR_CONST ecma_is_value_prop_name (ecma_value_t value);
bool JERRY_ATTR_CONST ecma_is_value_direct_string (ecma_value_t value);
bool JERRY_ATTR_CONST ecma_is_value_non_direct_string (ecma_value_t value);
bool JERRY_ATTR_CONST ecma_is_value_object (ecma_value_t value);
@@ -169,6 +185,10 @@ ecma_value_t ecma_make_number_value (ecma_number_t ecma_number);
ecma_value_t ecma_make_int32_value (int32_t int32_number);
ecma_value_t ecma_make_uint32_value (uint32_t uint32_number);
ecma_value_t JERRY_ATTR_PURE ecma_make_string_value (const ecma_string_t *ecma_string_p);
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
ecma_value_t JERRY_ATTR_PURE ecma_make_symbol_value (const ecma_string_t *ecma_symbol_p);
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
ecma_value_t JERRY_ATTR_PURE ecma_make_prop_name_value (const ecma_string_t *ecma_prop_name_p);
ecma_value_t JERRY_ATTR_PURE ecma_make_magic_string_value (lit_magic_string_id_t id);
ecma_value_t JERRY_ATTR_PURE ecma_make_object_value (const ecma_object_t *object_p);
ecma_value_t JERRY_ATTR_PURE ecma_make_error_reference_value (const ecma_error_reference_t *error_ref_p);
@@ -177,6 +197,10 @@ ecma_integer_value_t JERRY_ATTR_CONST ecma_get_integer_from_value (ecma_value_t
ecma_number_t JERRY_ATTR_PURE ecma_get_float_from_value (ecma_value_t value);
ecma_number_t JERRY_ATTR_PURE ecma_get_number_from_value (ecma_value_t value);
ecma_string_t JERRY_ATTR_PURE *ecma_get_string_from_value (ecma_value_t value);
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
ecma_string_t JERRY_ATTR_PURE *ecma_get_symbol_from_value (ecma_value_t value);
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
ecma_string_t JERRY_ATTR_PURE *ecma_get_prop_name_from_value (ecma_value_t value);
ecma_object_t JERRY_ATTR_PURE *ecma_get_object_from_value (ecma_value_t value);
ecma_error_reference_t JERRY_ATTR_PURE *ecma_get_error_reference_from_value (ecma_value_t value);
void * JERRY_ATTR_PURE ecma_get_pointer_from_value (ecma_value_t value);
@@ -194,6 +218,10 @@ void ecma_free_number (ecma_value_t value);
lit_magic_string_id_t ecma_get_typeof_lit_id (ecma_value_t value);
/* ecma-helpers-string.c */
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
ecma_string_t *ecma_new_symbol_from_descriptor_string (ecma_value_t string_desc);
bool ecma_prop_name_is_symbol (ecma_string_t *string_p);
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
ecma_string_t *ecma_new_ecma_string_from_utf8 (const lit_utf8_byte_t *string_p, lit_utf8_size_t string_size);
ecma_string_t *ecma_new_ecma_string_from_utf8_converted_to_cesu8 (const lit_utf8_byte_t *string_p,
lit_utf8_size_t string_size);
@@ -57,6 +57,9 @@ ecma_free_string_list (ecma_lit_storage_item_t *string_list_p) /**< string list
void
ecma_finalize_lit_storage (void)
{
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
ecma_free_string_list (JERRY_CONTEXT (symbol_list_first_p));
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
ecma_free_string_list (JERRY_CONTEXT (string_list_first_p));
ecma_free_string_list (JERRY_CONTEXT (number_list_first_p));
} /* ecma_finalize_lit_storage */