1 // Copyright 2016 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #if !V8_ENABLE_WEBASSEMBLY 6 #error This header should only be included if WebAssembly is enabled. 7 #endif // !V8_ENABLE_WEBASSEMBLY 8 9 #ifndef V8_WASM_SIGNATURE_MAP_H_ 10 #define V8_WASM_SIGNATURE_MAP_H_ 11 12 #include <unordered_map> 13 14 #include "src/codegen/signature.h" 15 #include "src/wasm/value-type.h" 16 17 namespace v8 { 18 namespace internal { 19 20 namespace wasm { 21 22 // A signature map canonicalizes signatures into a range of indices so that 23 // two different {FunctionSig} instances with the same contents map to the 24 // same index. 25 class V8_EXPORT_PRIVATE SignatureMap { 26 public: 27 // Allow default construction and move construction (because we have vectors 28 // of objects containing SignatureMaps), but disallow copy or assign. It's 29 // too easy to get security bugs by accidentally updating a copy of the map. 30 MOVE_ONLY_WITH_DEFAULT_CONSTRUCTORS(SignatureMap); 31 32 // Gets the index for a signature, assigning a new index if necessary. 33 uint32_t FindOrInsert(const FunctionSig& sig); 34 35 // Gets the index for a signature, returning {-1} if not found. 36 int32_t Find(const FunctionSig& sig) const; 37 38 // Disallows further insertions to this signature map. Freeze()39 void Freeze() { frozen_ = true; } 40 size()41 size_t size() const { return map_.size(); } 42 is_frozen()43 bool is_frozen() const { return frozen_; } 44 45 private: 46 bool frozen_ = false; 47 std::unordered_map<FunctionSig, uint32_t, base::hash<FunctionSig>> map_; 48 }; 49 50 } // namespace wasm 51 } // namespace internal 52 } // namespace v8 53 54 #endif // V8_WASM_SIGNATURE_MAP_H_ 55