changeset 13:4f583f7e242e

Added a constructor for every init method
author Jacob Carlborg <doob@me.com>
date Mon, 03 Aug 2009 15:21:59 +0200
parents 9f0b49a2f64e
children 89f3c3ef1fd2
files scripts/dgen.rb
diffstat 1 files changed, 51 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/dgen.rb	Mon Aug 03 14:00:21 2009 +0200
+++ b/scripts/dgen.rb	Mon Aug 03 15:21:59 2009 +0200
@@ -1099,13 +1099,25 @@
 			index = 0
 			declaration = build_function(method, true, static)[0 ... -2].indent.nl(false)
 			index = declaration.index_of(" ") if static
-			index = declaration.index_of(" ", index) 
+			index = declaration.index_of(" ", index)
+			name = get_method_name(method.selector)
 			
-			str << (declaration[0 .. index] + get_method_name(method["selector"]) + declaration[index .. -1]).gsub(/ +/, " ")
+			str << (declaration[0 .. index] + name + declaration[index .. -1]).gsub(/ +/, " ")
 			str << "{".indent.nl(false)
-			str << "return invokeObjcSelf!(".indent(2) unless static
-			str << "return invokeObjcSelfClass!(".indent(2) if static
-			str << return_type
+			
+			if static
+				str << "return invokeObjcSelfClass!(".indent(2)
+				str << return_type
+			else
+				if return_type == class_name
+					str << "id result = invokeObjcSelf!(".indent(2)
+					str << "id"
+				else
+					str << "return invokeObjcSelf!(".indent(2)
+					str << return_type
+				end
+			end
+			
 			str << ', "'
 			str << method.selector
 			str << '"'
@@ -1123,11 +1135,43 @@
 			method.arg.each_with_index do |arg, i|
 				str << get_identifier(arg.name)
 				str << ", " unless i == method.arg.length - 1
-			end unless method.arg.nil?
+			end unless method.arg.nil?			
 			
-			str << ")".nl
+			if return_type == class_name
+				str << "return result is this.objcObject ? this : (result !is null ? new #{return_type}(result) : null);"
+			else
+				str << ")".nl
+			end
 			
 			str << "}".indent.nl(false).nl(false)
+			
+			if name.length >= 4 && name[0 ... 4] == "init" && name != "initialize"
+				str << ("this" + declaration[index .. -1]).gsub(/ +/, " ").indent
+				str << "{".indent.nl(false)
+				str << 'objcObject = Bridge.invokeObjcClassMethod!(id, "alloc")(objcClass)'.indent(2).nl
+				str << 'id result = Bridge.invokeObjcMethod!(id, "'.indent(2)
+				str << method.selector
+				str << '"'
+				
+				method.arg.each do |arg|
+					str << ", "
+					str << get_type(arg.type, arg.type, arg.declaredType)
+				end unless method.arg.nil?
+				
+				str << ")(objcObject"
+				
+				method.arg.each do |arg|
+					str << ", "
+					str << get_identifier(arg.name)
+				end unless method.arg.nil?
+				
+				str << ")".nl.nl(false)
+				
+				str << "if (result)".indent(2).nl(false)
+				str << "objcObject = ret".indent(3).nl.nl(false)
+				str << "dObject = this".indent(2).nl
+				str << "}".indent.nl(false).nl(false)
+			end
 		end
 		
 		str.string[0 .. -2]