• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef V8_WASM_SIGNATURE_MAP_H_
6 #define V8_WASM_SIGNATURE_MAP_H_
7 
8 #include <unordered_map>
9 
10 #include "src/signature.h"
11 #include "src/wasm/value-type.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 namespace wasm {
17 
18 using FunctionSig = Signature<ValueType>;
19 
20 // A signature map canonicalizes signatures into a range of indices so that
21 // two different {FunctionSig} instances with the same contents map to the
22 // same index.
23 class V8_EXPORT_PRIVATE SignatureMap {
24  public:
25   // Allow default construction and move construction (because we have vectors
26   // of objects containing SignatureMaps), but disallow copy or assign. It's
27   // too easy to get security bugs by accidentally updating a copy of the map.
28   MOVE_ONLY_WITH_DEFAULT_CONSTRUCTORS(SignatureMap);
29 
30   // Gets the index for a signature, assigning a new index if necessary.
31   uint32_t FindOrInsert(const FunctionSig& sig);
32 
33   // Gets the index for a signature, returning {-1} if not found.
34   int32_t Find(const FunctionSig& sig) const;
35 
36   // Disallows further insertions to this signature map.
Freeze()37   void Freeze() { frozen_ = true; }
38 
39  private:
40   uint32_t next_ = 0;
41   bool frozen_ = false;
42   std::unordered_map<FunctionSig, uint32_t, base::hash<FunctionSig>> map_;
43 };
44 
45 }  // namespace wasm
46 }  // namespace internal
47 }  // namespace v8
48 
49 #endif  // V8_WASM_SIGNATURE_MAP_H_
50