• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "moduleHelpers.h"
17 
18 #include <util/helpers.h>
19 #include <libpandabase/utils/hash.h>
20 #include <protobufSnapshotGenerator.h>
21 
22 namespace panda::es2panda::util {
CompileNpmModuleEntryList(const std::string & entriesInfo,panda::es2panda::CompilerOptions & options,std::map<std::string,panda::es2panda::util::ProgramCache * > & progsInfo,panda::ArenaAllocator * allocator)23 void ModuleHelpers::CompileNpmModuleEntryList(const std::string &entriesInfo,
24     panda::es2panda::CompilerOptions &options, std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo,
25     panda::ArenaAllocator *allocator)
26 {
27     std::stringstream ss;
28     if (!util::Helpers::ReadFileToBuffer(entriesInfo, ss)) {
29         return;
30     }
31 
32     uint32_t hash = 0;
33     auto cacheFileIter = options.cacheFiles.find(entriesInfo);
34     if (cacheFileIter != options.cacheFiles.end()) {
35         hash = GetHash32String(reinterpret_cast<const uint8_t *>(ss.str().c_str()));
36 
37         auto cacheProgramInfo = panda::proto::ProtobufSnapshotGenerator::GetCacheContext(cacheFileIter->second,
38             allocator);
39         if (cacheProgramInfo != nullptr && cacheProgramInfo->hashCode == hash) {
40             auto *cache = allocator->New<util::ProgramCache>(hash, std::move(cacheProgramInfo->program));
41             progsInfo.insert({entriesInfo, cache});
42             return;
43         }
44     }
45 
46     auto *prog = allocator->New<panda::pandasm::Program>();
47     std::string line;
48     while (getline(ss, line)) {
49         std::size_t pos = line.find(":");
50         std::string recordName = line.substr(0, pos);
51         std::string field = line.substr(pos + 1);
52 
53         auto langExt = panda::pandasm::extensions::Language::ECMASCRIPT;
54         auto entryNameField = panda::pandasm::Field(langExt);
55         entryNameField.name = field;
56         entryNameField.type = panda::pandasm::Type("u8", 0);
57         entryNameField.metadata->SetValue(panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::U8>(
58             static_cast<bool>(0)));
59 
60         panda::pandasm::Record *entryRecord = new panda::pandasm::Record(recordName, langExt);
61         entryRecord->field_list.emplace_back(std::move(entryNameField));
62         prog->record_table.emplace(recordName, std::move(*entryRecord));
63     }
64 
65     auto *cache = allocator->New<util::ProgramCache>(hash, std::move(*prog), true);
66     progsInfo.insert({entriesInfo, cache});
67 }
68 }  // namespace panda::es2panda::util
69