Introducing interfaces for invoking built-in functions and constructors.

Implementing property instantiation routine dispatcher for Global object.
Adding instantiation for 'undefined', 'NaN', 'Infinity' and built-in routine
properties of the Global Object. Implementing isNaN and isFinite built-in routines.
This commit is contained in:
Ruben Ayrapetyan
2014-09-19 18:10:49 +04:00
parent 1dd631178f
commit f46853bdd8
9 changed files with 557 additions and 178 deletions
+275 -39
View File
@@ -13,11 +13,14 @@
* limitations under the License.
*/
#include "globals.h"
#include "ecma-alloc.h"
#include "ecma-builtins.h"
#include "ecma-conversion.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-try-catch-macro.h"
#include "globals.h"
#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"
@@ -32,6 +35,53 @@
* @{
*/
/**
* List of the Global object's built-in property names
*
* Warning:
* values in the array should be sorted in ascending order
* that is checked in the ecma_builtin_init_global_object.
*/
static const ecma_magic_string_id_t ecma_builtin_global_property_names[] =
{
ECMA_MAGIC_STRING_EVAL,
ECMA_MAGIC_STRING_UNDEFINED,
ECMA_MAGIC_STRING_NAN,
ECMA_MAGIC_STRING_INFINITY_UL,
ECMA_MAGIC_STRING_OBJECT_UL,
ECMA_MAGIC_STRING_FUNCTION_UL,
ECMA_MAGIC_STRING_ARRAY_UL,
ECMA_MAGIC_STRING_STRING_UL,
ECMA_MAGIC_STRING_BOOLEAN_UL,
ECMA_MAGIC_STRING_NUMBER_UL,
ECMA_MAGIC_STRING_DATE_UL,
ECMA_MAGIC_STRING_REG_EXP_UL,
ECMA_MAGIC_STRING_ERROR_UL,
ECMA_MAGIC_STRING_EVAL_ERROR_UL,
ECMA_MAGIC_STRING_RANGE_ERROR_UL,
ECMA_MAGIC_STRING_REFERENCE_ERROR_UL,
ECMA_MAGIC_STRING_SYNTAX_ERROR_UL,
ECMA_MAGIC_STRING_TYPE_ERROR_UL,
ECMA_MAGIC_STRING_URI_ERROR_UL,
ECMA_MAGIC_STRING_MATH_UL,
ECMA_MAGIC_STRING_JSON_U,
ECMA_MAGIC_STRING_PARSE_INT,
ECMA_MAGIC_STRING_PARSE_FLOAT,
ECMA_MAGIC_STRING_IS_NAN,
ECMA_MAGIC_STRING_IS_FINITE,
ECMA_MAGIC_STRING_DECODE_URI,
ECMA_MAGIC_STRING_DECODE_URI_COMPONENT,
ECMA_MAGIC_STRING_ENCODE_URI,
ECMA_MAGIC_STRING_ENCODE_URI_COMPONENT
};
/**
* Number of the Global object's built-in properties
*/
static const ecma_length_t ecma_builtin_global_property_number = (sizeof (ecma_builtin_global_property_names) /
sizeof (ecma_magic_string_id_t));
JERRY_STATIC_ASSERT (sizeof (ecma_builtin_global_property_names) > sizeof (void*));
/**
* Global object
*/
@@ -78,16 +128,23 @@ ecma_builtin_init_global_object (void)
ecma_object_t *glob_obj_p = ecma_create_object (NULL, true, ECMA_OBJECT_TYPE_GENERAL);
ecma_string_t* undefined_identifier_p = ecma_get_magic_string (ECMA_MAGIC_STRING_UNDEFINED);
ecma_property_t *undefined_prop_p = ecma_create_named_data_property (glob_obj_p,
undefined_identifier_p,
ECMA_PROPERTY_NOT_WRITABLE,
ECMA_PROPERTY_NOT_ENUMERABLE,
ECMA_PROPERTY_NOT_CONFIGURABLE);
ecma_deref_ecma_string (undefined_identifier_p);
JERRY_ASSERT(ecma_is_value_undefined (undefined_prop_p->u.named_data_property.value));
ecma_property_t *class_prop_p = ecma_create_internal_property (glob_obj_p,
ECMA_INTERNAL_PROPERTY_CLASS);
class_prop_p->u.internal_property.value = ECMA_OBJECT_CLASS_OBJECT;
TODO(/* Define NaN, Infinity, eval, parseInt, parseFloat, isNaN, isFinite properties */);
ecma_property_t *built_in_id_prop_p = ecma_create_internal_property (glob_obj_p,
ECMA_INTERNAL_PROPERTY_BUILT_IN_ID);
built_in_id_prop_p->u.internal_property.value = ECMA_BUILTIN_ID_GLOBAL;
ecma_property_t *mask_0_31_prop_p;
mask_0_31_prop_p = ecma_create_internal_property (glob_obj_p,
ECMA_INTERNAL_PROPERTY_NON_INSTANTIATED_BUILT_IN_MASK_0_31);
JERRY_STATIC_ASSERT (ecma_builtin_global_property_number < sizeof (uint32_t) * JERRY_BITSINBYTE);
uint32_t builtin_mask = ((uint32_t)1u << ecma_builtin_global_property_number) - 1;
mask_0_31_prop_p->u.internal_property.value = builtin_mask;
ecma_set_object_is_builtin (glob_obj_p, true);
ecma_global_object_p = glob_obj_p;
} /* ecma_builtin_init_global_object */
@@ -163,9 +220,22 @@ ecma_builtin_global_object_parse_float (ecma_value_t string) /**< routine's firs
* Returned value must be freed with ecma_free_completion_value.
*/
static ecma_completion_value_t
ecma_builtin_global_object_is_nan (ecma_value_t number) /**< routine's first argument */
ecma_builtin_global_object_is_nan (ecma_value_t arg) /**< routine's first argument */
{
JERRY_UNIMPLEMENTED_REF_UNUSED_VARS (number);
ecma_completion_value_t ret_value;
ECMA_TRY_CATCH (num_value, ecma_op_to_number (arg), ret_value);
ecma_number_t *num_p = ECMA_GET_POINTER (num_value.u.value.value);
bool is_nan = ecma_number_is_nan (*num_p);
ret_value = ecma_make_return_completion_value (ecma_make_simple_value (is_nan ? ECMA_SIMPLE_VALUE_TRUE
: ECMA_SIMPLE_VALUE_FALSE));
ECMA_FINALIZE (num_value);
return ret_value;
} /* ecma_builtin_global_object_is_nan */
/**
@@ -178,9 +248,23 @@ ecma_builtin_global_object_is_nan (ecma_value_t number) /**< routine's first arg
* Returned value must be freed with ecma_free_completion_value.
*/
static ecma_completion_value_t
ecma_builtin_global_object_is_finite (ecma_value_t number) /**< routine's first argument */
ecma_builtin_global_object_is_finite (ecma_value_t arg) /**< routine's first argument */
{
JERRY_UNIMPLEMENTED_REF_UNUSED_VARS (number);
ecma_completion_value_t ret_value;
ECMA_TRY_CATCH (num_value, ecma_op_to_number (arg), ret_value);
ecma_number_t *num_p = ECMA_GET_POINTER (num_value.u.value.value);
bool is_finite = !(ecma_number_is_nan (*num_p)
|| ecma_number_is_infinity (*num_p));
ret_value = ecma_make_return_completion_value (ecma_make_simple_value (is_finite ? ECMA_SIMPLE_VALUE_TRUE
: ECMA_SIMPLE_VALUE_FALSE));
ECMA_FINALIZE (num_value);
return ret_value;
} /* ecma_builtin_global_object_is_finite */
/**
@@ -249,24 +333,24 @@ ecma_builtin_global_object_encode_uri_component (ecma_value_t uri_component) /**
* @return number of parameters
*/
ecma_length_t
ecma_builtin_global_get_routine_parameters_number (ecma_builtin_global_detail_id_t builtin_routine_id) /**< routine's
id */
ecma_builtin_global_get_routine_parameters_number (ecma_magic_string_id_t builtin_routine_id) /**< built-in routine's
name */
{
switch (builtin_routine_id)
{
case ECMA_BUILTIN_GLOBAL_DETAIL_ID_EVAL:
case ECMA_BUILTIN_GLOBAL_DETAIL_ID_PARSE_FLOAT:
case ECMA_BUILTIN_GLOBAL_DETAIL_ID_IS_NAN:
case ECMA_BUILTIN_GLOBAL_DETAIL_ID_IS_FINITE:
case ECMA_BUILTIN_GLOBAL_DETAIL_ID_DECODE_URI:
case ECMA_BUILTIN_GLOBAL_DETAIL_ID_DECODE_URI_COMPONENT:
case ECMA_BUILTIN_GLOBAL_DETAIL_ID_ENCODE_URI:
case ECMA_BUILTIN_GLOBAL_DETAIL_ID_ENCODE_URI_COMPONENT:
case ECMA_MAGIC_STRING_EVAL:
case ECMA_MAGIC_STRING_PARSE_FLOAT:
case ECMA_MAGIC_STRING_IS_NAN:
case ECMA_MAGIC_STRING_IS_FINITE:
case ECMA_MAGIC_STRING_DECODE_URI:
case ECMA_MAGIC_STRING_DECODE_URI_COMPONENT:
case ECMA_MAGIC_STRING_ENCODE_URI:
case ECMA_MAGIC_STRING_ENCODE_URI_COMPONENT:
{
return 1;
}
case ECMA_BUILTIN_GLOBAL_DETAIL_ID_PARSE_INT:
case ECMA_MAGIC_STRING_PARSE_INT:
{
return 2;
}
@@ -285,11 +369,8 @@ ecma_builtin_global_get_routine_parameters_number (ecma_builtin_global_detail_id
* Returned value must be freed with ecma_free_completion_value.
*/
ecma_completion_value_t
ecma_builtin_global_dispatch_routine (ecma_builtin_global_detail_id_t builtin_routine_id, /**< identifier of
the Global object's
initial property that
corresponds to
routine to be called */
ecma_builtin_global_dispatch_routine (ecma_magic_string_id_t builtin_routine_id, /**< Global object's
built-in routine's name */
ecma_value_t arguments_list [], /**< list of arguments passed to routine */
ecma_length_t arguments_number) /**< length of arguments' list */
{
@@ -297,14 +378,14 @@ ecma_builtin_global_dispatch_routine (ecma_builtin_global_detail_id_t builtin_ro
switch (builtin_routine_id)
{
case ECMA_BUILTIN_GLOBAL_DETAIL_ID_EVAL:
case ECMA_MAGIC_STRING_EVAL:
{
ecma_value_t arg = (arguments_number >= 1 ? arguments_list[0] : value_undefined);
return ecma_builtin_global_object_eval (arg);
}
case ECMA_BUILTIN_GLOBAL_DETAIL_ID_PARSE_INT:
case ECMA_MAGIC_STRING_PARSE_INT:
{
ecma_value_t arg1 = (arguments_number >= 1 ? arguments_list[0] : value_undefined);
ecma_value_t arg2 = (arguments_number >= 2 ? arguments_list[1] : value_undefined);
@@ -312,49 +393,49 @@ ecma_builtin_global_dispatch_routine (ecma_builtin_global_detail_id_t builtin_ro
return ecma_builtin_global_object_parse_int (arg1, arg2);
}
case ECMA_BUILTIN_GLOBAL_DETAIL_ID_PARSE_FLOAT:
case ECMA_MAGIC_STRING_PARSE_FLOAT:
{
ecma_value_t arg = (arguments_number >= 1 ? arguments_list[0] : value_undefined);
return ecma_builtin_global_object_parse_float (arg);
}
case ECMA_BUILTIN_GLOBAL_DETAIL_ID_IS_NAN:
case ECMA_MAGIC_STRING_IS_NAN:
{
ecma_value_t arg = (arguments_number >= 1 ? arguments_list[0] : value_undefined);
return ecma_builtin_global_object_is_nan (arg);
}
case ECMA_BUILTIN_GLOBAL_DETAIL_ID_IS_FINITE:
case ECMA_MAGIC_STRING_IS_FINITE:
{
ecma_value_t arg = (arguments_number >= 1 ? arguments_list[0] : value_undefined);
return ecma_builtin_global_object_is_finite (arg);
}
case ECMA_BUILTIN_GLOBAL_DETAIL_ID_DECODE_URI:
case ECMA_MAGIC_STRING_DECODE_URI:
{
ecma_value_t arg = (arguments_number >= 1 ? arguments_list[0] : value_undefined);
return ecma_builtin_global_object_decode_uri (arg);
}
case ECMA_BUILTIN_GLOBAL_DETAIL_ID_DECODE_URI_COMPONENT:
case ECMA_MAGIC_STRING_DECODE_URI_COMPONENT:
{
ecma_value_t arg = (arguments_number >= 1 ? arguments_list[0] : value_undefined);
return ecma_builtin_global_object_decode_uri_component (arg);
}
case ECMA_BUILTIN_GLOBAL_DETAIL_ID_ENCODE_URI:
case ECMA_MAGIC_STRING_ENCODE_URI:
{
ecma_value_t arg = (arguments_number >= 1 ? arguments_list[0] : value_undefined);
return ecma_builtin_global_object_encode_uri (arg);
}
case ECMA_BUILTIN_GLOBAL_DETAIL_ID_ENCODE_URI_COMPONENT:
case ECMA_MAGIC_STRING_ENCODE_URI_COMPONENT:
{
ecma_value_t arg = (arguments_number >= 1 ? arguments_list[0] : value_undefined);
@@ -368,6 +449,161 @@ ecma_builtin_global_dispatch_routine (ecma_builtin_global_detail_id_t builtin_ro
}
} /* ecma_builtin_global_dispatch_routine */
/**
* If the property's name is one of built-in properties of the Global 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_global_try_to_instantiate_property (ecma_object_t *obj_p, /**< object */
ecma_string_t *prop_name_p) /**< property's name */
{
JERRY_ASSERT (ecma_builtin_is_global_object (obj_p));
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_global_property_names,
ecma_builtin_global_property_number,
id);
if (index == -1)
{
return NULL;
}
JERRY_ASSERT (index >= 0 && (uint32_t) index < sizeof (uint32_t) * JERRY_BITSINBYTE);
uint32_t bit = (uint32_t) 1u << index;
ecma_property_t *mask_0_31_prop_p;
mask_0_31_prop_p = ecma_get_internal_property (obj_p,
ECMA_INTERNAL_PROPERTY_NON_INSTANTIATED_BUILT_IN_MASK_0_31);
uint32_t bit_mask = mask_0_31_prop_p->u.internal_property.value;
if (!(bit_mask & bit))
{
return NULL;
}
bit_mask &= ~bit;
mask_0_31_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)
{
case ECMA_MAGIC_STRING_EVAL:
case ECMA_MAGIC_STRING_PARSE_INT:
case ECMA_MAGIC_STRING_PARSE_FLOAT:
case ECMA_MAGIC_STRING_IS_NAN:
case ECMA_MAGIC_STRING_IS_FINITE:
case ECMA_MAGIC_STRING_DECODE_URI:
case ECMA_MAGIC_STRING_DECODE_URI_COMPONENT:
case ECMA_MAGIC_STRING_ENCODE_URI:
case ECMA_MAGIC_STRING_ENCODE_URI_COMPONENT:
{
ecma_object_t *func_obj_p = ecma_builtin_make_function_object_for_routine (ECMA_BUILTIN_ID_GLOBAL,
id);
value = ecma_make_object_value (func_obj_p);
writable = ECMA_PROPERTY_NOT_WRITABLE;
enumerable = ECMA_PROPERTY_NOT_ENUMERABLE;
configurable = ECMA_PROPERTY_NOT_CONFIGURABLE;
break;
}
case ECMA_MAGIC_STRING_UNDEFINED:
{
value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
writable = ECMA_PROPERTY_NOT_WRITABLE;
enumerable = ECMA_PROPERTY_NOT_ENUMERABLE;
configurable = ECMA_PROPERTY_NOT_CONFIGURABLE;
break;
}
case ECMA_MAGIC_STRING_NAN:
{
ecma_number_t *num_p = ecma_alloc_number ();
*num_p = ecma_number_make_nan ();
value = ecma_make_number_value (num_p);
writable = ECMA_PROPERTY_NOT_WRITABLE;
enumerable = ECMA_PROPERTY_NOT_ENUMERABLE;
configurable = ECMA_PROPERTY_NOT_CONFIGURABLE;
break;
}
case ECMA_MAGIC_STRING_INFINITY_UL:
{
ecma_number_t *num_p = ecma_alloc_number ();
*num_p = ecma_number_make_infinity (false);
value = ecma_make_number_value (num_p);
writable = ECMA_PROPERTY_NOT_WRITABLE;
enumerable = ECMA_PROPERTY_NOT_ENUMERABLE;
configurable = ECMA_PROPERTY_NOT_CONFIGURABLE;
break;
}
case ECMA_MAGIC_STRING_OBJECT_UL:
case ECMA_MAGIC_STRING_FUNCTION_UL:
case ECMA_MAGIC_STRING_ARRAY_UL:
case ECMA_MAGIC_STRING_STRING_UL:
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:
case ECMA_MAGIC_STRING_EVAL_ERROR_UL:
case ECMA_MAGIC_STRING_RANGE_ERROR_UL:
case ECMA_MAGIC_STRING_REFERENCE_ERROR_UL:
case ECMA_MAGIC_STRING_SYNTAX_ERROR_UL:
case ECMA_MAGIC_STRING_TYPE_ERROR_UL:
case ECMA_MAGIC_STRING_URI_ERROR_UL:
case ECMA_MAGIC_STRING_MATH_UL:
case ECMA_MAGIC_STRING_JSON_U:
{
JERRY_UNIMPLEMENTED ();
}
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_global_try_to_instantiate_property */
/**
* @}
* @}