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