Browse Source

[java] Added support for native String fields access without private fields

Caue Waneck 13 years ago
parent
commit
5becd511b8
2 changed files with 31 additions and 32 deletions
  1. 14 8
      genjava.ml
  2. 17 24
      std/StringTools.hx

+ 14 - 8
genjava.ml

@@ -504,14 +504,16 @@ struct
           { e with eexpr = TCall(Type.map_expr run e, []) }
           { e with eexpr = TCall(Type.map_expr run e, []) }
         | TCall( ( { eexpr = TField({ eexpr = TTypeExpr (TTypeDecl t) }, "fromCharCode") } ), [cc] ) when is_string (follow (TType(t,List.map snd t.t_types))) ->
         | TCall( ( { eexpr = TField({ eexpr = TTypeExpr (TTypeDecl t) }, "fromCharCode") } ), [cc] ) when is_string (follow (TType(t,List.map snd t.t_types))) ->
           { e with eexpr = TNew(get_cl_from_t basic.tstring, [], [mk_cast tchar (run cc); mk_int gen 1 cc.epos]) }
           { e with eexpr = TNew(get_cl_from_t basic.tstring, [], [mk_cast tchar (run cc); mk_int gen 1 cc.epos]) }
-        | TCall( ( { eexpr = TField(ef, ("charAt" as field)) } ), args )
-        | TCall( ( { eexpr = TField(ef, ("charCodeAt" as field)) } ), args )
-        | TCall( ( { eexpr = TField(ef, ("split" as field)) } ), args )
-        | TCall( ( { eexpr = TField(ef, ("indexOf" as field)) } ), args )
-        | TCall( ( { eexpr = TField(ef, ("lastIndexOf" as field)) } ), args )
-        | TCall( ( { eexpr = TField(ef, ("substring" as field)) } ), args )
-        | TCall( ( { eexpr = TField(ef, ("substr" as field)) } ), args ) when is_string ef.etype ->
-          { e with eexpr = TCall(mk_static_field_access_infer string_ext field e.epos [], [run ef] @ (List.map run args)) }
+        | TCall( ( { eexpr = TField(ef, field) } as efield ), args ) when is_string ef.etype ->
+          (match field with
+            | "charAt" | "charCodeAt" | "split" | "indexOf"
+            | "lastIndexOf" | "substring" | "substr" ->
+              { e with eexpr = TCall(mk_static_field_access_infer string_ext field e.epos [], [run ef] @ (List.map run args)) }
+            | _ when String.get field 0 = '_' ->
+              { e with eexpr = TCall({ efield with eexpr = TField(run ef, String.sub field 1 ( (String.length field) - 1)) }, List.map run args) }
+            | _ ->
+              { e with eexpr = TCall(run efield, List.map run args) }
+          )
         
         
         | TCast(expr, m) when is_boxed_type e.etype ->
         | TCast(expr, m) when is_boxed_type e.etype ->
           (* let unboxed_type gen t tbyte tshort tchar tfloat = match follow t with *)
           (* let unboxed_type gen t tbyte tshort tchar tfloat = match follow t with *)
@@ -1764,6 +1766,10 @@ let configure gen =
   JavaSpecificSynf.configure gen (JavaSpecificSynf.traverse gen runtime_cl);
   JavaSpecificSynf.configure gen (JavaSpecificSynf.traverse gen runtime_cl);
   JavaSpecificESynf.configure gen (JavaSpecificESynf.traverse gen runtime_cl);
   JavaSpecificESynf.configure gen (JavaSpecificESynf.traverse gen runtime_cl);
   
   
+  (* add native String as a String superclass *)
+  let str_cl = match gen.gcon.basic.tstring with | TInst(cl,_) -> cl | _ -> assert false in
+  str_cl.cl_super <- Some (get_cl (get_type gen (["haxe";"lang"], "NativeString")), []);
+  
   run_filters gen;
   run_filters gen;
   
   
   TypeParams.RenameTypeParameters.run gen;
   TypeParams.RenameTypeParameters.run gen;

+ 17 - 24
std/StringTools.hx

@@ -106,12 +106,9 @@ class StringTools {
 	/**
 	/**
 		Tells if the string [s] starts with the string [start].
 		Tells if the string [s] starts with the string [start].
 	**/
 	**/
-	#if java
-	@:functionBody('return s.startsWith(start);')
-	#end
-	public static #if (cs) inline #end function startsWith( s : String, start : String ) : Bool {
+	public static #if (cs || java) inline #end function startsWith( s : String, start : String ) : Bool {
 		#if java
 		#if java
-		return false;
+		return untyped s.startsWith(start);
 		#elseif cs
 		#elseif cs
 		return untyped s.StartsWith(start);
 		return untyped s.StartsWith(start);
 		#else
 		#else
@@ -122,12 +119,9 @@ class StringTools {
 	/**
 	/**
 		Tells if the string [s] ends with the string [end].
 		Tells if the string [s] ends with the string [end].
 	**/
 	**/
-	#if java
-	@:functionBody('return s.endsWith(end);')
-	#end
-	public static #if (cs) inline #end function endsWith( s : String, end : String ) : Bool {
+	public static #if (cs || java) inline #end function endsWith( s : String, end : String ) : Bool {
 		#if java
 		#if java
-		return false;
+		return untyped s.endsWith(end);
 		#elseif cs
 		#elseif cs
 		return untyped s.EndsWith(end);
 		return untyped s.EndsWith(end);
 		#else
 		#else
@@ -191,11 +185,13 @@ class StringTools {
 	/**
 	/**
 		Removes spaces at the beginning and the end of the String [s].
 		Removes spaces at the beginning and the end of the String [s].
 	**/
 	**/
-	public #if (php || cs) inline #end static function trim( s : String ) : String {
+	public #if (php || cs || java) inline #end static function trim( s : String ) : String {
 		#if php
 		#if php
 		return untyped __call__("trim", s);
 		return untyped __call__("trim", s);
 		#elseif cs
 		#elseif cs
 		return untyped s.Trim();
 		return untyped s.Trim();
+		#elseif java
+		return untyped s.trim();
 		#else
 		#else
 		return ltrim(rtrim(s));
 		return ltrim(rtrim(s));
 		#end
 		#end
@@ -251,10 +247,7 @@ class StringTools {
 	/**
 	/**
 		Replace all occurences of the string [sub] in the string [s] by the string [by].
 		Replace all occurences of the string [sub] in the string [s] by the string [by].
 	**/
 	**/
-	#if java
-	@:functionBody('return s.replace(sub, by);')
-	#end
-	public #if (php) inline #end static function replace( s : String, sub : String, by : String ) : String {
+	public #if (php || java || cs) inline #end static function replace( s : String, sub : String, by : String ) : String {
 		#if php
 		#if php
 		return untyped __call__("str_replace", sub, by, s);
 		return untyped __call__("str_replace", sub, by, s);
 		#elseif java
 		#elseif java
@@ -292,7 +285,7 @@ class StringTools {
 		Provides a fast native string charCodeAt access. Since the EOF value might vary depending on the platforms, always test with StringTools.isEOF.
 		Provides a fast native string charCodeAt access. Since the EOF value might vary depending on the platforms, always test with StringTools.isEOF.
 		Only guaranteed to work if index in [0,s.length] range. Might not work with strings containing \0 char.
 		Only guaranteed to work if index in [0,s.length] range. Might not work with strings containing \0 char.
 	**/
 	**/
-		public static #if !cs inline #end function fastCodeAt( s : String, index : Int ) : Int untyped {
+	public static inline function fastCodeAt( s : String, index : Int ) : Int untyped {
 		#if neko
 		#if neko
 		return untyped __dollar__sget(s.__s, index);
 		return untyped __dollar__sget(s.__s, index);
 		#elseif cpp
 		#elseif cpp
@@ -302,12 +295,9 @@ class StringTools {
 		#elseif flash
 		#elseif flash
 		return s["cca"](index);
 		return s["cca"](index);
 		#elseif java
 		#elseif java
-		return cast s.charCodeAt(index);
+		return ( index < s.length ) ? cast(_charAt(s, index), Int) : -1;
 		#elseif cs
 		#elseif cs
-		if (cast(index, UInt) >= s.length)
-			return 0;
-		else
-			return cast(untyped s[index], Int);
+		return ( cast(index, UInt) < s.length ) ? cast(untyped s[index], Int) : -1;
 		#elseif js
 		#elseif js
 			#if mt
 			#if mt
 		return (untyped s).cca(index);
 		return (untyped s).cca(index);
@@ -318,7 +308,10 @@ class StringTools {
 		return s.cca(index);
 		return s.cca(index);
 		#end
 		#end
 	}
 	}
-
+	
+	#if java
+	private static inline function _charAt(str:String, idx:Int):java.StdTypes.Char16 return untyped str._charAt(idx)
+	#end
 	/*
 	/*
 		Only to use together with fastCodeAt.
 		Only to use together with fastCodeAt.
 	*/
 	*/
@@ -332,9 +325,9 @@ class StringTools {
 		#elseif neko
 		#elseif neko
 		return c == null;
 		return c == null;
 		#elseif cs
 		#elseif cs
-		return c == 0;
+		return c == -1;
 		#elseif java
 		#elseif java
-		return c == 0;
+		return c == -1;
 		#else
 		#else
 		return false;
 		return false;
 		#end
 		#end