Browse Source

php adds __toString to classes that have toString defined

Franco Ponticelli 17 years ago
parent
commit
6457f14a42
3 changed files with 24 additions and 55 deletions
  1. 1 0
      doc/CHANGES.txt
  2. 12 20
      genphp.ml
  3. 11 35
      std/php/Boot.hx

+ 1 - 0
doc/CHANGES.txt

@@ -35,6 +35,7 @@ TODO inlining : substitute class+function type parameters in order to have fully
 	fixed blocks in if statements for haXe/PHP
 	added php check on the full hierarchy for colliding names
 	added support for "g" modifier in EReg for PHP
+	PHP now generates __toString for classes that have toString defined
 
 2008-07-28: 2.0
 	fixed current package bug in inherited constructor type

+ 12 - 20
genphp.ml

@@ -22,6 +22,7 @@ TODO
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *)
+open Ast
 open Type
 open Common
 
@@ -998,26 +999,11 @@ and gen_expr ctx e =
 		| Ast.OpAssignOp(Ast.OpAdd) when (is_string_expr e1 || is_string_expr e2) ->
 			gen_value_op ctx e1;
 			spr ctx " .= ";
-			if is_uncertain_expr e2 then begin
-				spr ctx "php_Boot::__string(";
-				gen_value_op ctx e2;
-				spr ctx ")";
-			end else
-				gen_value_op ctx e2;
+			gen_value_op ctx e2;
 		| Ast.OpAdd when (is_string_expr e1 || is_string_expr e2) ->
-			if is_uncertain_expr e1 then begin
-				spr ctx "php_Boot::__string(";
-				gen_value_op ctx e1;
-				spr ctx ")";
-			end else
-				gen_value_op ctx e1;
+			gen_value_op ctx e1;
 			spr ctx " . ";
-			if is_uncertain_expr e2 then begin
-				spr ctx "php_Boot::__string(";
-				gen_value_op ctx e2;
-				spr ctx ")";
-			end else
-				gen_value_op ctx e2;
+			gen_value_op ctx e2;
 		| Ast.OpAssignOp(Ast.OpShl) ->
 			gen_value_op ctx e1;
 			spr ctx " <<= ";
@@ -1734,7 +1720,7 @@ let rec super_has_dynamic c =
 	| Some (csup, _) -> (match csup.cl_dynamic with
 		| Some _ -> true
 		| _ -> super_has_dynamic csup)
-
+		
 let generate_class ctx c =
 	let requires_constructor = ref true in
 	ctx.curclass <- c;
@@ -1755,7 +1741,7 @@ let generate_class ctx c =
 		concat ctx ", " (fun (i,_) ->
 		print ctx "%s" (s_path ctx i.cl_path i.cl_extern c.cl_pos)) l);
 	spr ctx "{";
-
+	
 	let get_dynamic_methods = List.filter is_dynamic_method c.cl_ordered_fields in
 
 	if not ctx.curclass.cl_interface then ctx.dynamic_methods <- get_dynamic_methods;
@@ -1794,6 +1780,12 @@ let generate_class ctx c =
 
 	cl();
 	newline ctx;
+	
+	if PMap.exists "toString" c.cl_fields then begin
+			print ctx "\tfunction __toString() { return $this->toString(); }";
+			newline ctx;
+	end;
+	
 	print ctx "}"
 
 let createmain com c =

+ 11 - 35
std/php/Boot.hx

@@ -21,7 +21,6 @@ class Boot {
 	static public function __closure(locals : ArrayAccess<Dynamic>, params : String, body : String) : String {
 		var cid = __cid++;
 		var n = "__closure__"+cid+"__";
-//		if(locals == null) locals = [];
 		untyped __php__("php_Boot::$__scopes[$n] = array('scope' => null, 'locals' => $locals)");
 		var f : String = untyped __call__(
 			"create_function",
@@ -414,7 +413,17 @@ class Anonymous extends stdClass{
 	}
 
 	public function __toString() {
-		return php_Boot::__string_rec($this, '');
+		$rfl = new ReflectionObject($this);
+		$b = '{ ';
+		$properties = $rfl->getProperties();
+		foreach($properties as $prop) {
+			$f = $prop->getName();
+			if(strlen($b) > 2)
+				$b .= ', ';
+			$b .= $f . ' => ' . $prop->getValue($this);
+		}
+		$b .= ' }';
+		return $b;
 	}
 }
 
@@ -613,39 +622,6 @@ function __haxe_autoload($name) {
 spl_autoload_register('__haxe_autoload')");
 	}
 
-	static public function __string(o : Dynamic) {
-		if( o == null )
-			return "null";
-		if(untyped __call__("is_int", o) || __call__("is_float", o))
-			return o;
-		if(untyped __call__("is_bool", o))
-			return o ? "true" : "false";
-		if(untyped __call__("is_object", o)) {
-			var c = untyped __call__("get_class", o);
-			if(untyped __php__("$o instanceof Anonymous")) {
-				return "Object";
-			} else if(untyped __php__("$o instanceof __type__")) {
-				return untyped __qtype(o.__qname__);
-			} else {
-				if(untyped __call__("is_callable", [o, "__toString"]))
-					return o.__toString();
-				else
-					return "[" + __ttype(c) + "]";
-			}
-		}
-
-		if(untyped __call__("is_string", o)) {
-			if(__is_lambda(o)) return "«function»";
-			return o;
-		}
-		if(untyped __call__("is_array", o)) {
-			if(untyped __call__("is_callable", o)) return "«function»";
-			return "Array";
-		}
-
-		return '';
-	}
-
 	static public function __string_rec(o : Dynamic, s : String) {
 		if( o == null )
 			return "null";