OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef ScriptModule_h | 5 #ifndef ScriptModule_h |
6 #define ScriptModule_h | 6 #define ScriptModule_h |
7 | 7 |
| 8 #include <v8.h> |
8 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
9 #include "bindings/core/v8/SharedPersistent.h" | 10 #include "bindings/core/v8/SharedPersistent.h" |
10 #include "core/CoreExport.h" | 11 #include "core/CoreExport.h" |
11 #include "v8/include/v8.h" | 12 #include "v8/include/v8.h" |
12 #include "wtf/Allocator.h" | 13 #include "wtf/Allocator.h" |
| 14 #include "wtf/HashTableDeletedValueType.h" |
| 15 #include "wtf/HashTraits.h" |
| 16 #include "wtf/Vector.h" |
13 #include "wtf/text/WTFString.h" | 17 #include "wtf/text/WTFString.h" |
14 | 18 |
15 namespace blink { | 19 namespace blink { |
16 | 20 |
| 21 class ScriptState; |
| 22 |
| 23 // ScriptModule wraps a handle to a v8::Module for use in core. |
| 24 // |
| 25 // Using ScriptModules needs a ScriptState and its scope to operate in. You |
| 26 // should always provide the same ScriptState and not try to reuse ScriptModules |
| 27 // across different contexts. |
| 28 // Currently all ScriptModule users can easily access its context Modulator, so |
| 29 // we use it to fill ScriptState in. |
17 class CORE_EXPORT ScriptModule final { | 30 class CORE_EXPORT ScriptModule final { |
18 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); | 31 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); |
19 | 32 |
20 public: | 33 public: |
| 34 class Hash; |
| 35 |
21 static ScriptModule compile(v8::Isolate*, | 36 static ScriptModule compile(v8::Isolate*, |
22 const String& source, | 37 const String& source, |
23 const String& fileName); | 38 const String& fileName); |
24 | 39 |
| 40 // TODO(kouhei): Remove copy ctor |
25 ScriptModule() {} | 41 ScriptModule() {} |
26 ScriptModule(const ScriptModule& module) : m_module(module.m_module) {} | 42 ScriptModule(WTF::HashTableDeletedValueType) |
| 43 : m_module(WTF::HashTableDeletedValue) {} |
| 44 |
| 45 ScriptModule(const ScriptModule&) = default; |
| 46 ScriptModule(ScriptModule&&) = default; |
| 47 ScriptModule& operator=(ScriptModule&&) = default; |
| 48 ScriptModule& operator=(const ScriptModule&) = default; |
27 ~ScriptModule(); | 49 ~ScriptModule(); |
28 | 50 |
29 bool instantiate(ScriptState*); | 51 ScriptValue instantiate(ScriptState*); |
30 void evaluate(ScriptState*); | 52 void evaluate(ScriptState*); |
31 | 53 |
| 54 Vector<String> moduleRequests(ScriptState*); |
| 55 |
| 56 bool isHashTableDeletedValue() const { |
| 57 return m_module.isHashTableDeletedValue(); |
| 58 } |
| 59 |
| 60 bool operator==(const blink::ScriptModule& rhs) const { |
| 61 if (m_module.isHashTableDeletedValue() && |
| 62 rhs.m_module.isHashTableDeletedValue()) |
| 63 return true; |
| 64 |
| 65 blink::SharedPersistent<v8::Module>* lhsp = m_module.get(); |
| 66 blink::SharedPersistent<v8::Module>* rhsp = rhs.m_module.get(); |
| 67 if (lhsp == rhsp) |
| 68 return true; |
| 69 if (!lhsp || !rhsp) |
| 70 return false; |
| 71 return *lhsp == *rhsp; |
| 72 } |
| 73 |
32 bool isNull() const { return m_module->isEmpty(); } | 74 bool isNull() const { return m_module->isEmpty(); } |
33 | 75 |
34 private: | 76 private: |
35 ScriptModule(v8::Isolate*, v8::Local<v8::Module>); | 77 ScriptModule(v8::Isolate*, v8::Local<v8::Module>); |
36 | 78 |
| 79 static v8::MaybeLocal<v8::Module> resolveModuleCallback( |
| 80 v8::Local<v8::Context>, |
| 81 v8::Local<v8::String> specifier, |
| 82 v8::Local<v8::Module> referrer); |
| 83 |
| 84 // TODO(kouhei): Make ScriptModule move-only and make this ScopedPersistent. |
37 RefPtr<SharedPersistent<v8::Module>> m_module; | 85 RefPtr<SharedPersistent<v8::Module>> m_module; |
| 86 unsigned m_identityHash = 0; |
| 87 |
| 88 friend struct ScriptModuleHash; |
| 89 }; |
| 90 |
| 91 struct ScriptModuleHash { |
| 92 STATIC_ONLY(ScriptModuleHash); |
| 93 |
| 94 public: |
| 95 static unsigned hash(const blink::ScriptModule& key) { |
| 96 return static_cast<unsigned>(key.m_identityHash); |
| 97 } |
| 98 |
| 99 static bool equal(const blink::ScriptModule& a, |
| 100 const blink::ScriptModule& b) { |
| 101 return a == b; |
| 102 } |
| 103 |
| 104 static constexpr bool safeToCompareToEmptyOrDeleted = true; |
38 }; | 105 }; |
39 | 106 |
40 } // namespace blink | 107 } // namespace blink |
41 | 108 |
| 109 namespace WTF { |
| 110 |
| 111 template <> |
| 112 struct DefaultHash<blink::ScriptModule> { |
| 113 using Hash = blink::ScriptModuleHash; |
| 114 }; |
| 115 |
| 116 template <> |
| 117 struct HashTraits<blink::ScriptModule> |
| 118 : public SimpleClassHashTraits<blink::ScriptModule> {}; |
| 119 |
| 120 } // namespace WTF |
| 121 |
42 #endif // ScriptModule_h | 122 #endif // ScriptModule_h |
OLD | NEW |