14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#include "ecmascript/template_string.h" 174514f5e3Sopenharmony_ci 184514f5e3Sopenharmony_ci#include "ecmascript/global_env.h" 194514f5e3Sopenharmony_ci#include "ecmascript/template_map.h" 204514f5e3Sopenharmony_ci 214514f5e3Sopenharmony_cinamespace panda::ecmascript { 224514f5e3Sopenharmony_ciJSHandle<JSTaggedValue> TemplateString::GetTemplateObject(JSThread *thread, JSHandle<JSTaggedValue> templateLiteral) 234514f5e3Sopenharmony_ci{ 244514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 254514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> rawStringsTag = JSObject::GetProperty(thread, templateLiteral, 0).GetValue(); 264514f5e3Sopenharmony_ci RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread); 274514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> templateMapTag = env->GetTemplateMap(); 284514f5e3Sopenharmony_ci JSHandle<TemplateMap> templateMap(templateMapTag); 294514f5e3Sopenharmony_ci int32_t element = templateMap->FindEntry(rawStringsTag.GetTaggedValue()); 304514f5e3Sopenharmony_ci if (element != -1) { 314514f5e3Sopenharmony_ci return JSHandle<JSTaggedValue>(thread, templateMap->GetValue(element)); 324514f5e3Sopenharmony_ci } 334514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> cookedStringsTag = JSObject::GetProperty(thread, templateLiteral, 1).GetValue(); 344514f5e3Sopenharmony_ci RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread); 354514f5e3Sopenharmony_ci JSHandle<JSArray> cookedStrings(cookedStringsTag); 364514f5e3Sopenharmony_ci uint32_t count = cookedStrings->GetArrayLength(); 374514f5e3Sopenharmony_ci auto countNum = JSTaggedNumber(count); 384514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> templateArr = JSArray::ArrayCreate(thread, countNum); 394514f5e3Sopenharmony_ci RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread); 404514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> rawArr = JSArray::ArrayCreate(thread, countNum); 414514f5e3Sopenharmony_ci RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread); 424514f5e3Sopenharmony_ci JSHandle<JSObject> templateObj(templateArr); 434514f5e3Sopenharmony_ci JSHandle<JSObject> rawObj(rawArr); 444514f5e3Sopenharmony_ci for (uint32_t i = 0; i < count; i++) { 454514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> cookedValue = JSObject::GetProperty(thread, cookedStringsTag, i).GetValue(); 464514f5e3Sopenharmony_ci RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread); 474514f5e3Sopenharmony_ci PropertyDescriptor descCooked(thread, cookedValue, true, false, false); 484514f5e3Sopenharmony_ci JSArray::DefineOwnProperty(thread, templateObj, i, descCooked); 494514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> rawValue = JSObject::GetProperty(thread, rawStringsTag, i).GetValue(); 504514f5e3Sopenharmony_ci RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread); 514514f5e3Sopenharmony_ci PropertyDescriptor descRaw(thread, rawValue, true, false, false); 524514f5e3Sopenharmony_ci JSArray::DefineOwnProperty(thread, rawObj, i, descRaw); 534514f5e3Sopenharmony_ci } 544514f5e3Sopenharmony_ci JSObject::SetIntegrityLevel(thread, rawObj, IntegrityLevel::FROZEN); 554514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 564514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> raw(factory->NewFromASCII("raw")); 574514f5e3Sopenharmony_ci PropertyDescriptor desc(thread, rawArr, false, false, false); 584514f5e3Sopenharmony_ci JSArray::DefineOwnProperty(thread, templateObj, raw, desc); 594514f5e3Sopenharmony_ci JSObject::SetIntegrityLevel(thread, templateObj, IntegrityLevel::FROZEN); 604514f5e3Sopenharmony_ci TemplateMap::Insert(thread, templateMap, rawStringsTag, templateArr); 614514f5e3Sopenharmony_ci env->SetTemplateMap(thread, templateMap.GetTaggedValue()); 624514f5e3Sopenharmony_ci return templateArr; 634514f5e3Sopenharmony_ci} 644514f5e3Sopenharmony_ci} // namespace panda::ecmascript 65