• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 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_OBJECTS_COMPILATION_CACHE_TABLE_INL_H_
6 #define V8_OBJECTS_COMPILATION_CACHE_TABLE_INL_H_
7 
8 #include "src/objects/compilation-cache-table.h"
9 #include "src/objects/name-inl.h"
10 #include "src/objects/script-inl.h"
11 #include "src/objects/shared-function-info.h"
12 #include "src/objects/smi.h"
13 #include "src/objects/string.h"
14 
15 // Has to be the last include (doesn't have include guards):
16 #include "src/objects/object-macros.h"
17 
18 namespace v8 {
19 namespace internal {
20 
CompilationCacheTable(Address ptr)21 CompilationCacheTable::CompilationCacheTable(Address ptr)
22     : HashTable<CompilationCacheTable, CompilationCacheShape>(ptr) {
23   SLOW_DCHECK(IsCompilationCacheTable());
24 }
25 
26 NEVER_READ_ONLY_SPACE_IMPL(CompilationCacheTable)
CAST_ACCESSOR(CompilationCacheTable)27 CAST_ACCESSOR(CompilationCacheTable)
28 
29 uint32_t CompilationCacheShape::RegExpHash(String string, Smi flags) {
30   return string.Hash() + flags.value();
31 }
32 
StringSharedHash(String source,SharedFunctionInfo shared,LanguageMode language_mode,int position)33 uint32_t CompilationCacheShape::StringSharedHash(String source,
34                                                  SharedFunctionInfo shared,
35                                                  LanguageMode language_mode,
36                                                  int position) {
37   uint32_t hash = source.Hash();
38   if (shared.HasSourceCode()) {
39     // Instead of using the SharedFunctionInfo pointer in the hash
40     // code computation, we use a combination of the hash of the
41     // script source code and the start position of the calling scope.
42     // We do this to ensure that the cache entries can survive garbage
43     // collection.
44     Script script(Script::cast(shared.script()));
45     hash ^= String::cast(script.source()).Hash();
46     STATIC_ASSERT(LanguageModeSize == 2);
47     if (is_strict(language_mode)) hash ^= 0x8000;
48     hash += position;
49   }
50   return hash;
51 }
52 
HashForObject(ReadOnlyRoots roots,Object object)53 uint32_t CompilationCacheShape::HashForObject(ReadOnlyRoots roots,
54                                               Object object) {
55   // Eval: The key field contains the hash as a Number.
56   if (object.IsNumber()) return static_cast<uint32_t>(object.Number());
57 
58   // Code: The key field contains the SFI key.
59   if (object.IsSharedFunctionInfo()) {
60     return SharedFunctionInfo::cast(object).Hash();
61   }
62 
63   // Script: See StringSharedKey::ToHandle for the encoding.
64   FixedArray val = FixedArray::cast(object);
65   if (val.map() == roots.fixed_cow_array_map()) {
66     DCHECK_EQ(4, val.length());
67     SharedFunctionInfo shared = SharedFunctionInfo::cast(val.get(0));
68     String source = String::cast(val.get(1));
69     int language_unchecked = Smi::ToInt(val.get(2));
70     DCHECK(is_valid_language_mode(language_unchecked));
71     LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked);
72     int position = Smi::ToInt(val.get(3));
73     return StringSharedHash(source, shared, language_mode, position);
74   }
75 
76   // RegExp: The key field (and the value field) contains the
77   // JSRegExp::data fixed array.
78   DCHECK_GE(val.length(), JSRegExp::kMinDataArrayLength);
79   return RegExpHash(String::cast(val.get(JSRegExp::kSourceIndex)),
80                     Smi::cast(val.get(JSRegExp::kFlagsIndex)));
81 }
82 
InfoCellPair(Isolate * isolate,SharedFunctionInfo shared,FeedbackCell feedback_cell)83 InfoCellPair::InfoCellPair(Isolate* isolate, SharedFunctionInfo shared,
84                            FeedbackCell feedback_cell)
85     : is_compiled_scope_(!shared.is_null() ? shared.is_compiled_scope(isolate)
86                                            : IsCompiledScope()),
87       shared_(shared),
88       feedback_cell_(feedback_cell) {}
89 
90 }  // namespace internal
91 }  // namespace v8
92 
93 #include "src/objects/object-macros-undef.h"
94 
95 #endif  // V8_OBJECTS_COMPILATION_CACHE_TABLE_INL_H_
96