V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
compilation-cache-inl.h
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_INL_H_
6 #define V8_OBJECTS_COMPILATION_CACHE_INL_H_
7 
8 #include "src/objects/compilation-cache.h"
9 
10 #include "src/objects/name-inl.h"
11 #include "src/objects/script-inl.h"
12 #include "src/objects/shared-function-info.h"
13 #include "src/objects/smi.h"
14 #include "src/objects/string.h"
15 
16 // Has to be the last include (doesn't have include guards):
17 #include "src/objects/object-macros.h"
18 
19 namespace v8 {
20 namespace internal {
21 
22 CompilationCacheTable::CompilationCacheTable(Address ptr)
23  : HashTable<CompilationCacheTable, CompilationCacheShape>(ptr) {
24  SLOW_DCHECK(IsCompilationCacheTable());
25 }
26 
27 NEVER_READ_ONLY_SPACE_IMPL(CompilationCacheTable)
28 CAST_ACCESSOR2(CompilationCacheTable)
29 
30 uint32_t CompilationCacheShape::RegExpHash(String string, Smi flags) {
31  return string->Hash() + flags->value();
32 }
33 
34 uint32_t CompilationCacheShape::StringSharedHash(String source,
35  SharedFunctionInfo* shared,
36  LanguageMode language_mode,
37  int position) {
38  uint32_t hash = source->Hash();
39  if (shared->HasSourceCode()) {
40  // Instead of using the SharedFunctionInfo pointer in the hash
41  // code computation, we use a combination of the hash of the
42  // script source code and the start position of the calling scope.
43  // We do this to ensure that the cache entries can survive garbage
44  // collection.
45  Script* script(Script::cast(shared->script()));
46  hash ^= String::cast(script->source())->Hash();
47  STATIC_ASSERT(LanguageModeSize == 2);
48  if (is_strict(language_mode)) hash ^= 0x8000;
49  hash += position;
50  }
51  return hash;
52 }
53 
54 uint32_t CompilationCacheShape::HashForObject(Isolate* isolate,
55  Object* object) {
56  if (object->IsNumber()) return static_cast<uint32_t>(object->Number());
57 
58  FixedArray val = FixedArray::cast(object);
59  if (val->map() == val->GetReadOnlyRoots().fixed_cow_array_map()) {
60  DCHECK_EQ(4, val->length());
61  SharedFunctionInfo* shared = SharedFunctionInfo::cast(val->get(0));
62  String source = String::cast(val->get(1));
63  int language_unchecked = Smi::ToInt(val->get(2));
64  DCHECK(is_valid_language_mode(language_unchecked));
65  LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked);
66  int position = Smi::ToInt(val->get(3));
67  return StringSharedHash(source, shared, language_mode, position);
68  }
69  DCHECK_LT(2, val->length());
70  return RegExpHash(String::cast(val->get(JSRegExp::kSourceIndex)),
71  Smi::cast(val->get(JSRegExp::kFlagsIndex)));
72 }
73 
74 } // namespace internal
75 } // namespace v8
76 
77 #include "src/objects/object-macros-undef.h"
78 
79 #endif // V8_OBJECTS_COMPILATION_CACHE_INL_H_
Definition: libplatform.h:13