OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 15 matching lines...) Expand all Loading... |
26 #ifndef V8ValueCache_h | 26 #ifndef V8ValueCache_h |
27 #define V8ValueCache_h | 27 #define V8ValueCache_h |
28 | 28 |
29 #include "bindings/core/v8/V8GlobalValueMap.h" | 29 #include "bindings/core/v8/V8GlobalValueMap.h" |
30 #include "core/CoreExport.h" | 30 #include "core/CoreExport.h" |
31 #include "wtf/Allocator.h" | 31 #include "wtf/Allocator.h" |
32 #include "wtf/HashMap.h" | 32 #include "wtf/HashMap.h" |
33 #include "wtf/Noncopyable.h" | 33 #include "wtf/Noncopyable.h" |
34 #include "wtf/RefPtr.h" | 34 #include "wtf/RefPtr.h" |
35 #include "wtf/text/AtomicString.h" | 35 #include "wtf/text/AtomicString.h" |
| 36 #include "wtf/text/CompressibleString.h" |
36 #include "wtf/text/WTFString.h" | 37 #include "wtf/text/WTFString.h" |
37 #include <v8.h> | 38 #include <v8.h> |
38 | 39 |
39 namespace blink { | 40 namespace blink { |
40 | 41 |
41 class StringCacheMapTraits : public V8GlobalValueMapTraits<StringImpl*, v8::Stri
ng, v8::kWeakWithParameter> { | 42 class StringCacheMapTraits : public V8GlobalValueMapTraits<StringImpl*, v8::Stri
ng, v8::kWeakWithParameter> { |
42 STATIC_ONLY(StringCacheMapTraits); | 43 STATIC_ONLY(StringCacheMapTraits); |
43 public: | 44 public: |
44 // Weak traits: | 45 // Weak traits: |
45 typedef StringImpl WeakCallbackDataType; | 46 typedef StringImpl WeakCallbackDataType; |
(...skipping 11 matching lines...) Expand all Loading... |
57 { | 58 { |
58 return data.GetParameter(); | 59 return data.GetParameter(); |
59 } | 60 } |
60 | 61 |
61 static void OnWeakCallback(const v8::WeakCallbackInfo<WeakCallbackDataType>&
); | 62 static void OnWeakCallback(const v8::WeakCallbackInfo<WeakCallbackDataType>&
); |
62 | 63 |
63 static void Dispose(v8::Isolate*, v8::Global<v8::String> value, StringImpl*
key); | 64 static void Dispose(v8::Isolate*, v8::Global<v8::String> value, StringImpl*
key); |
64 static void DisposeWeak(const v8::WeakCallbackInfo<WeakCallbackDataType>&); | 65 static void DisposeWeak(const v8::WeakCallbackInfo<WeakCallbackDataType>&); |
65 }; | 66 }; |
66 | 67 |
| 68 class CompressibleStringCacheMapTraits : public V8GlobalValueMapTraits<Compressi
bleStringImpl*, v8::String, v8::kWeakWithParameter> { |
| 69 STATIC_ONLY(CompressibleStringCacheMapTraits); |
| 70 public: |
| 71 // Weak traits: |
| 72 typedef CompressibleStringImpl WeakCallbackDataType; |
| 73 typedef v8::GlobalValueMap<CompressibleStringImpl*, v8::String, Compressible
StringCacheMapTraits> MapType; |
| 74 |
| 75 static WeakCallbackDataType* WeakCallbackParameter( |
| 76 MapType* map, CompressibleStringImpl* key, v8::Local<v8::String>& value)
{ return key; } |
| 77 static void DisposeCallbackData(WeakCallbackDataType* callbackData) { } |
| 78 |
| 79 static MapType* MapFromWeakCallbackInfo( |
| 80 const v8::WeakCallbackInfo<WeakCallbackDataType>&); |
| 81 |
| 82 static CompressibleStringImpl* KeyFromWeakCallbackInfo( |
| 83 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) |
| 84 { |
| 85 return data.GetParameter(); |
| 86 } |
| 87 |
| 88 static void OnWeakCallback(const v8::WeakCallbackInfo<WeakCallbackDataType>&
); |
| 89 |
| 90 static void Dispose(v8::Isolate*, v8::Global<v8::String> value, Compressible
StringImpl* key); |
| 91 static void DisposeWeak(const v8::WeakCallbackInfo<WeakCallbackDataType>&); |
| 92 }; |
67 | 93 |
68 class CORE_EXPORT StringCache { | 94 class CORE_EXPORT StringCache { |
69 USING_FAST_MALLOC(StringCache); | 95 USING_FAST_MALLOC(StringCache); |
70 WTF_MAKE_NONCOPYABLE(StringCache); | 96 WTF_MAKE_NONCOPYABLE(StringCache); |
71 public: | 97 public: |
72 explicit StringCache(v8::Isolate* isolate) : m_stringCache(isolate) { } | 98 explicit StringCache(v8::Isolate* isolate) |
| 99 : m_stringCache(isolate) |
| 100 , m_compressibleStringCache(isolate) |
| 101 { |
| 102 } |
73 | 103 |
74 v8::Local<v8::String> v8ExternalString(v8::Isolate* isolate, StringImpl* str
ingImpl) | 104 v8::Local<v8::String> v8ExternalString(v8::Isolate* isolate, StringImpl* str
ingImpl) |
75 { | 105 { |
| 106 // Note that the last CompressibleString is not cached. |
76 ASSERT(stringImpl); | 107 ASSERT(stringImpl); |
77 if (m_lastStringImpl.get() == stringImpl) | 108 if (m_lastStringImpl.get() == stringImpl) |
78 return m_lastV8String.NewLocal(isolate); | 109 return m_lastV8String.NewLocal(isolate); |
79 return v8ExternalStringSlow(isolate, stringImpl); | 110 return v8ExternalStringSlow(isolate, stringImpl); |
80 } | 111 } |
81 | 112 |
| 113 v8::Local<v8::String> v8ExternalString(v8::Isolate* isolate, const Compressi
bleString& string) |
| 114 { |
| 115 ASSERT(!string.isNull()); |
| 116 return v8ExternalStringSlow(isolate, string); |
| 117 } |
| 118 |
82 void setReturnValueFromString(v8::ReturnValue<v8::Value> returnValue, String
Impl* stringImpl) | 119 void setReturnValueFromString(v8::ReturnValue<v8::Value> returnValue, String
Impl* stringImpl) |
83 { | 120 { |
84 ASSERT(stringImpl); | 121 ASSERT(stringImpl); |
85 if (m_lastStringImpl.get() == stringImpl) | 122 if (m_lastStringImpl.get() == stringImpl) |
86 m_lastV8String.SetReturnValue(returnValue); | 123 m_lastV8String.SetReturnValue(returnValue); |
87 else | 124 else |
88 setReturnValueFromStringSlow(returnValue, stringImpl); | 125 setReturnValueFromStringSlow(returnValue, stringImpl); |
89 } | 126 } |
90 | 127 |
91 void dispose(); | 128 void dispose(); |
92 | 129 |
93 friend class StringCacheMapTraits; | 130 friend class StringCacheMapTraits; |
| 131 friend class CompressibleStringCacheMapTraits; |
94 | 132 |
95 private: | 133 private: |
96 v8::Local<v8::String> v8ExternalStringSlow(v8::Isolate*, StringImpl*); | 134 v8::Local<v8::String> v8ExternalStringSlow(v8::Isolate*, StringImpl*); |
| 135 v8::Local<v8::String> v8ExternalStringSlow(v8::Isolate*, const CompressibleS
tring&); |
97 void setReturnValueFromStringSlow(v8::ReturnValue<v8::Value>, StringImpl*); | 136 void setReturnValueFromStringSlow(v8::ReturnValue<v8::Value>, StringImpl*); |
98 v8::Local<v8::String> createStringAndInsertIntoCache(v8::Isolate*, StringImp
l*); | 137 v8::Local<v8::String> createStringAndInsertIntoCache(v8::Isolate*, StringImp
l*); |
| 138 v8::Local<v8::String> createStringAndInsertIntoCache(v8::Isolate*, const Com
pressibleString&); |
99 void InvalidateLastString(); | 139 void InvalidateLastString(); |
100 | 140 |
101 StringCacheMapTraits::MapType m_stringCache; | 141 StringCacheMapTraits::MapType m_stringCache; |
102 StringCacheMapTraits::MapType::PersistentValueReference m_lastV8String; | 142 StringCacheMapTraits::MapType::PersistentValueReference m_lastV8String; |
103 | 143 |
| 144 CompressibleStringCacheMapTraits::MapType m_compressibleStringCache; |
| 145 |
104 // Note: RefPtr is a must as we cache by StringImpl* equality, not identity | 146 // Note: RefPtr is a must as we cache by StringImpl* equality, not identity |
105 // hence lastStringImpl might be not a key of the cache (in sense of identit
y) | 147 // hence lastStringImpl might be not a key of the cache (in sense of identit
y) |
106 // and hence it's not refed on addition. | 148 // and hence it's not refed on addition. |
107 RefPtr<StringImpl> m_lastStringImpl; | 149 RefPtr<StringImpl> m_lastStringImpl; |
108 }; | 150 }; |
109 | 151 |
110 } // namespace blink | 152 } // namespace blink |
111 | 153 |
112 #endif // V8ValueCache_h | 154 #endif // V8ValueCache_h |
OLD | NEW |