Kaynağa Gözat

applies patch from Greg and fixes issue #1123

Franco Ponticelli 12 yıl önce
ebeveyn
işleme
8bdd5ddce9
2 değiştirilmiş dosya ile 20 ekleme ve 1 silme
  1. 15 0
      genphp.ml
  2. 5 1
      std/php/Boot.hx

+ 15 - 0
genphp.ml

@@ -218,6 +218,21 @@ let as_string_expr ctx e =
 	| _ when not (is_string_expr e) ->
 		to_string ctx e
 	| _ -> e
+(* for known String type that could have null value *)
+let to_string_null ctx e = 
+	let v = alloc_var "__call__" t_dynamic in
+	let f = mk (TLocal v) t_dynamic e.epos in
+	mk (TCall (f, [ Codegen.string ctx.com "_hx_string_or_null" e.epos; e])) ctx.com.basic.tstring e.epos
+	
+	
+let as_string_expr ctx e =	match e.eexpr with
+	| TConst (TNull) ->  to_string ctx e
+	| TConst (TString s) -> e
+	| TBinop (op,_,_) when (is_string_expr e)-> e
+	| TCall ({eexpr = TField({eexpr = TTypeExpr(TClassDecl {cl_path = ([],"Std")})},FStatic(c,f) )}, [_]) when (f.cf_name="string") -> e
+	| TCall ({eexpr = TLocal _}, [{eexpr = TConst (TString ("_hx_string_rec" | "_hx_str_or_null"))}]) -> e
+	| _ when not (is_string_expr e) -> to_string ctx e
+	| _ -> to_string_null ctx e
 
 let spr ctx s = Buffer.add_string ctx.buf s
 let print ctx = Printf.kprintf (fun s -> Buffer.add_string ctx.buf s)

+ 5 - 1
std/php/Boot.hx

@@ -32,7 +32,7 @@ class Boot {
 		untyped __php__("
 function _hx_add($a, $b) {
 	if (!_hx_is_numeric($a) || !_hx_is_numeric($b)) {
-		return $a . $b;
+		return _hx_string_or_null($a) . _hx_string_or_null($b);
 	} else {
 		return $a + $b;
 	}
@@ -547,6 +547,10 @@ function _hx_string_call($s, $method, $params) {
 	}
 }
 
+function _hx_string_or_null($s) {
+	return $s === null ? 'null' : $s;
+}
+
 function _hx_string_rec($o, $s) {
 	if($o === null)                return 'null';
 	if(strlen($s) >= 5)            return '<...>';