Explorar el Código

fixed php.Web.setCookie() for expire time
fixed php.Sys.sleep()
compilers now generates php.Boot.__field instead of Reflect.field
fixed php.net.Socket.setTimeout()

Franco Ponticelli hace 17 años
padre
commit
a797f06f20
Se han modificado 7 ficheros con 111 adiciones y 97 borrados
  1. 2 0
      doc/CHANGES.txt
  2. 2 2
      genphp.ml
  3. 10 75
      std/Reflect.hx
  4. 74 1
      std/php/Boot.hx
  5. 6 2
      std/php/Sys.hx
  6. 9 9
      std/php/Web.hx
  7. 8 8
      std/php/net/Socket.hx

+ 2 - 0
doc/CHANGES.txt

@@ -7,6 +7,8 @@ TODO inlining : allow inlined getter/setter
 TODO inlining : substitute class+function type parameters in order to have fully typed expressions
 
 2008-??-??: 2.01
+	fixed php.Web.setCookie() for expire time
+	fixed php.net.Socket.setTimeout()
 	fixed SPOD/MySql for PHP
 	fixed Type.enumParameters() and Type.typeOf() for PHP
 	fixed null references in class constructors for array arguments

+ 2 - 2
genphp.ml

@@ -1041,7 +1041,7 @@ and gen_expr ctx e =
 			then begin	
 				(match e1.eexpr with 
 				| TField (f, s) when is_anonym_expr e1 || is_unknown_expr e1 ->
-					spr ctx "Reflect::field(";
+					spr ctx "php_Boot::__field(";
 					gen_value ctx f;
 					print ctx ", \"%s\")" s;
 				| _ ->
@@ -1051,7 +1051,7 @@ and gen_expr ctx e =
 				
 				(match e2.eexpr with 
 				| TField (f, s) when is_anonym_expr e2 || is_unknown_expr e2 ->
-					spr ctx "Reflect::field(";
+					spr ctx "php_Boot::__field(";
 					gen_value ctx f;
 					print ctx ", \"%s\")" s;
 				| _ ->

+ 10 - 75
std/Reflect.hx

@@ -32,7 +32,7 @@ class Reflect {
 	/**
 		Tells if an object has a field set. This doesn't take into account the object prototype (class methods).
 	**/
-	public static function hasField( o : Dynamic, field : String ) : Bool untyped {
+	public #if php inline #end static function hasField( o : Dynamic, field : String ) : Bool untyped {
 		#if flash9
 			return o.hasOwnProperty( field );
 		#elseif flash
@@ -47,13 +47,7 @@ class Reflect {
 		#elseif neko
 			return __dollar__typeof(o) == __dollar__tobject && __dollar__objfield(o,__dollar__hash(field.__s));
 		#elseif php
-			return __php__("
-			(is_object($o) && (method_exists($o, $field) || isset($o->$field) || property_exists($o, $field)))
-			||
-			(is_string($o) && (in_array($field, array('toUpperCase', 'toLowerCase', 'charAt', 'charCodeAt', 'indexOf', 'lastIndexOf', 'split', 'substr', 'toString', 'length'))))
-			||
-			(is_array($o)  && (in_array($field, array('concat', 'copy', 'insert', 'iterator', 'join', 'pop', 'push', 'remove', 'reverse', 'shift', 'slice', 'sort', 'splice', 'unshift', 'toString', 'length'))))
-			");
+			return php.Boot.__has_field(o, field);
 		#else
 			return false;
 		#end
@@ -62,7 +56,7 @@ class Reflect {
 	/**
 		Returns the field of an object, or null if [o] is not an object or doesn't have this field.
 	**/
-	public #if !php inline #end static function field( o : Dynamic, field : String ) : Dynamic untyped {
+	public inline static function field( o : Dynamic, field : String ) : Dynamic untyped {
 		#if flash9
 			return (o == null) ? null : o[field];
 		#elseif flash
@@ -77,66 +71,7 @@ class Reflect {
 		#elseif neko
 			return if( __dollar__typeof(o) != __dollar__tobject ) null else __dollar__objget(o,__dollar__hash(field.__s));
 		#elseif php
-			if(hasField(o, field)) {
-				if(__php__("$o instanceof __type__")) {
-					if(__php__("is_callable(array($o->__tname__, $field))")) {
-						return __php__("array($o->__tname__, $field)");
-					} else {
-						return __php__("eval('return '.$o->__tname__.'::$'.$field.';')");
-					}
-				} else if(__call__("is_string", o)) {
-					if(field == 'length')
-						return php.Boot.__len(o);
-					else {
-						switch(field) {
-							case 'charAt':      return php.Boot.__closure(__php__("array('o' => $o)"), '$index', 'return substr($o, $index,1 );');
-							case 'charCodeAt':  return php.Boot.__closure(__php__("array('o' => $o)"), '$index', 'return ord(substr($o, $index, 1));');
-							case 'indexOf':     return php.Boot.__closure(__php__("array('o' => $o)"), '$value,$startIndex', 'return php_Boot::__index_of($o, $value, $startIndex);');
-							case 'lastIndexOf': return php.Boot.__closure(__php__("array('o' => $o)"), '$value,$startIndex', 'return php_Boot::__last_index_of($o, $value, $startIndex);');
-							case 'split':       return php.Boot.__closure(__php__("array('o' => $o)"), '$delimiter', 'return explode($delimiter, $o);');
-							case 'substr':      return php.Boot.__closure(__php__("array('o' => $o)"), '$pos,$len', 'return php_Boot::__substr($o, $pos, $len);');
-							case 'toUpperCase': return php.Boot.__closure(__php__("array('o' => $o)"), '', 'return strtoupper($o);');
-							case 'toLowerCase': return php.Boot.__closure(__php__("array('o' => $o)"), '', 'return strtolower($o);');
-							case 'toString':    return php.Boot.__closure(__php__("array('o' => $o)"), '', 'return $o;');
-						}
-						return null;
-					}
-				} else if(__call__("is_array", o)) {
-					if(field == 'length')
-						return php.Boot.__len(o);
-					else
-						switch(field) {
-							case 'concat':   return php.Boot.__closure(__php__("array('o' => &$o)"), '$a', 'return array_merge($o, $a);');
-							case 'join':     return php.Boot.__closure(__php__("array('o' => &$o)"), '$sep', 'return join($sep, $o);');
-							case 'pop':      return php.Boot.__closure(__php__("array('o' => &$o)"), '', 'return array_pop($o);');
-							case 'push':     return php.Boot.__closure(__php__("array('o' => &$o)"), '$x', 'return array_push($o, $x);');
-							case 'reverse':  return php.Boot.__closure(__php__("array('o' => &$o)"), '', 'return rsort($o);');
-							case 'shift':    return php.Boot.__closure(__php__("array('o' => &$o)"), '', 'return array_shift($o);');
-							case 'slice':    return php.Boot.__closure(__php__("array('o' => &$o)"), '$pos,$end', 'return php_Boot::__array_slice(array(&$o), $pos, $end);');
-							case 'sort':     return php.Boot.__closure(__php__("array('o' => &$o)"), '$f', 'return php_Boot::__array_sort($o, $f);');
-							case 'splice':   return php.Boot.__closure(__php__("array('o' => &$o)"), '$pos,$len', 'return php_Boot::__array_splice(array(&$o), $pos, $len);');
-							case 'toString': return php.Boot.__closure(__php__("array('o' => &$o)"), '', 'return "[".join(", ", $o)."]";');
-							case 'unshift':  return php.Boot.__closure(__php__("array('o' => &$o)"), '$x', 'return array_unshift($o, $x);');
-							case 'insert':   return php.Boot.__closure(__php__("array('o' => &$o)"), '$pos,$x', 'return php_Boot::__array_insert(array(&$o), $pos, $x);');
-							case 'remove':   return php.Boot.__closure(__php__("array('o' => &$o)"), '$x', 'return php_Boot::__array_remove(array(&$o), $x);');
-							case 'iterator': return php.Boot.__closure(__php__("array('o' => &$o)"), '', 'return new HArrayIterator($o);');
-							case 'copy':     return php.Boot.__closure(__php__("array('o' => $o)"), '', 'return $o;');
-						}
-					return null;
-				} else if(__php__("property_exists($o, $field)")) {
-					if(__php__("is_array($o->$field) && is_callable($o->$field)")) {
-						return __php__("$o->$field");
-					} else if(__php__("is_string($o->$field) && php_Boot::__is_lambda($o->$field)")) {
-						return __php__("array($o, $field)");
-					} else {
-						return __php__("$o->$field");
-					}
-				} else {
-					return __php__("array($o, $field)");
-				}
-			} else {
-				return null;
-			}
+			return php.Boot.__field(o, field);
 		#else
 			return null;
 		#end
@@ -243,10 +178,10 @@ class Reflect {
 				return Array.new1(a,l);
 			}
 		#elseif php
-			return __call__('is_array', o) 
-					? __call__('array', 'concat', 'copy', 'insert', 'iterator', 'length', 'join', 'pop', 'push', 'remove', 'reverse', 'shift', 'slice', 'sort', 'splice', 'toString', 'unshift') 
-					: (__call__('is_string', o) 
-						? __call__('array', 'charAt', 'charCodeAt', 'indexOf', 'lastIndexOf', 'length', 'split', 'substr', 'toLowerCase', 'toString', 'toUpperCase') 
+			return __call__('is_array', o)
+					? __call__('array', 'concat', 'copy', 'insert', 'iterator', 'length', 'join', 'pop', 'push', 'remove', 'reverse', 'shift', 'slice', 'sort', 'splice', 'toString', 'unshift')
+					: (__call__('is_string', o)
+						? __call__('array', 'charAt', 'charCodeAt', 'indexOf', 'lastIndexOf', 'length', 'split', 'substr', 'toLowerCase', 'toString', 'toUpperCase')
 						: __call__('array_keys', __call__('get_object_vars', o)));
 		#else
 			return new Array();
@@ -415,9 +350,9 @@ class Reflect {
 		#elseif flash
 			return function() { return f(untyped __arguments__); };
 		#elseif php
-			return function() { 
+			return function() {
 				var args = untyped __call__("func_get_args");
-				return f(args); 
+				return f(args);
 			};
 		#else
 			return null;

+ 74 - 1
std/php/Boot.hx

@@ -3,7 +3,7 @@ package php;
 class Boot {
 	public static function __trace(v,i : haxe.PosInfos) {
 		var msg = if( i != null ) i.fileName+":"+i.lineNumber+": " else "";
-		untyped __call__("echo", msg+ __string_rec(v)+"<br/>"); // TODO: __unhtml
+		untyped __call__("echo", msg+ __string_rec(v)+"\n"); // TODO: __unhtml
 	}
 
 	static public function __anonymous(?p : Dynamic) : Dynamic {
@@ -215,6 +215,79 @@ class Boot {
 		}
 	}
 
+	static public function __has_field( o : Dynamic, field : String ) : Bool {
+		return untyped __php__("
+			(is_object($o) && (method_exists($o, $field) || isset($o->$field) || property_exists($o, $field)))
+			||
+			(is_string($o) && (in_array($field, array('toUpperCase', 'toLowerCase', 'charAt', 'charCodeAt', 'indexOf', 'lastIndexOf', 'split', 'substr', 'toString', 'length'))))
+			||
+			(is_array($o)  && (in_array($field, array('concat', 'copy', 'insert', 'iterator', 'join', 'pop', 'push', 'remove', 'reverse', 'shift', 'slice', 'sort', 'splice', 'unshift', 'toString', 'length'))))
+		");
+	}
+
+	static public function __field( o : Dynamic, field : String) : Dynamic untyped {
+		if(__has_field(o, field)) {
+			if(__php__("$o instanceof __type__")) {
+				if(__php__("is_callable(array($o->__tname__, $field))")) {
+					return __php__("array($o->__tname__, $field)");
+				} else {
+					return __php__("eval('return '.$o->__tname__.'::$'.$field.';')");
+				}
+			} else if(__call__("is_string", o)) {
+				if(field == 'length')
+					return php.Boot.__len(o);
+				else {
+					switch(field) {
+						case 'charAt':      return php.Boot.__closure(__php__("array('o' => $o)"), '$index', 'return substr($o, $index,1 );');
+						case 'charCodeAt':  return php.Boot.__closure(__php__("array('o' => $o)"), '$index', 'return ord(substr($o, $index, 1));');
+						case 'indexOf':     return php.Boot.__closure(__php__("array('o' => $o)"), '$value,$startIndex', 'return php_Boot::__index_of($o, $value, $startIndex);');
+						case 'lastIndexOf': return php.Boot.__closure(__php__("array('o' => $o)"), '$value,$startIndex', 'return php_Boot::__last_index_of($o, $value, $startIndex);');
+						case 'split':       return php.Boot.__closure(__php__("array('o' => $o)"), '$delimiter', 'return explode($delimiter, $o);');
+						case 'substr':      return php.Boot.__closure(__php__("array('o' => $o)"), '$pos,$len', 'return php_Boot::__substr($o, $pos, $len);');
+						case 'toUpperCase': return php.Boot.__closure(__php__("array('o' => $o)"), '', 'return strtoupper($o);');
+						case 'toLowerCase': return php.Boot.__closure(__php__("array('o' => $o)"), '', 'return strtolower($o);');
+						case 'toString':    return php.Boot.__closure(__php__("array('o' => $o)"), '', 'return $o;');
+					}
+					return null;
+				}
+			} else if(__call__("is_array", o)) {
+				if(field == 'length')
+					return php.Boot.__len(o);
+				else
+					switch(field) {
+						case 'concat':   return php.Boot.__closure(__php__("array('o' => &$o)"), '$a', 'return array_merge($o, $a);');
+						case 'join':     return php.Boot.__closure(__php__("array('o' => &$o)"), '$sep', 'return join($sep, $o);');
+						case 'pop':      return php.Boot.__closure(__php__("array('o' => &$o)"), '', 'return array_pop($o);');
+						case 'push':     return php.Boot.__closure(__php__("array('o' => &$o)"), '$x', 'return array_push($o, $x);');
+						case 'reverse':  return php.Boot.__closure(__php__("array('o' => &$o)"), '', 'return rsort($o);');
+						case 'shift':    return php.Boot.__closure(__php__("array('o' => &$o)"), '', 'return array_shift($o);');
+						case 'slice':    return php.Boot.__closure(__php__("array('o' => &$o)"), '$pos,$end', 'return php_Boot::__array_slice(array(&$o), $pos, $end);');
+						case 'sort':     return php.Boot.__closure(__php__("array('o' => &$o)"), '$f', 'return php_Boot::__array_sort($o, $f);');
+						case 'splice':   return php.Boot.__closure(__php__("array('o' => &$o)"), '$pos,$len', 'return php_Boot::__array_splice(array(&$o), $pos, $len);');
+						case 'toString': return php.Boot.__closure(__php__("array('o' => &$o)"), '', 'return "[".join(", ", $o)."]";');
+						case 'unshift':  return php.Boot.__closure(__php__("array('o' => &$o)"), '$x', 'return array_unshift($o, $x);');
+						case 'insert':   return php.Boot.__closure(__php__("array('o' => &$o)"), '$pos,$x', 'return php_Boot::__array_insert(array(&$o), $pos, $x);');
+						case 'remove':   return php.Boot.__closure(__php__("array('o' => &$o)"), '$x', 'return php_Boot::__array_remove(array(&$o), $x);');
+						case 'iterator': return php.Boot.__closure(__php__("array('o' => &$o)"), '', 'return new HArrayIterator($o);');
+						case 'copy':     return php.Boot.__closure(__php__("array('o' => $o)"), '', 'return $o;');
+					}
+				return null;
+			} else if(__php__("property_exists($o, $field)")) {
+				if(__php__("is_array($o->$field) && is_callable($o->$field)")) {
+					return __php__("$o->$field");
+				} else if(__php__("is_string($o->$field) && php_Boot::__is_lambda($o->$field)")) {
+					return __php__("array($o, $field)");
+				} else {
+					return __php__("$o->$field");
+				}
+			} else {
+				return __php__("array($o, $field)");
+			}
+		} else {
+			return null;
+		}
+	}
+
 	static private var __qtypes : Array<Dynamic>;
 	static private var __ttypes : Array<Dynamic>;
 	static private var __tpaths : Array<Dynamic>;

+ 6 - 2
std/php/Sys.hx

@@ -15,7 +15,7 @@ class Sys {
 	}
 
 	public static function sleep( seconds : Float ) {
-		return untyped __call__("sleep", seconds);
+		return untyped __call__("usleep", seconds*1000000);
 	}
 
 	public static function setTimeLocale( loc : String ) : Bool {
@@ -61,7 +61,11 @@ class Sys {
 			for( a in args )
 				cmd += " "+escapeArgument(a);
 		}
-		return untyped __call__("system", cmd);
+		var result = 0;
+		var output = "";
+//		untyped __call__("exec", cmd, output, result);
+		untyped __call__("system", cmd, result);
+		return result;
 	}
 
 	public static function exit( code : Int ) {

+ 9 - 9
std/php/Web.hx

@@ -87,7 +87,7 @@ class Web {
 		See status code explanation here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
 	**/
 	public static function setReturnCode( r : Int ) {
-		var code : String; 
+		var code : String;
 		switch(r) {
 			case 100: code = "100 Continue";
 			case 101: code = "101 Switching Protocols";
@@ -145,7 +145,7 @@ class Web {
 		return null;
 	}
 
-	
+
 	private static var _client_headers : List<{header : String, value : String}>;
 	/**
 		Retrieve all the client headers.
@@ -153,7 +153,7 @@ class Web {
 	public static function getClientHeaders() {
 		if(_client_headers == null) {
 			_client_headers = new List();
-			var h = Lib.hashOfAssociativeArray(untyped __php__("$_SERVER"));			
+			var h = Lib.hashOfAssociativeArray(untyped __php__("$_SERVER"));
 			for(k in h.keys()) {
 				if(k.substr(0,5) == "HTTP_") {
 					_client_headers.add({ header : k.substr(5), value : h.get(k)});
@@ -211,7 +211,7 @@ class Web {
 		Set a Cookie value in the HTTP headers. Same remark as setHeader.
 	**/
 	public static function setCookie( key : String, value : String, ?expire: Date, ?domain: String, ?path: String, ?secure: Bool ) {
-		var t = expire == null ? 0 : (expire.getTime()*1000.0);
+		var t = expire == null ? 0 : (expire.getTime()/1000.0);
 		if(path == null) path = '';
 		if(domain == null) domain = '';
 		if(secure == null) secure = false;
@@ -280,7 +280,7 @@ class Web {
 			var tmp : String = untyped info['tmp_name'];
 			var file : String = untyped info['name'];
 			var err : Int = untyped info['error'];
-			
+
 			if(err > 0) {
 				switch(err) {
 					case 1: throw "The uploaded file exceeds the max size of " + untyped __call__('ini_get', 'upload_max_filesize');
@@ -314,19 +314,19 @@ class Web {
 	public static inline function flush() : Void {
 		untyped __call__("flush");
 	}
-	
+
 	/**
 		Get the HTTP method used by the client.
 	**/
 	public static function getMethod() : String {
-		if(untyped __php__("isset($_SERVER['REQUEST_METHOD'])")) 
+		if(untyped __php__("isset($_SERVER['REQUEST_METHOD'])"))
 			return untyped __php__("$_SERVER['REQUEST_METHOD']");
 		else
 			return null;
 	}
-	
+
 	public static var isModNeko(default,null) : Bool;
-	
+
 	static function __init__() {
 		isModNeko = !php.Lib.isCli();
 	}

+ 8 - 8
std/php/net/Socket.hx

@@ -37,13 +37,13 @@ class Socket {
 	public var custom : Dynamic;
 
 	public var isUdp(default, null) : Bool;
-	
+
 	public function new( ?s ) {
 		__s = s;
 		input = new SocketInput(__s);
 		output = new SocketOutput(__s);
 	}
-	
+
 	private function assignHandler() {
 		untyped input.__f = __s;
 		untyped output.__f = __s;
@@ -114,7 +114,7 @@ class Socket {
 			return { host : new Host(parts[1].substr(2)), port : Std.parseInt(parts[2]) };
 		}
 	}
-	
+
 	public function peer() : { host : Host, port : Int } {
 		var r : String = untyped __call__('stream_socket_get_name', __s, true);
 		checkError(cast r, 0, 'Unable to retrieve the peer name');
@@ -129,7 +129,7 @@ class Socket {
 
 	public function setTimeout( timeout : Float ) {
 		var s = Std.int(timeout);
-		var ms = Std.int((timeout % 1) * 100000);
+		var ms = Std.int((timeout-s)*1000000);
 		var r = untyped __call__('stream_set_timeout', __s, s, ms);
 		checkError(r, 0, 'Unable to set timeout');
 	}
@@ -145,19 +145,19 @@ class Socket {
 		untyped s.isUdp = true;
 		return s;
 	}
-	
+
 	private static function checkError(r : Bool, code : Int, msg : String) {
 		if(!untyped __physeq__(r, false)) return;
 		throw haxe.io.Error.Custom('Error ['+code+']: ' +msg);
 	}
-	
+
 	private static function getType(isUdp : Bool) : Int {
 		return isUdp ? untyped __php__('SOCK_DGRAM') : untyped __php__('SOCK_STREAM');
 	}
-	
+
 	private static function getProtocol(isUdp : Bool) : Int {
 		return isUdp ? untyped __call__('getprotobyname', 'udp') : untyped __call__('getprotobyname', 'tcp');
-	}	
+	}
 }
 
 enum SocketDomain {