OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "bindings/core/v8/ScriptCustomElementDefinition.h" | 5 #include "bindings/core/v8/ScriptCustomElementDefinition.h" |
6 | 6 |
7 #include "bindings/core/v8/ScriptState.h" | 7 #include "bindings/core/v8/ScriptState.h" |
8 #include "bindings/core/v8/V8Binding.h" | 8 #include "bindings/core/v8/V8Binding.h" |
9 #include "bindings/core/v8/V8BindingMacros.h" | |
10 #include "bindings/core/v8/V8CustomElementsRegistry.h" | 9 #include "bindings/core/v8/V8CustomElementsRegistry.h" |
11 #include "bindings/core/v8/V8Element.h" | |
12 #include "bindings/core/v8/V8HiddenValue.h" | 10 #include "bindings/core/v8/V8HiddenValue.h" |
13 #include "bindings/core/v8/V8ScriptRunner.h" | |
14 #include "bindings/core/v8/V8ThrowException.h" | |
15 #include "core/dom/ExceptionCode.h" | |
16 #include "v8.h" | |
17 #include "wtf/Allocator.h" | 11 #include "wtf/Allocator.h" |
18 | 12 |
19 namespace blink { | 13 namespace blink { |
20 | 14 |
21 // Retrieves the custom elements constructor -> name map, creating it | 15 // Retrieves the custom elements constructor -> name map, creating it |
22 // if necessary. The same map is used to keep prototypes alive. | 16 // if necessary. The same map is used to keep prototypes alive. |
23 static v8::Local<v8::Map> ensureCustomElementsRegistryMap( | 17 static v8::Local<v8::Map> ensureCustomElementsRegistryMap( |
24 ScriptState* scriptState, | 18 ScriptState* scriptState, |
25 CustomElementsRegistry* registry) | 19 CustomElementsRegistry* registry) |
26 { | 20 { |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 , m_constructor(scriptState->isolate(), constructor) | 113 , m_constructor(scriptState->isolate(), constructor) |
120 , m_prototype(scriptState->isolate(), prototype) | 114 , m_prototype(scriptState->isolate(), prototype) |
121 { | 115 { |
122 // These objects are kept alive by references from the | 116 // These objects are kept alive by references from the |
123 // CustomElementsRegistry wrapper set up by | 117 // CustomElementsRegistry wrapper set up by |
124 // ScriptCustomElementDefinition::create. | 118 // ScriptCustomElementDefinition::create. |
125 m_constructor.setPhantom(); | 119 m_constructor.setPhantom(); |
126 m_prototype.setPhantom(); | 120 m_prototype.setPhantom(); |
127 } | 121 } |
128 | 122 |
129 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades | |
130 bool ScriptCustomElementDefinition::runConstructor(Element* element) | |
131 { | |
132 if (!m_scriptState->contextIsValid()) | |
133 return false; | |
134 ScriptState::Scope scope(m_scriptState.get()); | |
135 v8::Isolate* isolate = m_scriptState->isolate(); | |
136 | |
137 // Step 5 says to rethrow the exception; but there is no one to | |
138 // catch it. The side effect is to report the error. | |
139 v8::TryCatch tryCatch(isolate); | |
140 tryCatch.SetVerbose(true); | |
141 | |
142 ExecutionContext* executionContext = m_scriptState->getExecutionContext(); | |
143 v8::Local<v8::Value> result; | |
144 if (!v8Call(V8ScriptRunner::callAsConstructor( | |
145 isolate, | |
146 constructor(), | |
147 executionContext, | |
148 0, | |
149 nullptr), | |
150 result)) | |
151 return false; | |
152 | |
153 if (V8Element::toImplWithTypeCheck(isolate, result) != element) { | |
154 V8ThrowException::throwException( | |
155 V8ThrowException::createDOMException( | |
156 m_scriptState->isolate(), | |
157 InvalidStateError, | |
158 "custom element constructors must call super() first and must " | |
159 "not return a different object", | |
160 constructor()), | |
161 m_scriptState->isolate()); | |
162 return false; | |
163 } | |
164 | |
165 return true; | |
166 } | |
167 | |
168 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const | 123 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const |
169 { | 124 { |
170 DCHECK(!m_constructor.isEmpty()); | 125 DCHECK(!m_constructor.isEmpty()); |
171 return m_constructor.newLocal(m_scriptState->isolate()); | 126 return m_constructor.newLocal(m_scriptState->isolate()); |
172 } | 127 } |
173 | 128 |
174 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const | 129 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const |
175 { | 130 { |
176 DCHECK(!m_prototype.isEmpty()); | 131 DCHECK(!m_prototype.isEmpty()); |
177 return m_prototype.newLocal(m_scriptState->isolate()); | 132 return m_prototype.newLocal(m_scriptState->isolate()); |
178 } | 133 } |
179 | 134 |
180 // CustomElementDefinition | 135 // CustomElementDefinition |
181 ScriptValue ScriptCustomElementDefinition::getConstructorForScript() | 136 ScriptValue ScriptCustomElementDefinition::getConstructorForScript() |
182 { | 137 { |
183 return ScriptValue(m_scriptState.get(), constructor()); | 138 return ScriptValue(m_scriptState.get(), constructor()); |
184 } | 139 } |
185 | 140 |
186 } // namespace blink | 141 } // namespace blink |
OLD | NEW |