14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2022 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#ifndef ECMASCRIPT_REQUIRE_JS_CJS_MODULE_CACHE_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_REQUIRE_JS_CJS_MODULE_CACHE_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h"
204514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h"
214514f5e3Sopenharmony_ci#include "ecmascript/tagged_hash_table.h"
224514f5e3Sopenharmony_ci#include "ecmascript/ecma_string.h"
234514f5e3Sopenharmony_ci#include "ecmascript/require/js_cjs_module.h"
244514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h"
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_cinamespace panda::ecmascript {
274514f5e3Sopenharmony_ciclass CjsModuleCache : public TaggedHashTable<CjsModuleCache> {
284514f5e3Sopenharmony_cipublic:
294514f5e3Sopenharmony_ci    using HashTable = TaggedHashTable<CjsModuleCache>;
304514f5e3Sopenharmony_ci
314514f5e3Sopenharmony_ci    static CjsModuleCache *Cast(TaggedObject *object)
324514f5e3Sopenharmony_ci    {
334514f5e3Sopenharmony_ci        ASSERT(JSTaggedValue(object).IsTaggedArray());
344514f5e3Sopenharmony_ci        return reinterpret_cast<CjsModuleCache *>(object);
354514f5e3Sopenharmony_ci    }
364514f5e3Sopenharmony_ci
374514f5e3Sopenharmony_ci    inline static int GetKeyIndex(int entry)
384514f5e3Sopenharmony_ci    {
394514f5e3Sopenharmony_ci        return HashTable::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_KEY_INDEX;
404514f5e3Sopenharmony_ci    }
414514f5e3Sopenharmony_ci
424514f5e3Sopenharmony_ci    inline static int GetValueIndex(int entry)
434514f5e3Sopenharmony_ci    {
444514f5e3Sopenharmony_ci        return HashTable::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_VALUE_INDEX;
454514f5e3Sopenharmony_ci    }
464514f5e3Sopenharmony_ci
474514f5e3Sopenharmony_ci    inline static int GetEntryIndex(int entry)
484514f5e3Sopenharmony_ci    {
494514f5e3Sopenharmony_ci        return HashTable::TABLE_HEADER_SIZE + entry * GetEntrySize();
504514f5e3Sopenharmony_ci    }
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_ci    inline static int GetEntrySize()
534514f5e3Sopenharmony_ci    {
544514f5e3Sopenharmony_ci        return ENTRY_SIZE;
554514f5e3Sopenharmony_ci    }
564514f5e3Sopenharmony_ci
574514f5e3Sopenharmony_ci    static inline bool IsMatch(const JSTaggedValue &fileName, const JSTaggedValue &other)
584514f5e3Sopenharmony_ci    {
594514f5e3Sopenharmony_ci        if (fileName.IsHole() || fileName.IsUndefined()) {
604514f5e3Sopenharmony_ci            return false;
614514f5e3Sopenharmony_ci        }
624514f5e3Sopenharmony_ci
634514f5e3Sopenharmony_ci        auto *nameString = static_cast<EcmaString *>(fileName.GetTaggedObject());
644514f5e3Sopenharmony_ci        auto *otherString = static_cast<EcmaString *>(other.GetTaggedObject());
654514f5e3Sopenharmony_ci        return EcmaStringAccessor::StringsAreEqual(nameString, otherString);
664514f5e3Sopenharmony_ci    }
674514f5e3Sopenharmony_ci
684514f5e3Sopenharmony_ci    static inline uint32_t Hash(const JSTaggedValue &key)
694514f5e3Sopenharmony_ci    {
704514f5e3Sopenharmony_ci        ASSERT(key.IsString());
714514f5e3Sopenharmony_ci        EcmaString *nameStr = static_cast<EcmaString *>(key.GetTaggedObject());
724514f5e3Sopenharmony_ci        return EcmaStringAccessor(nameStr).GetHashcode();
734514f5e3Sopenharmony_ci    }
744514f5e3Sopenharmony_ci
754514f5e3Sopenharmony_ci    static const int DEFAULT_ELEMENTS_NUMBER = 64;
764514f5e3Sopenharmony_ci
774514f5e3Sopenharmony_ci    static JSHandle<CjsModuleCache> Create(JSThread *thread, int numberOfElements = DEFAULT_ELEMENTS_NUMBER)
784514f5e3Sopenharmony_ci    {
794514f5e3Sopenharmony_ci        return HashTable::Create(thread, numberOfElements);
804514f5e3Sopenharmony_ci    }
814514f5e3Sopenharmony_ci
824514f5e3Sopenharmony_ci    inline int FindEntry(const JSTaggedValue &key)
834514f5e3Sopenharmony_ci    {
844514f5e3Sopenharmony_ci        int size = Size();
854514f5e3Sopenharmony_ci        int count = 1;
864514f5e3Sopenharmony_ci        JSTaggedValue keyValue;
874514f5e3Sopenharmony_ci        uint32_t hash = Hash(key);
884514f5e3Sopenharmony_ci
894514f5e3Sopenharmony_ci        for (uint32_t entry = GetFirstPosition(hash, size);; entry = GetNextPosition(entry, count++, size)) {
904514f5e3Sopenharmony_ci            keyValue = GetKey(entry);
914514f5e3Sopenharmony_ci            if (keyValue.IsHole()) {
924514f5e3Sopenharmony_ci                continue;
934514f5e3Sopenharmony_ci            }
944514f5e3Sopenharmony_ci            if (keyValue.IsUndefined()) {
954514f5e3Sopenharmony_ci                return -1;
964514f5e3Sopenharmony_ci            }
974514f5e3Sopenharmony_ci            if (IsMatch(key, keyValue)) {
984514f5e3Sopenharmony_ci                return entry;
994514f5e3Sopenharmony_ci            }
1004514f5e3Sopenharmony_ci        }
1014514f5e3Sopenharmony_ci        return -1;
1024514f5e3Sopenharmony_ci    }
1034514f5e3Sopenharmony_ci
1044514f5e3Sopenharmony_ci    inline bool ContainsModule(const JSTaggedValue &key)
1054514f5e3Sopenharmony_ci    {
1064514f5e3Sopenharmony_ci        int entry = FindEntry(key);
1074514f5e3Sopenharmony_ci        return entry != -1;
1084514f5e3Sopenharmony_ci    }
1094514f5e3Sopenharmony_ci
1104514f5e3Sopenharmony_ci    inline JSTaggedValue GetModule(const JSTaggedValue &key)
1114514f5e3Sopenharmony_ci    {
1124514f5e3Sopenharmony_ci        int entry = FindEntry(key);
1134514f5e3Sopenharmony_ci        ASSERT(entry != -1);
1144514f5e3Sopenharmony_ci        return GetValue(entry);
1154514f5e3Sopenharmony_ci    }
1164514f5e3Sopenharmony_ci
1174514f5e3Sopenharmony_ci    inline void SetEntry(const JSThread *thread, int entry,
1184514f5e3Sopenharmony_ci                         const JSHandle<JSTaggedValue> &key,
1194514f5e3Sopenharmony_ci                         const JSHandle<JSTaggedValue> &value)
1204514f5e3Sopenharmony_ci    {
1214514f5e3Sopenharmony_ci        JSTaggedValue keyValue = key.GetTaggedValue();
1224514f5e3Sopenharmony_ci        JSTaggedValue valueValue = value.GetTaggedValue();
1234514f5e3Sopenharmony_ci        SetKey(thread, entry, keyValue);
1244514f5e3Sopenharmony_ci        SetValue(thread, entry, valueValue);
1254514f5e3Sopenharmony_ci    }
1264514f5e3Sopenharmony_ci
1274514f5e3Sopenharmony_ci    static JSHandle<CjsModuleCache> PutIfAbsentAndReset(const JSThread *thread,
1284514f5e3Sopenharmony_ci                                                        const JSHandle<CjsModuleCache> &dictionary,
1294514f5e3Sopenharmony_ci                                                        const JSHandle<JSTaggedValue> &key,
1304514f5e3Sopenharmony_ci                                                        const JSHandle<JSTaggedValue> &value);
1314514f5e3Sopenharmony_ci    static JSHandle<CjsModuleCache> ResetModule(const JSThread *thread,
1324514f5e3Sopenharmony_ci                                                const JSHandle<CjsModuleCache> &dictionary,
1334514f5e3Sopenharmony_ci                                                const JSHandle<JSTaggedValue> &key,
1344514f5e3Sopenharmony_ci                                                const JSHandle<JSTaggedValue> &value);
1354514f5e3Sopenharmony_ci    static int ComputeCompactSize([[maybe_unused]] const JSHandle<CjsModuleCache> &table, int computeHashTableSize,
1364514f5e3Sopenharmony_ci        [[maybe_unused]] int tableSize, [[maybe_unused]] int addedElements)
1374514f5e3Sopenharmony_ci    {
1384514f5e3Sopenharmony_ci        return computeHashTableSize;
1394514f5e3Sopenharmony_ci    }
1404514f5e3Sopenharmony_ci    static constexpr int ENTRY_KEY_INDEX = 0;
1414514f5e3Sopenharmony_ci    static constexpr int ENTRY_VALUE_INDEX = 1;
1424514f5e3Sopenharmony_ci    static constexpr int ENTRY_SIZE = 2;
1434514f5e3Sopenharmony_ci    static constexpr int DEAULT_DICTIONART_CAPACITY = 4;
1444514f5e3Sopenharmony_ci};
1454514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
1464514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_REQUIRE_JS_CJS_MODULE_CACHE_H