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
22namespace panda::es2panda::util {
23void 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    CHECK_NOT_NULL(prog);
48    std::string line;
49    while (getline(ss, line)) {
50        std::size_t pos = line.find(":");
51        std::string recordName = line.substr(0, pos);
52        std::string field = line.substr(pos + 1);
53
54        auto langExt = panda::pandasm::extensions::Language::ECMASCRIPT;
55        auto entryNameField = panda::pandasm::Field(langExt);
56        entryNameField.name = field;
57        entryNameField.type = panda::pandasm::Type("u8", 0);
58        entryNameField.metadata->SetValue(panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::U8>(
59            static_cast<bool>(0)));
60
61        panda::pandasm::Record *entryRecord = new panda::pandasm::Record(recordName, langExt);
62        entryRecord->field_list.emplace_back(std::move(entryNameField));
63        prog->record_table.emplace(recordName, std::move(*entryRecord));
64        delete entryRecord;
65        entryRecord = nullptr;
66    }
67
68    auto *cache = allocator->New<util::ProgramCache>(hash, std::move(*prog), true);
69    progsInfo.insert({entriesInfo, cache});
70}
71}  // namespace panda::es2panda::util
72