1 /*
2 * Copyright (c) 2022 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/jspandafile/module_data_extractor.h"
17 #include "ecmascript/jspandafile/accessor/module_data_accessor.h"
18 #include "ecmascript/base/string_helper.h"
19 #include "ecmascript/ecma_string.h"
20 #include "ecmascript/global_env.h"
21 #include "ecmascript/jspandafile/js_pandafile_manager.h"
22 #include "ecmascript/tagged_array-inl.h"
23 #include "ecmascript/module/js_module_manager.h"
24
25 #include "libpandafile/class_data_accessor-inl.h"
26 #include "libpandafile/literal_data_accessor-inl.h"
27
28 namespace panda::ecmascript {
29 using StringData = panda_file::StringData;
30
ParseModule(JSThread * thread,const JSPandaFile * jsPandaFile,const CString & descriptor,const CString & moduleFilename)31 JSHandle<JSTaggedValue> ModuleDataExtractor::ParseModule(JSThread *thread, const JSPandaFile *jsPandaFile,
32 const CString &descriptor, const CString &moduleFilename)
33 {
34 int moduleIdx = jsPandaFile->GetModuleRecordIdx(descriptor);
35 ASSERT(moduleIdx != -1);
36 const panda_file::File *pf = jsPandaFile->GetPandaFile();
37 panda_file::File::EntityId literalArraysId = pf->GetLiteralArraysId();
38 panda_file::LiteralDataAccessor lda(*pf, literalArraysId);
39 panda_file::File::EntityId moduleId;
40 if (jsPandaFile->IsNewVersion()) { // new pandafile version use new literal offset mechanism
41 moduleId = panda_file::File::EntityId(static_cast<uint32_t>(moduleIdx));
42 } else {
43 moduleId = lda.GetLiteralArrayId(static_cast<size_t>(moduleIdx));
44 }
45
46 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
47 JSHandle<SourceTextModule> moduleRecord = factory->NewSourceTextModule();
48 ModuleDataExtractor::ExtractModuleDatas(thread, jsPandaFile, moduleId, moduleRecord);
49
50 JSHandle<EcmaString> ecmaModuleFilename = factory->NewFromUtf8(moduleFilename);
51 moduleRecord->SetEcmaModuleFilename(thread, ecmaModuleFilename);
52
53 moduleRecord->SetStatus(ModuleStatus::UNINSTANTIATED);
54 moduleRecord->SetTypes(ModuleTypes::ECMA_MODULE);
55 moduleRecord->SetIsNewBcVersion(jsPandaFile->IsNewVersion());
56
57 return JSHandle<JSTaggedValue>::Cast(moduleRecord);
58 }
59
ExtractModuleDatas(JSThread * thread,const JSPandaFile * jsPandaFile,panda_file::File::EntityId moduleId,JSHandle<SourceTextModule> & moduleRecord)60 void ModuleDataExtractor::ExtractModuleDatas(JSThread *thread, const JSPandaFile *jsPandaFile,
61 panda_file::File::EntityId moduleId,
62 JSHandle<SourceTextModule> &moduleRecord)
63 {
64 [[maybe_unused]] EcmaHandleScope scope(thread);
65 const panda_file::File *pf = jsPandaFile->GetPandaFile();
66 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
67 ModuleDataAccessor mda(*pf, moduleId);
68 const std::vector<uint32_t> &requestModules = mda.getRequestModules();
69 JSHandle<TaggedArray> requestModuleArray = factory->NewTaggedArray(requestModules.size());
70 for (size_t idx = 0; idx < requestModules.size(); idx++) {
71 StringData sd = pf->GetStringData(panda_file::File::EntityId(requestModules[idx]));
72 JSTaggedValue value(factory->GetRawStringFromStringTable(sd.data, sd.utf16_length, sd.is_ascii));
73 requestModuleArray->Set(thread, idx, value);
74 }
75 if (requestModules.size()) {
76 moduleRecord->SetRequestedModules(thread, requestModuleArray);
77 }
78
79 // note the order can't change
80 mda.EnumerateImportEntry(thread, requestModuleArray, moduleRecord);
81 mda.EnumerateLocalExportEntry(thread, moduleRecord);
82 mda.EnumerateIndirectExportEntry(thread, requestModuleArray, moduleRecord);
83 mda.EnumerateStarExportEntry(thread, requestModuleArray, moduleRecord);
84 }
85
ParseCjsModule(JSThread * thread,const JSPandaFile * jsPandaFile)86 JSHandle<JSTaggedValue> ModuleDataExtractor::ParseCjsModule(JSThread *thread, const JSPandaFile *jsPandaFile)
87 {
88 const CString &descriptor = jsPandaFile->GetJSPandaFileDesc();
89 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
90 JSHandle<SourceTextModule> moduleRecord = factory->NewSourceTextModule();
91
92 JSHandle<EcmaString> cjsModuleFilename = factory->NewFromUtf8(descriptor);
93 moduleRecord->SetEcmaModuleFilename(thread, cjsModuleFilename);
94
95 JSHandle<JSTaggedValue> defaultName = thread->GlobalConstants()->GetHandledDefaultString();
96 JSHandle<LocalExportEntry> localExportEntry = factory->NewLocalExportEntry(defaultName, defaultName);
97 SourceTextModule::AddLocalExportEntry(thread, moduleRecord, localExportEntry, 0, 1); // 1 means len
98 moduleRecord->SetStatus(ModuleStatus::UNINSTANTIATED);
99 moduleRecord->SetTypes(ModuleTypes::CJS_MODULE);
100 moduleRecord->SetIsNewBcVersion(jsPandaFile->IsNewVersion());
101
102 return JSHandle<JSTaggedValue>::Cast(moduleRecord);
103 }
104
ParseJsonModule(JSThread * thread,const JSPandaFile * jsPandaFile,const CString & moduleFilename,const CString & recordName)105 JSHandle<JSTaggedValue> ModuleDataExtractor::ParseJsonModule(JSThread *thread, const JSPandaFile *jsPandaFile,
106 const CString &moduleFilename, const CString &recordName)
107 {
108 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
109 JSHandle<SourceTextModule> moduleRecord = factory->NewSourceTextModule();
110
111 JSHandle<JSTaggedValue> defaultName = thread->GlobalConstants()->GetHandledDefaultString();
112 JSHandle<LocalExportEntry> localExportEntry = factory->NewLocalExportEntry(defaultName, defaultName);
113 SourceTextModule::AddLocalExportEntry(thread, moduleRecord, localExportEntry, 0, 1); // 1 means len
114 JSTaggedValue jsonData = ModuleManager::JsonParse(thread, jsPandaFile, recordName);
115 moduleRecord->StoreModuleValue(thread, 0, JSHandle<JSTaggedValue>(thread, jsonData)); // index = 0
116
117 JSHandle<EcmaString> ecmaModuleFilename = factory->NewFromUtf8(moduleFilename);
118 moduleRecord->SetEcmaModuleFilename(thread, ecmaModuleFilename);
119
120 moduleRecord->SetStatus(ModuleStatus::UNINSTANTIATED);
121 moduleRecord->SetTypes(ModuleTypes::JSON_MODULE);
122 moduleRecord->SetIsNewBcVersion(jsPandaFile->IsNewVersion());
123
124 return JSHandle<JSTaggedValue>::Cast(moduleRecord);
125 }
126
ParseNativeModule(JSThread * thread,const CString & moduleRequestName,ModuleTypes moduleType)127 JSHandle<JSTaggedValue> ModuleDataExtractor::ParseNativeModule(JSThread *thread,
128 const CString &moduleRequestName, ModuleTypes moduleType)
129 {
130 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
131 JSHandle<SourceTextModule> moduleRecord = factory->NewSourceTextModule();
132
133 // set moduleRecordName as non-undefined to distinguish between merge and non-merge mode
134 JSHandle<EcmaString> falsyRecordName = factory->NewFromUtf8(moduleRequestName);
135 moduleRecord->SetEcmaModuleRecordName(thread, falsyRecordName);
136 JSHandle<JSTaggedValue> defaultName = thread->GlobalConstants()->GetHandledDefaultString();
137 JSHandle<LocalExportEntry> localExportEntry = factory->NewLocalExportEntry(defaultName, defaultName);
138 SourceTextModule::AddLocalExportEntry(thread, moduleRecord, localExportEntry, 0, 1);
139 moduleRecord->SetTypes(moduleType);
140 moduleRecord->SetIsNewBcVersion(true);
141 moduleRecord->SetStatus(ModuleStatus::INSTANTIATED);
142 moduleRecord->StoreModuleValue(thread, 0, thread->GlobalConstants()->GetHandledUndefined());
143
144 return JSHandle<JSTaggedValue>::Cast(moduleRecord);
145 }
146 } // namespace panda::ecmascript
147