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