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

Side by Side Diff: Source/bindings/scripts/code_generator_v8.pm

Issue 111603006: Simplify invokeCallback() and support void return values for IDL callbacks (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 years 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 | Annotate | Revision Log
OLDNEW
1 # Copyright (C) 2005, 2006 Nikolas Zimmermann <zimmermann@kde.org> 1 # Copyright (C) 2005, 2006 Nikolas Zimmermann <zimmermann@kde.org>
2 # Copyright (C) 2006 Anders Carlsson <andersca@mac.com> 2 # Copyright (C) 2006 Anders Carlsson <andersca@mac.com>
3 # Copyright (C) 2006 Samuel Weinig <sam.weinig@gmail.com> 3 # Copyright (C) 2006 Samuel Weinig <sam.weinig@gmail.com>
4 # Copyright (C) 2006 Alexey Proskuryakov <ap@webkit.org> 4 # Copyright (C) 2006 Alexey Proskuryakov <ap@webkit.org>
5 # Copyright (C) 2006 Apple Computer, Inc. 5 # Copyright (C) 2006 Apple Computer, Inc.
6 # Copyright (C) 2007, 2008, 2009, 2012 Google Inc. 6 # Copyright (C) 2007, 2008, 2009, 2012 Google Inc.
7 # Copyright (C) 2009 Cameron McCormack <cam@mcc.id.au> 7 # Copyright (C) 2009 Cameron McCormack <cam@mcc.id.au>
8 # Copyright (C) Research In Motion Limited 2010. All rights reserved. 8 # Copyright (C) Research In Motion Limited 2010. All rights reserved.
9 # Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) 9 # Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
10 # Copyright (C) 2012 Ericsson AB. All rights reserved. 10 # Copyright (C) 2012 Ericsson AB. All rights reserved.
(...skipping 4846 matching lines...) Expand 10 before | Expand all | Expand 10 after
4857 push(@includes, "bindings/v8/ScopedPersistent.h"); 4857 push(@includes, "bindings/v8/ScopedPersistent.h");
4858 push(@includes, HeaderFilesForInterface($interfaceName, $implClassName)); 4858 push(@includes, HeaderFilesForInterface($interfaceName, $implClassName));
4859 for my $include (sort @includes) { 4859 for my $include (sort @includes) {
4860 $header{includes}->add("#include \"$include\"\n"); 4860 $header{includes}->add("#include \"$include\"\n");
4861 } 4861 }
4862 $header{nameSpaceWebCore}->addHeader("\nclass ExecutionContext;\n"); 4862 $header{nameSpaceWebCore}->addHeader("\nclass ExecutionContext;\n");
4863 $header{class}->addHeader("class $v8ClassName : public $implClassName, publi c ActiveDOMCallback {"); 4863 $header{class}->addHeader("class $v8ClassName : public $implClassName, publi c ActiveDOMCallback {");
4864 $header{class}->addFooter("};\n"); 4864 $header{class}->addFooter("};\n");
4865 4865
4866 $header{classPublic}->add(<<END); 4866 $header{classPublic}->add(<<END);
4867 static PassOwnPtr<${v8ClassName}> create(v8::Handle<v8::Object> callback, Ex ecutionContext* context) 4867 static PassOwnPtr<${v8ClassName}> create(v8::Handle<v8::Function> callback, ExecutionContext* context)
4868 { 4868 {
4869 ASSERT(context); 4869 ASSERT(context);
4870 return adoptPtr(new ${v8ClassName}(callback, context)); 4870 return adoptPtr(new ${v8ClassName}(callback, context));
4871 } 4871 }
4872 4872
4873 virtual ~${v8ClassName}(); 4873 virtual ~${v8ClassName}();
4874 4874
4875 END 4875 END
4876 4876
4877 # Functions 4877 # Functions
(...skipping 10 matching lines...) Expand all
4888 foreach my $param (@params) { 4888 foreach my $param (@params) {
4889 push(@args, GetNativeTypeForCallbacks($param->type) . " " . $par am->name); 4889 push(@args, GetNativeTypeForCallbacks($param->type) . " " . $par am->name);
4890 } 4890 }
4891 $code .= join(", ", @args); 4891 $code .= join(", ", @args);
4892 $code .= ");\n"; 4892 $code .= ");\n";
4893 $header{classPublic}->add($code); 4893 $header{classPublic}->add($code);
4894 } 4894 }
4895 } 4895 }
4896 4896
4897 $header{classPrivate}->add(<<END); 4897 $header{classPrivate}->add(<<END);
4898 ${v8ClassName}(v8::Handle<v8::Object>, ExecutionContext*); 4898 ${v8ClassName}(v8::Handle<v8::Function>, ExecutionContext*);
4899 4899
4900 ScopedPersistent<v8::Object> m_callback; 4900 ScopedPersistent<v8::Function> m_callback;
4901 RefPtr<DOMWrapperWorld> m_world; 4901 RefPtr<DOMWrapperWorld> m_world;
4902 END 4902 END
4903 } 4903 }
4904 4904
4905 sub GenerateCallbackImplementation 4905 sub GenerateCallbackImplementation
4906 { 4906 {
4907 my $object = shift; 4907 my $object = shift;
4908 my $interface = shift; 4908 my $interface = shift;
4909 my $v8ClassName = GetV8ClassName($interface); 4909 my $v8ClassName = GetV8ClassName($interface);
4910 4910
4911 AddToImplIncludes("core/dom/ExecutionContext.h"); 4911 AddToImplIncludes("core/dom/ExecutionContext.h");
4912 AddToImplIncludes("bindings/v8/V8Binding.h"); 4912 AddToImplIncludes("bindings/v8/V8Binding.h");
4913 AddToImplIncludes("bindings/v8/V8Callback.h"); 4913 AddToImplIncludes("bindings/v8/V8Callback.h");
4914 AddToImplIncludes("wtf/Assertions.h"); 4914 AddToImplIncludes("wtf/Assertions.h");
4915 4915
4916 $implementation{nameSpaceWebCore}->add(<<END); 4916 $implementation{nameSpaceWebCore}->add(<<END);
4917 ${v8ClassName}::${v8ClassName}(v8::Handle<v8::Object> callback, ExecutionContext * context) 4917 ${v8ClassName}::${v8ClassName}(v8::Handle<v8::Function> callback, ExecutionConte xt* context)
4918 : ActiveDOMCallback(context) 4918 : ActiveDOMCallback(context)
4919 , m_callback(toIsolate(context), callback) 4919 , m_callback(toIsolate(context), callback)
4920 , m_world(DOMWrapperWorld::current()) 4920 , m_world(DOMWrapperWorld::current())
4921 { 4921 {
4922 } 4922 }
4923 4923
4924 END 4924 END
4925 4925
4926 $implementation{nameSpaceWebCore}->add(<<END); 4926 $implementation{nameSpaceWebCore}->add(<<END);
4927 ${v8ClassName}::~${v8ClassName}() 4927 ${v8ClassName}::~${v8ClassName}()
4928 { 4928 {
4929 } 4929 }
4930 4930
4931 END 4931 END
4932 4932
4933 # Functions 4933 # Functions
4934 my $numFunctions = @{$interface->functions}; 4934 my $numFunctions = @{$interface->functions};
4935 if ($numFunctions > 0) { 4935 if ($numFunctions > 0) {
4936 foreach my $function (@{$interface->functions}) { 4936 foreach my $function (@{$interface->functions}) {
4937 my $code = ""; 4937 my $code = "";
4938 my @params = @{$function->parameters}; 4938 my @params = @{$function->parameters};
4939 next if $function->extendedAttributes->{"Custom"}; 4939 next if $function->extendedAttributes->{"Custom"};
4940 4940
4941 AddIncludesForType($function->type); 4941 AddIncludesForType($function->type);
4942 die "We don't yet support callbacks that return non-boolean values.\ n" if $function->type ne "boolean"; 4942 die "We only support callbacks that return boolean or void values.\n " unless ($function->type eq "boolean" || $function->type eq "void");
4943 my $defaultReturn = $function->type eq "boolean" ? " true" : "";
4943 $code .= GetNativeTypeForCallbacks($function->type) . " ${v8ClassNam e}::" . $function->name . "("; 4944 $code .= GetNativeTypeForCallbacks($function->type) . " ${v8ClassNam e}::" . $function->name . "(";
4944 my $callWithThisValue = ExtendedAttributeContains($function->extende dAttributes->{"CallWith"}, "ThisValue"); 4945 my $callWithThisValue = ExtendedAttributeContains($function->extende dAttributes->{"CallWith"}, "ThisValue");
4945 4946
4946 my @args = (); 4947 my @args = ();
4947 if ($callWithThisValue) { 4948 if ($callWithThisValue) {
4948 push(@args, GetNativeTypeForCallbacks("any") . " thisValue"); 4949 push(@args, GetNativeTypeForCallbacks("any") . " thisValue");
4949 } 4950 }
4950 foreach my $param (@params) { 4951 foreach my $param (@params) {
4951 my $paramName = $param->name; 4952 my $paramName = $param->name;
4952 my $type = $param->type; 4953 my $type = $param->type;
4953 my $arrayOrSequenceType = GetArrayOrSequenceType($type); 4954 my $arrayOrSequenceType = GetArrayOrSequenceType($type);
4954 4955
4955 if ($arrayOrSequenceType) { 4956 if ($arrayOrSequenceType) {
4956 if (IsRefPtrType($arrayOrSequenceType)) { 4957 if (IsRefPtrType($arrayOrSequenceType)) {
4957 AddIncludesForType($arrayOrSequenceType); 4958 AddIncludesForType($arrayOrSequenceType);
4958 } 4959 }
4959 } else { 4960 } else {
4960 AddIncludesForType($type); 4961 AddIncludesForType($type);
4961 } 4962 }
4962 4963
4963 push(@args, GetNativeTypeForCallbacks($type) . " " . $paramName) ; 4964 push(@args, GetNativeTypeForCallbacks($type) . " " . $paramName) ;
4964 } 4965 }
4965 $code .= join(", ", @args); 4966 $code .= join(", ", @args);
4966 4967
4967 $code .= ")\n"; 4968 $code .= ")\n";
4968 $code .= "{\n"; 4969 $code .= "{\n";
4969 $code .= " if (!canInvokeCallback())\n"; 4970 $code .= " if (!canInvokeCallback())\n";
4970 $code .= " return true;\n\n"; 4971 $code .= " return${defaultReturn};\n\n";
4971 $code .= " v8::Isolate* isolate = v8::Isolate::GetCurrent();\n"; 4972 $code .= " v8::Isolate* isolate = v8::Isolate::GetCurrent();\n";
4972 $code .= " v8::HandleScope handleScope(isolate);\n\n"; 4973 $code .= " v8::HandleScope handleScope(isolate);\n\n";
4973 $code .= " v8::Handle<v8::Context> v8Context = toV8Context(execut ionContext(), m_world.get());\n"; 4974 $code .= " v8::Handle<v8::Context> v8Context = toV8Context(execut ionContext(), m_world.get());\n";
4974 $code .= " if (v8Context.IsEmpty())\n"; 4975 $code .= " if (v8Context.IsEmpty())\n";
4975 $code .= " return true;\n\n"; 4976 $code .= " return${defaultReturn};\n\n";
4976 $code .= " v8::Context::Scope scope(v8Context);\n"; 4977 $code .= " v8::Context::Scope scope(v8Context);\n";
4977 4978
4978 my $thisObjectHandle = ""; 4979 my $thisObjectHandle = "";
4979 if ($callWithThisValue) { 4980 if ($callWithThisValue) {
4980 $code .= " v8::Handle<v8::Value> thisHandle = thisValue.v8Val ue();\n"; 4981 $code .= " v8::Handle<v8::Value> thisHandle = thisValue.v8Val ue();\n";
4981 $code .= " if (thisHandle.IsEmpty()) {\n"; 4982 $code .= " if (thisHandle.IsEmpty()) {\n";
4982 $code .= " if (!isScriptControllerTerminating())\n"; 4983 $code .= " if (!isScriptControllerTerminating())\n";
4983 $code .= " CRASH();\n"; 4984 $code .= " CRASH();\n";
4984 $code .= " return true;\n"; 4985 $code .= " return${defaultReturn};\n";
4985 $code .= " }\n"; 4986 $code .= " }\n";
4986 $code .= " ASSERT(thisHandle->IsObject());\n"; 4987 $code .= " ASSERT(thisHandle->IsObject());\n";
4987 $thisObjectHandle = "v8::Handle<v8::Object>::Cast(thisHandle), " ; 4988 $thisObjectHandle = "v8::Handle<v8::Object>::Cast(thisHandle), " ;
4988 } 4989 }
4989 @args = (); 4990 @args = ();
4990 foreach my $param (@params) { 4991 foreach my $param (@params) {
4991 my $paramName = $param->name; 4992 my $paramName = $param->name;
4992 $code .= NativeToJSValue($param->type, $param->extendedAttribute s, $paramName, " ", "v8::Handle<v8::Value> ${paramName}Handle =", "isolate", "") . "\n"; 4993 $code .= NativeToJSValue($param->type, $param->extendedAttribute s, $paramName, " ", "v8::Handle<v8::Value> ${paramName}Handle =", "isolate", "") . "\n";
4993 $code .= " if (${paramName}Handle.IsEmpty()) {\n"; 4994 $code .= " if (${paramName}Handle.IsEmpty()) {\n";
4994 $code .= " if (!isScriptControllerTerminating())\n"; 4995 $code .= " if (!isScriptControllerTerminating())\n";
4995 $code .= " CRASH();\n"; 4996 $code .= " CRASH();\n";
4996 $code .= " return true;\n"; 4997 $code .= " return${defaultReturn};\n";
4997 $code .= " }\n"; 4998 $code .= " }\n";
4998 push(@args, "${paramName}Handle"); 4999 push(@args, "${paramName}Handle");
4999 } 5000 }
5000 5001
5001 if (scalar(@args) > 0) { 5002 if (scalar(@args) > 0) {
5002 $code .= " v8::Handle<v8::Value> argv[] = { "; 5003 $code .= " v8::Handle<v8::Value> argv[] = { ";
5003 $code .= join(", ", @args); 5004 $code .= join(", ", @args);
5004 $code .= " };\n\n"; 5005 $code .= " };\n\n";
5005 } else { 5006 } else {
5006 $code .= " v8::Handle<v8::Value> *argv = 0;\n\n"; 5007 $code .= " v8::Handle<v8::Value> *argv = 0;\n\n";
5007 } 5008 }
5008 $code .= " bool callbackReturnValue = false;\n"; 5009 $code .= " ";
5009 $code .= " return !invokeCallback(m_callback.newLocal(isolate), $ {thisObjectHandle}" . scalar(@args) . ", argv, callbackReturnValue, executionCon text(), isolate);\n"; 5010 if ($function->type eq "boolean") {
5011 $code .= "return ";
5012 }
5013 $code .= "invokeCallback(m_callback.newLocal(isolate), ${thisObjectH andle}" . scalar(@args) . ", argv, executionContext(), isolate);\n";
5010 $code .= "}\n\n"; 5014 $code .= "}\n\n";
5011 $implementation{nameSpaceWebCore}->add($code); 5015 $implementation{nameSpaceWebCore}->add($code);
5012 } 5016 }
5013 } 5017 }
5014 } 5018 }
5015 5019
5016 sub GenerateSpecialWrap 5020 sub GenerateSpecialWrap
5017 { 5021 {
5018 my $interface = shift; 5022 my $interface = shift;
5019 my $v8ClassName = shift; 5023 my $v8ClassName = shift;
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after
5404 5408
5405 # Default, assume native type is a pointer with same type name as idl type 5409 # Default, assume native type is a pointer with same type name as idl type
5406 return "${type}*"; 5410 return "${type}*";
5407 } 5411 }
5408 5412
5409 sub GetNativeTypeForCallbacks 5413 sub GetNativeTypeForCallbacks
5410 { 5414 {
5411 my $type = shift; 5415 my $type = shift;
5412 return "const String&" if $type eq "DOMString"; 5416 return "const String&" if $type eq "DOMString";
5413 return "PassRefPtr<SerializedScriptValue>" if $type eq "SerializedScriptValu e"; 5417 return "PassRefPtr<SerializedScriptValue>" if $type eq "SerializedScriptValu e";
5418 return "void" if $type eq "void";
5414 5419
5415 # Callbacks use raw pointers, so pass isParameter = 1 5420 # Callbacks use raw pointers, so pass isParameter = 1
5416 my $nativeType = GetNativeType($type, {}, "parameter"); 5421 my $nativeType = GetNativeType($type, {}, "parameter");
5417 return "const $nativeType&" if $nativeType =~ /^Vector/; 5422 return "const $nativeType&" if $nativeType =~ /^Vector/;
5418 return $nativeType; 5423 return $nativeType;
5419 } 5424 }
5420 5425
5421 sub JSValueToNativeStatement 5426 sub JSValueToNativeStatement
5422 { 5427 {
5423 my $type = shift; 5428 my $type = shift;
(...skipping 954 matching lines...) Expand 10 before | Expand all | Expand 10 after
6378 6383
6379 return 1 if ExtendedAttributeContains($interface->extendedAttributes->{"Cust om"}, "ToV8"); 6384 return 1 if ExtendedAttributeContains($interface->extendedAttributes->{"Cust om"}, "ToV8");
6380 return 1 if ExtendedAttributeContains($interface->extendedAttributes->{"Cust om"}, "Wrap"); 6385 return 1 if ExtendedAttributeContains($interface->extendedAttributes->{"Cust om"}, "Wrap");
6381 return 1 if $interface->extendedAttributes->{"SpecialWrapFor"}; 6386 return 1 if $interface->extendedAttributes->{"SpecialWrapFor"};
6382 return 1 if InheritsInterface($interface, "Document"); 6387 return 1 if InheritsInterface($interface, "Document");
6383 6388
6384 return 0; 6389 return 0;
6385 } 6390 }
6386 6391
6387 1; 6392 1;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698