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/symbol_table.h"
20 #include "ecmascript/ecma_string_table.h"
21 #include "ecmascript/template_map.h"
22
23 namespace panda::ecmascript {
Init(JSThread * thread)24 void GlobalEnv::Init(JSThread *thread)
25 {
26 SetRegisterSymbols(thread, SymbolTable::Create(thread));
27 SetGlobalRecord(thread, GlobalDictionary::Create(thread));
28 JSTaggedValue emptyStr = thread->GlobalConstants()->GetEmptyString();
29 EcmaStringTable *stringTable = thread->GetEcmaVM()->GetEcmaStringTable();
30 stringTable->GetOrInternFlattenString(thread->GetEcmaVM(), EcmaString::Cast(emptyStr.GetTaggedObject()));
31 SetTemplateMap(thread, TemplateMap::Create(thread));
32 SetObjectLiteralHClassCache(thread, JSTaggedValue::Hole());
33 SetJSThread(thread);
34 }
GetSymbol(JSThread * thread,const JSHandle<JSTaggedValue> & string)35 JSHandle<JSTaggedValue> GlobalEnv::GetSymbol(JSThread *thread, const JSHandle<JSTaggedValue> &string)
36 {
37 JSHandle<JSTaggedValue> symbolFunction(GetSymbolFunction());
38 return JSObject::GetProperty(thread, symbolFunction, string).GetValue();
39 }
40
GetStringPrototypeFunctionByName(JSThread * thread,const char * name)41 JSHandle<JSTaggedValue> GlobalEnv::GetStringPrototypeFunctionByName(JSThread *thread, const char *name)
42 {
43 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
44 JSHandle<JSTaggedValue> stringFuncPrototype(thread,
45 JSObject::GetPrototype(JSHandle<JSObject>(GetStringFunction())));
46 JSHandle<JSTaggedValue> nameKey(factory->NewFromUtf8(name));
47 return JSObject::GetProperty(thread, stringFuncPrototype, nameKey).GetValue();
48 }
49
GetStringFunctionByName(JSThread * thread,const char * name)50 JSHandle<JSTaggedValue> GlobalEnv::GetStringFunctionByName(JSThread *thread, const char *name)
51 {
52 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
53 JSHandle<JSTaggedValue> stringFuncObj = GetStringFunction();
54 JSHandle<JSTaggedValue> nameKey(factory->NewFromUtf8(name));
55 return JSObject::GetProperty(thread, stringFuncObj, nameKey).GetValue();
56 }
57 } // namespace panda::ecmascript
58