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 SetJsonObjectHclassCache(thread, JSTaggedValue::Hole());
45 SetJSThread(thread);
46 }
GetSymbol(JSThread * thread,const JSHandle<JSTaggedValue> & string)47 JSHandle<JSTaggedValue> GlobalEnv::GetSymbol(JSThread *thread, const JSHandle<JSTaggedValue> &string)
48 {
49 JSHandle<JSTaggedValue> symbolFunction(GetSymbolFunction());
50 return JSObject::GetProperty(thread, symbolFunction, string).GetValue();
51 }
52
GetStringPrototypeFunctionByName(JSThread * thread,const char * name)53 JSHandle<JSTaggedValue> GlobalEnv::GetStringPrototypeFunctionByName(JSThread *thread, const char *name)
54 {
55 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
56 JSHandle<JSTaggedValue> stringFuncPrototype(thread,
57 JSObject::GetPrototype(JSHandle<JSObject>(GetStringFunction())));
58 JSHandle<JSTaggedValue> nameKey(factory->NewFromUtf8(name));
59 return JSObject::GetProperty(thread, stringFuncPrototype, nameKey).GetValue();
60 }
61
GetStringFunctionByName(JSThread * thread,const char * name)62 JSHandle<JSTaggedValue> GlobalEnv::GetStringFunctionByName(JSThread *thread, const char *name)
63 {
64 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
65 JSHandle<JSTaggedValue> stringFuncObj = GetStringFunction();
66 JSHandle<JSTaggedValue> nameKey(factory->NewFromUtf8(name));
67 return JSObject::GetProperty(thread, stringFuncObj, nameKey).GetValue();
68 }
69 } // namespace panda::ecmascript
70