ソースを参照

[php7] get rid of untyped code in sys.*

Alexander Kuzmenko 8 年 前
コミット
024565269c

+ 28 - 0
std/php7/Const.hx

@@ -137,6 +137,34 @@ extern class Const {
 	static var STR_PAD_RIGHT : Int;
 	static var STR_PAD_LEFT : Int;
 	static var STR_PAD_BOTH : Int;
+	/**
+		@see http://php.net/manual/en/function.feof.php
+	**/
+	static var SEEK_SET : Int;
+	static var SEEK_CUR : Int;
+	static var SEEK_END : Int;
+	/**
+		@see http://php.net/manual/en/function.stream-socket-server.php
+	**/
+	static var STREAM_SERVER_BIND : Int;
+	static var STREAM_SERVER_LISTEN : Int;
+	/**
+		@see http://php.net/manual/en/function.stream-socket-client.php
+	**/
+	static var STREAM_CLIENT_CONNECT : Int;
+	static var STREAM_CLIENT_ASYNC_CONNECT : Int;
+	static var STREAM_CLIENT_PERSISTENT : Int;
+	/**
+		@see http://php.net/manual/en/function.socket-create.php
+	**/
+	static var SOCK_STREAM : Int;
+	static var SOCK_DGRAM : Int;
+	static var SOCK_SEQPACKET : Int;
+	static var SOCK_RAW : Int;
+	static var SOCK_RDM : Int;
+	static var AF_INET : Int;
+	static var AF_INET6 : Int;
+	static var AF_UNIX : Int;
 	/**
 		@see http://php.net/manual/en/json.constants.php
 	**/

+ 85 - 0
std/php7/Global.hx

@@ -284,6 +284,11 @@ extern class Global {
 	**/
 	static function echo( args:Rest<String> ) : Void;
 
+	/**
+		@see http://php.net/manual/en/function.sprintf.php
+	**/
+	static function sprintf( format:String, args:Rest<Dynamic> ) : EitherType<String,Bool>;
+
 	/**
 		@see http://php.net/manual/en/function.method-exists.php
 	**/
@@ -606,6 +611,16 @@ extern class Global {
 	**/
 	static function feof( handle:Resource ) : Bool;
 
+	/**
+		@see http://php.net/manual/en/function.fseek.php
+	**/
+	static function fseek( handle:Resource, offset:Int, ?whence:Int ) : EitherType<Int,Bool>;
+
+	/**
+		@see http://php.net/manual/en/function.ftell.php
+	**/
+	static function ftell( handle:Resource ) : EitherType<Int,Bool>;
+
 	/**
 		@see http://php.net/manual/en/function.rewind.php
 	**/
@@ -616,6 +631,16 @@ extern class Global {
 	**/
 	static function fgetc( handle:Resource ) : EitherType<Bool,String>;
 
+	/**
+		@see http://php.net/manual/en/function.fgets.php
+	**/
+	static function fgets( handle:Resource ) : EitherType<Bool,String>;
+
+	/**
+		@see http://php.net/manual/en/function.fflush.php
+	**/
+	static function fflush( handle:Resource ) : Bool;
+
 	/**
 		@see http://php.net/manual/en/function.fwrite.php
 	**/
@@ -631,6 +656,16 @@ extern class Global {
 	**/
 	static function file_exists( filename:String ) : Bool;
 
+	/**
+		@see http://php.net/manual/en/function.file-get-contents.php
+	**/
+	static function file_get_contents( filename:String, use_include_path:Bool = false, ?context:Resource, offset:Int = 0, ?maxlen:Int ) : EitherType<String,Bool>;
+
+	/**
+		@see http://php.net/manual/en/function.file-put-contents.php
+	**/
+	static function file_put_contents( filename:String, data:Dynamic, flags:Int = 0, ?context:Resource ) : EitherType<Int,Bool>;
+
 	/**
 		@see http://php.net/manual/en/function.clearstatcache.php
 	**/
@@ -911,6 +946,41 @@ extern class Global {
 	**/
 	static function stream_select( read:NativeArray, write:NativeArray, except:NativeArray, tv_sec:Int, tv_usec:Int = 0 ) : Bool;
 
+	/**
+		@see http://php.net/manual/en/function.stream-socket-shutdown.php
+	**/
+	static function stream_socket_shutdown( stream:Resource, how:Int ) : Bool;
+
+	/**
+		@see http://php.net/manual/en/function.stream-set-timeout.php
+	**/
+	static function stream_set_timeout( stream:Resource, seconds:Int, microseconds:Int = 0 ) : Bool;
+
+	/**
+		@see http://php.net/manual/en/function.stream-set-blocking.php
+	**/
+	static function stream_set_blocking( stream:Resource, mode:Bool ) : Bool;
+
+	/**
+		@see http://php.net/manual/en/function.stream-socket-accept.php
+	**/
+	static function stream_socket_accept( server_socket:Resource, ?timeout:Float, ?peername:Ref<String> ) : EitherType<Resource,Bool>;
+
+	/**
+		@see http://php.net/manual/en/function.stream-socket-client.php
+	**/
+	static function stream_socket_client( remote_socket:String, ?errno:Ref<Int>, ?errstr:Ref<String>, ?timeout:Float, ?flags:Int, ?context:Resource ) : EitherType<Resource,Bool>;
+
+	/**
+		@see http://php.net/manual/en/function.stream-socket-server.php
+	**/
+	static function stream_socket_server( local_socket:String, ?errno:Ref<Int>, ?errstr:Ref<String>, ?flags:Int, ?context:Resource ) : EitherType<Resource,Bool>;
+
+	/**
+		@see http://php.net/manual/en/function.stream-socket-get-name.php
+	**/
+	static function stream_socket_get_name( stream:Resource, want_peer:Bool ) : EitherType<String,Bool>;
+
 	/**
 		@see http://php.net/manual/en/function.ini-get.php
 	**/
@@ -960,4 +1030,19 @@ extern class Global {
 		@see http://php.net/manual/en/function.base64-decode.php
 	**/
 	static function base64_decode( data:String, strict:Bool = false ) : EitherType<String,Bool>;
+
+	/**
+		@see http://php.net/manual/en/function.gethostbyname.php
+	**/
+	static function gethostbyname( hostname:String ) : String;
+
+	/**
+		@see http://php.net/manual/en/function.gethostbyaddr.php
+	**/
+	static function gethostbyaddr( ip_address:String ) : EitherType<String,Bool>;
+
+	/**
+		@see http://php.net/manual/en/function.getprotobyname.php
+	**/
+	static function getprotobyname( name:String ) : EitherType<Int,Bool>;
 }

+ 1 - 1
std/php7/Resource.hx

@@ -2,6 +2,6 @@ package php;
 
 import sys.io.File.FileHandle;
 
-abstract Resource(Dynamic) to FileHandle {
+abstract Resource(Dynamic) {
 
 }

+ 8 - 9
std/php7/_std/sys/io/File.hx

@@ -21,21 +21,20 @@
  */
 package sys.io;
 
-enum FileHandle {
-}
+import php.Global.*;
 
 @:coreApi class File {
 
 	public static function getContent( path : String ) : String {
-		return untyped __call__("file_get_contents", path);
+		return file_get_contents(path);
 	}
 
 	public static function getBytes( path : String ) : haxe.io.Bytes {
-		return haxe.io.Bytes.ofString(getContent(path));
+		return haxe.io.Bytes.ofString(file_get_contents(path));
 	}
 
 	public static function saveContent( path : String, content : String) : Void {
-		untyped __call__("file_put_contents", path, content);
+		file_put_contents(path, content);
 	}
 
 	public static function saveBytes( path : String, bytes : haxe.io.Bytes ) : Void {
@@ -45,19 +44,19 @@ enum FileHandle {
 	}
 
 	public static function read( path : String, binary : Bool = true ) : FileInput {
-		return untyped new FileInput(__call__('fopen', path, binary ? "rb" : "r"));
+		return @:privateAccess new FileInput(fopen(path, binary ? "rb" : "r"));
 	}
 
 	public static function write( path : String, binary : Bool = true ) : FileOutput {
-		return untyped new FileOutput(untyped __call__('fopen', path, binary ? "wb" : "w"));
+		return untyped new FileOutput(fopen(path, binary ? "wb" : "w"));
 	}
 
 	public static function append( path : String, binary : Bool = true ) : FileOutput {
-		return untyped new FileOutput(untyped __call__('fopen', path, binary ? "ab" : "a"));
+		return untyped new FileOutput(fopen(path, binary ? "ab" : "a"));
 	}
 
 	public static function copy( srcPath : String, dstPath : String ) : Void {
-		untyped __call__("copy", srcPath, dstPath);
+		copy(srcPath, dstPath);
 	}
 
 }

+ 31 - 26
std/php7/_std/sys/io/FileInput.hx

@@ -20,63 +20,68 @@
  * DEALINGS IN THE SOFTWARE.
  */
 package sys.io;
+
 import haxe.io.Eof;
+import haxe.io.Error;
+import haxe.io.Bytes;
+import php.*;
+import php.Global.*;
+import php.Const.*;
 
 @:coreApi
 class FileInput extends haxe.io.Input {
 
-	private var __f : File.FileHandle;
+	private var __f : Resource;
 
-	function new(f:File.FileHandle) : Void {
+	function new(f:Resource) : Void {
 		__f = f;
 	}
 
 	public override function readByte() : Int {
-		var r = untyped __call__('fread', __f, 1);
-		if(untyped __call__('feof', __f)) return throw new haxe.io.Eof();
-		if(untyped __physeq__(r, false)) return throw haxe.io.Error.Custom('An error occurred');
-		return untyped __call__('ord', r);
+		var r = fread(__f, 1);
+		if(feof(__f)) throw new Eof();
+		if(r == false) throw Custom('An error occurred');
+		return ord(r);
 	}
 
-	public override function readBytes( s : haxe.io.Bytes, p : Int, l : Int ) : Int {
-		if(untyped __call__('feof', __f)) return throw new haxe.io.Eof();
-		var r : String = untyped __call__('fread', __f, l);
-		if(untyped __physeq__(r, false)) return throw haxe.io.Error.Custom('An error occurred');
-		var b = haxe.io.Bytes.ofString(r);
-		s.blit(p, b, 0, r.length);
-		return r.length;
+	public override function readBytes( s : Bytes, p : Int, l : Int ) : Int {
+		if(feof(__f)) throw new Eof();
+		var r = fread(__f, l);
+		if(r == false) throw Custom('An error occurred');
+		var b = Bytes.ofString(r);
+		s.blit(p, b, 0, (r:String).length);
+		return (r:String).length;
 	}
 
 	public override function close() : Void {
 		super.close();
-		if(__f != null)	untyped __call__('fclose', __f);
+		if(__f != null)	fclose(__f);
 	}
 
 	public function seek( p : Int, pos : FileSeek ) : Void {
 		var w;
 		switch( pos ) {
-			case SeekBegin: w = untyped __php__('SEEK_SET');
-			case SeekCur  : w = untyped __php__('SEEK_CUR');
-			case SeekEnd  : w = untyped __php__('SEEK_END');
+			case SeekBegin: w = SEEK_SET;
+			case SeekCur  : w = SEEK_CUR;
+			case SeekEnd  : w = SEEK_END;
 		}
-		var r = untyped __call__('fseek', __f, p, w);
-		if(untyped __physeq__(r, false)) throw haxe.io.Error.Custom('An error occurred');
+		var r = fseek(__f, p, w);
+		if(r == false) throw Custom('An error occurred');
 	}
 
 	public function tell() : Int {
-		var r = untyped __call__('ftell', __f);
-		if(untyped __physeq__(r, false)) return throw haxe.io.Error.Custom('An error occurred');
+		var r = ftell(__f);
+		if(r == false) throw Custom('An error occurred');
 		return cast r;
 	}
 
 	public function eof() : Bool {
-		return untyped __call__('feof', __f);
+		return feof(__f);
 	}
 
 	override function readLine() : String {
-		var r : String = untyped __call__('fgets', __f);
-		if (untyped __physeq__(false, r))
-			throw new Eof();
-		return untyped __call__("rtrim", r, "\r\n");
+		var r = fgets(__f);
+		if (false == r) throw new Eof();
+		return rtrim(r, "\r\n");
 	}
 }

+ 22 - 18
std/php7/_std/sys/io/FileOutput.hx

@@ -21,52 +21,56 @@
  */
 package sys.io;
 
+import php.*;
+import php.Global.*;
+import php.Const.*;
+
 @:coreApi
 class FileOutput extends haxe.io.Output {
-	private var __f : File.FileHandle;
+	private var __f : Resource;
 
-	function new(f:File.FileHandle) : Void {
+	function new(f:Resource) : Void {
 		__f = f;
 	}
 
 	public override function writeByte( c : Int ) : Void {
-		var r = untyped __call__('fwrite', __f, __call__('chr', c));
-		if(untyped __physeq__(r, false)) throw haxe.io.Error.Custom('An error occurred');
+		var r = fwrite(__f, chr(c));
+		if(r == false) throw haxe.io.Error.Custom('An error occurred');
 	}
 
 	public override function writeBytes( b : haxe.io.Bytes, p : Int, l : Int ) : Int {
 		var s = b.getString(p, l);
-		if(untyped __call__('feof', __f)) return throw new haxe.io.Eof();
-		var r = untyped __call__('fwrite', __f, s, l);
-		if(untyped __physeq__(r, false)) return throw haxe.io.Error.Custom('An error occurred');
+		if(feof(__f)) throw new haxe.io.Eof();
+		var r = fwrite(__f, s, l);
+		if(r == false) throw haxe.io.Error.Custom('An error occurred');
 		return r;
 	}
 
 	public override function flush() : Void {
-		var r = untyped __call__('fflush', __f);
-		if(untyped __physeq__(r, false)) throw haxe.io.Error.Custom('An error occurred');
+		var r = fflush(__f);
+		if(r == false) throw haxe.io.Error.Custom('An error occurred');
 	}
 
 	public override function close() : Void {
 		super.close();
-		if(__f != null)	untyped __call__('fclose', __f);
+		if(__f != null)	fclose(__f);
 	}
 
 	public function seek( p : Int, pos : FileSeek ) : Void {
 		var w;
 		switch( pos ) {
-			case SeekBegin: w = untyped __php__('SEEK_SET');
-			case SeekCur  : w = untyped __php__('SEEK_CUR');
-			case SeekEnd  : w = untyped __php__('SEEK_END');
+			case SeekBegin: w = SEEK_SET;
+			case SeekCur  : w = SEEK_CUR;
+			case SeekEnd  : w = SEEK_END;
 		}
-		var r = untyped __call__('fseek', __f, p, w);
-		if(untyped __physeq__(r, false)) throw haxe.io.Error.Custom('An error occurred');
+		var r = fseek(__f, p, w);
+		if(r == false) throw haxe.io.Error.Custom('An error occurred');
 	}
 
 	public function tell() : Int {
-		var r = untyped __call__('ftell', __f);
-		if(untyped __physeq__(r, false)) return throw haxe.io.Error.Custom('An error occurred');
-		return cast r;
+		var r = ftell(__f);
+		if(r == false) throw haxe.io.Error.Custom('An error occurred');
+		return r;
 	}
 
 }

+ 7 - 4
std/php7/_std/sys/net/Host.hx

@@ -21,6 +21,9 @@
  */
 package sys.net;
 
+import php.Global.*;
+import php.SuperGlobal.*;
+
 @:coreApi
 class Host {
 
@@ -34,14 +37,14 @@ class Host {
 		if(~/^(\d{1,3}\.){3}\d{1,3}$/.match(name)) {
 		  _ip = name;
 		} else {
-			_ip = untyped __call__('gethostbyname', name);
+			_ip = gethostbyname(name);
 			if(_ip == name) {
 				ip = 0;
 				return;
 			}
 		}
 		var p = _ip.split('.');
-		ip = untyped __call__('intval', __call__('sprintf', '%02X%02X%02X%02X', p[3], p[2], p[1], p[0]), 16);
+		ip = intval(sprintf('%02X%02X%02X%02X', p[3], p[2], p[1], p[0]), 16);
 	}
 
 	public function toString() : String {
@@ -49,10 +52,10 @@ class Host {
 	}
 
 	public function reverse() : String {
-		return untyped __call__('gethostbyaddr', _ip);
+		return gethostbyaddr(_ip);
 	}
 
 	public static function localhost() : String {
-		return untyped __var__('_SERVER', 'HTTP_HOST');
+		return _SERVER['HTTP_HOST'];
 	}
 }

+ 42 - 40
std/php7/_std/sys/net/Socket.hx

@@ -21,90 +21,92 @@
  */
 package sys.net;
 
-import sys.io.File;
+import php.*;
+import php.Global.*;
+import php.Const.*;
+import sys.io.FileInput;
+import sys.io.FileOutput;
 
 @:coreApi
 class Socket {
 
-	private var __s : FileHandle;
+	private var __s : Resource;
 	private var protocol : String;
 	public var input(default,null) : haxe.io.Input;
 	public var output(default,null) : haxe.io.Output;
 	public var custom : Dynamic;
 
 	public function new() : Void {
-		input = untyped new sys.io.FileInput(null);
-		output = untyped new sys.io.FileOutput(null);
+		input = @:privateAccess new sys.io.FileInput(null);
+		output = @:privateAccess new sys.io.FileOutput(null);
 		protocol = "tcp";
 	}
 
 	private function assignHandler() : Void {
-		untyped input.__f = __s;
-		untyped output.__f = __s;
+		@:privateAccess (cast input:FileInput).__f = __s;
+		@:privateAccess (cast output:FileOutput).__f = __s;
 	}
 
 	public function close() : Void {
-		untyped __call__('fclose', __s);
-		untyped {
-			input.__f = null;
-			output.__f = null;
-		}
+		fclose(__s);
+		@:privateAccess (cast input:FileInput).__f = null;
+		@:privateAccess (cast output:FileOutput).__f = null;
 		input.close();
 		output.close();
 	}
 
 	public function read() : String {
 		var b = '';
-		untyped __php__("while (!feof($this->__s)) $b .= fgets($this->__s)");
+		while(!feof(__s)) Syntax.binop(b, '.=', fgets(__s));
 		return b;
 	}
 
 	public function write( content : String ) : Void {
-		return untyped __call__('fwrite', __s, content);
+		fwrite(__s, content);
 	}
 
 	public function connect(host : Host, port : Int) : Void {
 		var errs = null;
 		var errn = null;
-		var r = untyped __call__('stream_socket_client', protocol + '://' +host.host + ':' + port, errn, errs);
+		var r = stream_socket_client(protocol + '://' + host.host + ':' + port, errn, errs);
 		Socket.checkError(r, errn, errs);
-		__s = cast r;
+		__s = r;
 		assignHandler();
 	}
 
 	public function listen(connections : Int) : Void {
 		throw "Not implemented";
 /* TODO: ??????
-		var r = untyped __call__('socket_listen', __s, connections);
+		var r = socket_listen(__s, connections);
 		checkError(r);
 */
 	}
 
 	public function shutdown( read : Bool, write : Bool ) : Void {
-		var r;
-		if(untyped __call__("function_exists", "stream_socket_shutdown")) {
-			var rw = read && write ? 2 : (write ? 1 : (read ? 0 : 2));
-			r = untyped __call__('stream_socket_shutdown', __s, rw);
-		} else {
-			r = untyped __call__('fclose', __s);
-		}
+		var rw = read && write ? 2 : (write ? 1 : (read ? 0 : 2));
+		var r = stream_socket_shutdown(__s, rw);
 		checkError(r, 0, 'Unable to Shutdown');
 	}
 
 	public function bind(host : Host, port : Int) : Void {
-		var errs = null;
-		var errn = null;
-		var r = untyped __call__('stream_socket_server', protocol + '://' +host._ip + ':' + port, errn, errs, (protocol=="udp") ? __php__('STREAM_SERVER_BIND') : __php__('STREAM_SERVER_BIND | STREAM_SERVER_LISTEN'));
+		var errs = Boot.deref(null);
+		var errn = Boot.deref(null);
+		var r = stream_socket_server(
+			protocol + '://' + host.ip + ':' + port,
+			errn,
+			errs,
+			(protocol == "udp" ? STREAM_SERVER_BIND : STREAM_SERVER_BIND | STREAM_SERVER_LISTEN)
+		);
 		Socket.checkError(r, errn, errs);
 		__s = cast r;
 		assignHandler();
 	}
 
 	public function accept() : Socket {
-		var r = untyped __call__('stream_socket_accept', __s);
+		var r = stream_socket_accept(__s);
 		checkError(r, 0, 'Unable to accept connections on socket');
 		var s = new Socket();
-		s.__s = cast r;
+		s.__s = r;
 		s.assignHandler();
 		return s;
 	}
@@ -119,26 +121,26 @@ class Socket {
 	}
 
 	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');
+		var r = stream_socket_get_name(__s, true);
+		checkError(r, 0, 'Unable to retrieve the peer name');
 		return hpOfString(r);
 	}
 
 	public function host() : { host : Host, port : Int } {
-		var r : String = untyped __call__('stream_socket_get_name', __s, false);
-		checkError(cast r, 0, 'Unable to retrieve the host name');
+		var r = stream_socket_get_name(__s, false);
+		checkError(r, 0, 'Unable to retrieve the host name');
 		return hpOfString(r);
 	}
 
 	public function setTimeout( timeout : Float ) : Void {
 		var s = Std.int(timeout);
-		var ms = Std.int((timeout-s)*1000000);
-		var r = untyped __call__('stream_set_timeout', __s, s, ms);
+		var ms = Std.int((timeout - s) * 1000000);
+		var r = stream_set_timeout(__s, s, ms);
 		checkError(r, 0, 'Unable to set timeout');
 	}
 
 	public function setBlocking( b : Bool ) : Void {
-		var r = untyped __call__('stream_set_blocking', __s, b);
+		var r = stream_set_blocking(__s, b);
 		checkError(r, 0, 'Unable to block');
 	}
 
@@ -147,20 +149,20 @@ class Socket {
 	}
 
 	public function waitForRead() : Void {
-		select([this],null,null);
+		select([this], null, null);
 	}
 
 	private static function checkError(r : Bool, code : Int, msg : String) : Void {
-		if(!untyped __physeq__(r, false)) return;
-		throw haxe.io.Error.Custom('Error ['+code+']: ' +msg);
+		if(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');
+		return isUdp ? SOCK_DGRAM : SOCK_STREAM;
 	}
 
 	private static function getProtocol(protocol : String) : Int {
-		return untyped __call__('getprotobyname', protocol);
+		return getprotobyname(protocol);
  	}
 
 	public static function select(read : Array<Socket>, write : Array<Socket>, others : Array<Socket>, ?timeout : Float) : { read: Array<Socket>,write: Array<Socket>,others: Array<Socket> }