• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ECMASCRIPT_LEXICALENV_H
17 #define ECMASCRIPT_LEXICALENV_H
18 
19 #include "ecmascript/base_env.h"
20 
21 namespace panda::ecmascript {
22 class LexicalEnv : public BaseEnv {
23 public:
24     // the first field is used to store GlobalEnv
25     static constexpr uint32_t PARENT_ENV_INDEX = 1;
26     static constexpr uint32_t SCOPE_INFO_INDEX = 2;
27     static constexpr uint32_t RESERVED_ENV_LENGTH = 3;
28 
Cast(TaggedObject * object)29     static LexicalEnv *Cast(TaggedObject *object)
30     {
31         ASSERT(JSTaggedValue(object).IsLexicalEnv());
32         return static_cast<LexicalEnv *>(object);
33     }
34 
ComputeSize(uint32_t numSlots)35     static size_t ComputeSize(uint32_t numSlots)
36     {
37         return TaggedArray::ComputeSize(JSTaggedValue::TaggedTypeSize(), numSlots + RESERVED_ENV_LENGTH);
38     }
39 
SetParentEnv(JSThread * thread,JSTaggedValue value)40     void SetParentEnv(JSThread *thread, JSTaggedValue value)
41     {
42         Set(thread, PARENT_ENV_INDEX, value);
43     }
44 
GetParentEnv(const JSThread * thread)45     JSTaggedValue GetParentEnv(const JSThread *thread) const
46     {
47         return Get(thread, PARENT_ENV_INDEX);
48     }
49 
GetProperties(const JSThread * thread,uint32_t index)50     JSTaggedValue GetProperties(const JSThread *thread, uint32_t index) const
51     {
52         return Get(thread, index + RESERVED_ENV_LENGTH);
53     }
54 
SetProperties(JSThread * thread,uint32_t index,JSTaggedValue value)55     void SetProperties(JSThread *thread, uint32_t index, JSTaggedValue value)
56     {
57         Set(thread, index + RESERVED_ENV_LENGTH, value);
58     }
59 
GetScopeInfo(const JSThread * thread)60     JSTaggedValue GetScopeInfo(const JSThread *thread) const
61     {
62         return Get(thread, SCOPE_INFO_INDEX);
63     }
64 
SetScopeInfo(JSThread * thread,JSTaggedValue value)65     void SetScopeInfo(JSThread *thread, JSTaggedValue value)
66     {
67         Set(thread, SCOPE_INFO_INDEX, value);
68     }
69 
70     DECL_DUMP()
71 };
72 
73 class SFunctionEnv : public BaseEnv {
74 public:
75     // the first field is used to store GlobalEnv
76     static constexpr uint32_t CONSTRUCTOR_INDEX = 1;
77     static constexpr uint32_t RESERVED_ENV_LENGTH = 2;
78 
Cast(TaggedObject * object)79     static SFunctionEnv *Cast(TaggedObject *object)
80     {
81         ASSERT(JSTaggedValue(object).IsSFunctionEnv());
82         return static_cast<SFunctionEnv *>(object);
83     }
84 
ComputeSize(uint32_t numSlots)85     static size_t ComputeSize(uint32_t numSlots)
86     {
87         return TaggedArray::ComputeSize(JSTaggedValue::TaggedTypeSize(), numSlots + RESERVED_ENV_LENGTH);
88     }
89 
SetConstructor(JSThread * thread,JSTaggedValue value)90     void SetConstructor(JSThread *thread, JSTaggedValue value)
91     {
92         Set(thread, CONSTRUCTOR_INDEX, value);
93     }
94 
GetConstructor(const JSThread * thread)95     JSTaggedValue GetConstructor(const JSThread *thread) const
96     {
97         return Get(thread, CONSTRUCTOR_INDEX);
98     }
99 
100     DECL_DUMP()
101 };
102 }  // namespace panda::ecmascript
103 #endif  // ECMASCRIPT_LEXICALENV_H
104