Browse Source

Merge branch 'genpy' of github.com:Simn/haxe into genpy

frabbit 11 years ago
parent
commit
d229dbff34
4 changed files with 50 additions and 36 deletions
  1. 2 9
      genpy.ml
  2. 27 27
      std/python/_std/String.hx
  3. 8 0
      std/python/internal/HxOverrides.hx
  4. 13 0
      tests/unit/TestPython.hx

+ 2 - 9
genpy.ml

@@ -1172,9 +1172,8 @@ module Printer = struct
 
 
 	and print_call pctx e1 el =
 	and print_call pctx e1 el =
 		match e1.eexpr with
 		match e1.eexpr with
-			| TField(e1,FAnon {cf_name = "toUpperCase"}) ->
-				(* TODO: toLowerCase and other string methods? *)
-				Printf.sprintf "HxOverrides.hx_toUpperCase(%s)" (print_expr pctx e1)
+			| TField(e1,((FAnon {cf_name = ("toUpperCase" | "toLowerCase" as s)}) | FDynamic ("toUpperCase" | "toLowerCase" as s))) ->
+				Printf.sprintf "HxOverrides.hx_%s(%s)" s (print_expr pctx e1)
 			| _ ->
 			| _ ->
 				print_call2 pctx e1 el
 				print_call2 pctx e1 el
 
 
@@ -1619,12 +1618,6 @@ module Generator = struct
 		print ctx "_hx_classes[\"%s\"] = %s\n" p_name p;
 		print ctx "_hx_classes[\"%s\"] = %s\n" p_name p;
 		print ctx "_hx_c.%s = %s\n" p p
 		print ctx "_hx_c.%s = %s\n" p p
 
 
-(* 	Bool._hx_class_name = 'Bool'
-Bool._hx_class = Bool
-
-_hx_classes['Bool'] = Bool
-_hx_c.Bool = Bool	 *)
-
 	let gen_type ctx mt = match mt with
 	let gen_type ctx mt = match mt with
 		| TClassDecl c -> gen_class ctx c
 		| TClassDecl c -> gen_class ctx c
 		| TEnumDecl en -> gen_enum ctx en
 		| TEnumDecl en -> gen_enum ctx en

+ 27 - 27
std/python/_std/String.hx

@@ -22,12 +22,12 @@ package;
  */
  */
 /**
 /**
 	The basic String class.
 	The basic String class.
-	
+
 	A haxe String is immutable, it is not possible to modify individual
 	A haxe String is immutable, it is not possible to modify individual
 	characters. No method of this class changes the state of [this] String.
 	characters. No method of this class changes the state of [this] String.
-	
+
 	Strings can be constructed using the string literal syntax "string value".
 	Strings can be constructed using the string literal syntax "string value".
-	
+
 	String can be concatenated by using the + operator. If an operand is not a
 	String can be concatenated by using the + operator. If an operand is not a
 	String, it is passed through Std.string() first.
 	String, it is passed through Std.string() first.
 **/
 **/
@@ -55,7 +55,7 @@ extern class String extends StringImpl {
 
 
 	/**
 	/**
 		Returns a String where all characters of [this] String are upper case.
 		Returns a String where all characters of [this] String are upper case.
-		
+
 		Affects the characters [a-z]. Other characters remain unchanged.
 		Affects the characters [a-z]. Other characters remain unchanged.
 	**/
 	**/
     @:runtime public inline function toUpperCase() : String {
     @:runtime public inline function toUpperCase() : String {
@@ -64,16 +64,16 @@ extern class String extends StringImpl {
 
 
 	/**
 	/**
 		Returns a String where all characters of [this] String are lower case.
 		Returns a String where all characters of [this] String are lower case.
-		
+
 		Affects the characters [A-Z]. Other characters remain unchanged.
 		Affects the characters [A-Z]. Other characters remain unchanged.
 	**/
 	**/
-	inline function toLowerCase() : String {
+	@:runtime public inline function toLowerCase() : String {
 		return untyped this.lower();
 		return untyped this.lower();
 	}
 	}
 
 
 	/**
 	/**
 		Returns the character at position [index] of [this] String.
 		Returns the character at position [index] of [this] String.
-		
+
 		If [index] is negative or exceeds [this].length, the empty String ""
 		If [index] is negative or exceeds [this].length, the empty String ""
 		is returned.
 		is returned.
 	**/
 	**/
@@ -84,9 +84,9 @@ extern class String extends StringImpl {
 
 
 	/**
 	/**
 		Returns the character code at position [index] of [this] String.
 		Returns the character code at position [index] of [this] String.
-		
+
 		If [index] is negative or exceeds [this].length, null is returned.
 		If [index] is negative or exceeds [this].length, null is returned.
-		
+
 		To obtain the character code of a single character, "x".code can be used
 		To obtain the character code of a single character, "x".code can be used
 		instead to inline the character code at compile time. Note that this
 		instead to inline the character code at compile time. Note that this
 		only works on String literals of length 1.
 		only works on String literals of length 1.
@@ -99,12 +99,12 @@ extern class String extends StringImpl {
 	/**
 	/**
 		Returns the position of the leftmost occurence of [str] within [this]
 		Returns the position of the leftmost occurence of [str] within [this]
 		String.
 		String.
-		
+
 		If [startIndex] is given, the search is performed within the substring
 		If [startIndex] is given, the search is performed within the substring
 		of [this] String starting from [startIndex]. Otherwise the search is
 		of [this] String starting from [startIndex]. Otherwise the search is
 		performed within [this] String. In either case, the returned position
 		performed within [this] String. In either case, the returned position
 		is relative to the beginning of [this] String.
 		is relative to the beginning of [this] String.
-		
+
 		If [str] cannot be found, -1 is returned.
 		If [str] cannot be found, -1 is returned.
 	**/
 	**/
 	inline function indexOf( str : String, ?startIndex : Int ) : Int {
 	inline function indexOf( str : String, ?startIndex : Int ) : Int {
@@ -117,12 +117,12 @@ extern class String extends StringImpl {
 	/**
 	/**
 		Returns the position of the rightmost occurence of [str] within [this]
 		Returns the position of the rightmost occurence of [str] within [this]
 		String.
 		String.
-		
+
 		If [startIndex] is given, the search is performed within the substring
 		If [startIndex] is given, the search is performed within the substring
 		of [this] String from 0 to [startIndex]. Otherwise the search is
 		of [this] String from 0 to [startIndex]. Otherwise the search is
 		performed within [this] String. In either case, the returned position
 		performed within [this] String. In either case, the returned position
 		is relative to the beginning of [this] String.
 		is relative to the beginning of [this] String.
-		
+
 		If [str] cannot be found, -1 is returned.
 		If [str] cannot be found, -1 is returned.
 	**/
 	**/
     inline function lastIndexOf( str : String, ?startIndex : Int ) : Int {
     inline function lastIndexOf( str : String, ?startIndex : Int ) : Int {
@@ -131,16 +131,16 @@ extern class String extends StringImpl {
 
 
 	/**
 	/**
 		Splits [this] String at each occurence of [delimiter].
 		Splits [this] String at each occurence of [delimiter].
-		
+
 		If [delimiter] is the empty String "", [this] String is split into an
 		If [delimiter] is the empty String "", [this] String is split into an
 		Array of [this].length elements, where the elements correspond to the
 		Array of [this].length elements, where the elements correspond to the
 		characters of [this] String.
 		characters of [this] String.
-		
+
 		If [delimiter] is not found within [this] String, the result is an Array
 		If [delimiter] is not found within [this] String, the result is an Array
 		with one element, which equals [this] String.
 		with one element, which equals [this] String.
-		
+
 		If [delimiter] is null, the result is unspecified.
 		If [delimiter] is null, the result is unspecified.
-		
+
 		Otherwise, [this] String is split into parts at each occurence of
 		Otherwise, [this] String is split into parts at each occurence of
 		[delimiter]. If [this] String starts (or ends) with [delimiter}, the
 		[delimiter]. If [this] String starts (or ends) with [delimiter}, the
 		result Array contains a leading (or trailing) empty String "" element.
 		result Array contains a leading (or trailing) empty String "" element.
@@ -150,17 +150,17 @@ extern class String extends StringImpl {
 
 
 	/**
 	/**
 		Returns [len] characters of [this] String, starting at position [pos].
 		Returns [len] characters of [this] String, starting at position [pos].
-		
+
 		If [len] is omitted, all characters from position [pos] to the end of
 		If [len] is omitted, all characters from position [pos] to the end of
 		[this] String are included.
 		[this] String are included.
-		
+
 		If [pos] is negative, its value is calculated from the end of [this]
 		If [pos] is negative, its value is calculated from the end of [this]
 		String by [this].length + [pos]. If this yields a negative value, 0 is
 		String by [this].length + [pos]. If this yields a negative value, 0 is
 		used instead.
 		used instead.
-		
+
 		If the calculated position + [len] exceeds [this].length, the characters
 		If the calculated position + [len] exceeds [this].length, the characters
 		from that position to the end of [this] String are returned.
 		from that position to the end of [this] String are returned.
-		
+
 		If [len] is negative, the result is unspecified.
 		If [len] is negative, the result is unspecified.
 	**/
 	**/
 	inline public function substr( pos : Int, ?len : Int ) : String
 	inline public function substr( pos : Int, ?len : Int ) : String
@@ -170,14 +170,14 @@ extern class String extends StringImpl {
 
 
 	/**
 	/**
 		Returns the part of [this] String from [startIndex] to [endIndex].
 		Returns the part of [this] String from [startIndex] to [endIndex].
-		
+
 		If [startIndex] or [endIndex] are negative, 0 is used instead.
 		If [startIndex] or [endIndex] are negative, 0 is used instead.
-		
+
 		If [startIndex] exceeds [endIndex], they are swapped.
 		If [startIndex] exceeds [endIndex], they are swapped.
-		
+
 		If the (possibly swapped) [endIndex] is omitted or exceeds
 		If the (possibly swapped) [endIndex] is omitted or exceeds
 		[this].length, [this].length is used instead.
 		[this].length, [this].length is used instead.
-		
+
 		If the (possibly swapped) [startIndex] exceeds [this].length, the empty
 		If the (possibly swapped) [startIndex] exceeds [this].length, the empty
 		String "" is returned.
 		String "" is returned.
 	**/
 	**/
@@ -192,7 +192,7 @@ extern class String extends StringImpl {
 
 
 	/**
 	/**
 		Returns the String corresponding to the character code [code].
 		Returns the String corresponding to the character code [code].
-		
+
 		If [code] is negative or has another invalid value, the result is
 		If [code] is negative or has another invalid value, the result is
 		unspecified.
 		unspecified.
 	**/
 	**/
@@ -201,7 +201,7 @@ extern class String extends StringImpl {
 	}
 	}
 
 
 	static function __init__ ():Void {
 	static function __init__ ():Void {
-		
+
 		python.Macros.importFromAs("builtins", "str", "String");
 		python.Macros.importFromAs("builtins", "str", "String");
 		//untyped __python__("String = __builtin__.str");
 		//untyped __python__("String = __builtin__.str");
 	}
 	}

+ 8 - 0
std/python/internal/HxOverrides.hx

@@ -75,4 +75,12 @@ class HxOverrides {
 			return x.toUpperCase();
 			return x.toUpperCase();
 		}
 		}
 	}
 	}
+
+	static public function hx_toLowerCase(x) {
+		if (Std.is(x, String)) {
+			return x.lower();
+		} else {
+			return x.toLowerCase();
+		}
+	}
 }
 }

+ 13 - 0
tests/unit/TestPython.hx

@@ -18,4 +18,17 @@ class TestPython extends Test {
 		var list = new Array();
 		var list = new Array();
 	}
 	}
 
 
+	public function testStringMethod() {
+		var d:Dynamic = "foo";
+		eq("FOO", d.toUpperCase());
+
+		var o:{function toUpperCase():String;} = "foo";
+		eq("FOO", o.toUpperCase());
+
+		var d:Dynamic = "FOO";
+		eq("foo", d.toLowerCase());
+
+		var o:{function toLowerCase():String;} = "FOO";
+		eq("foo", o.toLowerCase());
+	}
 }
 }