From b81e535e1ccfa46a5e859ae5210d5442d6e47f58 Mon Sep 17 00:00:00 2001 From: Ruben Ayrapetyan Date: Wed, 15 Apr 2015 21:14:50 +0300 Subject: [PATCH] Providing interface for getting reference to Global lexical environment. --- jerry-core/ecma/base/ecma-init-finalize.cpp | 3 + jerry-core/ecma/operations/ecma-lex-env.cpp | 117 ++++++++++++-------- jerry-core/ecma/operations/ecma-lex-env.h | 15 ++- jerry-core/vm/vm.cpp | 6 +- 4 files changed, 90 insertions(+), 51 deletions(-) diff --git a/jerry-core/ecma/base/ecma-init-finalize.cpp b/jerry-core/ecma/base/ecma-init-finalize.cpp index 7e15991f5..de69bdf16 100644 --- a/jerry-core/ecma/base/ecma-init-finalize.cpp +++ b/jerry-core/ecma/base/ecma-init-finalize.cpp @@ -18,6 +18,7 @@ #include "ecma-helpers.h" #include "ecma-init-finalize.h" #include "ecma-lcache.h" +#include "ecma-lex-env.h" #include "ecma-stack.h" #include "mem-allocator.h" @@ -38,6 +39,7 @@ ecma_init (void) ecma_init_builtins (); ecma_lcache_init (); ecma_stack_init (); + ecma_init_environment (); mem_register_a_try_give_memory_back_callback (ecma_try_to_give_back_some_memory); } /* ecma_init */ @@ -50,6 +52,7 @@ ecma_finalize (void) { mem_unregister_a_try_give_memory_back_callback (ecma_try_to_give_back_some_memory); + ecma_finalize_environment (); ecma_stack_finalize (); ecma_finalize_builtins (); ecma_lcache_invalidate_all (); diff --git a/jerry-core/ecma/operations/ecma-lex-env.cpp b/jerry-core/ecma/operations/ecma-lex-env.cpp index bf9509388..7c6048cf0 100644 --- a/jerry-core/ecma/operations/ecma-lex-env.cpp +++ b/jerry-core/ecma/operations/ecma-lex-env.cpp @@ -31,6 +31,77 @@ * @{ */ +/** + * \addtogroup globallexicalenvironment Global lexical environment + * @{ + */ + +/** + * Global lexical environment + * + * See also: ECMA-262 v5, 10.2.3 + */ +ecma_object_t* ecma_global_lex_env_p = NULL; + +/** + * Initialize Global environment + */ +void +ecma_init_environment (void) +{ +#ifdef CONFIG_ECMA_GLOBAL_ENVIRONMENT_DECLARATIVE + ecma_global_lex_env_p = ecma_create_decl_lex_env (NULL); +#else /* !CONFIG_ECMA_GLOBAL_ENVIRONMENT_DECLARATIVE */ + ecma_object_t *glob_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_GLOBAL); + + ecma_global_lex_env_p = ecma_create_object_lex_env (NULL, glob_obj_p, false); + + ecma_deref_object (glob_obj_p); +#endif /* !CONFIG_ECMA_GLOBAL_ENVIRONMENT_DECLARATIVE */ +} /* ecma_init_environment */ + +/** + * Finalize Global environment + */ +void +ecma_finalize_environment (void) +{ + ecma_deref_object (ecma_global_lex_env_p); + ecma_global_lex_env_p = NULL; +} /* ecma_finalize_environment */ + +/** + * Get reference to Global lexical environment + * + * @return pointer to the object's instance + */ +ecma_object_t* +ecma_get_global_environment (void) +{ + ecma_ref_object (ecma_global_lex_env_p); + + return ecma_global_lex_env_p; +} /* ecma_get_global_environment */ + +/** + * Figure out whether the lexical environment is global. + * + * @return true - if lexical environment is object-bound and corresponding object is global object, + * false - otherwise. + */ +bool +ecma_is_lexical_environment_global (ecma_object_t *lex_env_p) /**< lexical environment */ +{ + JERRY_ASSERT(lex_env_p != NULL + && ecma_is_lexical_environment (lex_env_p)); + + return (lex_env_p == ecma_global_lex_env_p); +} /* ecma_is_lexical_environment_global */ + +/** + * @} + */ + /** * HasBinding operation. * @@ -423,52 +494,6 @@ ecma_op_initialize_immutable_binding (ecma_object_t *lex_env_p, /**< lexical env ecma_named_data_property_assign_value (lex_env_p, prop_p, value); } /* ecma_op_initialize_immutable_binding */ -/** - * The Global Environment constructor. - * - * See also: ECMA-262 v5, 10.2.3 - * - * @return pointer to created lexical environment - */ -ecma_object_t* -ecma_op_create_global_environment (ecma_object_t *glob_obj_p) /**< the Global object */ -{ -#ifdef CONFIG_ECMA_GLOBAL_ENVIRONMENT_DECLARATIVE - (void) glob_obj_p; - ecma_object_t *glob_env_p = ecma_create_decl_lex_env (NULL); -#else /* !CONFIG_ECMA_GLOBAL_ENVIRONMENT_DECLARATIVE */ - ecma_object_t *glob_env_p = ecma_create_object_lex_env (NULL, glob_obj_p, false); -#endif /* !CONFIG_ECMA_GLOBAL_ENVIRONMENT_DECLARATIVE */ - - return glob_env_p; -} /* ecma_op_create_global_environment */ - -/** - * Figure out whether the lexical environment is global. - * - * @return true - if lexical environment is object-bound and corresponding object is global object, - * false - otherwise. - */ -bool -ecma_is_lexical_environment_global (ecma_object_t *lex_env_p) /**< lexical environment */ -{ - JERRY_ASSERT(lex_env_p != NULL - && ecma_is_lexical_environment (lex_env_p)); - - ecma_lexical_environment_type_t type = ecma_get_lex_env_type (lex_env_p); - - if (type == ECMA_LEXICAL_ENVIRONMENT_OBJECTBOUND) - { - ecma_object_t *binding_obj_p = ecma_get_lex_env_binding_object (lex_env_p); - - return ecma_builtin_is (binding_obj_p, ECMA_BUILTIN_ID_GLOBAL); - } - else - { - return false; - } -} /* ecma_is_lexical_environment_global */ - /** * @} * @} diff --git a/jerry-core/ecma/operations/ecma-lex-env.h b/jerry-core/ecma/operations/ecma-lex-env.h index 5b40f1aeb..e2f72c8b4 100644 --- a/jerry-core/ecma/operations/ecma-lex-env.h +++ b/jerry-core/ecma/operations/ecma-lex-env.h @@ -29,6 +29,20 @@ * @{ */ +/** + * \addtogroup globallexicalenvironment Global lexical environment + * @{ + */ + +extern void ecma_init_environment (void); +extern void ecma_finalize_environment (void); +extern ecma_object_t* ecma_get_global_environment (void); +extern bool ecma_is_lexical_environment_global (ecma_object_t *lex_env_p); + +/** + * @} + */ + /* ECMA-262 v5, 8.7.1 and 8.7.2 */ extern ecma_completion_value_t ecma_op_get_value_lex_env_base (ecma_object_t *ref_base_lex_env_p, ecma_string_t *var_name_string_p, @@ -66,7 +80,6 @@ extern void ecma_op_initialize_immutable_binding (ecma_object_t *lex_env_p, const ecma_value_t& value); extern ecma_object_t* ecma_op_create_global_environment (ecma_object_t *glob_obj_p); -extern bool ecma_is_lexical_environment_global (ecma_object_t *lex_env_p); /** * @} diff --git a/jerry-core/vm/vm.cpp b/jerry-core/vm/vm.cpp index 13e86c82f..6f9da6111 100644 --- a/jerry-core/vm/vm.cpp +++ b/jerry-core/vm/vm.cpp @@ -366,12 +366,10 @@ run_int (void) } ecma_object_t *glob_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_GLOBAL); - - ecma_object_t *lex_env_p = ecma_op_create_global_environment (glob_obj_p); - ecma_value_t this_binding_value = ecma_make_object_value (glob_obj_p); + ecma_object_t *lex_env_p = ecma_get_global_environment (); ecma_completion_value_t completion = run_int_from_pos (start_pos, - this_binding_value, + ecma_make_object_value (glob_obj_p), lex_env_p, is_strict, false);