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
@@ -205,6 +205,13 @@ OBJECT_VALUE (LIT_MAGIC_STRING_MAP_UL,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
#endif /* !CONFIG_DISABLE_ES2015_MAP_BUILTIN */
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
/* ECMA-262 v6, 19.4.1.1 */
OBJECT_VALUE (LIT_MAGIC_STRING_SYMBOL_UL,
ECMA_BUILTIN_ID_SYMBOL,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
/* Routine properties:
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
@@ -25,6 +25,12 @@
#define STRING_VALUE(name, magic_string_id, prop_attributes)
#endif /* !STRING_VALUE */
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
#ifndef SYMBOL_VALUE
#define SYMBOL_VALUE(name, desc_string_id)
#endif /* !SYMBOL_VALUE */
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
#ifndef OBJECT_VALUE
#define OBJECT_VALUE(name, obj_builtin_id, prop_attributes)
#endif /* !OBJECT_VALUE */
@@ -33,6 +39,10 @@
#define ROUTINE(name, c_function_name, args_number, length_prop_value)
#endif /* !ROUTINE */
#ifndef ROUTINE_CONFIGURABLE_ONLY
#define ROUTINE_CONFIGURABLE_ONLY(name, c_function_name, args_number, length_prop_value)
#endif /* !ROUTINE_CONFIGURABLE_ONLY */
#ifndef ACCESSOR_READ_WRITE
#define ACCESSOR_READ_WRITE(name, c_getter_func_name, c_setter_func_name, prop_attributes)
#endif /* !ACCESSOR_READ_WRITE */
@@ -16,7 +16,11 @@
#undef SIMPLE_VALUE
#undef NUMBER_VALUE
#undef STRING_VALUE
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
#undef SYMBOL_VALUE
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
#undef OBJECT_VALUE
#undef ROUTINE
#undef ROUTINE_CONFIGURABLE_ONLY
#undef ACCESSOR_READ_WRITE
#undef ACCESSOR_READ_ONLY
@@ -187,7 +187,7 @@ ecma_builtin_helper_get_to_locale_string_at_index (ecma_object_t *obj_p, /**< th
*/
ecma_value_t
ecma_builtin_helper_object_get_properties (ecma_object_t *obj_p, /**< object */
bool only_enumerable_properties) /**< list enumerable properties? */
uint32_t opts) /**< any combination of ecma_list_properties_options_t */
{
JERRY_ASSERT (obj_p != NULL);
@@ -197,9 +197,7 @@ ecma_builtin_helper_object_get_properties (ecma_object_t *obj_p, /**< object */
uint32_t index = 0;
ecma_collection_header_t *props_p;
props_p = ecma_op_object_get_property_names (obj_p,
only_enumerable_properties ? ECMA_LIST_ENUMERABLE : ECMA_LIST_NO_OPTS);
ecma_collection_header_t *props_p = ecma_op_object_get_property_names (obj_p, opts);
ecma_value_t *ecma_value_p = ecma_collection_iterator_init (props_p);
@@ -31,7 +31,7 @@ ecma_builtin_helper_object_to_string (const ecma_value_t this_arg);
ecma_value_t
ecma_builtin_helper_get_to_locale_string_at_index (ecma_object_t *obj_p, uint32_t index);
ecma_value_t
ecma_builtin_helper_object_get_properties (ecma_object_t *obj_p, bool only_enumerable_properties);
ecma_builtin_helper_object_get_properties (ecma_object_t *obj_p, uint32_t opts);
ecma_value_t
ecma_builtin_helper_array_concat_value (ecma_object_t *obj_p, uint32_t *length_p, ecma_value_t value);
uint32_t
@@ -43,6 +43,8 @@
const ecma_value_t *arguments_list_p, ecma_length_t arguments_list_len
#define ROUTINE(name, c_function_name, args_number, length_prop_value) \
static ecma_value_t c_function_name (ROUTINE_ARG_LIST_ ## args_number);
#define ROUTINE_CONFIGURABLE_ONLY(name, c_function_name, args_number, length_prop_value) \
static ecma_value_t c_function_name (ROUTINE_ARG_LIST_ ## args_number);
#define ACCESSOR_READ_WRITE(name, c_getter_func_name, c_setter_func_name, prop_attributes) \
static ecma_value_t c_getter_func_name (ROUTINE_ARG_LIST_0); \
static ecma_value_t c_setter_func_name (ROUTINE_ARG_LIST_1);
@@ -64,6 +66,8 @@ enum
PASTE (ECMA_ROUTINE_START_, BUILTIN_UNDERSCORED_ID) = ECMA_BUILTIN_ID__COUNT - 1,
#define ROUTINE(name, c_function_name, args_number, length_prop_value) \
ECMA_ROUTINE_ ## name ## c_function_name,
#define ROUTINE_CONFIGURABLE_ONLY(name, c_function_name, args_number, length_prop_value) \
ECMA_ROUTINE_ ## name ## c_function_name,
#define ACCESSOR_READ_WRITE(name, c_getter_func_name, c_setter_func_name, prop_attributes) \
ECMA_ACCESSOR_ ## name ## c_getter_func_name, \
ECMA_ACCESSOR_ ## name ## c_setter_func_name,
@@ -87,6 +91,13 @@ const ecma_builtin_property_descriptor_t PROPERTY_DESCRIPTOR_LIST_NAME[] =
ECMA_PROPERTY_CONFIGURABLE_WRITABLE, \
ECMA_ROUTINE_VALUE (ECMA_ROUTINE_ ## name ## c_function_name, length_prop_value) \
},
#define ROUTINE_CONFIGURABLE_ONLY(name, c_function_name, args_number, length_prop_value) \
{ \
name, \
ECMA_BUILTIN_PROPERTY_ROUTINE, \
ECMA_PROPERTY_FLAG_CONFIGURABLE, \
ECMA_ROUTINE_VALUE (ECMA_ROUTINE_ ## name ## c_function_name, length_prop_value) \
},
#else /* BUILTIN_CUSTOM_DISPATCH */
#define ROUTINE(name, c_function_name, args_number, length_prop_value) \
{ \
@@ -95,6 +106,13 @@ const ecma_builtin_property_descriptor_t PROPERTY_DESCRIPTOR_LIST_NAME[] =
ECMA_PROPERTY_CONFIGURABLE_WRITABLE, \
ECMA_ROUTINE_VALUE (c_function_name, length_prop_value) \
},
#define ROUTINE_CONFIGURABLE_ONLY(name, c_function_name, args_number, length_prop_value) \
{ \
name, \
ECMA_BUILTIN_PROPERTY_ROUTINE, \
ECMA_PROPERTY_FLAG_CONFIGURABLE, \
ECMA_ROUTINE_VALUE (c_function_name, length_prop_value) \
},
#endif /* !BUILTIN_CUSTOM_DISPATCH */
#define OBJECT_VALUE(name, obj_builtin_id, prop_attributes) \
{ \
@@ -124,6 +142,15 @@ const ecma_builtin_property_descriptor_t PROPERTY_DESCRIPTOR_LIST_NAME[] =
prop_attributes, \
magic_string_id \
},
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
#define SYMBOL_VALUE(name, desc_string_id) \
{ \
name, \
ECMA_BUILTIN_PROPERTY_SYMBOL, \
ECMA_PROPERTY_FIXED, \
desc_string_id \
},
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
#define ACCESSOR_READ_WRITE(name, c_getter_name, c_setter_name, prop_attributes) \
{ \
name, \
@@ -183,6 +210,11 @@ DISPATCH_ROUTINE_ROUTINE_NAME (uint16_t builtin_routine_id, /**< built-in wide r
{ \
return c_function_name (this_arg_value ROUTINE_ARG_LIST_ ## args_number); \
}
#define ROUTINE_CONFIGURABLE_ONLY(name, c_function_name, args_number, length_prop_value) \
case ECMA_ROUTINE_ ## name ## c_function_name: \
{ \
return c_function_name (this_arg_value ROUTINE_ARG_LIST_ ## args_number); \
}
#define ACCESSOR_READ_WRITE(name, c_getter_func_name, c_setter_func_name, prop_attributes) \
case ECMA_ACCESSOR_ ## name ## c_getter_func_name: \
{ \
@@ -296,12 +296,41 @@ ecma_builtin_object_object_get_own_property_names (ecma_value_t this_arg, /**< '
{
ecma_object_t *obj_p = ecma_get_object_from_value (arg);
/* 2-5. */
ret_value = ecma_builtin_helper_object_get_properties (obj_p, false);
ret_value = ecma_builtin_helper_object_get_properties (obj_p, ECMA_LIST_NO_OPTS);
}
return ret_value;
} /* ecma_builtin_object_object_get_own_property_names */
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
/**
* The Object object's 'getOwnPropertySymbols' routine
*
* See also:
* ECMA-262 v6, 19.1.2.7
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_object_object_get_own_property_symbols (ecma_value_t this_arg, /**< 'this' argument */
ecma_value_t arg) /**< routine's argument */
{
JERRY_UNUSED (this_arg);
if (!ecma_is_value_object (arg))
{
/* 1. */
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument is not an object."));
}
/* 2 - 5. */
ecma_object_t *obj_p = ecma_get_object_from_value (arg);
return ecma_builtin_helper_object_get_properties (obj_p, ECMA_LIST_SYMBOLS);
} /* ecma_builtin_object_object_get_own_property_symbols */
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
/**
* The Object object's 'seal' routine
*
@@ -643,7 +672,7 @@ ecma_builtin_object_object_keys (ecma_value_t this_arg, /**< 'this' argument */
{
ecma_object_t *obj_p = ecma_get_object_from_value (arg);
/* 3-6. */
ret_value = ecma_builtin_helper_object_get_properties (obj_p, true);
ret_value = ecma_builtin_helper_object_get_properties (obj_p, ECMA_LIST_ENUMERABLE);
}
return ret_value;
@@ -669,19 +698,18 @@ ecma_builtin_object_object_get_own_property_descriptor (ecma_value_t this_arg, /
/* 1. */
if (!ecma_is_value_object (arg1))
{
ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("Argument is not an object."));
return ret_value;
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument is not an object."));
}
ecma_string_t *name_str_p = ecma_op_to_prop_name (arg2);
if (JERRY_UNLIKELY (name_str_p == NULL))
{
return ECMA_VALUE_ERROR;
}
ecma_object_t *obj_p = ecma_get_object_from_value (arg1);
/* 2. */
ECMA_TRY_CATCH (name_str_value,
ecma_op_to_string (arg2),
ret_value);
ecma_string_t *name_str_p = ecma_get_string_from_value (name_str_value);
/* 3. */
ecma_property_descriptor_t prop_desc;
@@ -699,7 +727,7 @@ ecma_builtin_object_object_get_own_property_descriptor (ecma_value_t this_arg, /
ret_value = ECMA_VALUE_UNDEFINED;
}
ECMA_FINALIZE (name_str_value);
ecma_deref_ecma_string (name_str_p);
return ret_value;
} /* ecma_builtin_object_object_get_own_property_descriptor */
@@ -886,39 +914,38 @@ ecma_builtin_object_object_define_property (ecma_value_t this_arg, /**< 'this' a
if (!ecma_is_value_object (arg1))
{
ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("Argument is not an object."));
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument is not an object."));
}
else
ecma_string_t *name_str_p = ecma_op_to_prop_name (arg2);
if (JERRY_UNLIKELY (name_str_p == NULL))
{
ecma_object_t *obj_p = ecma_get_object_from_value (arg1);
ECMA_TRY_CATCH (name_str_value,
ecma_op_to_string (arg2),
ret_value);
ecma_string_t *name_str_p = ecma_get_string_from_value (name_str_value);
ecma_property_descriptor_t prop_desc;
ECMA_TRY_CATCH (conv_result,
ecma_op_to_property_descriptor (arg3, &prop_desc),
ret_value);
ECMA_TRY_CATCH (define_own_prop_ret,
ecma_op_object_define_own_property (obj_p,
name_str_p,
&prop_desc,
true),
ret_value);
ret_value = ecma_copy_value (arg1);
ECMA_FINALIZE (define_own_prop_ret);
ecma_free_property_descriptor (&prop_desc);
ECMA_FINALIZE (conv_result);
ECMA_FINALIZE (name_str_value);
return ECMA_VALUE_ERROR;
}
ecma_object_t *obj_p = ecma_get_object_from_value (arg1);
ecma_property_descriptor_t prop_desc;
ECMA_TRY_CATCH (conv_result,
ecma_op_to_property_descriptor (arg3, &prop_desc),
ret_value);
ECMA_TRY_CATCH (define_own_prop_ret,
ecma_op_object_define_own_property (obj_p,
name_str_p,
&prop_desc,
true),
ret_value);
ret_value = ecma_copy_value (arg1);
ECMA_FINALIZE (define_own_prop_ret);
ecma_free_property_descriptor (&prop_desc);
ECMA_FINALIZE (conv_result);
ecma_deref_ecma_string (name_str_p);
return ret_value;
} /* ecma_builtin_object_object_define_property */
@@ -39,6 +39,9 @@ OBJECT_VALUE (LIT_MAGIC_STRING_PROTOTYPE,
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
ROUTINE (LIT_MAGIC_STRING_GET_PROTOTYPE_OF_UL, ecma_builtin_object_object_get_prototype_of, 1, 1)
ROUTINE (LIT_MAGIC_STRING_GET_OWN_PROPERTY_NAMES_UL, ecma_builtin_object_object_get_own_property_names, 1, 1)
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
ROUTINE (LIT_MAGIC_STRING_GET_OWN_PROPERTY_SYMBOLS_UL, ecma_builtin_object_object_get_own_property_symbols, 1, 1)
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
ROUTINE (LIT_MAGIC_STRING_SEAL, ecma_builtin_object_object_seal, 1, 1)
ROUTINE (LIT_MAGIC_STRING_FREEZE, ecma_builtin_object_object_freeze, 1, 1)
ROUTINE (LIT_MAGIC_STRING_PREVENT_EXTENSIONS_UL, ecma_builtin_object_object_prevent_extensions, 1, 1)
@@ -0,0 +1,98 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecma-alloc.h"
#include "ecma-builtins.h"
#include "ecma-conversion.h"
#include "ecma-exceptions.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-objects.h"
#include "ecma-symbol-object.h"
#include "ecma-try-catch-macro.h"
#include "jrt.h"
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-symbol-prototype.inc.h"
#define BUILTIN_UNDERSCORED_ID symbol_prototype
#include "ecma-builtin-internal-routines-template.inc.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmabuiltins
* @{
*
* \addtogroup symbolprototype ECMA Symbol prototype object built-in
* @{
*/
/**
* The Symbol.prototype object's 'toString' routine
*
* See also:
* ECMA-262 v6, 19.4.3.2
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_symbol_prototype_object_to_string (ecma_value_t this_arg) /**< this argument */
{
return ecma_symbol_to_string_helper (this_arg, true);
} /* ecma_builtin_symbol_prototype_object_to_string */
/**
* The Symbol.prototype object's 'valueOf' routine
*
* See also:
* ECMA-262 v6, 19.4.3.3
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_symbol_prototype_object_value_of (ecma_value_t this_arg) /**< this argument */
{
return ecma_symbol_to_string_helper (this_arg, false);
} /* ecma_builtin_symbol_prototype_object_value_of */
/**
* The Symbol.prototype object's '@@toPrimitive' routine
*
* See also:
* ECMA-262 v6, 19.4.3.3
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_symbol_prototype_object_to_primitive (ecma_value_t this_arg) /**< this argument */
{
return ecma_builtin_symbol_prototype_object_value_of (this_arg);
} /* ecma_builtin_symbol_prototype_object_to_primitive */
/**
* @}
* @}
* @}
*/
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
@@ -0,0 +1,49 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Symbol prototype built-in description
*/
#include "ecma-builtin-helpers-macro-defines.inc.h"
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
/* Object properties:
* (property name, object pointer getter) */
OBJECT_VALUE (LIT_MAGIC_STRING_CONSTRUCTOR,
ECMA_BUILTIN_ID_SYMBOL,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
NUMBER_VALUE (LIT_MAGIC_STRING_LENGTH,
0,
ECMA_PROPERTY_FLAG_WRITABLE)
ROUTINE (LIT_MAGIC_STRING_TO_STRING_UL, ecma_builtin_symbol_prototype_object_to_string, 0, 0)
ROUTINE (LIT_MAGIC_STRING_VALUE_OF_UL, ecma_builtin_symbol_prototype_object_value_of, 0, 0)
ROUTINE_CONFIGURABLE_ONLY (LIT_GLOBAL_SYMBOL_TO_PRIMITIVE,
ecma_builtin_symbol_prototype_object_to_primitive,
0,
0)
/* ECMA-262 v6, 19.4.3.4 */
STRING_VALUE (LIT_GLOBAL_SYMBOL_TO_STRING_TAG,
LIT_MAGIC_STRING_SYMBOL_UL,
ECMA_PROPERTY_FLAG_CONFIGURABLE)
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
#include "ecma-builtin-helpers-macro-undefs.inc.h"
@@ -0,0 +1,243 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecma-alloc.h"
#include "ecma-builtins.h"
#include "ecma-conversion.h"
#include "ecma-exceptions.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-objects.h"
#include "ecma-symbol-object.h"
#include "ecma-literal-storage.h"
#include "ecma-try-catch-macro.h"
#include "jcontext.h"
#include "jrt.h"
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-symbol.inc.h"
#define BUILTIN_UNDERSCORED_ID symbol
#include "ecma-builtin-internal-routines-template.inc.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmabuiltins
* @{
*
* \addtogroup symbol ECMA Symbol object built-in
* @{
*/
/**
* Handle calling [[Call]] of built-in Symbol object.
*
* @return ecma value
*/
ecma_value_t
ecma_builtin_symbol_dispatch_call (const ecma_value_t *arguments_list_p, /**< arguments list */
ecma_length_t arguments_list_len) /**< number of arguments */
{
JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
return ecma_op_create_symbol (arguments_list_p, arguments_list_len);
} /* ecma_builtin_symbol_dispatch_call */
/**
* Handle calling [[Construct]] of built-in Symbol object.
*
* Symbol constructor is not intended to be used
* with the new operator or to be subclassed.
*
* See also:
* ECMA-262 v6, 19.4.1
* @return ecma value
*/
ecma_value_t
ecma_builtin_symbol_dispatch_construct (const ecma_value_t *arguments_list_p, /**< arguments list */
ecma_length_t arguments_list_len) /**< number of arguments */
{
JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
return ecma_raise_type_error (ECMA_ERR_MSG ("Symbol is not a constructor."));
} /* ecma_builtin_symbol_dispatch_construct */
/**
* Helper function for Symbol object's 'for' and `keyFor`
* routines common parts
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_symbol_for_helper (ecma_value_t value_to_find) /**< symbol or ecma-string */
{
ecma_string_t *string_p;
bool is_for = ecma_is_value_string (value_to_find);
if (is_for)
{
string_p = ecma_get_string_from_value (value_to_find);
}
else
{
string_p = ecma_get_symbol_from_value (value_to_find);
}
ecma_lit_storage_item_t *symbol_list_p = JERRY_CONTEXT (symbol_list_first_p);
jmem_cpointer_t *empty_cpointer_p = NULL;
while (symbol_list_p != NULL)
{
for (int i = 0; i < ECMA_LIT_STORAGE_VALUE_COUNT; i++)
{
if (symbol_list_p->values[i] != JMEM_CP_NULL)
{
ecma_string_t *value_p = JMEM_CP_GET_NON_NULL_POINTER (ecma_string_t,
symbol_list_p->values[i]);
if (is_for)
{
ecma_string_t *symbol_desc_p = ecma_get_symbol_description (value_p);
if (ecma_compare_ecma_strings (symbol_desc_p, string_p))
{
/* The current symbol's descriptor matches with the value_to_find,
so the value is no longer needed. */
ecma_deref_ecma_string (string_p);
return ecma_copy_value (ecma_make_symbol_value (value_p));
}
}
else
{
if (string_p == value_p)
{
ecma_string_t *symbol_desc_p = ecma_get_symbol_description (string_p);
ecma_ref_ecma_string (symbol_desc_p);
return ecma_make_string_value (symbol_desc_p);
}
}
}
else
{
if (empty_cpointer_p == NULL)
{
empty_cpointer_p = symbol_list_p->values + i;
}
}
}
symbol_list_p = JMEM_CP_GET_POINTER (ecma_lit_storage_item_t, symbol_list_p->next_cp);
}
if (!is_for)
{
return ECMA_VALUE_UNDEFINED;
}
/* There was no matching, sp a new symbol should be added the the global symbol list. The symbol creation requires
an extra reference to the descriptor string, but this reference has already been added. */
ecma_string_t *new_symbol_p = ecma_new_symbol_from_descriptor_string (value_to_find);
jmem_cpointer_t result;
JMEM_CP_SET_NON_NULL_POINTER (result, new_symbol_p);
if (empty_cpointer_p != NULL)
{
*empty_cpointer_p = result;
return ecma_copy_value (ecma_make_symbol_value (new_symbol_p));
}
ecma_lit_storage_item_t *new_item_p;
new_item_p = (ecma_lit_storage_item_t *) jmem_pools_alloc (sizeof (ecma_lit_storage_item_t));
new_item_p->values[0] = result;
for (int i = 1; i < ECMA_LIT_STORAGE_VALUE_COUNT; i++)
{
new_item_p->values[i] = JMEM_CP_NULL;
}
JMEM_CP_SET_POINTER (new_item_p->next_cp, JERRY_CONTEXT (symbol_list_first_p));
JERRY_CONTEXT (symbol_list_first_p) = new_item_p;
return ecma_copy_value (ecma_make_symbol_value (new_symbol_p));
} /* ecma_builtin_symbol_for_helper */
/**
* The Symbol object's 'for' routine
*
* See also:
* ECMA-262 v6, 19.4.2.1
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_symbol_for (ecma_value_t this_arg, /**< this argument */
ecma_value_t key) /**< key string */
{
JERRY_UNUSED (this_arg);
ecma_value_t string_desc = ecma_op_to_string (key);
/* 1. */
if (ECMA_IS_VALUE_ERROR (string_desc))
{
/* 2. */
return string_desc;
}
/* 4-7. */
JERRY_ASSERT (ecma_is_value_string (string_desc));
return ecma_builtin_symbol_for_helper (string_desc);
} /* ecma_builtin_symbol_for */
/**
* The Symbol object's 'keyFor' routine
*
* See also:
* ECMA-262 v6, 19.4.2.
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_symbol_key_for (ecma_value_t this_arg, /**< this argument */
ecma_value_t symbol) /**< symbol */
{
JERRY_UNUSED (this_arg);
/* 1. */
if (!ecma_is_value_symbol (symbol))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("The given argument is not a Symbol."));
}
/* 2-4. */
return ecma_builtin_symbol_for_helper (symbol);
} /* ecma_builtin_symbol_key_for */
/**
* @}
* @}
* @}
*/
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
@@ -0,0 +1,91 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Symbol built-in description
*/
#include "ecma-builtin-helpers-macro-defines.inc.h"
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
/* Number properties:
* (property name, number value, writable, enumerable, configurable) */
/* ECMA-262 v6, 19.4.2 */
NUMBER_VALUE (LIT_MAGIC_STRING_LENGTH,
0,
ECMA_PROPERTY_FIXED)
/* Object properties:
* (property name, object pointer getter) */
/* ECMA-262 v6, 19.4.2.7 */
OBJECT_VALUE (LIT_MAGIC_STRING_PROTOTYPE,
ECMA_BUILTIN_ID_SYMBOL_PROTOTYPE,
ECMA_PROPERTY_FIXED)
/* ECMA-262 v6, 19.4.2.2 */
SYMBOL_VALUE (LIT_MAGIC_STRING_HAS_INSTANCE,
LIT_GLOBAL_SYMBOL_HAS_INSTANCE)
/* ECMA-262 v6, 19.4.2.3 */
SYMBOL_VALUE (LIT_MAGIC_STRING_IS_CONCAT_SPREADABLE,
LIT_GLOBAL_SYMBOL_IS_CONCAT_SPREADABLE)
/* ECMA-262 v6, 19.4.2.4 */
SYMBOL_VALUE (LIT_MAGIC_STRING_ITERATOR,
LIT_GLOBAL_SYMBOL_ITERATOR)
/* ECMA-262 v6, 19.4.2.6 */
SYMBOL_VALUE (LIT_MAGIC_STRING_MATCH,
LIT_GLOBAL_SYMBOL_MATCH)
/* ECMA-262 v6, 19.4.2.8 */
SYMBOL_VALUE (LIT_MAGIC_STRING_REPLACE,
LIT_GLOBAL_SYMBOL_REPLACE)
/* ECMA-262 v6, 19.4.2.9 */
SYMBOL_VALUE (LIT_MAGIC_STRING_SEARCH,
LIT_GLOBAL_SYMBOL_SEARCH)
/* ECMA-262 v6, 19.4.2.10 */
SYMBOL_VALUE (LIT_MAGIC_STRING_SPECIES,
LIT_GLOBAL_SYMBOL_SPECIES)
/* ECMA-262 v6, 19.4.2.11 */
SYMBOL_VALUE (LIT_MAGIC_STRING_SPLIT,
LIT_GLOBAL_SYMBOL_SPLIT)
/* ECMA-262 v6, 19.4.2.12 */
SYMBOL_VALUE (LIT_MAGIC_STRING_TO_PRIMITIVE,
LIT_GLOBAL_SYMBOL_TO_PRIMITIVE)
/* ECMA-262 v6, 19.4.2.13 */
SYMBOL_VALUE (LIT_MAGIC_STRING_TO_STRING_TAG,
LIT_GLOBAL_SYMBOL_TO_STRING_TAG)
/* ECMA-262 v6, 19.4.2.14 */
SYMBOL_VALUE (LIT_MAGIC_STRING_UNSCOPABLES,
LIT_GLOBAL_SYMBOL_UNSCOPABLES)
/* Routine properties:
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
ROUTINE (LIT_MAGIC_STRING_FOR, ecma_builtin_symbol_for, 1, 1)
ROUTINE (LIT_MAGIC_STRING_KEY_FOR, ecma_builtin_symbol_key_for, 1, 1)
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
#include "ecma-builtin-helpers-macro-undefs.inc.h"
@@ -31,6 +31,9 @@ typedef enum
ECMA_BUILTIN_PROPERTY_SIMPLE, /**< simple value property */
ECMA_BUILTIN_PROPERTY_NUMBER, /**< number value property */
ECMA_BUILTIN_PROPERTY_STRING, /**< string value property */
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
ECMA_BUILTIN_PROPERTY_SYMBOL, /**< symbol value property */
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
ECMA_BUILTIN_PROPERTY_OBJECT, /**< builtin object property */
ECMA_BUILTIN_PROPERTY_ROUTINE, /**< routine property */
ECMA_BUILTIN_PROPERTY_ACCESSOR_READ_WRITE, /**< full accessor property */
@@ -653,6 +653,16 @@ ecma_builtin_try_to_instantiate_property (ecma_object_t *object_p, /**< object *
lit_magic_string_id_t magic_string_id = ecma_get_string_magic (string_p);
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
if (JERRY_UNLIKELY (ecma_prop_name_is_symbol (string_p)))
{
if (string_p->hash & ECMA_GLOBAL_SYMBOL_FLAG)
{
magic_string_id = (string_p->hash >> ECMA_GLOBAL_SYMBOL_SHIFT);
}
}
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
if (magic_string_id == LIT_MAGIC_STRING__COUNT)
{
return NULL;
@@ -772,6 +782,25 @@ ecma_builtin_try_to_instantiate_property (ecma_object_t *object_p, /**< object *
value = ecma_make_magic_string_value ((lit_magic_string_id_t) curr_property_p->value);
break;
}
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
case ECMA_BUILTIN_PROPERTY_SYMBOL:
{
lit_magic_string_id_t symbol_desc_id = (lit_magic_string_id_t) curr_property_p->magic_string_id;
ecma_string_t *symbol_desc_p;
symbol_desc_p = ecma_append_magic_string_to_string (ecma_get_magic_string (LIT_MAGIC_STRING_SYMBOL_DOT_UL),
symbol_desc_id);
ecma_value_t symbol_desc_value = ecma_make_string_value (symbol_desc_p);
ecma_string_t *symbol_p = ecma_new_symbol_from_descriptor_string (symbol_desc_value);
lit_magic_string_id_t symbol_id = (lit_magic_string_id_t) curr_property_p->value;
symbol_p->hash = (uint16_t) ((symbol_id << ECMA_GLOBAL_SYMBOL_SHIFT) | ECMA_GLOBAL_SYMBOL_FLAG);
value = ecma_make_symbol_value (symbol_p);
break;
}
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
case ECMA_BUILTIN_PROPERTY_OBJECT:
{
ecma_object_t *builtin_object_p = ecma_builtin_get ((ecma_builtin_id_t) curr_property_p->value);
@@ -441,6 +441,24 @@ BUILTIN_ROUTINE (ECMA_BUILTIN_ID_MAP,
#endif /* !CONFIG_DISABLE_ES2015_MAP_BUILTIN */
#ifndef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
/* The Symbol prototype object (ECMA-262 v6, 19.4.2.7) */
BUILTIN (ECMA_BUILTIN_ID_SYMBOL_PROTOTYPE,
ECMA_OBJECT_TYPE_GENERAL,
ECMA_BUILTIN_ID_OBJECT_PROTOTYPE,
true,
symbol_prototype)
/* The Symbol routine (ECMA-262 v6, 19.4.2.1) */
BUILTIN_ROUTINE (ECMA_BUILTIN_ID_SYMBOL,
ECMA_OBJECT_TYPE_FUNCTION,
ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE,
true,
symbol)
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
/* The Global object (15.1) */
BUILTIN (ECMA_BUILTIN_ID_GLOBAL,
ECMA_OBJECT_TYPE_GENERAL,