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/template_string.h"
17
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/template_map.h"
20
21 namespace panda::ecmascript {
GetTemplateObject(JSThread * thread,JSHandle<JSTaggedValue> templateLiteral)22 JSHandle<JSTaggedValue> TemplateString::GetTemplateObject(JSThread *thread, JSHandle<JSTaggedValue> templateLiteral)
23 {
24 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
25 JSHandle<JSTaggedValue> rawStringsTag = JSObject::GetProperty(thread, templateLiteral, 0).GetValue();
26 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
27 JSHandle<JSTaggedValue> templateMapTag = env->GetTemplateMap();
28 JSHandle<TemplateMap> templateMap(templateMapTag);
29 int32_t element = templateMap->FindEntry(rawStringsTag.GetTaggedValue());
30 if (element != -1) {
31 return JSHandle<JSTaggedValue>(thread, templateMap->GetValue(element));
32 }
33 JSHandle<JSTaggedValue> cookedStringsTag = JSObject::GetProperty(thread, templateLiteral, 1).GetValue();
34 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
35 JSHandle<JSArray> cookedStrings(cookedStringsTag);
36 uint32_t count = cookedStrings->GetArrayLength();
37 auto countNum = JSTaggedNumber(count);
38 JSHandle<JSTaggedValue> templateArr = JSArray::ArrayCreate(thread, countNum);
39 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
40 JSHandle<JSTaggedValue> rawArr = JSArray::ArrayCreate(thread, countNum);
41 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
42 JSHandle<JSObject> templateObj(templateArr);
43 JSHandle<JSObject> rawObj(rawArr);
44 for (uint32_t i = 0; i < count; i++) {
45 JSHandle<JSTaggedValue> cookedValue = JSObject::GetProperty(thread, cookedStringsTag, i).GetValue();
46 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
47 PropertyDescriptor descCooked(thread, cookedValue, true, false, false);
48 JSArray::DefineOwnProperty(thread, templateObj, i, descCooked);
49 JSHandle<JSTaggedValue> rawValue = JSObject::GetProperty(thread, rawStringsTag, i).GetValue();
50 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
51 PropertyDescriptor descRaw(thread, rawValue, true, false, false);
52 JSArray::DefineOwnProperty(thread, rawObj, i, descRaw);
53 }
54 JSObject::SetIntegrityLevel(thread, rawObj, IntegrityLevel::FROZEN);
55 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
56 JSHandle<JSTaggedValue> raw(factory->NewFromASCII("raw"));
57 PropertyDescriptor desc(thread, rawArr, false, false, false);
58 JSArray::DefineOwnProperty(thread, templateObj, raw, desc);
59 JSObject::SetIntegrityLevel(thread, templateObj, IntegrityLevel::FROZEN);
60 TemplateMap::Insert(thread, templateMap, rawStringsTag, templateArr);
61 env->SetTemplateMap(thread, templateMap.GetTaggedValue());
62 return templateArr;
63 }
64 } // namespace panda::ecmascript
65