#ifndef V8_WASM_WASM_EXPORT_WRAPPER_CACHE_H_
#define V8_WASM_WASM_EXPORT_WRAPPER_CACHE_H_
#if !V8_ENABLE_WEBASSEMBLY
#error This header should only be included if WebAssembly is enabled.
#endif
#include "src/objects/code.h"
#include "src/objects/fixed-array.h"
#include "src/objects/internal-index.h"
#include "src/wasm/canonical-types.h"
#include "src/wasm/value-type.h"
namespace v8::internal::wasm {
class WasmExportWrapperCache : public AllStatic {
public:
static void Put(Isolate* isolate, CanonicalTypeIndex sig_index,
bool receiver_is_first_param, DirectHandle<Code> code);
static Tagged<CodeWrapper> Get(Isolate* isolate, CanonicalTypeIndex sig_index,
bool receiver_is_first_param);
#ifdef VERIFY_HEAP
static void Verify(Heap* heap);
#endif
static int CountWrappersForTesting(Isolate* isolate);
private:
static constexpr int kUnused = -1;
static constexpr int kNumUsedElementsIndex = 0;
static constexpr int kReservedSlots = 1;
static constexpr int kSlotsPerEntry = 2;
static DirectHandle<WeakFixedArray> New(Isolate* isolate, int capacity);
static uint32_t Capacity(Tagged<WeakFixedArray> cache);
static uint32_t Hash(CanonicalTypeIndex sig_index,
bool receiver_is_first_param) {
static_assert(kMaxCanonicalTypes < (1u << 20));
return ((receiver_is_first_param ? 1 : 0) << 20) | sig_index.index;
}
static int ToArraySlot(InternalIndex entry) {
return kReservedSlots + entry.as_int() * kSlotsPerEntry;
}
static InternalIndex FirstProbe(uint32_t hash, uint32_t capacity) {
#if V8_HASHES_COLLIDE
if (v8_flags.hashes_collide) hash = base::kCollidingHash;
#endif
return InternalIndex(hash & (capacity - 1));
}
static InternalIndex NextProbe(InternalIndex last, uint32_t number,
uint32_t capacity) {
return InternalIndex((last.as_uint32() + number) & (capacity - 1));
}
V8_WARN_UNUSED_RESULT static Tagged<WeakFixedArray> EnsureCapacity(
Isolate* isolate);
template <bool entry_may_exist>
static void PutInternal(Tagged<WeakFixedArray> cache, uint32_t hash,
Tagged<MaybeWeak<Object>> value);
};
}
#endif