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

Side by Side 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: 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 unified diff | Download patch
OLDNEW
1 {% extends 'interface_base.cpp.tmpl' %} 1 {% extends 'interface_base.cpp.tmpl' %}
2 2
3 {% set has_prepare_prototype_and_interface_object = 3 {% set has_prepare_prototype_and_interface_object =
4 unscopables or has_conditional_attributes_on_prototype or 4 unscopables or has_conditional_attributes_on_prototype or
5 methods | conditionally_exposed(is_partial) %} 5 methods | conditionally_exposed(is_partial) %}
6 {% set prepare_prototype_and_interface_object_func = 6 {% set prepare_prototype_and_interface_object_func =
7 '%s::preparePrototypeAndInterfaceObject' % v8_class 7 '%s::preparePrototypeAndInterfaceObject' % v8_class
8 if has_prepare_prototype_and_interface_object 8 if has_prepare_prototype_and_interface_object
9 else 'nullptr' %} 9 else 'nullptr' %}
10 10
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 v8::Local<v8::FunctionTemplate> {{v8_class}}Constructor::domTemplate(v8::Isolate * isolate, const DOMWrapperWorld& world) { 541 v8::Local<v8::FunctionTemplate> {{v8_class}}Constructor::domTemplate(v8::Isolate * isolate, const DOMWrapperWorld& world) {
542 static int domTemplateKey; // This address is used for a key to look up the do m template. 542 static int domTemplateKey; // This address is used for a key to look up the do m template.
543 V8PerIsolateData* data = V8PerIsolateData::from(isolate); 543 V8PerIsolateData* data = V8PerIsolateData::from(isolate);
544 v8::Local<v8::FunctionTemplate> result = data->findInterfaceTemplate(world, &d omTemplateKey); 544 v8::Local<v8::FunctionTemplate> result = data->findInterfaceTemplate(world, &d omTemplateKey);
545 if (!result.IsEmpty()) 545 if (!result.IsEmpty())
546 return result; 546 return result;
547 547
548 result = v8::FunctionTemplate::New(isolate, {{v8_class}}ConstructorCallback); 548 result = v8::FunctionTemplate::New(isolate, {{v8_class}}ConstructorCallback);
549 v8::Local<v8::ObjectTemplate> instanceTemplate = result->InstanceTemplate(); 549 v8::Local<v8::ObjectTemplate> instanceTemplate = result->InstanceTemplate();
550 instanceTemplate->SetInternalFieldCount({{v8_class}}::internalFieldCount); 550 instanceTemplate->SetInternalFieldCount({{v8_class}}::internalFieldCount);
551 result->SetClassName(v8AtomicString(isolate, "{{cpp_class}}")); 551 result->SetClassName(v8AtomicString(isolate, "{{cpp_class}}"));
Yuki 2017/01/19 09:57:28 You may want to change the class name to "{{named_
552 result->Inherit({{v8_class}}::domTemplate(isolate, world)); 552 result->Inherit({{v8_class}}::domTemplate(isolate, world));
553 data->setInterfaceTemplate(world, &domTemplateKey, result); 553 data->setInterfaceTemplate(world, &domTemplateKey, result);
554 return result; 554 return result;
555 } 555 }
556 556
557 void {{v8_class}}Constructor::NamedConstructorAttributeGetter(
558 v8::Local<v8::Name> propertyName,
559 const v8::PropertyCallbackInfo<v8::Value>& info) {
560 ExceptionState exceptionState(info.GetIsolate(), ExceptionState::ConstructionC ontext, "{{interface_name}}");
Yuki 2017/01/19 09:57:28 I think this should be ExceptionState::GetterConte
Yuki 2017/01/19 10:09:30 And the interface name should be "Window" instead
sashab 2017/01/20 04:37:05 Hard-coded window, added propertyName argument and
561 v8::Local<v8::Value> data = info.Data();
562 DCHECK(data->IsExternal());
563 V8PerContextData* perContextData =
564 V8PerContextData::from(info.Holder()->CreationContext());
565 if (!perContextData)
566 return;
567
568 v8::Local<v8::Function> namedConstructorInterface = perContextData->constructo rForType(WrapperTypeInfo::unwrap(data));
Yuki 2017/01/19 09:57:28 Exactly speaking, this is a "named constructor" an
Yuki 2017/01/19 09:57:28 Now this function is specific to {{v8_class}} and
sashab 2017/01/20 04:37:05 Done the name change, and removed use of data :)
569 v8::Local<v8::Function> interface = perContextData->constructorForType(&{{v8_c lass}}::wrapperTypeInfo);
570
571 // Set the prototype of named constructors to the regular constructor.
572 v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
573 auto privateProperty = V8PrivateProperty::getNamedConstructorInitialized(info. GetIsolate());
574 v8::Local<v8::Value> privateValue = privateProperty.get(context, namedConstruc torInterface);
575 if (privateValue.IsEmpty()) {
576 v8::MaybeLocal<v8::Value> interfacePrototype = interface->Get(context, v8Ato micString(info.GetIsolate(), "prototype"));
577 if (interfacePrototype.IsEmpty())
578 exceptionState.throwTypeError("Could not get prototype for {{v8_class}}.") ;
Yuki 2017/01/19 09:57:28 Please return immediately right after throwing an
sashab 2017/01/20 04:37:05 Done
579
580 v8::Maybe<bool> setResult = namedConstructorInterface->Set(context, v8Atomic String(info.GetIsolate(), "prototype"), interfacePrototype.ToLocalChecked());
581 if (setResult.IsNothing() || setResult.ToChecked() != true) {
582 exceptionState.throwTypeError("Could not set prototype for {{interface_nam e}}.");
Yuki 2017/01/19 09:57:28 Please return immediately after throwing an except
sashab 2017/01/20 04:37:05 Yes, done.
583 }
584
585 privateProperty.set(context, namedConstructorInterface, v8::True(info.GetIso late()));
586 }
587
588 v8SetReturnValue(
589 info, perContextData->constructorForType(WrapperTypeInfo::unwrap(data)));
Yuki 2017/01/19 09:57:28 |namedConstructorInterface| is what you'd like to
sashab 2017/01/20 04:37:05 Ahh of course, thank you. Done.
590 }
591
557 {% endif %} 592 {% endif %}
558 {% endblock %} 593 {% endblock %}
559 594
560 {##############################################################################} 595 {##############################################################################}
561 {% block overloaded_constructor %} 596 {% block overloaded_constructor %}
562 {% if constructor_overloads %} 597 {% if constructor_overloads %}
563 static void constructor(const v8::FunctionCallbackInfo<v8::Value>& info) { 598 static void constructor(const v8::FunctionCallbackInfo<v8::Value>& info) {
564 ExceptionState exceptionState(info.GetIsolate(), ExceptionState::ConstructionC ontext, "{{interface_name}}"); 599 ExceptionState exceptionState(info.GetIsolate(), ExceptionState::ConstructionC ontext, "{{interface_name}}");
565 {# 2. Initialize argcount to be min(maxarg, n). #} 600 {# 2. Initialize argcount to be min(maxarg, n). #}
566 switch (std::min({{constructor_overloads.maxarg}}, info.Length())) { 601 switch (std::min({{constructor_overloads.maxarg}}, info.Length())) {
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after
956 } 991 }
957 992
958 {% for method in methods if method.overloads and method.overloads.has_partial_ov erloads %} 993 {% for method in methods if method.overloads and method.overloads.has_partial_ov erloads %}
959 void {{v8_class}}::register{{method.name | blink_capitalize}}MethodForPartialInt erface(void (*method)(const v8::FunctionCallbackInfo<v8::Value>&)) { 994 void {{v8_class}}::register{{method.name | blink_capitalize}}MethodForPartialInt erface(void (*method)(const v8::FunctionCallbackInfo<v8::Value>&)) {
960 {{cpp_class}}V8Internal::{{method.name}}MethodForPartialInterface = method; 995 {{cpp_class}}V8Internal::{{method.name}}MethodForPartialInterface = method;
961 } 996 }
962 997
963 {% endfor %} 998 {% endfor %}
964 {% endif %} 999 {% endif %}
965 {% endblock %} 1000 {% endblock %}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698