OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef ScriptModule_h | |
6 #define ScriptModule_h | |
7 | |
8 #include "bindings/core/v8/ScriptState.h" | |
9 #include "bindings/core/v8/SharedPersistent.h" | |
10 #include "core/CoreExport.h" | |
11 #include "wtf/Allocator.h" | |
12 #include "wtf/HashTableDeletedValueType.h" | |
13 #include "wtf/HashTraits.h" | |
14 #include "wtf/Vector.h" | |
15 #include "wtf/text/WTFString.h" | |
16 #include <v8.h> | |
17 | |
18 namespace blink { | |
19 | |
20 class ScriptModuleIdentifier; | |
21 class ScriptState; | |
22 | |
23 // A ScriptModule wraps a handle to v8::Module for its carry outside V8 | |
24 // bindings. | |
25 // | |
26 // Use ModuleController to actually operate or extract information from | |
27 // ScriptModule. | |
28 class CORE_EXPORT ScriptModule final { | |
29 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); | |
30 | |
31 public: | |
32 static ScriptModule compile(v8::Isolate*, | |
33 const String& source, | |
34 const String& fileName); | |
35 | |
36 ScriptModule() {} | |
37 ScriptModule(const ScriptModule& module) | |
38 : m_module(module.m_module), m_identityHash(module.m_identityHash) {} | |
39 ~ScriptModule(); | |
40 | |
41 bool instantiate(ScriptState*); | |
42 void evaluate(ScriptState*); | |
43 | |
44 Vector<String> moduleRequests(ScriptState*); | |
45 | |
46 bool isNull() const { return !m_module || m_module->isEmpty(); } | |
47 ScriptModuleIdentifier identifier(); | |
48 | |
49 private: | |
50 friend class ScriptModuleIdentifier; | |
51 ScriptModule(v8::Isolate*, v8::Local<v8::Module>); | |
52 | |
53 static v8::MaybeLocal<v8::Module> resolveModuleCallback( | |
54 v8::Local<v8::Context>, | |
55 v8::Local<v8::String> specifier, | |
56 v8::Local<v8::Module> referrer); | |
57 | |
58 RefPtr<SharedPersistent<v8::Module>> m_module; | |
haraken
2017/01/06 05:47:58
Will ScopedPersistent work? (SharedPersistent shou
kouhei (in TOK)
2017/01/11 01:41:57
Probably. Added a TODO comment to fix this.
| |
59 unsigned m_identityHash = 0; | |
60 }; | |
61 | |
62 // TODO(kouhei): Split header/cpp | |
63 // TODO(kouhei): PeekType which can be construct w/ only v8::Local | |
64 class ScriptModuleIdentifier { | |
65 public: | |
66 class Hash; | |
67 | |
68 ScriptModuleIdentifier() = default; | |
69 ScriptModuleIdentifier(const ScriptModuleIdentifier& id) | |
70 : m_module(id.m_module), m_identityHash(id.m_identityHash) {} | |
71 ScriptModuleIdentifier(WTF::HashTableDeletedValueType) | |
72 : m_module(WTF::HashTableDeletedValue) {} | |
73 /* | |
74 FIXME(kouhei): need move op= | |
75 ScriptModuleIdentifier(ScriptModuleIdentifier&& id) | |
76 : m_module(std::move(id.m_module)), m_identityHash(id.m_identityHash) {} | |
77 */ | |
78 ~ScriptModuleIdentifier() = default; | |
79 | |
80 bool isHashTableDeletedValue() const { | |
81 return m_module.isHashTableDeletedValue(); | |
82 } | |
83 | |
84 bool operator==(const blink::ScriptModuleIdentifier& rhs) const { | |
85 if (m_module.isHashTableDeletedValue() && | |
86 rhs.m_module.isHashTableDeletedValue()) | |
87 return true; | |
88 | |
89 blink::SharedPersistent<v8::Module>* lhsp = m_module.get(); | |
90 blink::SharedPersistent<v8::Module>* rhsp = rhs.m_module.get(); | |
91 if (lhsp == rhsp) | |
92 return true; | |
93 if (!lhsp || !rhsp) | |
94 return false; | |
95 return *lhsp == *rhsp; | |
96 } | |
97 | |
98 private: | |
99 friend class ScriptModule; | |
100 friend struct ScriptModuleIdentifierHash; | |
101 | |
102 explicit ScriptModuleIdentifier( | |
103 const RefPtr<SharedPersistent<v8::Module>>& module, | |
104 unsigned identityHash) | |
105 : m_module(module), m_identityHash(identityHash) {} | |
106 | |
107 RefPtr<SharedPersistent<v8::Module>> m_module; | |
108 unsigned m_identityHash = 0; | |
109 }; | |
110 | |
111 struct ScriptModuleIdentifierHash { | |
112 STATIC_ONLY(ScriptModuleIdentifierHash); | |
113 | |
114 public: | |
115 static unsigned hash(const blink::ScriptModuleIdentifier& key) { | |
116 return static_cast<unsigned>(key.m_identityHash); | |
117 } | |
118 | |
119 static bool equal(const blink::ScriptModuleIdentifier& a, | |
120 const blink::ScriptModuleIdentifier& b) { | |
121 return a == b; | |
122 } | |
123 | |
124 static const bool safeToCompareToEmptyOrDeleted = false; | |
125 }; | |
126 | |
127 } // namespace blink | |
128 | |
129 namespace WTF { | |
130 | |
131 template <> | |
132 struct DefaultHash<blink::ScriptModuleIdentifier> { | |
133 using Hash = blink::ScriptModuleIdentifierHash; | |
134 }; | |
135 | |
136 template <> | |
137 struct HashTraits<blink::ScriptModuleIdentifier> | |
138 : public SimpleClassHashTraits<blink::ScriptModuleIdentifier> {}; | |
139 | |
140 } // namespace WTF | |
141 | |
142 #endif // ScriptModule_h | |
OLD | NEW |