From c231893b282da7a53e989775d1afce31217c68e0 Mon Sep 17 00:00:00 2001 From: Ruben Ayrapetyan Date: Thu, 16 Oct 2014 13:23:33 +0400 Subject: [PATCH] Implementation of Number built-in object and partial implementation of Number.prototype built-in object. Fixing [[Prototype]] and [[Class]] properties of Array and String built-in objects. --- src/globals.h | 1 + .../ecma-builtin-global-object.c | 9 +- .../ecma-builtin-number-object.c | 301 ++++++++++++ .../ecma-builtin-number-prototype-object.c | 436 ++++++++++++++++++ src/libecmabuiltins/ecma-builtins-internal.h | 18 +- src/libecmaobjects/ecma-globals.h | 17 + src/libecmaobjects/ecma-helpers-string.c | 4 + src/libruntime/jerry-libc.h | 4 - 8 files changed, 781 insertions(+), 9 deletions(-) create mode 100644 src/libecmabuiltins/ecma-builtin-number-object.c create mode 100644 src/libecmabuiltins/ecma-builtin-number-prototype-object.c diff --git a/src/globals.h b/src/globals.h index df797806f..d1e7f3161 100644 --- a/src/globals.h +++ b/src/globals.h @@ -19,6 +19,7 @@ #include #include #include +#include /** * Types diff --git a/src/libecmabuiltins/ecma-builtin-global-object.c b/src/libecmabuiltins/ecma-builtin-global-object.c index af6ee0caf..7842e4fbe 100644 --- a/src/libecmabuiltins/ecma-builtin-global-object.c +++ b/src/libecmabuiltins/ecma-builtin-global-object.c @@ -497,6 +497,14 @@ ecma_builtin_global_try_to_instantiate_property (ecma_object_t *obj_p, /**< obje break; } + case ECMA_MAGIC_STRING_NUMBER_UL: + { + ecma_object_t *number_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_NUMBER); + + value = ecma_make_object_value (number_obj_p); + + break; + } case ECMA_MAGIC_STRING_ARRAY_UL: { ecma_object_t *array_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_ARRAY); @@ -515,7 +523,6 @@ ecma_builtin_global_try_to_instantiate_property (ecma_object_t *obj_p, /**< obje } case ECMA_MAGIC_STRING_BOOLEAN_UL: - case ECMA_MAGIC_STRING_NUMBER_UL: case ECMA_MAGIC_STRING_DATE_UL: case ECMA_MAGIC_STRING_REG_EXP_UL: case ECMA_MAGIC_STRING_ERROR_UL: diff --git a/src/libecmabuiltins/ecma-builtin-number-object.c b/src/libecmabuiltins/ecma-builtin-number-object.c new file mode 100644 index 000000000..133ced8f6 --- /dev/null +++ b/src/libecmabuiltins/ecma-builtin-number-object.c @@ -0,0 +1,301 @@ +/* Copyright 2014 Samsung Electronics Co., Ltd. + * + * 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-try-catch-macro.h" +#include "globals.h" + +#define ECMA_BUILTINS_INTERNAL +#include "ecma-builtins-internal.h" + +/** \addtogroup ecma ECMA + * @{ + * + * \addtogroup ecmabuiltins + * @{ + * + * \addtogroup number ECMA Number object built-in + * @{ + */ + +/** + * List of the Number object built-in object value properties in format 'macro (name, value)'. + */ +#define ECMA_BUILTIN_NUMBER_OBJECT_OBJECT_VALUES_PROPERTY_LIST(macro) \ + macro (ECMA_MAGIC_STRING_PROTOTYPE, ecma_builtin_get (ECMA_BUILTIN_ID_NUMBER_PROTOTYPE)) + +#define ECMA_BUILTIN_NUMBER_OBJECT_NUMBER_VALUES_PROPERTY_LIST(macro) \ + macro (ECMA_MAGIC_STRING_LENGTH, 1) \ + macro (ECMA_MAGIC_STRING_NAN, ecma_number_make_nan ()) \ + macro (ECMA_MAGIC_STRING_MAX_VALUE_U, ECMA_NUMBER_MAX_VALUE) \ + macro (ECMA_MAGIC_STRING_MIN_VALUE_U, ECMA_NUMBER_MIN_VALUE) \ + macro (ECMA_MAGIC_STRING_POSITIVE_INFINITY_U, ecma_number_make_infinity (false)) \ + macro (ECMA_MAGIC_STRING_NEGATIVE_INFINITY_U, ecma_number_make_infinity (true)) + +/** + * List of the Number object's built-in property names + */ +static const ecma_magic_string_id_t ecma_builtin_number_property_names[] = +{ +#define VALUE_PROP_LIST(name, value) name, +#define ROUTINE_PROP_LIST(name, c_function_name, args_number, length) name, + ECMA_BUILTIN_NUMBER_OBJECT_OBJECT_VALUES_PROPERTY_LIST (VALUE_PROP_LIST) + ECMA_BUILTIN_NUMBER_OBJECT_NUMBER_VALUES_PROPERTY_LIST (VALUE_PROP_LIST) +#undef VALUE_PROP_LIST +#undef ROUTINE_PROP_LIST +}; + +/** + * Number of the Number object's built-in properties + */ +const ecma_length_t ecma_builtin_number_property_number = (sizeof (ecma_builtin_number_property_names) / + sizeof (ecma_magic_string_id_t)); + +/** + * If the property's name is one of built-in properties of the Number object + * that is not instantiated yet, instantiate the property and + * return pointer to the instantiated property. + * + * @return pointer property, if one was instantiated, + * NULL - otherwise. + */ +ecma_property_t* +ecma_builtin_number_try_to_instantiate_property (ecma_object_t *obj_p, /**< object */ + ecma_string_t *prop_name_p) /**< property's name */ +{ + JERRY_ASSERT (ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_NUMBER)); + JERRY_ASSERT (ecma_find_named_property (obj_p, prop_name_p) == NULL); + + ecma_magic_string_id_t id; + + if (!ecma_is_string_magic (prop_name_p, &id)) + { + return NULL; + } + + int32_t index = ecma_builtin_bin_search_for_magic_string_id_in_array (ecma_builtin_number_property_names, + ecma_builtin_number_property_number, + id); + + if (index == -1) + { + return NULL; + } + + JERRY_ASSERT (index >= 0 && (uint32_t) index < sizeof (uint64_t) * JERRY_BITSINBYTE); + + uint32_t bit; + ecma_internal_property_id_t mask_prop_id; + + if (index >= 32) + { + mask_prop_id = ECMA_INTERNAL_PROPERTY_NON_INSTANTIATED_BUILT_IN_MASK_32_63; + bit = (uint32_t) 1u << (index - 32); + } + else + { + mask_prop_id = ECMA_INTERNAL_PROPERTY_NON_INSTANTIATED_BUILT_IN_MASK_0_31; + bit = (uint32_t) 1u << index; + } + + ecma_property_t *mask_prop_p = ecma_get_internal_property (obj_p, mask_prop_id); + uint32_t bit_mask = mask_prop_p->u.internal_property.value; + + if (!(bit_mask & bit)) + { + return NULL; + } + + bit_mask &= ~bit; + + mask_prop_p->u.internal_property.value = bit_mask; + + ecma_value_t value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY); + ecma_property_writable_value_t writable = ECMA_PROPERTY_WRITABLE; + ecma_property_enumerable_value_t enumerable = ECMA_PROPERTY_NOT_ENUMERABLE; + ecma_property_configurable_value_t configurable = ECMA_PROPERTY_CONFIGURABLE; + + switch (id) + { +#define CASE_VALUE_PROP_LIST(name, value) case name: + ECMA_BUILTIN_NUMBER_OBJECT_OBJECT_VALUES_PROPERTY_LIST (CASE_VALUE_PROP_LIST) + ECMA_BUILTIN_NUMBER_OBJECT_NUMBER_VALUES_PROPERTY_LIST (CASE_VALUE_PROP_LIST) +#undef CASE_VALUE_PROP_LIST + { + writable = ECMA_PROPERTY_NOT_WRITABLE; + enumerable = ECMA_PROPERTY_NOT_ENUMERABLE; + configurable = ECMA_PROPERTY_NOT_CONFIGURABLE; + + switch (id) + { +#define CASE_OBJECT_VALUE_PROP_LIST(name, value_obj) case name: { value = ecma_make_object_value (value_obj); break; } + ECMA_BUILTIN_NUMBER_OBJECT_OBJECT_VALUES_PROPERTY_LIST (CASE_OBJECT_VALUE_PROP_LIST) +#undef CASE_OBJECT_VALUE_PROP_LIST +#define CASE_NUMBER_VALUE_PROP_LIST(name, value_num) case name: { ecma_number_t *num_p = ecma_alloc_number (); \ + *num_p = (ecma_number_t) value_num; \ + value = ecma_make_number_value (num_p); break; } + ECMA_BUILTIN_NUMBER_OBJECT_NUMBER_VALUES_PROPERTY_LIST (CASE_NUMBER_VALUE_PROP_LIST) +#undef CASE_NUMBER_VALUE_PROP_LIST + default: + { + JERRY_UNREACHABLE (); + } + } + + break; + } + + default: + { + JERRY_UNREACHABLE (); + } + } + + ecma_property_t *prop_p = ecma_create_named_data_property (obj_p, + prop_name_p, + writable, + enumerable, + configurable); + + prop_p->u.named_data_property.value = ecma_copy_value (value, false); + ecma_gc_update_may_ref_younger_object_flag_by_value (obj_p, + prop_p->u.named_data_property.value); + + ecma_free_value (value, true); + + return prop_p; +} /* ecma_builtin_number_try_to_instantiate_property */ + +/** + * Dispatcher of the Number object's built-in routines + * + * @return completion-value + * Returned value must be freed with ecma_free_completion_value. + */ +ecma_completion_value_t +ecma_builtin_number_dispatch_routine (ecma_magic_string_id_t builtin_routine_id __unused, /**< Number object's + built-in routine's + name */ + ecma_value_t this_arg_value __unused, /**< 'this' argument value */ + ecma_value_t arguments_list [] __unused, /**< list of arguments + passed to routine */ + ecma_length_t arguments_number __unused) /**< length of arguments' list */ +{ + JERRY_UNREACHABLE (); +} /* ecma_builtin_number_dispatch_routine */ + +/** + * Get number of routine's parameters + * + * @return number of parameters + */ +ecma_length_t +ecma_builtin_number_get_routine_parameters_number (ecma_magic_string_id_t builtin_routine_id __unused) /**< built-in + routine's + name */ +{ + JERRY_UNREACHABLE (); +} /* ecma_builtin_number_get_routine_parameters_number */ + +/** + * Handle calling [[Call]] of built-in Number object + * + * @return completion-value + */ +ecma_completion_value_t +ecma_builtin_number_dispatch_call (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); + + ecma_completion_value_t ret_value; + + if (arguments_list_len == 0) + { + ecma_number_t *zero_num_p = ecma_alloc_number (); + *zero_num_p = ECMA_NUMBER_ZERO; + + ret_value = ecma_make_normal_completion_value (ecma_make_number_value (zero_num_p)); + } + else + { + ret_value = ecma_op_to_number (arguments_list_p [0]); + } + + return ret_value; +} /* ecma_builtin_number_dispatch_call */ + +/** + * Handle calling [[Construct]] of built-in Number object + * + * @return completion-value + */ +ecma_completion_value_t +ecma_builtin_number_dispatch_construct (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); + + ecma_number_t *prim_value_p; + + if (arguments_list_len == 0) + { + ecma_number_t *zero_num_p = ecma_alloc_number (); + *zero_num_p = ECMA_NUMBER_ZERO; + + prim_value_p = zero_num_p; + } + else + { + ecma_completion_value_t conv_to_num_completion = ecma_op_to_number (arguments_list_p [0]); + + if (!ecma_is_completion_value_normal (conv_to_num_completion)) + { + return conv_to_num_completion; + } + else + { + prim_value_p = ECMA_GET_POINTER (conv_to_num_completion.u.value.value); + } + } + + ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_NUMBER_PROTOTYPE); + ecma_object_t *obj_p = ecma_create_object (prototype_obj_p, + true, + ECMA_OBJECT_TYPE_GENERAL); + ecma_deref_object (prototype_obj_p); + + ecma_property_t *class_prop_p = ecma_create_internal_property (obj_p, ECMA_INTERNAL_PROPERTY_CLASS); + class_prop_p->u.internal_property.value = ECMA_MAGIC_STRING_NUMBER_UL; + + ecma_property_t *prim_value_prop_p = ecma_create_internal_property (obj_p, + ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE); + ECMA_SET_POINTER (prim_value_prop_p->u.internal_property.value, prim_value_p); + + return ecma_make_normal_completion_value (ecma_make_object_value (obj_p)); +} /* ecma_builtin_number_dispatch_construct */ + +/** + * @} + * @} + * @} + */ diff --git a/src/libecmabuiltins/ecma-builtin-number-prototype-object.c b/src/libecmabuiltins/ecma-builtin-number-prototype-object.c new file mode 100644 index 000000000..99a9cc138 --- /dev/null +++ b/src/libecmabuiltins/ecma-builtin-number-prototype-object.c @@ -0,0 +1,436 @@ +/* Copyright 2014 Samsung Electronics Co., Ltd. + * + * 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-string-object.h" +#include "ecma-try-catch-macro.h" +#include "globals.h" + +#define ECMA_BUILTINS_INTERNAL +#include "ecma-builtins-internal.h" + +/** \addtogroup ecma ECMA + * @{ + * + * \addtogroup ecmabuiltins + * @{ + * + * \addtogroup numberprototype ECMA Number.prototype object built-in + * @{ + */ + +/** + * List of the Number.prototype object built-in object value properties in format 'macro (name, value)'. + */ +#define ECMA_BUILTIN_NUMBER_PROTOTYPE_OBJECT_OBJECT_VALUES_PROPERTY_LIST(macro) \ + macro (ECMA_MAGIC_STRING_CONSTRUCTOR, ecma_builtin_get (ECMA_BUILTIN_ID_STRING)) + +/** + * List of the Number.prototype object built-in routine properties in format + * 'macro (name, C function name, arguments number of the routine, length value of the routine)'. + */ +#define ECMA_BUILTIN_NUMBER_PROTOTYPE_OBJECT_ROUTINES_PROPERTY_LIST(macro) \ + macro (ECMA_MAGIC_STRING_TO_STRING_UL, \ + ecma_builtin_number_prototype_object_to_string, \ + NON_FIXED, \ + 1) \ + macro (ECMA_MAGIC_STRING_VALUE_OF_UL, \ + ecma_builtin_number_prototype_object_value_of, \ + 0, \ + 0) \ + macro (ECMA_MAGIC_STRING_TO_LOCALE_STRING_UL, \ + ecma_builtin_number_prototype_object_to_locale_string, \ + 0, \ + 0) \ + macro (ECMA_MAGIC_STRING_TO_FIXED_UL, \ + ecma_builtin_number_prototype_object_to_fixed, \ + 1, \ + 1) \ + macro (ECMA_MAGIC_STRING_TO_EXPONENTIAL_UL, \ + ecma_builtin_number_prototype_object_to_exponential, \ + 1, \ + 1) \ + macro (ECMA_MAGIC_STRING_TO_PRECISION_UL, \ + ecma_builtin_number_prototype_object_to_precision, \ + 1, \ + 1) + +/** + * List of the Number.prototype object's built-in property names + */ +static const ecma_magic_string_id_t ecma_builtin_property_names[] = +{ +#define VALUE_PROP_LIST(name, value) name, +#define ROUTINE_PROP_LIST(name, c_function_name, args_number, length) name, + ECMA_BUILTIN_NUMBER_PROTOTYPE_OBJECT_OBJECT_VALUES_PROPERTY_LIST (VALUE_PROP_LIST) + ECMA_BUILTIN_NUMBER_PROTOTYPE_OBJECT_ROUTINES_PROPERTY_LIST (ROUTINE_PROP_LIST) +#undef VALUE_PROP_LIST +#undef ROUTINE_PROP_LIST +}; + +/** + * Number of the Number.prototype object's built-in properties + */ +const ecma_length_t ecma_builtin_number_prototype_property_number = (sizeof (ecma_builtin_property_names) / + sizeof (ecma_magic_string_id_t)); + +/** + * The Number.prototype object's 'toString' routine + * + * See also: + * ECMA-262 v5, 15.7.4.2 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value. + */ +static ecma_completion_value_t +ecma_builtin_number_prototype_object_to_string (ecma_value_t this, /**< this argument */ + ecma_value_t* arguments_list_p, /**< arguments list */ + ecma_length_t arguments_list_len) /**< number of arguments */ +{ + if (this.value_type == ECMA_TYPE_NUMBER) + { + return ecma_make_normal_completion_value (ecma_copy_value (this, true)); + } + else if (this.value_type == ECMA_TYPE_OBJECT) + { + ecma_object_t *obj_p = ECMA_GET_POINTER (this.value); + + ecma_property_t *class_prop_p = ecma_get_internal_property (obj_p, ECMA_INTERNAL_PROPERTY_CLASS); + + if (class_prop_p->u.internal_property.value == ECMA_MAGIC_STRING_NUMBER_UL) + { + ecma_property_t *prim_value_prop_p = ecma_get_internal_property (obj_p, + ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE); + + ecma_number_t *prim_value_num_p = ECMA_GET_POINTER (prim_value_prop_p->u.internal_property.value); + + if (arguments_list_len == 0) + { + ecma_string_t *ret_str_p = ecma_new_ecma_string_from_number (*prim_value_num_p); + + return ecma_make_normal_completion_value (ecma_make_string_value (ret_str_p)); + } + { + JERRY_UNIMPLEMENTED_REF_UNUSED_VARS (arguments_list_p); + } + } + } + + return ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE)); +} /* ecma_builtin_number_prototype_object_to_string */ + +/** + * The Number.prototype object's 'toLocaleString' routine + * + * See also: + * ECMA-262 v5, 15.7.4.3 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value. + */ +static ecma_completion_value_t +ecma_builtin_number_prototype_object_to_locale_string (ecma_value_t this) /**< this argument */ +{ + return ecma_builtin_number_prototype_object_to_string (this, NULL, 0); +} /* ecma_builtin_number_prototype_object_to_locale_string */ + +/** + * The Number.prototype object's 'valueOf' routine + * + * See also: + * ECMA-262 v5, 15.7.4.4 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value. + */ +static ecma_completion_value_t +ecma_builtin_number_prototype_object_value_of (ecma_value_t this) /**< this argument */ +{ + if (this.value_type == ECMA_TYPE_NUMBER) + { + return ecma_make_normal_completion_value (ecma_copy_value (this, true)); + } + else if (this.value_type == ECMA_TYPE_OBJECT) + { + ecma_object_t *obj_p = ECMA_GET_POINTER (this.value); + + ecma_property_t *class_prop_p = ecma_get_internal_property (obj_p, ECMA_INTERNAL_PROPERTY_CLASS); + + if (class_prop_p->u.internal_property.value == ECMA_MAGIC_STRING_NUMBER_UL) + { + ecma_property_t *prim_value_prop_p = ecma_get_internal_property (obj_p, + ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE); + + ecma_number_t *prim_value_num_p = ECMA_GET_POINTER (prim_value_prop_p->u.internal_property.value); + + ecma_number_t *ret_num_p = ecma_alloc_number (); + *ret_num_p = *prim_value_num_p; + + return ecma_make_normal_completion_value (ecma_make_number_value (ret_num_p)); + } + } + + return ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE)); +} /* ecma_builtin_number_prototype_object_value_of */ + +/** + * The Number.prototype object's 'toFixed' routine + * + * See also: + * ECMA-262 v5, 15.7.4.5 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value. + */ +static ecma_completion_value_t +ecma_builtin_number_prototype_object_to_fixed (ecma_value_t this, /**< this argument */ + ecma_value_t arg) /**< routine's argument */ +{ + JERRY_UNIMPLEMENTED_REF_UNUSED_VARS (this, arg); +} /* ecma_builtin_number_prototype_object_to_fixed */ + +/** + * The Number.prototype object's 'toExponential' routine + * + * See also: + * ECMA-262 v5, 15.7.4.6 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value. + */ +static ecma_completion_value_t +ecma_builtin_number_prototype_object_to_exponential (ecma_value_t this, /**< this argument */ + ecma_value_t arg) /**< routine's argument */ +{ + JERRY_UNIMPLEMENTED_REF_UNUSED_VARS (this, arg); +} /* ecma_builtin_number_prototype_object_to_exponential */ + +/** + * The Number.prototype object's 'toPrecision' routine + * + * See also: + * ECMA-262 v5, 15.7.4.7 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value. + */ +static ecma_completion_value_t +ecma_builtin_number_prototype_object_to_precision (ecma_value_t this, /**< this argument */ + ecma_value_t arg) /**< routine's argument */ +{ + JERRY_UNIMPLEMENTED_REF_UNUSED_VARS (this, arg); +} /* ecma_builtin_number_prototype_object_to_precision */ + +/** + * If the property's name is one of built-in properties of the Number.prototype object + * that is not instantiated yet, instantiate the property and + * return pointer to the instantiated property. + * + * @return pointer property, if one was instantiated, + * NULL - otherwise. + */ +ecma_property_t* +ecma_builtin_number_prototype_try_to_instantiate_property (ecma_object_t *obj_p, /**< object */ + ecma_string_t *prop_name_p) /**< property's name */ +{ + JERRY_ASSERT (ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_NUMBER_PROTOTYPE)); + JERRY_ASSERT (ecma_find_named_property (obj_p, prop_name_p) == NULL); + + ecma_magic_string_id_t id; + + if (!ecma_is_string_magic (prop_name_p, &id)) + { + return NULL; + } + + int32_t index = ecma_builtin_bin_search_for_magic_string_id_in_array (ecma_builtin_property_names, + ecma_builtin_number_prototype_property_number, + id); + + if (index == -1) + { + return NULL; + } + + JERRY_ASSERT (index >= 0 && (uint32_t) index < sizeof (uint64_t) * JERRY_BITSINBYTE); + + uint32_t bit; + ecma_internal_property_id_t mask_prop_id; + + if (index >= 32) + { + mask_prop_id = ECMA_INTERNAL_PROPERTY_NON_INSTANTIATED_BUILT_IN_MASK_32_63; + bit = (uint32_t) 1u << (index - 32); + } + else + { + mask_prop_id = ECMA_INTERNAL_PROPERTY_NON_INSTANTIATED_BUILT_IN_MASK_0_31; + bit = (uint32_t) 1u << index; + } + + ecma_property_t *mask_prop_p = ecma_get_internal_property (obj_p, mask_prop_id); + uint32_t bit_mask = mask_prop_p->u.internal_property.value; + + if (!(bit_mask & bit)) + { + return NULL; + } + + bit_mask &= ~bit; + + mask_prop_p->u.internal_property.value = bit_mask; + + ecma_value_t value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY); + ecma_property_writable_value_t writable = ECMA_PROPERTY_WRITABLE; + ecma_property_enumerable_value_t enumerable = ECMA_PROPERTY_NOT_ENUMERABLE; + ecma_property_configurable_value_t configurable = ECMA_PROPERTY_CONFIGURABLE; + + switch (id) + { +#define CASE_ROUTINE_PROP_LIST(name, c_function_name, args_number, length) case name: + ECMA_BUILTIN_NUMBER_PROTOTYPE_OBJECT_ROUTINES_PROPERTY_LIST (CASE_ROUTINE_PROP_LIST) +#undef CASE_ROUTINE_PROP_LIST + { + ecma_object_t *func_obj_p = ecma_builtin_make_function_object_for_routine (ECMA_BUILTIN_ID_NUMBER_PROTOTYPE, + id); + + value = ecma_make_object_value (func_obj_p); + + break; + } +#define CASE_VALUE_PROP_LIST(name, value) case name: + ECMA_BUILTIN_NUMBER_PROTOTYPE_OBJECT_OBJECT_VALUES_PROPERTY_LIST (CASE_VALUE_PROP_LIST) +#undef CASE_VALUE_PROP_LIST + { + writable = ECMA_PROPERTY_NOT_WRITABLE; + enumerable = ECMA_PROPERTY_NOT_ENUMERABLE; + configurable = ECMA_PROPERTY_NOT_CONFIGURABLE; + + switch (id) + { +#define CASE_OBJECT_VALUE_PROP_LIST(name, value_obj) case name: { value = ecma_make_object_value (value_obj); break; } + ECMA_BUILTIN_NUMBER_PROTOTYPE_OBJECT_OBJECT_VALUES_PROPERTY_LIST (CASE_OBJECT_VALUE_PROP_LIST) +#undef CASE_OBJECT_VALUE_PROP_LIST + default: + { + JERRY_UNREACHABLE (); + } + } + + break; + } + + default: + { + JERRY_UNREACHABLE (); + } + } + + ecma_property_t *prop_p = ecma_create_named_data_property (obj_p, + prop_name_p, + writable, + enumerable, + configurable); + + prop_p->u.named_data_property.value = ecma_copy_value (value, false); + ecma_gc_update_may_ref_younger_object_flag_by_value (obj_p, + prop_p->u.named_data_property.value); + + ecma_free_value (value, true); + + return prop_p; +} /* ecma_builtin_number_prototype_try_to_instantiate_property */ + +/** + * Dispatcher of the Number.prototype object's built-in routines + * + * @return completion-value + * Returned value must be freed with ecma_free_completion_value. + */ +ecma_completion_value_t +ecma_builtin_number_prototype_dispatch_routine (ecma_magic_string_id_t builtin_routine_id, /**< Number.prototype + object's built-in + routine's name */ + ecma_value_t this_arg_value __unused, /**< 'this' argument value */ + ecma_value_t arguments_list [], /**< list of arguments + passed to routine */ + ecma_length_t arguments_number) /**< length of arguments' list */ +{ + const ecma_value_t value_undefined = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED); + + switch (builtin_routine_id) + { +#define ROUTINE_ARG(n) (arguments_number >= n ? arguments_list[n - 1] : value_undefined) +#define ROUTINE_ARG_LIST_0 +#define ROUTINE_ARG_LIST_1 , ROUTINE_ARG(1) +#define ROUTINE_ARG_LIST_2 ROUTINE_ARG_LIST_1, ROUTINE_ARG(2) +#define ROUTINE_ARG_LIST_3 ROUTINE_ARG_LIST_2, ROUTINE_ARG(3) +#define ROUTINE_ARG_LIST_NON_FIXED , arguments_list, arguments_number +#define CASE_ROUTINE_PROP_LIST(name, c_function_name, args_number, length) \ + case name: \ + { \ + return c_function_name (this_arg_value ROUTINE_ARG_LIST_ ## args_number); \ + } + ECMA_BUILTIN_NUMBER_PROTOTYPE_OBJECT_ROUTINES_PROPERTY_LIST (CASE_ROUTINE_PROP_LIST) +#undef CASE_ROUTINE_PROP_LIST +#undef ROUTINE_ARG_LIST_0 +#undef ROUTINE_ARG_LIST_1 +#undef ROUTINE_ARG_LIST_2 +#undef ROUTINE_ARG_LIST_3 +#undef ROUTINE_ARG_LIST_NON_FIXED + + default: + { + JERRY_UNREACHABLE (); + } + } +} /* ecma_builtin_number_prototype_dispatch_routine */ + +/** + * Get number of routine's parameters + * + * @return number of parameters + */ +ecma_length_t +ecma_builtin_number_prototype_get_routine_parameters_number (ecma_magic_string_id_t builtin_routine_id) /**< built-in + routine's + name */ +{ + switch (builtin_routine_id) + { +#define CASE_ROUTINE_PROP_LIST(name, c_function_name, args_number, length) case name: return length; + ECMA_BUILTIN_NUMBER_PROTOTYPE_OBJECT_ROUTINES_PROPERTY_LIST (CASE_ROUTINE_PROP_LIST) +#undef CASE_ROUTINE_PROP_LIST + + default: + { + JERRY_UNREACHABLE (); + } + } +} /* ecma_builtin_number_prototype_get_routine_parameters_number */ + +/** + * @} + * @} + * @} + */ diff --git a/src/libecmabuiltins/ecma-builtins-internal.h b/src/libecmabuiltins/ecma-builtins-internal.h index 272abdd59..0730c21a6 100644 --- a/src/libecmabuiltins/ecma-builtins-internal.h +++ b/src/libecmabuiltins/ecma-builtins-internal.h @@ -74,6 +74,11 @@ ecma_builtin_bin_search_for_magic_string_id_in_array (const ecma_magic_string_id ARRAY_UL, \ ECMA_BUILTIN_ID_OBJECT_PROTOTYPE, \ array_prototype) \ + macro (NUMBER_PROTOTYPE, \ + TYPE_GENERAL, \ + NUMBER_UL, \ + ECMA_BUILTIN_ID_OBJECT_PROTOTYPE, \ + number_prototype) \ macro (STRING_PROTOTYPE, \ TYPE_GENERAL, \ STRING_UL, \ @@ -91,13 +96,18 @@ ecma_builtin_bin_search_for_magic_string_id_in_array (const ecma_magic_string_id math) \ macro (ARRAY, \ TYPE_FUNCTION, \ - ARRAY_UL, \ - ECMA_BUILTIN_ID_ARRAY_PROTOTYPE, \ + FUNCTION_UL, \ + ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE, \ array) \ + macro (NUMBER, \ + TYPE_FUNCTION, \ + FUNCTION_UL, \ + ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE, \ + number) \ macro (STRING, \ TYPE_FUNCTION, \ - STRING_UL, \ - ECMA_BUILTIN_ID_STRING_PROTOTYPE, \ + FUNCTION_UL, \ + ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE, \ string) \ macro (FUNCTION, \ TYPE_FUNCTION, \ diff --git a/src/libecmaobjects/ecma-globals.h b/src/libecmaobjects/ecma-globals.h index c53f7f151..cc95a802f 100644 --- a/src/libecmaobjects/ecma-globals.h +++ b/src/libecmaobjects/ecma-globals.h @@ -549,6 +549,19 @@ typedef double ecma_number_t; */ #define ECMA_NUMBER_TWO ((ecma_number_t) 2) +/** + * Minimum positive and maximum value of ecma-number + */ +#ifdef CONFIG_ECMA_NUMBER_FLOAT32 +# define ECMA_NUMBER_MIN_VALUE (FLT_MIN) +# define ECMA_NUMBER_MAX_VALUE (FLT_MAX) +#elif defined (CONFIG_ECMA_NUMBER_FLOAT64) +# define ECMA_NUMBER_MAX_VALUE (DBL_MAX) +# define ECMA_NUMBER_MIN_VALUE (DBL_MIN) +#else /* !CONFIG_ECMA_NUMBER_FLOAT32 && !CONFIG_ECMA_NUMBER_FLOAT64 */ +# error "!CONFIG_ECMA_NUMBER_FLOAT32 && !CONFIG_ECMA_NUMBER_FLOAT64" +#endif /* !CONFIG_ECMA_NUMBER_FLOAT32 && !CONFIG_ECMA_NUMBER_FLOAT64 */ + /** * Value '0.5' of ecma_number_t */ @@ -811,6 +824,10 @@ typedef enum ECMA_MAGIC_STRING_TO_UTC_STRING_UL, /**< "toUTCString" */ ECMA_MAGIC_STRING_TO_ISO_STRING_UL, /**< "toISOString" */ ECMA_MAGIC_STRING_TO_JSON_UL, /**< "toJSON" */ + ECMA_MAGIC_STRING_MAX_VALUE_U, /**< "MAX_VALUE" */ + ECMA_MAGIC_STRING_MIN_VALUE_U, /**< "MIN_VALUE" */ + ECMA_MAGIC_STRING_POSITIVE_INFINITY_U, /**< "POSITIVE_INFINITY" */ + ECMA_MAGIC_STRING_NEGATIVE_INFINITY_U, /**< "NEGATIVE_INFINITY" */ ECMA_MAGIC_STRING_APPLY, /**< "apply" */ ECMA_MAGIC_STRING_CALL, /**< "call" */ ECMA_MAGIC_STRING_BIND, /**< "bind" */ diff --git a/src/libecmaobjects/ecma-helpers-string.c b/src/libecmaobjects/ecma-helpers-string.c index 1342ed996..bbe8a9689 100644 --- a/src/libecmaobjects/ecma-helpers-string.c +++ b/src/libecmaobjects/ecma-helpers-string.c @@ -1448,6 +1448,10 @@ ecma_get_magic_string_zt (ecma_magic_string_id_t id) /**< magic string id */ case ECMA_MAGIC_STRING_TO_UTC_STRING_UL: return (ecma_char_t*) "toUTCString"; case ECMA_MAGIC_STRING_TO_ISO_STRING_UL: return (ecma_char_t*) "toISOString"; case ECMA_MAGIC_STRING_TO_JSON_UL: return (ecma_char_t*) "toJSON"; + case ECMA_MAGIC_STRING_MAX_VALUE_U: return (ecma_char_t*) "MAX_VALUE"; + case ECMA_MAGIC_STRING_MIN_VALUE_U: return (ecma_char_t*) "MIN_VALUE"; + case ECMA_MAGIC_STRING_POSITIVE_INFINITY_U: return (ecma_char_t*) "POSITIVE_INFINITY"; + case ECMA_MAGIC_STRING_NEGATIVE_INFINITY_U: return (ecma_char_t*) "NEGATIVE_INFINITY"; case ECMA_MAGIC_STRING_APPLY: return (ecma_char_t*) "apply"; case ECMA_MAGIC_STRING_CALL: return (ecma_char_t*) "call"; case ECMA_MAGIC_STRING_BIND: return (ecma_char_t*) "bind"; diff --git a/src/libruntime/jerry-libc.h b/src/libruntime/jerry-libc.h index a191040a1..119c6a967 100644 --- a/src/libruntime/jerry-libc.h +++ b/src/libruntime/jerry-libc.h @@ -102,10 +102,6 @@ extern int __fprintf (_FILE *, const char *, ...); extern void jrt_set_mem_limits (size_t data_size, size_t stack_size); -#define DBL_MANT_DIG (52) -#define DBL_DIG (10) -#define DBL_MIN_EXP (-324) -#define DBL_MAX_EXP (308) #define HUGE_VAL (1e37f) #endif /* JERRY_LIBC_H */