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 "wtf/Allocator.h" | 12 #include "wtf/Allocator.h" |
| 13 #include "wtf/HashTableDeletedValueType.h" |
| 14 #include "wtf/HashTraits.h" |
| 15 #include "wtf/Vector.h" |
12 #include "wtf/text/WTFString.h" | 16 #include "wtf/text/WTFString.h" |
13 #include <v8.h> | |
14 | 17 |
15 namespace blink { | 18 namespace blink { |
16 | 19 |
| 20 class ScriptModuleIdentifier; |
| 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: |
21 static ScriptModule compile(v8::Isolate*, | 34 static ScriptModule compile(v8::Isolate*, |
22 const String& source, | 35 const String& source, |
23 const String& fileName); | 36 const String& fileName); |
24 | 37 |
| 38 // TODO(kouhei): Remove copy ctor |
25 ScriptModule() {} | 39 ScriptModule() {} |
26 ScriptModule(const ScriptModule& module) : m_module(module.m_module) {} | 40 ScriptModule(const ScriptModule&) = default; |
| 41 ScriptModule(ScriptModule&&) = default; |
| 42 ScriptModule& operator=(ScriptModule&&) = default; |
27 ~ScriptModule(); | 43 ~ScriptModule(); |
28 | 44 |
29 bool instantiate(ScriptState*); | 45 ScriptValue instantiate(ScriptState*); |
30 void evaluate(ScriptState*); | 46 void evaluate(ScriptState*); |
31 | 47 |
| 48 Vector<String> moduleRequests(ScriptState*); |
| 49 |
| 50 bool isNull() const { return !m_module || m_module->isEmpty(); } |
| 51 ScriptModuleIdentifier identifier(); |
| 52 |
| 53 private: |
| 54 friend class ScriptModuleIdentifier; |
| 55 ScriptModule(v8::Isolate*, v8::Local<v8::Module>); |
| 56 |
| 57 static v8::MaybeLocal<v8::Module> resolveModuleCallback( |
| 58 v8::Local<v8::Context>, |
| 59 v8::Local<v8::String> specifier, |
| 60 v8::Local<v8::Module> referrer); |
| 61 |
| 62 RefPtr<SharedPersistent<v8::Module>> m_module; |
| 63 unsigned m_identityHash = 0; |
| 64 }; |
| 65 |
| 66 // TODO(kouhei): Split header/cpp |
| 67 // TODO(kouhei): PeekType which can be construct w/ only v8::Local |
| 68 class ScriptModuleIdentifier { |
| 69 public: |
| 70 class Hash; |
| 71 |
| 72 ScriptModuleIdentifier() = default; |
| 73 ScriptModuleIdentifier(const ScriptModuleIdentifier& id) |
| 74 : m_module(id.m_module), m_identityHash(id.m_identityHash) {} |
| 75 ScriptModuleIdentifier(WTF::HashTableDeletedValueType) |
| 76 : m_module(WTF::HashTableDeletedValue) {} |
| 77 /* |
| 78 FIXME(kouhei): need move op= |
| 79 ScriptModuleIdentifier(ScriptModuleIdentifier&& id) |
| 80 : m_module(std::move(id.m_module)), m_identityHash(id.m_identityHash) {} |
| 81 */ |
| 82 ~ScriptModuleIdentifier() = default; |
| 83 |
| 84 bool isHashTableDeletedValue() const { |
| 85 return m_module.isHashTableDeletedValue(); |
| 86 } |
| 87 |
| 88 bool operator==(const blink::ScriptModuleIdentifier& rhs) const { |
| 89 if (m_module.isHashTableDeletedValue() && |
| 90 rhs.m_module.isHashTableDeletedValue()) |
| 91 return true; |
| 92 |
| 93 blink::SharedPersistent<v8::Module>* lhsp = m_module.get(); |
| 94 blink::SharedPersistent<v8::Module>* rhsp = rhs.m_module.get(); |
| 95 if (lhsp == rhsp) |
| 96 return true; |
| 97 if (!lhsp || !rhsp) |
| 98 return false; |
| 99 return *lhsp == *rhsp; |
| 100 } |
| 101 |
32 bool isNull() const { return m_module->isEmpty(); } | 102 bool isNull() const { return m_module->isEmpty(); } |
33 | 103 |
34 private: | 104 private: |
35 ScriptModule(v8::Isolate*, v8::Local<v8::Module>); | 105 friend class ScriptModule; |
| 106 friend struct ScriptModuleIdentifierHash; |
36 | 107 |
| 108 explicit ScriptModuleIdentifier( |
| 109 const RefPtr<SharedPersistent<v8::Module>>& module, |
| 110 unsigned identityHash) |
| 111 : m_module(module), m_identityHash(identityHash) {} |
| 112 |
| 113 // TODO(kouhei): Make ScriptModule move-only and make this ScopedPersistent. |
37 RefPtr<SharedPersistent<v8::Module>> m_module; | 114 RefPtr<SharedPersistent<v8::Module>> m_module; |
| 115 unsigned m_identityHash = 0; |
| 116 }; |
| 117 |
| 118 struct ScriptModuleIdentifierHash { |
| 119 STATIC_ONLY(ScriptModuleIdentifierHash); |
| 120 |
| 121 public: |
| 122 static unsigned hash(const blink::ScriptModuleIdentifier& key) { |
| 123 return static_cast<unsigned>(key.m_identityHash); |
| 124 } |
| 125 |
| 126 static bool equal(const blink::ScriptModuleIdentifier& a, |
| 127 const blink::ScriptModuleIdentifier& b) { |
| 128 return a == b; |
| 129 } |
| 130 |
| 131 static constexpr bool safeToCompareToEmptyOrDeleted = true; |
38 }; | 132 }; |
39 | 133 |
40 } // namespace blink | 134 } // namespace blink |
41 | 135 |
| 136 namespace WTF { |
| 137 |
| 138 template <> |
| 139 struct DefaultHash<blink::ScriptModuleIdentifier> { |
| 140 using Hash = blink::ScriptModuleIdentifierHash; |
| 141 }; |
| 142 |
| 143 template <> |
| 144 struct HashTraits<blink::ScriptModuleIdentifier> |
| 145 : public SimpleClassHashTraits<blink::ScriptModuleIdentifier> {}; |
| 146 |
| 147 } // namespace WTF |
| 148 |
42 #endif // ScriptModule_h | 149 #endif // ScriptModule_h |
OLD | NEW |