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 #include "ecmascript/global_env.h"
17
18 #include "ecmascript/global_dictionary.h"
19 #include "ecmascript/builtins/builtins_promise_handler.h"
20 #include "ecmascript/ic/ic_handler.h"
21 #include "ecmascript/ic/proto_change_details.h"
22 #include "ecmascript/jobs/micro_job_queue.h"
23 #include "ecmascript/jspandafile/program_object.h"
24 #include "ecmascript/js_generator_object.h"
25 #include "ecmascript/js_handle.h"
26 #include "ecmascript/js_promise.h"
27 #include "ecmascript/object_factory.h"
28 #include "ecmascript/symbol_table.h"
29 #include "ecmascript/ecma_string_table.h"
30 #include "ecmascript/template_map.h"
31 #include "ecmascript/js_array.h"
32 #include "ecmascript/js_realm.h"
33
34 namespace panda::ecmascript {
Init(JSThread * thread)35 void GlobalEnv::Init(JSThread *thread)
36 {
37 SetRegisterSymbols(thread, SymbolTable::Create(thread));
38 SetGlobalRecord(thread, GlobalDictionary::Create(thread));
39 JSTaggedValue emptyStr = thread->GlobalConstants()->GetEmptyString();
40 EcmaStringTable *stringTable = thread->GetEcmaVM()->GetEcmaStringTable();
41 stringTable->InternEmptyString(EcmaString::Cast(emptyStr.GetTaggedObject()));
42 SetTemplateMap(thread, TemplateMap::Create(thread));
43 SetObjectLiteralHClassCache(thread, JSTaggedValue::Hole());
44 }
GetSymbol(JSThread * thread,const JSHandle<JSTaggedValue> & string)45 JSHandle<JSTaggedValue> GlobalEnv::GetSymbol(JSThread *thread, const JSHandle<JSTaggedValue> &string)
46 {
47 JSHandle<JSTaggedValue> symbolFunction(GetSymbolFunction());
48 return JSObject::GetProperty(thread, symbolFunction, string).GetValue();
49 }
50
GetStringPrototypeFunctionByName(JSThread * thread,const char * name)51 JSHandle<JSTaggedValue> GlobalEnv::GetStringPrototypeFunctionByName(JSThread *thread, const char *name)
52 {
53 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
54 JSHandle<JSTaggedValue> stringFuncPrototype(thread,
55 JSObject::GetPrototype(JSHandle<JSObject>(GetStringFunction())));
56 JSHandle<JSTaggedValue> nameKey(factory->NewFromUtf8(name));
57 return JSObject::GetProperty(thread, stringFuncPrototype, nameKey).GetValue();
58 }
59
GetStringFunctionByName(JSThread * thread,const char * name)60 JSHandle<JSTaggedValue> GlobalEnv::GetStringFunctionByName(JSThread *thread, const char *name)
61 {
62 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
63 JSHandle<JSTaggedValue> stringFuncObj = GetStringFunction();
64 JSHandle<JSTaggedValue> nameKey(factory->NewFromUtf8(name));
65 return JSObject::GetProperty(thread, stringFuncObj, nameKey).GetValue();
66 }
67 } // namespace panda::ecmascript
68