• 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_GLOBAL_ENV_H
17 #define ECMASCRIPT_GLOBAL_ENV_H
18 
19 #include "ecmascript/js_global_object.h"
20 #include "ecmascript/js_function.h"
21 #include "ecmascript/lexical_env.h"
22 #include "ecmascript/js_handle.h"
23 #include "ecmascript/global_env_constants-inl.h"
24 
25 namespace panda::ecmascript {
26 class JSThread;
27 
28 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
29 #define GLOBAL_ENV_FIELDS(V)                                                                        \
30     /* Function */                                                                                  \
31     V(JSTaggedValue, ObjectFunction, OBJECT_FUNCTION_INDEX)                                         \
32     V(JSTaggedValue, ObjectFunctionPrototype, OBJECT_FUNCTION_PROTOTYPE_INDEX)                      \
33     V(JSTaggedValue, ObjectFunctionPrototypeClass, OBJECT_FUNCTION_PROTOTYPE_CLASS_INDEX)           \
34     V(JSTaggedValue, FunctionFunction, FUNCTION_FUNCTION_INDEX)                                     \
35     V(JSTaggedValue, FunctionPrototype, FUNCTION_PROTOTYPE_INDEX)                                   \
36     V(JSTaggedValue, NumberFunction, NUMBER_FUNCTION_INDEX)                                         \
37     V(JSTaggedValue, BigIntFunction, BIGINT_FUNCTION_INDEX)                                         \
38     V(JSTaggedValue, DateFunction, DATE_FUNCTION_INDEX)                                             \
39     V(JSTaggedValue, BooleanFunction, BOOLEAN_FUNCTION_INDEX)                                       \
40     V(JSTaggedValue, ErrorFunction, ERROR_FUNCTION_INDEX)                                           \
41     V(JSTaggedValue, ArrayFunction, ARRAY_FUNCTION_INDEX)                                           \
42     V(JSTaggedValue, ArrayPrototype, ARRAY_PROTOTYPE_INDEX)                                         \
43     V(JSTaggedValue, TypedArrayFunction, TYPED_ARRAY_FUNCTION_INDEX)                                \
44     V(JSTaggedValue, TypedArrayPrototype, TYPED_ARRAY_PROTOTYPE_INDEX)                              \
45     V(JSTaggedValue, Int8ArrayFunction, INT8_ARRAY_FUNCTION_INDEX)                                  \
46     V(JSTaggedValue, Uint8ArrayFunction, UINT8_ARRAY_FUNCTION_INDEX)                                \
47     V(JSTaggedValue, Uint8ClampedArrayFunction, UINT8_CLAMPED_ARRAY_FUNCTION_INDEX)                 \
48     V(JSTaggedValue, Int16ArrayFunction, INT16_ARRAY_FUNCTION_INDEX)                                \
49     V(JSTaggedValue, Uint16ArrayFunction, UINT16_ARRAY_FUNCTION_INDEX)                              \
50     V(JSTaggedValue, Int32ArrayFunction, INT32_ARRAY_FUNCTION_INDEX)                                \
51     V(JSTaggedValue, Uint32ArrayFunction, UINT32_ARRAY_FUNCTION_INDEX)                              \
52     V(JSTaggedValue, Float32ArrayFunction, FLOAT32_ARRAY_FUNCTION_INDEX)                            \
53     V(JSTaggedValue, Float64ArrayFunction, FLOAT64_ARRAY_FUNCTION_INDEX)                            \
54     V(JSTaggedValue, ArrayBufferFunction, ARRAY_BUFFER_FUNCTION_INDEX)                              \
55     V(JSTaggedValue, ArrayProtoValuesFunction, ARRAY_PROTO_VALUES_FUNCTION_INDEX)                   \
56     V(JSTaggedValue, DataViewFunction, DATA_VIEW_FUNCTION_INDEX)                                    \
57     V(JSTaggedValue, SymbolFunction, SYMBOL_FUNCTION_INDEX)                                         \
58     V(JSTaggedValue, RangeErrorFunction, RANGE_ERROR_FUNCTION_INDEX)                                \
59     V(JSTaggedValue, ReferenceErrorFunction, REFERENCE_ERROR_FUNCTION_INDEX)                        \
60     V(JSTaggedValue, TypeErrorFunction, TYPE_ERROR_FUNCTION_INDEX)                                  \
61     V(JSTaggedValue, URIErrorFunction, URI_ERROR_FUNCTION_INDEX)                                    \
62     V(JSTaggedValue, SyntaxErrorFunction, SYNTAX_ERROR_FUNCTION_INDEX)                              \
63     V(JSTaggedValue, EvalErrorFunction, EVAL_ERROR_FUNCTION_INDEX)                                  \
64     V(JSTaggedValue, IntlFunction, INTL_FUNCTION_INDEX)                                             \
65     V(JSTaggedValue, LocaleFunction, LOCALE_FUNCTION_INDEX)                                         \
66     V(JSTaggedValue, DateTimeFormatFunction, DATE_TIME_FORMAT_FUNCTION_INDEX)                       \
67     V(JSTaggedValue, RelativeTimeFormatFunction, RELATIVE_TIME_FORMAT_FUNCTION_INDEX)               \
68     V(JSTaggedValue, NumberFormatFunction, NUMBER_FORMAT_FUNCTION_INDEX)                            \
69     V(JSTaggedValue, CollatorFunction, COLLATOR_FUNCTION_INDEX)                                     \
70     V(JSTaggedValue, PluralRulesFunction, PLURAL_RULES_FUNCTION_INDEX)                              \
71     V(JSTaggedValue, RegExpFunction, REGEXP_FUNCTION_INDEX)                                         \
72     V(JSTaggedValue, BuiltinsSetFunction, BUILTINS_SET_FUNCTION_INDEX)                              \
73     V(JSTaggedValue, SetPrototype, SET_PROTOTYPE_INDEX)                                             \
74     V(JSTaggedValue, BuiltinsMapFunction, BUILTINS_MAP_FUNCTION_INDEX)                              \
75     V(JSTaggedValue, BuiltinsWeakMapFunction, BUILTINS_WEAK_MAP_FUNCTION_INDEX)                     \
76     V(JSTaggedValue, BuiltinsWeakSetFunction, BUILTINS_WEAK_SET_FUNCTION_INDEX)                     \
77     V(JSTaggedValue, MapPrototype, MAP_PROTOTYPE_INDEX)                                             \
78     V(JSTaggedValue, MathFunction, MATH_FUNCTION_INDEX)                                             \
79     V(JSTaggedValue, JsonFunction, JSON_FUNCTION_INDEX)                                             \
80     V(JSTaggedValue, StringFunction, STRING_FUNCTION_INDEX)                                         \
81     V(JSTaggedValue, ProxyFunction, PROXY_FUNCTION_INDEX)                                           \
82     V(JSTaggedValue, GeneratorFunctionFunction, GENERATOR_FUNCTION_OFFSET)                          \
83     V(JSTaggedValue, GeneratorFunctionPrototype, GENERATOR_FUNCTION_PROTOTYPE_OFFSET)               \
84     V(JSTaggedValue, InitialGenerator, INITIAL_GENERATOR_OFFSET)                                    \
85     V(JSTaggedValue, GeneratorPrototype, GENERATOR_PROTOTYPE_OFFSET)                                \
86     V(JSTaggedValue, ReflectFunction, REFLECT_FUNCTION_INDEX)                                       \
87     V(JSTaggedValue, AsyncFunction, ASYNC_FUNCTION_INDEX)                                           \
88     V(JSTaggedValue, AsyncFunctionPrototype, ASYNC_FUNCTION_PROTOTYPE_INDEX)                        \
89     V(JSTaggedValue, JSGlobalObject, JS_GLOBAL_OBJECT_INDEX)                                        \
90     V(JSTaggedValue, EmptyArray, EMPTY_ARRAY_OBJECT_INDEX)                                          \
91     V(JSTaggedValue, EmptyLayoutInfo, EMPTY_LAYOUT_INFO_OBJECT_INDEX)                               \
92     V(JSTaggedValue, EmptyTaggedQueue, EMPTY_TAGGED_QUEUE_OBJECT_INDEX)                             \
93     V(JSTaggedValue, HasInstanceSymbol, HASINSTANCE_SYMBOL_INDEX)                                   \
94     V(JSTaggedValue, IsConcatSpreadableSymbol, ISCONCAT_SYMBOL_INDEX)                               \
95     V(JSTaggedValue, ToStringTagSymbol, TOSTRINGTAG_SYMBOL_INDEX)                                   \
96     V(JSTaggedValue, IteratorSymbol, ITERATOR_SYMBOL_INDEX)                                         \
97     V(JSTaggedValue, MatchSymbol, MATCH_SYMBOL_INDEX)                                               \
98     V(JSTaggedValue, ReplaceSymbol, REPLACE_SYMBOL_INDEX)                                           \
99     V(JSTaggedValue, SearchSymbol, SEARCH_SYMBOL_INDEX)                                             \
100     V(JSTaggedValue, SpeciesSymbol, SPECIES_SYMBOL_INDEX)                                           \
101     V(JSTaggedValue, SplitSymbol, SPLIT_SYMBOL_INDEX)                                               \
102     V(JSTaggedValue, ToPrimitiveSymbol, TOPRIMITIVE_SYMBOL_INDEX)                                   \
103     V(JSTaggedValue, UnscopablesSymbol, UNSCOPABLES_SYMBOL_INDEX)                                   \
104     V(JSTaggedValue, HoleySymbol, HOLEY_SYMBOL_OFFSET)                                              \
105     V(JSTaggedValue, ElementICSymbol, ELEMENT_IC_SYMBOL_OFFSET)                                     \
106     V(JSTaggedValue, IteratorPrototype, ITERATOR_PROTOTYPE_INDEX)                                   \
107     V(JSTaggedValue, ForinIteratorPrototype, FORIN_ITERATOR_PROTOTYPE_INDEX)                        \
108     V(JSTaggedValue, ForinIteratorClass, FOR_IN_ITERATOR_CLASS_INDEX)                               \
109     V(JSTaggedValue, StringIterator, STRING_ITERATOR_INDEX)                                         \
110     V(JSTaggedValue, MapIteratorPrototype, MAP_ITERATOR_PROTOTYPE_INDEX)                            \
111     V(JSTaggedValue, SetIteratorPrototype, SET_ITERATOR_PROTOTYPE_INDEX)                            \
112     V(JSTaggedValue, ArrayIteratorPrototype, ARRAY_ITERATOR_PROTOTYPE_INDEX)                        \
113     V(JSTaggedValue, StringIteratorPrototype, STRING_ITERATOR_PROTOTYPE_INDEX)                      \
114     /* SymbolTable *RegisterSymbols */                                                              \
115     V(JSTaggedValue, RegisterSymbols, SYMBOLS_INDEX)                                                \
116     V(JSTaggedValue, ThrowTypeError, THROW_TYPE_ERROR_INDEX)                                        \
117     V(JSTaggedValue, PromiseFunction, PROMISE_FUNCTION_INDEX)                                       \
118     V(JSTaggedValue, PromiseReactionJob, PROMISE_REACTION_JOB_INDEX)                                \
119     V(JSTaggedValue, PromiseResolveThenableJob, PROMISE_REACTION_THENABLE_JOB_INDEX)                \
120     V(JSTaggedValue, TemplateMap, TEMPLATE_MAP_INDEX)                                               \
121     V(JSTaggedValue, FunctionClassWithProto, FUNCTION_CLASS_WITH_PROTO)                             \
122     V(JSTaggedValue, FunctionClassWithoutProto, FUNCTION_CLASS_WITHOUT_PROTO)                       \
123     V(JSTaggedValue, FunctionClassWithoutName, FUNCTION_CLASS_WITHOUT_NAME)                         \
124     V(JSTaggedValue, ArgumentsClass, ARGUMENTS_CLASS)                                               \
125     V(JSTaggedValue, ArgumentsCallerAccessor, ARGUMENTSCALLERACCESSOR)                              \
126     V(JSTaggedValue, ArgumentsCalleeAccessor, ARGUMENTSCALLEEACCESSOR)                              \
127     V(JSTaggedValue, AsyncFunctionClass, ASYNC_FUNCTION_CLASS)                                      \
128     V(JSTaggedValue, AsyncAwaitStatusFunctionClass, ASYNC_AWAIT_STATUS_FUNCTION_CLASS)              \
129     V(JSTaggedValue, PromiseReactionFunctionClass, PROMISE_REACTION_FUNCTION_CLASS)                 \
130     V(JSTaggedValue, PromiseExecutorFunctionClass, PROMISE_EXECUTOR_FUNCTION_CLASS)                 \
131     V(JSTaggedValue, GeneratorFunctionClass, GENERATOR_FUNCTION_CLASS)                              \
132     V(JSTaggedValue, PromiseAllResolveElementFunctionClass, PROMISE_ALL_RESOLVE_ELEMENT_FUNC_CLASS) \
133     V(JSTaggedValue, ProxyRevocFunctionClass, PROXY_REVOC_FUNCTION_CLASS)                           \
134     V(JSTaggedValue, NativeErrorFunctionClass, NATIVE_ERROR_FUNCTION_CLASS)                         \
135     V(JSTaggedValue, SpecificTypedArrayFunctionClass, SPERCIFIC_TYPED_ARRAY_FUNCTION_CLASS)         \
136     V(JSTaggedValue, ConstructorFunctionClass, CONSTRUCTOR_FUNCTION_CLASS)                          \
137     V(JSTaggedValue, NormalFunctionClass, NORMAL_FUNCTION_CLASS)                                    \
138     V(JSTaggedValue, JSIntlBoundFunctionClass, JS_INTL_BOUND_FUNCTION_CLASS)                        \
139     V(JSTaggedValue, NumberFormatLocales, NUMBER_FORMAT_LOCALES_INDEX)                              \
140     V(JSTaggedValue, DateTimeFormatLocales, DATE_TIMEFORMAT_LOCALES_INDEX)                          \
141     V(JSTaggedValue, GlobalRecord, GLOBAL_RECORD)
142 
143 class GlobalEnv : public TaggedObject {
144 public:
GetGlobalObject()145     JSTaggedValue GetGlobalObject() const
146     {
147         return GetJSGlobalObject().GetTaggedValue();
148     }
149 
150     void InitGlobalObject();
151 
Cast(ObjectHeader * object)152     static GlobalEnv *Cast(ObjectHeader *object)
153     {
154         ASSERT(JSTaggedValue(object).IsJSGlobalEnv());
155         return reinterpret_cast<GlobalEnv *>(object);
156     }
157 
158     JSHandle<JSTaggedValue> GetSymbol(JSThread *thread, const JSHandle<JSTaggedValue> &string);
159     JSHandle<JSTaggedValue> GetStringFunctionByName(JSThread *thread, const char *name);
160     JSHandle<JSTaggedValue> GetStringPrototypeFunctionByName(JSThread *thread, const char *name);
161 
162     enum Field {
163 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
164 #define GLOBAL_ENV_SLOT(type, name, index) index,
165         GLOBAL_ENV_FIELDS(GLOBAL_ENV_SLOT)
166 #undef GLOBAL_ENV_SLOT
167             FINAL_INDEX
168     };
169 
170 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
171 #define GLOBAL_ENV_FIELD_ACCESSORS(type, name, index)                                                   \
172     inline JSHandle<type> Get##name() const                                                             \
173     {                                                                                                   \
174         const uintptr_t address =                                                                       \
175             reinterpret_cast<uintptr_t>(this) + HEADER_SIZE + index * JSTaggedValue::TaggedTypeSize();  \
176         JSHandle<type> result(address);                                                                 \
177         return result;                                                                                  \
178     }                                                                                                   \
179     template<typename T>                                                                               \
180     inline void Set##name(const JSThread *thread, JSHandle<T> value, BarrierMode mode = WRITE_BARRIER)  \
181     {                                                                                                   \
182         int offset = HEADER_SIZE + index * JSTaggedValue::TaggedTypeSize();                             \
183         if (mode == WRITE_BARRIER && value.GetTaggedValue().IsHeapObject()) {                           \
184             Barriers::SetDynObject<true>(thread, this, offset, value.GetTaggedValue().GetRawData());    \
185         } else {                                                                                        \
186             Barriers::SetDynPrimitive<JSTaggedType>(this, offset, value.GetTaggedValue().GetRawData()); \
187         }                                                                                               \
188     }                                                                                                   \
189     inline void Set##name(const JSThread *thread, type value, BarrierMode mode = WRITE_BARRIER)         \
190     {                                                                                                   \
191         int offset = HEADER_SIZE + index * JSTaggedValue::TaggedTypeSize();                             \
192         if (mode == WRITE_BARRIER && value.IsHeapObject()) {                                            \
193             Barriers::SetDynObject<true>(thread, this, offset, value.GetRawData());                     \
194         } else {                                                                                        \
195             Barriers::SetDynPrimitive<JSTaggedType>(this, offset, value.GetRawData());                  \
196         }                                                                                               \
197     }
198     GLOBAL_ENV_FIELDS(GLOBAL_ENV_FIELD_ACCESSORS)
199 #undef GLOBAL_ENV_FIELD_ACCESSORS
200 
201     static constexpr size_t HEADER_SIZE = TaggedObjectSize();
202     static constexpr size_t SIZE = HEADER_SIZE + FINAL_INDEX * JSTaggedValue::TaggedTypeSize();
203 
204     DECL_VISIT_OBJECT(HEADER_SIZE, SIZE);
205 
206     DECL_DUMP()
207 };
208 }  // namespace panda::ecmascript
209 
210 #endif  // ECMASCRIPT_GLOBAL_ENV_H
211