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