Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(604)

Unified Diff: third_party/WebKit/Source/bindings/templates/interface.cpp.tmpl

Issue 2647643002: Fix V8 bindings for named constructors to set prototype object correctly (Closed)
Patch Set: Review feedback Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/bindings/templates/interface.cpp.tmpl
diff --git a/third_party/WebKit/Source/bindings/templates/interface.cpp.tmpl b/third_party/WebKit/Source/bindings/templates/interface.cpp.tmpl
index a7e7b2ecbddb5cbcc66f4c711f6351ebb845ecc9..dba9c7332a07dd5c1510611a39fa224bd5cc7740 100644
--- a/third_party/WebKit/Source/bindings/templates/interface.cpp.tmpl
+++ b/third_party/WebKit/Source/bindings/templates/interface.cpp.tmpl
@@ -522,6 +522,8 @@ static void {{cpp_class}}OriginSafeMethodSetter(v8::Local<v8::Name> name, v8::Lo
{% block named_constructor %}
{% from 'methods.cpp.tmpl' import generate_constructor with context %}
{% if named_constructor %}
+{% set parent_wrapper_type_info = '&V8%s::wrapperTypeInfo' % parent_interface
+ if parent_interface else '0' %}
{% set active_scriptwrappable_inheritance =
'InheritFromActiveScriptWrappable'
if active_scriptwrappable else
@@ -532,7 +534,7 @@ static void {{cpp_class}}OriginSafeMethodSetter(v8::Local<v8::Name> name, v8::Lo
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wglobal-constructors"
#endif
-const WrapperTypeInfo {{v8_class}}Constructor::wrapperTypeInfo = { gin::kEmbedderBlink, {{v8_class}}Constructor::domTemplate, {{v8_class}}::trace, {{v8_class}}::traceWrappers, 0, {{prepare_prototype_and_interface_object_func}}, "{{interface_name}}", 0, WrapperTypeInfo::WrapperTypeObjectPrototype, WrapperTypeInfo::{{wrapper_class_id}}, WrapperTypeInfo::{{active_scriptwrappable_inheritance}}, WrapperTypeInfo::{{event_target_inheritance}}, WrapperTypeInfo::{{lifetime}} };
+const WrapperTypeInfo {{v8_class}}Constructor::wrapperTypeInfo = { gin::kEmbedderBlink, {{v8_class}}Constructor::domTemplate, {{v8_class}}::trace, {{v8_class}}::traceWrappers, 0, {{prepare_prototype_and_interface_object_func}}, "{{interface_name}}", {{parent_wrapper_type_info}}, WrapperTypeInfo::WrapperTypeObjectPrototype, WrapperTypeInfo::{{wrapper_class_id}}, WrapperTypeInfo::{{active_scriptwrappable_inheritance}}, WrapperTypeInfo::{{event_target_inheritance}}, WrapperTypeInfo::{{lifetime}} };
#if defined(COMPONENT_BUILD) && defined(WIN32) && COMPILER(CLANG)
#pragma clang diagnostic pop
#endif
@@ -548,12 +550,47 @@ v8::Local<v8::FunctionTemplate> {{v8_class}}Constructor::domTemplate(v8::Isolate
result = v8::FunctionTemplate::New(isolate, {{v8_class}}ConstructorCallback);
v8::Local<v8::ObjectTemplate> instanceTemplate = result->InstanceTemplate();
instanceTemplate->SetInternalFieldCount({{v8_class}}::internalFieldCount);
- result->SetClassName(v8AtomicString(isolate, "{{cpp_class}}"));
+ result->SetClassName(v8AtomicString(isolate, "{{named_constructor.name}}"));
result->Inherit({{v8_class}}::domTemplate(isolate, world));
data->setInterfaceTemplate(world, &domTemplateKey, result);
return result;
}
+void {{v8_class}}Constructor::NamedConstructorAttributeGetter(
+ v8::Local<v8::Name> propertyName,
+ const v8::PropertyCallbackInfo<v8::Value>& info) {
+ v8::Local<v8::Context> currentContext = info.Holder()->CreationContext();
haraken 2017/01/25 03:37:16 currentContext => creationContext
sashab 2017/01/25 04:54:15 Done.
+ V8PerContextData* perContextData = V8PerContextData::from(currentContext);
+ if (!perContextData)
haraken 2017/01/25 03:37:16 // TODO(yukishiino): Return a valid named construc
sashab 2017/01/25 04:54:15 Done :)
+ return;
+
+ v8::Local<v8::Function> namedConstructor = perContextData->constructorForType(&{{v8_class}}Constructor::wrapperTypeInfo);
+
+ // Set the prototype of named constructors to the regular constructor.
+ auto privateProperty = V8PrivateProperty::getNamedConstructorInitialized(info.GetIsolate());
+ v8::Local<v8::Value> privateValue = privateProperty.get(currentContext, namedConstructor);
+ if (privateValue.IsEmpty()) {
+ v8::Local<v8::Function> interface = perContextData->constructorForType(&{{v8_class}}::wrapperTypeInfo);
+ // Only Window exposes named constructors, so that will always be the interface name.
+ ExceptionState exceptionState(info.GetIsolate(), ExceptionState::GetterContext, "Window", "{{named_constructor.name}}");
+ v8::MaybeLocal<v8::Value> interfacePrototype = interface->Get(currentContext, v8AtomicString(info.GetIsolate(), "prototype"));
haraken 2017/01/25 03:37:16 We want to avoid using MaybeLocal. Also this Get s
sashab 2017/01/25 04:54:15 Sorry, did this locally but didn't upload it :/ Do
+ if (interfacePrototype.IsEmpty()) {
+ exceptionState.throwTypeError("Could not get prototype for {{v8_class}}.");
+ return;
+ }
+
+ v8::Maybe<bool> setResult = namedConstructor->Set(currentContext, v8AtomicString(info.GetIsolate(), "prototype"), interfacePrototype.ToLocalChecked());
haraken 2017/01/25 03:37:16 Ditto. Avoid using MaybeLocal. Also this Set shoul
sashab 2017/01/25 04:54:15 Done. Also removed exceptionContext and just retur
+ if (setResult.IsNothing() || setResult.ToChecked() != true) {
+ exceptionState.throwTypeError("Could not set prototype for {{named_constructor.name}}.");
+ return;
+ }
+
+ privateProperty.set(currentContext, namedConstructor, v8::True(info.GetIsolate()));
+ }
+
+ v8SetReturnValue(info, namedConstructor);
+}
+
{% endif %}
{% endblock %}

Powered by Google App Engine
This is Rietveld 408576698