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" |
9 #include "bindings/core/v8/V8CustomElementsRegistry.h" | 10 #include "bindings/core/v8/V8CustomElementsRegistry.h" |
| 11 #include "bindings/core/v8/V8Element.h" |
10 #include "bindings/core/v8/V8HiddenValue.h" | 12 #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" |
11 #include "wtf/Allocator.h" | 17 #include "wtf/Allocator.h" |
12 | 18 |
13 namespace blink { | 19 namespace blink { |
14 | 20 |
15 // Retrieves the custom elements constructor -> name map, creating it | 21 // Retrieves the custom elements constructor -> name map, creating it |
16 // if necessary. The same map is used to keep prototypes alive. | 22 // if necessary. The same map is used to keep prototypes alive. |
17 static v8::Local<v8::Map> ensureCustomElementsRegistryMap( | 23 static v8::Local<v8::Map> ensureCustomElementsRegistryMap( |
18 ScriptState* scriptState, | 24 ScriptState* scriptState, |
19 CustomElementsRegistry* registry) | 25 CustomElementsRegistry* registry) |
20 { | 26 { |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 , m_constructor(scriptState->isolate(), constructor) | 119 , m_constructor(scriptState->isolate(), constructor) |
114 , m_prototype(scriptState->isolate(), prototype) | 120 , m_prototype(scriptState->isolate(), prototype) |
115 { | 121 { |
116 // These objects are kept alive by references from the | 122 // These objects are kept alive by references from the |
117 // CustomElementsRegistry wrapper set up by | 123 // CustomElementsRegistry wrapper set up by |
118 // ScriptCustomElementDefinition::create. | 124 // ScriptCustomElementDefinition::create. |
119 m_constructor.setPhantom(); | 125 m_constructor.setPhantom(); |
120 m_prototype.setPhantom(); | 126 m_prototype.setPhantom(); |
121 } | 127 } |
122 | 128 |
| 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 |
123 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const | 168 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const |
124 { | 169 { |
125 DCHECK(!m_constructor.isEmpty()); | 170 DCHECK(!m_constructor.isEmpty()); |
126 return m_constructor.newLocal(m_scriptState->isolate()); | 171 return m_constructor.newLocal(m_scriptState->isolate()); |
127 } | 172 } |
128 | 173 |
129 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const | 174 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const |
130 { | 175 { |
131 DCHECK(!m_prototype.isEmpty()); | 176 DCHECK(!m_prototype.isEmpty()); |
132 return m_prototype.newLocal(m_scriptState->isolate()); | 177 return m_prototype.newLocal(m_scriptState->isolate()); |
133 } | 178 } |
134 | 179 |
135 // CustomElementDefinition | 180 // CustomElementDefinition |
136 ScriptValue ScriptCustomElementDefinition::getConstructorForScript() | 181 ScriptValue ScriptCustomElementDefinition::getConstructorForScript() |
137 { | 182 { |
138 return ScriptValue(m_scriptState.get(), constructor()); | 183 return ScriptValue(m_scriptState.get(), constructor()); |
139 } | 184 } |
140 | 185 |
141 } // namespace blink | 186 } // namespace blink |
OLD | NEW |