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/ScriptCustomElementDefinitionBuilder.h" | 5 #include "bindings/core/v8/ScriptCustomElementDefinitionBuilder.h" |
6 | 6 |
7 #include "bindings/core/v8/DOMWrapperWorld.h" | 7 #include "bindings/core/v8/DOMWrapperWorld.h" |
8 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
9 #include "bindings/core/v8/ScriptCustomElementDefinition.h" | 9 #include "bindings/core/v8/ScriptCustomElementDefinition.h" |
10 #include "bindings/core/v8/ScriptState.h" | 10 #include "bindings/core/v8/ScriptState.h" |
11 #include "bindings/core/v8/ScriptValue.h" | 11 #include "bindings/core/v8/ScriptValue.h" |
12 #include "bindings/core/v8/V8Binding.h" | 12 #include "bindings/core/v8/V8Binding.h" |
13 #include "bindings/core/v8/V8BindingMacros.h" | 13 #include "bindings/core/v8/V8BindingMacros.h" |
14 #include "bindings/core/v8/V8CustomElementsRegistry.h" | |
15 #include "bindings/core/v8/V8PrivateProperty.h" | |
14 #include "core/dom/ExceptionCode.h" | 16 #include "core/dom/ExceptionCode.h" |
15 | 17 |
16 namespace blink { | 18 namespace blink { |
17 | 19 |
18 ScriptCustomElementDefinitionBuilder::ScriptCustomElementDefinitionBuilder( | 20 ScriptCustomElementDefinitionBuilder::ScriptCustomElementDefinitionBuilder( |
19 ScriptState* scriptState, | 21 ScriptState* scriptState, |
20 CustomElementsRegistry* registry, | 22 CustomElementsRegistry* registry, |
21 const ScriptValue& constructor, | 23 const ScriptValue& constructor, |
22 ExceptionState& exceptionState) | 24 ExceptionState& exceptionState) |
23 : m_scriptState(scriptState) | 25 : m_scriptState(scriptState) |
24 , m_registry(registry) | 26 , m_registry(registry) |
25 , m_constructorValue(constructor.v8Value()) | 27 , m_constructorValue(constructor.v8Value()) |
26 , m_exceptionState(exceptionState) | 28 , m_exceptionState(exceptionState) |
29 , m_didStartDefiningConstructor(false) | |
27 { | 30 { |
28 } | 31 } |
29 | 32 |
33 ScriptCustomElementDefinitionBuilder::~ScriptCustomElementDefinitionBuilder() | |
34 { | |
35 if (m_didStartDefiningConstructor) { | |
36 bool didDelete = v8CallOrCrash(constructorsBeingDefined()->Delete( | |
37 m_scriptState->context(), | |
38 m_constructor)); | |
39 CHECK(didDelete); | |
40 } | |
41 } | |
42 | |
30 bool ScriptCustomElementDefinitionBuilder::checkConstructorIntrinsics() | 43 bool ScriptCustomElementDefinitionBuilder::checkConstructorIntrinsics() |
31 { | 44 { |
32 DCHECK(m_scriptState->world().isMainWorld()); | 45 DCHECK(m_scriptState->world().isMainWorld()); |
33 | 46 |
34 // The signature of CustomElementsRegistry.define says this is a | 47 // The signature of CustomElementsRegistry.define says this is a |
35 // Function | 48 // Function |
36 // https://html.spec.whatwg.org/multipage/scripting.html#customelementsregis try | 49 // https://html.spec.whatwg.org/multipage/scripting.html#customelementsregis try |
37 CHECK(m_constructorValue->IsFunction()); | 50 CHECK(m_constructorValue->IsFunction()); |
38 m_constructor = m_constructorValue.As<v8::Object>(); | 51 m_constructor = m_constructorValue.As<v8::Object>(); |
39 if (!m_constructor->IsConstructor()) { | 52 if (!m_constructor->IsConstructor()) { |
40 m_exceptionState.throwTypeError( | 53 m_exceptionState.throwTypeError( |
41 "constructor argument is not a constructor"); | 54 "constructor argument is not a constructor"); |
42 return false; | 55 return false; |
43 } | 56 } |
44 return true; | 57 return true; |
45 } | 58 } |
46 | 59 |
47 bool ScriptCustomElementDefinitionBuilder::checkConstructorNotRegistered() | 60 bool ScriptCustomElementDefinitionBuilder::checkConstructorNotRegistered() |
48 { | 61 { |
62 // Check existing constructors | |
49 if (ScriptCustomElementDefinition::forConstructor( | 63 if (ScriptCustomElementDefinition::forConstructor( |
50 m_scriptState.get(), | 64 m_scriptState.get(), |
51 m_registry, | 65 m_registry, |
52 m_constructor)) { | 66 m_constructor)) { |
53 | 67 |
54 // Constructor is already registered. | 68 // Constructor is already registered. |
55 m_exceptionState.throwDOMException( | 69 m_exceptionState.throwDOMException( |
56 NotSupportedError, | 70 NotSupportedError, |
57 "this constructor has already been used with this registry"); | 71 "this constructor has already been used with this registry"); |
58 return false; | 72 return false; |
59 } | 73 } |
74 | |
75 // Check constructors that are being processed by builders right now. | |
76 v8::Local<v8::Set> constructors = constructorsBeingDefined(); | |
77 if (v8CallOrCrash( | |
haraken
2016/06/02 02:29:36
Would you help me understand why it's not enough t
| |
78 constructors->Has(m_scriptState->context(), m_constructor))) { | |
79 m_exceptionState.throwDOMException( | |
80 NotSupportedError, | |
81 "this constructor is already being defined in this registry"); | |
82 return false; | |
83 } | |
84 m_didStartDefiningConstructor = true; | |
85 v8CallOrCrash(constructors->Add(m_scriptState->context(), m_constructor)); | |
60 return true; | 86 return true; |
61 } | 87 } |
62 | 88 |
63 bool ScriptCustomElementDefinitionBuilder::checkPrototype() | 89 bool ScriptCustomElementDefinitionBuilder::checkPrototype() |
64 { | 90 { |
65 v8::Isolate* isolate = m_scriptState->isolate(); | 91 v8::Isolate* isolate = m_scriptState->isolate(); |
66 v8::Local<v8::Context> context = m_scriptState->context(); | 92 v8::Local<v8::Context> context = m_scriptState->context(); |
67 v8::Local<v8::String> prototypeString = | 93 v8::Local<v8::String> prototypeString = |
68 v8AtomicString(isolate, "prototype"); | 94 v8AtomicString(isolate, "prototype"); |
69 v8::Local<v8::Value> prototypeValue; | 95 v8::Local<v8::Value> prototypeValue; |
(...skipping 16 matching lines...) Expand all Loading... | |
86 const CustomElementDescriptor& descriptor) | 112 const CustomElementDescriptor& descriptor) |
87 { | 113 { |
88 return ScriptCustomElementDefinition::create( | 114 return ScriptCustomElementDefinition::create( |
89 m_scriptState.get(), | 115 m_scriptState.get(), |
90 m_registry, | 116 m_registry, |
91 descriptor, | 117 descriptor, |
92 m_constructor, | 118 m_constructor, |
93 m_prototype); | 119 m_prototype); |
94 } | 120 } |
95 | 121 |
122 v8::Local<v8::Set> ScriptCustomElementDefinitionBuilder | |
123 ::constructorsBeingDefined() | |
124 { | |
125 V8PrivateProperty::Symbol symbol = | |
126 V8PrivateProperty::getCustomElementsRegistryConstructorsBeingDefined( | |
127 m_scriptState->isolate()); | |
128 v8::Local<v8::Object> wrapper = | |
129 toV8(m_registry.get(), m_scriptState.get()).As<v8::Object>(); | |
130 v8::Local<v8::Value> set = symbol.get(m_scriptState->context(), wrapper); | |
131 if (set.IsEmpty()) { | |
132 set = v8::Set::New(m_scriptState->isolate()); | |
133 symbol.set(m_scriptState->context(), wrapper, set); | |
134 } | |
135 return set.As<v8::Set>(); | |
136 } | |
137 | |
96 } // namespace blink | 138 } // namespace blink |
OLD | NEW |