diff --git a/src/libecmaoperations/ecma-get-put-value.c b/src/libecmaoperations/ecma-get-put-value.c index d9ea7758a..07051623b 100644 --- a/src/libecmaoperations/ecma-get-put-value.c +++ b/src/libecmaoperations/ecma-get-put-value.c @@ -18,7 +18,6 @@ */ #include "ecma-exceptions.h" -#include "ecma-gc.h" #include "ecma-helpers.h" #include "ecma-lex-env.h" #include "ecma-operations.h" @@ -30,49 +29,6 @@ * @{ */ -/** - * Resolve syntactic reference to ECMA-reference. - * - * Warning: string pointed by name_p - * must not be freed or reused - * until the reference is freed. - * - * @return ECMA-reference (if base value is an object, upon return - * it's reference counter is increased by one). - */ -ecma_Reference_t -ecma_OpGetIdentifierReference(ecma_Object_t *lex_env_p, /**< lexical environment */ - ecma_Char_t *name_p, /**< identifier's name */ - bool is_strict) /**< strict reference flag */ -{ - JERRY_ASSERT( lex_env_p != NULL ); - - ecma_Object_t *lex_env_iter_p = lex_env_p; - - while ( lex_env_iter_p != NULL ) - { - ecma_CompletionValue_t completion_value; - completion_value = ecma_OpHasBinding( lex_env_iter_p, name_p); - - JERRY_ASSERT( completion_value.type == ECMA_COMPLETION_TYPE_NORMAL ); - - if ( ecma_IsValueTrue( completion_value.value) ) - { - ecma_RefObject( lex_env_iter_p); - - return (ecma_Reference_t) { .base = ecma_MakeObjectValue( lex_env_iter_p), - .referenced_name_p = name_p, - .is_strict = is_strict }; - } - - lex_env_iter_p = ecma_GetPointer( lex_env_iter_p->u.m_LexicalEnvironment.m_pOuterReference); - } - - return (ecma_Reference_t) { .base = ecma_MakeSimpleValue( ECMA_SIMPLE_VALUE_UNDEFINED), - .referenced_name_p = NULL, - .is_strict = is_strict }; -} /* ecma_OpGetIdentifierReference */ - /** * GetValue operation. * diff --git a/src/libecmaoperations/ecma-reference.c b/src/libecmaoperations/ecma-reference.c new file mode 100644 index 000000000..51828091c --- /dev/null +++ b/src/libecmaoperations/ecma-reference.c @@ -0,0 +1,108 @@ +/* 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-gc.h" +#include "ecma-globals.h" +#include "ecma-helpers.h" +#include "ecma-lex-env.h" +#include "ecma-reference.h" +#include "globals.h" + +/** \addtogroup ecma ---TODO--- + * @{ + */ + +/** + * \addtogroup references ECMA-Reference + * @{ + */ + +/** + * Resolve syntactic reference to ECMA-reference. + * + * Warning: string pointed by name_p + * must not be freed or reused + * until the reference is freed. + * + * @return ECMA-reference (if base value is an object, upon return + * it's reference counter is increased by one). + */ +ecma_Reference_t +ecma_OpGetIdentifierReference(ecma_Object_t *lex_env_p, /**< lexical environment */ + ecma_Char_t *name_p, /**< identifier's name */ + bool is_strict) /**< strict reference flag */ +{ + JERRY_ASSERT( lex_env_p != NULL ); + + ecma_Object_t *lex_env_iter_p = lex_env_p; + + while ( lex_env_iter_p != NULL ) + { + ecma_CompletionValue_t completion_value; + completion_value = ecma_OpHasBinding( lex_env_iter_p, name_p); + + JERRY_ASSERT( completion_value.type == ECMA_COMPLETION_TYPE_NORMAL ); + + if ( ecma_IsValueTrue( completion_value.value) ) + { + return ecma_MakeReference( ecma_MakeObjectValue( lex_env_iter_p), + name_p, + is_strict); + } + + lex_env_iter_p = ecma_GetPointer( lex_env_iter_p->u.m_LexicalEnvironment.m_pOuterReference); + } + + return ecma_MakeReference( ecma_MakeSimpleValue( ECMA_SIMPLE_VALUE_UNDEFINED), + name_p, + is_strict); +} /* ecma_OpGetIdentifierReference */ + +/** + * ECMA-reference constructor. + * + * Warning: string pointed by name_p + * must not be freed or reused + * until the reference is freed. + * + * @return ECMA-reference (if base_p it not NULL, then upon return + * corresponding object's reference counter is increased by one). + */ +ecma_Reference_t +ecma_MakeReference(ecma_Value_t base, /**< base value */ + ecma_Char_t *name_p, /**< referenced name */ + bool is_strict) /**< strict reference flag */ +{ + return (ecma_Reference_t) { .base = ecma_CopyValue( base), + .referenced_name_p = name_p, + .is_strict = is_strict }; +} /* ecma_MakeReference */ + +/** + * Free specified ECMA-reference. + * + * Warning: + * after freeing all copy of the reference become invalid. + */ +void +ecma_FreeReference( const ecma_Reference_t ref) /**< reference */ +{ + ecma_FreeValue( ref.base); +} /* ecma_FreeReference */ + +/** + * @} + * @} + */ diff --git a/src/libecmaoperations/ecma-reference.h b/src/libecmaoperations/ecma-reference.h new file mode 100644 index 000000000..78e3b4172 --- /dev/null +++ b/src/libecmaoperations/ecma-reference.h @@ -0,0 +1,40 @@ +/* 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. + */ + +#ifndef ECMA_REFERENCE_H +#define ECMA_REFERENCE_H + +#include "ecma-globals.h" +#include "globals.h" + +/** \addtogroup ecma ---TODO--- + * @{ + */ + +/** + * \addtogroup references ECMA-Reference + * @{ + */ + +extern ecma_Reference_t ecma_OpGetIdentifierReference(ecma_Object_t *lex_env_p, ecma_Char_t *name_p, bool is_strict); +extern ecma_Reference_t ecma_MakeReference( ecma_Value_t base, ecma_Char_t *name_p, bool is_strict); +extern void ecma_FreeReference( const ecma_Reference_t ref); + +/** + * @} + * @} + */ + +#endif /* !ECMA_REFERENCE_H */