• 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 #ifndef ECMASCRIPT_IC_PROPERTIES_CACHE_INL_H
17 #define ECMASCRIPT_IC_PROPERTIES_CACHE_INL_H
18 
19 #include "ecmascript/ic/properties_cache.h"
20 #include "ecmascript/js_tagged_value-inl.h"
21 
22 namespace panda::ecmascript {
Get(JSHClass * jsHclass,JSTaggedValue key)23 int PropertiesCache::Get(JSHClass *jsHclass, JSTaggedValue key)
24 {
25     int hash = Hash(jsHclass, key);
26     PropertyKey &prop = keys_[hash];
27     if ((prop.hclass_ == jsHclass) && (prop.key_ == key)) {
28         return keys_[hash].results_;
29     }
30     return NOT_FOUND;
31 }
32 
Set(JSHClass * jsHclass,JSTaggedValue key,int index)33 void PropertiesCache::Set(JSHClass *jsHclass, JSTaggedValue key, int index)
34 {
35     int hash = Hash(jsHclass, key);
36     PropertyKey &prop = keys_[hash];
37     prop.hclass_ = jsHclass;
38     prop.key_ = key;
39     keys_[hash].results_ = index;
40 }
41 
Clear()42 void PropertiesCache::Clear()
43 {
44     for (auto &key : keys_) {
45         key.hclass_ = nullptr;
46     }
47 }
48 
Hash(JSHClass * cls,JSTaggedValue key)49 int PropertiesCache::Hash(JSHClass *cls, JSTaggedValue key)
50 {
51     uint32_t clsHash = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(cls)) >> 3U;  // skip 8bytes
52     uint32_t keyHash = key.GetKeyHashCode();
53     return static_cast<int>((clsHash ^ keyHash) & CACHE_LENGTH_MASK);
54 }
55 }  // namespace panda::ecmascript
56 #endif  // ECMASCRIPT_IC_PROPERTIES_CACHE_INL_H
57