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/require/js_cjs_module_cache.h"
17
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/tagged_array-inl.h"
20 #include "ecmascript/tagged_hash_table.h"
21
22 namespace panda::ecmascript {
PutIfAbsentAndReset(const JSThread * thread,const JSHandle<CjsModuleCache> & dictionary,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)23 JSHandle<CjsModuleCache> CjsModuleCache::PutIfAbsentAndReset(const JSThread *thread,
24 const JSHandle<CjsModuleCache> &dictionary,
25 const JSHandle<JSTaggedValue> &key,
26 const JSHandle<JSTaggedValue> &value)
27 {
28 int entry = dictionary->FindEntry(key.GetTaggedValue());
29 if (entry != -1) {
30 return ResetModule(thread, dictionary, key, value);
31 }
32
33 // Check whether the dictionary should be extended.
34 JSHandle<CjsModuleCache> newDictionary(HashTable::GrowHashTable(thread, dictionary));
35
36 // Compute the key object.
37 int hash = Hash(key.GetTaggedValue());
38 entry = newDictionary->FindInsertIndex(hash);
39 newDictionary->SetEntry(thread, entry, key, value);
40 newDictionary->IncreaseEntries(thread);
41
42 return newDictionary;
43 }
44
ResetModule(const JSThread * thread,const JSHandle<CjsModuleCache> & dictionary,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)45 JSHandle<CjsModuleCache> CjsModuleCache::ResetModule(const JSThread *thread,
46 const JSHandle<CjsModuleCache> &dictionary,
47 const JSHandle<JSTaggedValue> &key,
48 const JSHandle<JSTaggedValue> &value)
49 {
50 int entry = dictionary->FindEntry(key.GetTaggedValue());
51 ASSERT(entry != -1);
52 if (entry == -1) {
53 LOG_FULL(FATAL) << "CjsModuleCache::ResetModule Failed";
54 }
55 dictionary->SetEntry(thread, entry, key, value);
56 return dictionary;
57 }
58 } // namespace panda::ecmascript