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/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
CAST_ACCESSOR(CompilationCacheTable)21 CAST_ACCESSOR(CompilationCacheTable)
22
23 uint32_t CompilationCacheShape::RegExpHash(String* string, Smi* flags) {
24 return string->Hash() + flags->value();
25 }
26
StringSharedHash(String * source,SharedFunctionInfo * shared,LanguageMode language_mode,int position)27 uint32_t CompilationCacheShape::StringSharedHash(String* source,
28 SharedFunctionInfo* shared,
29 LanguageMode language_mode,
30 int position) {
31 uint32_t hash = source->Hash();
32 if (shared->HasSourceCode()) {
33 // Instead of using the SharedFunctionInfo pointer in the hash
34 // code computation, we use a combination of the hash of the
35 // script source code and the start position of the calling scope.
36 // We do this to ensure that the cache entries can survive garbage
37 // collection.
38 Script* script(Script::cast(shared->script()));
39 hash ^= String::cast(script->source())->Hash();
40 STATIC_ASSERT(LanguageModeSize == 2);
41 if (is_strict(language_mode)) hash ^= 0x8000;
42 hash += position;
43 }
44 return hash;
45 }
46
HashForObject(Isolate * isolate,Object * object)47 uint32_t CompilationCacheShape::HashForObject(Isolate* isolate,
48 Object* object) {
49 if (object->IsNumber()) return static_cast<uint32_t>(object->Number());
50
51 FixedArray* val = FixedArray::cast(object);
52 if (val->map() == val->GetReadOnlyRoots().fixed_cow_array_map()) {
53 DCHECK_EQ(4, val->length());
54 SharedFunctionInfo* shared = SharedFunctionInfo::cast(val->get(0));
55 String* source = String::cast(val->get(1));
56 int language_unchecked = Smi::ToInt(val->get(2));
57 DCHECK(is_valid_language_mode(language_unchecked));
58 LanguageMode language_mode = static_cast<LanguageMode>(language_unchecked);
59 int position = Smi::ToInt(val->get(3));
60 return StringSharedHash(source, shared, language_mode, position);
61 }
62 DCHECK_LT(2, val->length());
63 return RegExpHash(String::cast(val->get(JSRegExp::kSourceIndex)),
64 Smi::cast(val->get(JSRegExp::kFlagsIndex)));
65 }
66
67 } // namespace internal
68 } // namespace v8
69
70 #include "src/objects/object-macros-undef.h"
71
72 #endif // V8_OBJECTS_COMPILATION_CACHE_INL_H_
73