From bfad8e897d5ebbe906153022f162d2191a528530 Mon Sep 17 00:00:00 2001 From: Ruben Ayrapetyan Date: Thu, 31 Jul 2014 17:13:20 +0400 Subject: [PATCH] Implementing ECMA-262 v5 10.2.1.2 operations (object environment record's CreateMutableBinding, SetMutableBinding, etc.). --- src/libcoreint/opcodes.c | 16 +- src/libecmaoperations/ecma-lex-env.c | 159 +++++++++++++++--- .../ecma-objects-properties.c | 2 +- 3 files changed, 142 insertions(+), 35 deletions(-) diff --git a/src/libcoreint/opcodes.c b/src/libcoreint/opcodes.c index f831d88da..6fef111f0 100644 --- a/src/libcoreint/opcodes.c +++ b/src/libcoreint/opcodes.c @@ -1270,9 +1270,11 @@ opfunc_var_decl(OPCODE opdata, /**< operation data */ variable_name.str_p)) ) { FIXME( Pass configurableBindings that is true if and only if current code is eval code ); - ecma_op_create_mutable_binding (int_data->lex_env_p, - variable_name.str_p, - false); + ecma_completion_value_t completion = ecma_op_create_mutable_binding (int_data->lex_env_p, + variable_name.str_p, + false); + + JERRY_ASSERT( ecma_is_empty_completion_value( completion) ); /* Skipping SetMutableBinding as we have already checked that there were not * any binding with specified name in current lexical environment @@ -1339,9 +1341,11 @@ opfunc_func_decl_0(OPCODE opdata, /**< operation data */ if ( !func_already_declared ) { FIXME( Pass configurableBindings that is true if and only if current code is eval code ); - ecma_op_create_mutable_binding( int_data->lex_env_p, - fn, - false); + ecma_completion_value_t completion = ecma_op_create_mutable_binding( int_data->lex_env_p, + fn, + false); + + JERRY_ASSERT( ecma_is_empty_completion_value( completion) ); } // e. diff --git a/src/libecmaoperations/ecma-lex-env.c b/src/libecmaoperations/ecma-lex-env.c index b258dcf4c..8a47eb8d6 100644 --- a/src/libecmaoperations/ecma-lex-env.c +++ b/src/libecmaoperations/ecma-lex-env.c @@ -14,9 +14,12 @@ */ #include "ecma-exceptions.h" +#include "ecma-gc.h" #include "ecma-globals.h" +#include "ecma-global-object.h" #include "ecma-helpers.h" #include "ecma-lex-env.h" +#include "ecma-objects-properties.h" #include "globals.h" /** \addtogroup ecma ---TODO--- @@ -28,6 +31,25 @@ * @{ */ +/** + * Get binding object of object lexical environment. + * + * @return pointer to binding object + */ +static ecma_object_t* +ecma_get_lex_env_binding_object( ecma_object_t* obj_lex_env_p) /**< object lexical environment */ +{ + JERRY_ASSERT( obj_lex_env_p != NULL + && obj_lex_env_p->is_lexical_environment + && obj_lex_env_p->u.lexical_environment.type == ECMA_LEXICAL_ENVIRONMENT_OBJECTBOUND ); + + ecma_property_t *binding_obj_prop_p = ecma_get_pointer( obj_lex_env_p->properties_p); + JERRY_ASSERT( binding_obj_prop_p != NULL + && binding_obj_prop_p->u.internal_property.type == ECMA_INTERNAL_PROPERTY_BINDING_OBJECT ); + + return ecma_get_pointer( binding_obj_prop_p->u.internal_property.value); +} /* ecma_get_lex_env_binding_object */ + /** * HasBinding operation. * @@ -58,13 +80,18 @@ ecma_op_has_binding(ecma_object_t *lex_env_p, /**< lexical environment */ } case ECMA_LEXICAL_ENVIRONMENT_OBJECTBOUND: { - JERRY_UNIMPLEMENTED(); + ecma_object_t *binding_obj_p = ecma_get_lex_env_binding_object( lex_env_p); + + has_binding = ecma_op_object_has_property( binding_obj_p, name_p) ? ECMA_SIMPLE_VALUE_TRUE + : ECMA_SIMPLE_VALUE_FALSE; + + break; } } return ecma_make_completion_value(ECMA_COMPLETION_TYPE_NORMAL, - ecma_make_simple_value( has_binding), - ECMA_TARGET_ID_RESERVED); + ecma_make_simple_value( has_binding), + ECMA_TARGET_ID_RESERVED); } /* ecma_op_has_binding */ /** @@ -73,8 +100,7 @@ ecma_op_has_binding(ecma_object_t *lex_env_p, /**< lexical environment */ * see also: ecma-262 v5, 10.2.1 * * @return completion value - * Return value is simple and so need not be freed. - * However, ecma_free_completion_value may be called for it, but it is a no-op. + * Returned value must be freed with ecma_free_completion_value */ ecma_completion_value_t ecma_op_create_mutable_binding(ecma_object_t *lex_env_p, /**< lexical environment */ @@ -102,7 +128,38 @@ ecma_op_create_mutable_binding(ecma_object_t *lex_env_p, /**< lexical environmen } case ECMA_LEXICAL_ENVIRONMENT_OBJECTBOUND: { - JERRY_UNIMPLEMENTED(); + ecma_object_t *binding_obj_p = ecma_get_lex_env_binding_object( lex_env_p); + + JERRY_ASSERT( !ecma_op_object_has_property( binding_obj_p, name_p ) ); + + ecma_property_descriptor_t prop_desc = ecma_make_empty_property_descriptor(); + { + prop_desc.is_value_defined = true; + prop_desc.value = ecma_make_simple_value( ECMA_SIMPLE_VALUE_UNDEFINED); + + prop_desc.is_writable_defined = true; + prop_desc.writable = ECMA_PROPERTY_WRITABLE; + + prop_desc.is_enumerable_defined = true; + prop_desc.enumerable = ECMA_PROPERTY_ENUMERABLE; + + prop_desc.is_configurable_defined = true; + prop_desc.configurable = is_deletable ? ECMA_PROPERTY_CONFIGURABLE + : ECMA_PROPERTY_NOT_CONFIGURABLE; + } + + ecma_completion_value_t completion = ecma_op_object_define_own_property( binding_obj_p, + name_p, + prop_desc, + true); + + if ( !( ecma_is_completion_value_normal_true( completion) + || ecma_is_completion_value_normal_false( completion) ) ) + { + JERRY_ASSERT( ecma_is_completion_value_throw( completion) ); + + return completion; + } } } @@ -135,21 +192,36 @@ ecma_op_set_mutable_binding(ecma_object_t *lex_env_p, /**< lexical environment * ecma_property_t *property_p = ecma_get_named_data_property( lex_env_p, name_p); if ( property_p->u.named_data_property.writable == ECMA_PROPERTY_WRITABLE ) - { - ecma_free_value( property_p->u.named_data_property.value); - property_p->u.named_data_property.value = ecma_copy_value( value); - } else if ( is_strict ) - { - return ecma_make_throw_value( ecma_new_standard_error( ECMA_ERROR_TYPE)); - } + { + ecma_free_value( property_p->u.named_data_property.value); + property_p->u.named_data_property.value = ecma_copy_value( value); + } + else if ( is_strict ) + { + return ecma_make_throw_value( ecma_new_standard_error( ECMA_ERROR_TYPE)); + } break; } case ECMA_LEXICAL_ENVIRONMENT_OBJECTBOUND: { - JERRY_UNIMPLEMENTED(); + ecma_object_t *binding_obj_p = ecma_get_lex_env_binding_object( lex_env_p); + + ecma_completion_value_t completion = ecma_op_object_put( binding_obj_p, + name_p, + value, + is_strict); + if ( !( ecma_is_completion_value_normal_true( completion) + || ecma_is_completion_value_normal_false( completion) ) ) + { + JERRY_ASSERT( ecma_is_completion_value_throw( completion) ); + + return completion; + } + + break; } - } + } return ecma_make_empty_completion_value(); } /* ecma_op_set_mutable_binding */ @@ -184,8 +256,8 @@ ecma_op_get_binding_value(ecma_object_t *lex_env_p, /**< lexical environment */ if ( property_p->u.named_data_property.writable == ECMA_PROPERTY_WRITABLE ) { return ecma_make_completion_value( ECMA_COMPLETION_TYPE_NORMAL, - ecma_copy_value( prop_value), - ECMA_TARGET_ID_RESERVED); + ecma_copy_value( prop_value), + ECMA_TARGET_ID_RESERVED); } else if ( ecma_is_value_empty( prop_value) ) { /* unitialized immutable binding */ @@ -194,9 +266,7 @@ ecma_op_get_binding_value(ecma_object_t *lex_env_p, /**< lexical environment */ return ecma_make_throw_value( ecma_new_standard_error( ECMA_ERROR_REFERENCE)); } else { - return ecma_make_completion_value( ECMA_COMPLETION_TYPE_NORMAL, - ecma_make_simple_value( ECMA_SIMPLE_VALUE_UNDEFINED), - ECMA_TARGET_ID_RESERVED); + return ecma_make_simple_completion_value( ECMA_SIMPLE_VALUE_UNDEFINED); } } @@ -204,7 +274,23 @@ ecma_op_get_binding_value(ecma_object_t *lex_env_p, /**< lexical environment */ } case ECMA_LEXICAL_ENVIRONMENT_OBJECTBOUND: { - JERRY_UNIMPLEMENTED(); + ecma_object_t *binding_obj_p = ecma_get_lex_env_binding_object( lex_env_p); + + bool has_prop = ecma_op_object_has_property( binding_obj_p, name_p); + + if ( !has_prop ) + { + if ( is_strict ) + { + return ecma_make_throw_value( ecma_new_standard_error( ECMA_ERROR_REFERENCE)); + } + else + { + return ecma_make_simple_completion_value( ECMA_SIMPLE_VALUE_UNDEFINED); + } + } + + return ecma_op_object_get( binding_obj_p, name_p); } } @@ -253,12 +339,14 @@ ecma_op_delete_binding(ecma_object_t *lex_env_p, /**< lexical environment */ } return ecma_make_completion_value( ECMA_COMPLETION_TYPE_NORMAL, - ecma_make_simple_value( ret_val), - ECMA_TARGET_ID_RESERVED); + ecma_make_simple_value( ret_val), + ECMA_TARGET_ID_RESERVED); } case ECMA_LEXICAL_ENVIRONMENT_OBJECTBOUND: { - JERRY_UNIMPLEMENTED(); + ecma_object_t *binding_obj_p = ecma_get_lex_env_binding_object( lex_env_p); + + return ecma_op_object_delete( binding_obj_p, name_p, false); } } @@ -282,13 +370,28 @@ ecma_op_implicit_this_value( ecma_object_t *lex_env_p) /**< lexical environment { case ECMA_LEXICAL_ENVIRONMENT_DECLARATIVE: { - return ecma_make_completion_value( ECMA_COMPLETION_TYPE_NORMAL, - ecma_make_simple_value( ECMA_SIMPLE_VALUE_UNDEFINED), - ECMA_TARGET_ID_RESERVED); + return ecma_make_simple_completion_value( ECMA_SIMPLE_VALUE_UNDEFINED); } case ECMA_LEXICAL_ENVIRONMENT_OBJECTBOUND: { - JERRY_UNIMPLEMENTED(); + ecma_property_t *provide_this_prop_p = ecma_get_internal_property( lex_env_p, + ECMA_INTERNAL_PROPERTY_PROVIDE_THIS); + + bool provide_this = provide_this_prop_p->u.internal_property.value; + + if ( provide_this ) + { + ecma_object_t *binding_obj_p = ecma_get_lex_env_binding_object( lex_env_p); + ecma_ref_object( binding_obj_p); + + return ecma_make_completion_value( ECMA_COMPLETION_TYPE_NORMAL, + ecma_make_object_value( binding_obj_p), + ECMA_TARGET_ID_RESERVED); + } + else + { + return ecma_make_simple_completion_value( ECMA_SIMPLE_VALUE_UNDEFINED); + } } } diff --git a/src/libecmaoperations/ecma-objects-properties.c b/src/libecmaoperations/ecma-objects-properties.c index f3e2991c8..008647f9f 100644 --- a/src/libecmaoperations/ecma-objects-properties.c +++ b/src/libecmaoperations/ecma-objects-properties.c @@ -33,7 +33,7 @@ * Returned value must be freed with ecma_free_completion_value */ static ecma_completion_value_t -ecma_reject( bool is_throw) +ecma_reject( bool is_throw) /**< Throw flag */ { if ( is_throw ) {