Bläddra i källkod

fixed calling function referenced in array (haXe/PHP)
fixed issues with wrapped arrays (haXe/PHP)

Franco Ponticelli 17 år sedan
förälder
incheckning
34ac17db06
9 ändrade filer med 91 tillägg och 96 borttagningar
  1. 37 18
      genphp.ml
  2. 1 1
      std/Reflect.hx
  3. 1 1
      std/Type.hx
  4. 3 3
      std/haxe/io/Bytes.hx
  5. 2 2
      std/haxe/io/BytesBuffer.hx
  6. 41 46
      std/php/Boot.hx
  7. 5 0
      std/php/NativeArray.hx
  8. 1 6
      tests/unit/TestBasetypes.hx
  9. 0 19
      tests/unit/TestLocals.hx

+ 37 - 18
genphp.ml

@@ -2,6 +2,9 @@
 TODO
 - debug version
 - runtime check for undefined fields
+OPTIMIZATION
+- replace eval for statements with functions/inlines
+- replace closures (eval) with functions
 *)
 (*
  *  haXe/PHP Compiler
@@ -342,7 +345,7 @@ let handle_break ctx e =
 				ctx.handle_break <- snd old;
 				newline ctx;
 				let p = escphp ctx.quotes in
-				print ctx "} catch(Exception %s$e) { if( %s$e->getMessage() != \"__break__\" ) throw %s$e; }" p p p;
+				print ctx "} catch(Exception %s$e) { if( %s$e->getMessage() != %s\"__break__%s\" ) throw %s$e; }" p p p p p;
 			)
 
 let this ctx =
@@ -577,15 +580,6 @@ and gen_string_var ctx s e =
 	| _ ->
 		unsupported e.epos;
 
-and gen_array_var ctx s e =
-	match s with
-	| "length" ->
-		spr ctx "count(";
-		gen_value ctx e;
-		spr ctx ")"
-	| _ ->
-		unsupported e.epos;
-
 and gen_string_static_call ctx s e el =
 	match s with
 	| "fromCharCode" ->
@@ -923,6 +917,7 @@ and gen_expr ctx e =
 			let s_phop = if op = Ast.OpNotEq then " !== " else " === " in
 			let se1 = s_expr_name e1 in
 			let se2 = s_expr_name e2 in
+			let p = escphp ctx.quotes in
 			if
 				e1.eexpr = TConst (TNull)
 				|| e2.eexpr = TConst (TNull)
@@ -931,7 +926,7 @@ and gen_expr ctx e =
 				| TField (f, s) when is_anonym_expr e1 || is_unknown_expr e1 ->
 					spr ctx "_hx_field(";
 					gen_value ctx f;
-					print ctx ", \"%s\")" s;
+					print ctx ", %s\"%s%s\")" p s p;
 				| _ ->
 					gen_field_op ctx e1);
 
@@ -941,7 +936,7 @@ and gen_expr ctx e =
 				| TField (f, s) when is_anonym_expr e2 || is_unknown_expr e2 ->
 					spr ctx "_hx_field(";
 					gen_value ctx f;
-					print ctx ", \"%s\")" s;
+					print ctx ", %s\"%s%s\")" p s p;
 				| _ ->
 					gen_field_op ctx e2);
 			end else if
@@ -1057,7 +1052,8 @@ and gen_expr ctx e =
 			);
 	| TBreak ->
 		if not ctx.in_loop then unsupported e.epos;
-		if ctx.handle_break then spr ctx "throw new Exception(\"__break__\")" else spr ctx "break"
+		let p = escphp ctx.quotes in
+		if ctx.handle_break then print ctx "throw new Exception(%s\"__break__%s\")" p p else spr ctx "break"
 	| TContinue ->
 		if not ctx.in_loop then unsupported e.epos;
 		spr ctx "continue"
@@ -1113,17 +1109,26 @@ and gen_expr ctx e =
 		ctx.in_loop <- snd old;
 	| TCall (ec,el) ->
 		(match ec.eexpr with
+		| TArray _ ->
+			spr ctx "call_user_func_array(";
+			gen_value ctx ec;
+			spr ctx ", array(";
+			concat ctx ", " (gen_value ctx) el;
+			spr ctx "))";
 		| TField (ef,s) when is_static ef.etype && is_string_expr ef ->
 			gen_string_static_call ctx s ef el
 		| TField (ef,s) when is_string_expr ef ->
 			gen_string_call ctx s ef el
-		| TField (ef,s) when is_anonym_expr ef ->
+(*		| TField (ef,s) when is_anonym_expr ef ->
 			if could_be_string_call s then begin
 				gen_uncertain_string_call ctx s ef el
 			end else
 				gen_call ctx ec el
-		| TCall _ ->
-			gen_call ctx ec el
+*)
+		| TField (ef,s) when is_anonym_expr ef && could_be_string_call s ->
+			gen_uncertain_string_call ctx s ef el
+(*		| TCall _ ->
+			gen_call ctx ec el *)
 		| _ ->
 			gen_call ctx ec el);
 	| TArrayDecl el ->
@@ -1180,9 +1185,23 @@ and gen_expr ctx e =
 			gen_expr ctx (mk_block e));
 	| TUnop (op,Ast.Prefix,e) ->
 		spr ctx (Ast.s_unop op);
-		gen_value ctx e
+		(match e.eexpr with
+		| TArray(te1, te2) ->
+			gen_value ctx te1;
+			spr ctx "->__a[";
+			gen_value ctx te2;
+			spr ctx "]";
+		| _ ->
+			gen_value ctx e)
 	| TUnop (op,Ast.Postfix,e) ->
-		gen_value ctx e;
+		(match e.eexpr with
+		| TArray(te1, te2) ->
+			gen_value ctx te1;
+			spr ctx "->__a[";
+			gen_value ctx te2;
+			spr ctx "]";
+		| _ ->
+			gen_value ctx e);
 		spr ctx (Ast.s_unop op)
 	| TWhile (cond,e,Ast.NormalWhile) ->
 		let handle_break = handle_break ctx e in

+ 1 - 1
std/Reflect.hx

@@ -111,7 +111,7 @@ class Reflect {
 				else if(args.length == 1) return __call__("call_user_func", field(o, func), args[0]);
 				else return __call__("call_user_func", field(o, func), args[0], args[1]);
 			}
-			return __php__("call_user_func_array(is_callable($func) ? $func : array($o, $func) , $args == null ? array() : $args->a)");
+			return __php__("call_user_func_array(is_callable($func) ? $func : array($o, $func) , $args == null ? array() : $args->__a)");
 		#else
 			return null;
 		#end

+ 1 - 1
std/Type.hx

@@ -302,7 +302,7 @@ class Type {
 			if(cl.__qname__ == 'String') return args[0];
 			var c = cl.__rfl__();
 			if(c == null) return null;
-			return __php__("$inst = $c->getConstructor() ? $c->newInstanceArgs($args->a) : $c->newInstanceArgs()");
+			return __php__("$inst = $c->getConstructor() ? $c->newInstanceArgs($args->__a) : $c->newInstanceArgs()");
 		#else
 			return null;
 		#end

+ 3 - 3
std/haxe/io/Bytes.hx

@@ -91,7 +91,7 @@ class Bytes {
 		b.readBytes(b2,0,len);
 		return new Bytes(len,b2);
 		#elseif php
-		return new Bytes(len,untyped __call__("new _hx_array", __call__("array_slice", b.a, pos, len)));
+		return new Bytes(len,untyped __call__("new _hx_array", __call__("array_slice", b.__a, pos, len)));
 		#else
 		return new Bytes(len,b.slice(pos,pos+len));
 		#end
@@ -137,7 +137,7 @@ class Bytes {
 		b.position = pos;
 		return b.readUTFBytes(len);
 		#elseif php
-		return untyped __call__("call_user_func_array", "pack", __call__("array_merge", __call__("array", "C*"), __call__("array_slice", b.a, pos, len)));
+		return untyped __call__("call_user_func_array", "pack", __call__("array_merge", __call__("array", "C*"), __call__("array_slice", b.__a, pos, len)));
 		#else
 		var s = "";
 		var b = b;
@@ -172,7 +172,7 @@ class Bytes {
 		b.position = 0;
 		return b.readUTFBytes(length);
 		#elseif php
-		return untyped __call__("call_user_func_array", "pack", __call__("array_merge", __call__("array", "C*"), b.a));
+		return untyped __call__("call_user_func_array", "pack", __call__("array_merge", __call__("array", "C*"), b.__a));
 		#else
 		return readString(0,length);
 		#end

+ 2 - 2
std/haxe/io/BytesBuffer.hx

@@ -60,7 +60,7 @@ class BytesBuffer {
 		#elseif flash9
 		b.writeBytes(src.getData());
 		#elseif php
-		b = untyped __call__("new _hx_array", __call__("array_merge", b.a, src.getData().a));
+		b = untyped __call__("new _hx_array", __call__("array_merge", b.__a, src.getData().__a));
 		#else
 		var b1 = b;
 		var b2 = src.getData();
@@ -79,7 +79,7 @@ class BytesBuffer {
 		b.writeBytes(src.getData(),pos,len);
 		#elseif php
 		try {
-			b = untyped __call__("new _hx_array", __call__("array_merge", b.a, __call__("array_slice", src.getData().a, pos, len)));
+			b = untyped __call__("new _hx_array", __call__("array_merge", b.__a, __call__("array_slice", src.getData().__a, pos, len)));
 		} catch(e : Dynamic) {
 			throw Error.OutsideBounds;
 		}

+ 41 - 46
std/php/Boot.hx

@@ -18,52 +18,52 @@ class _hx_array implements ArrayAccess {
 	var $a;
 	var $length;
 	function __construct($a = array()) {
-		$this->a = $a;
+		$this->__a = $a;
 		$this->length = count($a);
 	}
 
 	function concat($a) {
-		return new _hx_array(array_merge($this->a, $a->a));
+		return new _hx_array(array_merge($this->__a, $a->__a));
 	}
 
 	function copy() {
-		return new _hx_array($this->a);
+		return new _hx_array($this->__a);
 	}
-/*
-	function get($index) {
-		if(isset($this->a[$index])) return $this->a[$index];
+
+	function &get($index) {
+		if(isset($this->__a[$index])) return $this->__a[$index];
 		return null;
 	}
-*/
+
 	function insert($pos, $x) {
-		array_splice($this->a, $pos, 0, array($x));
+		array_splice($this->__a, $pos, 0, array($x));
 		$this->length++;
 	}
 
 	function iterator() {
-		return new _hx_array_iterator($this->a);
+		return new _hx_array_iterator($this->__a);
 	}
 
 	function join($sep) {
-		return implode($this->a, $sep);
+		return implode($this->__a, $sep);
 	}
 
 	function pop() {
-		$r = array_pop($this->a);
-		$this->length = count($this->a);
+		$r = array_pop($this->__a);
+		$this->length = count($this->__a);
 		return $r;
 	}
 
 	function push($x) {
-		$this->a[] = $x;
+		$this->__a[] = $x;
 		$this->length++;
 	}
 
 	function remove($x) {
-		for($i = 0; $i < count($this->a); $i++)
-			if($this->a[$i] === $x) {
-				unset($this->a[$i]);
-				$this->a = array_values($this->a);
+		for($i = 0; $i < count($this->__a); $i++)
+			if($this->__a[$i] === $x) {
+				unset($this->__a[$i]);
+				$this->__a = array_values($this->__a);
 				$this->length--;
 				return true;
 			}
@@ -71,8 +71,8 @@ class _hx_array implements ArrayAccess {
 	}
 
 	function removeAt($pos) {
-		if(array_key_exists($pos, $this->a)) {
-			unset($this->a[$pos]);
+		if(array_key_exists($pos, $this->__a)) {
+			unset($this->__a[$pos]);
 			$this->length--;
 			return true;
 		} else
@@ -80,28 +80,28 @@ class _hx_array implements ArrayAccess {
 	}
 
 	function reverse() {
-		$this->a = array_reverse($this->a, false);
+		$this->__a = array_reverse($this->__a, false);
 	}
 /*
 	function set($pos, $v) {
 		if($this->length <= $pos) {
-			$this->a = array_merge($this->a, array_fill(0, $pos+1-$this->length, null));
+			$this->__a = array_merge($this->__a, array_fill(0, $pos+1-$this->length, null));
 			$this->length = $pos+1;
 		}
-		return $this->a[$pos] = $v;
+		return $this->__a[$pos] = $v;
 	}
 */
 	function shift() {
-		$r = array_shift($this->a);
-		$this->length = count($this->a);
+		$r = array_shift($this->__a);
+		$this->length = count($this->__a);
 		return $r;
 	}
 
 	function slice($pos, $end) {
 		if($end == null)
-			return new _hx_array(array_slice($this->a, $pos));
+			return new _hx_array(array_slice($this->__a, $pos));
 		else
-			return new _hx_array(array_slice($this->a, $pos, $end-$pos));
+			return new _hx_array(array_slice($this->__a, $pos, $end-$pos));
 	}
 
 	function sort($f) {
@@ -111,10 +111,10 @@ class _hx_array implements ArrayAccess {
 			$j = 0;
 			$max = $this->length - $i - 1;
 			while($j < $max) {
-				if(call_user_func($f, $this->a[$j], $this->a[$j+1]) > 0 ) {
-					$tmp = $this->a[$j+1];
-					$this->a[$j+1] = $this->a[$j];
-					$this->a[$j] = $tmp;
+				if(call_user_func($f, $this->__a[$j], $this->__a[$j+1]) > 0 ) {
+					$tmp = $this->__a[$j+1];
+					$this->__a[$j+1] = $this->__a[$j];
+					$this->__a[$j] = $tmp;
 					$swap = true;
 				}
 				$j += 1;
@@ -126,13 +126,13 @@ class _hx_array implements ArrayAccess {
 
 	function splice($pos, $len) {
 		if($len < 0) $len = 0;
-		$nh = new _hx_array(array_splice($this->a, $pos, $len));
-		$this->length = count($this->a);
+		$nh = new _hx_array(array_splice($this->__a, $pos, $len));
+		$this->length = count($this->__a);
 		return $nh;
 	}
 
 	function toString() {
-		return '['.implode($this->a, ', ').']';
+		return '['.implode($this->__a, ', ').']';
 	}
 
 	function __toString() {
@@ -140,31 +140,26 @@ class _hx_array implements ArrayAccess {
 	}
 
 	function unshift($x) {
-		array_unshift($this->a, $x);
+		array_unshift($this->__a, $x);
 		$this->length++;
 	}
 
 	// ArrayAccess methods:
-	function __get($offset) {
-		if(isset($this->a[$offset])) return $this->a[$offset];
-		return null;
-	}
-
 	function offsetExists($offset) {
-		return isset($this->a[$offset]);
+		return isset($this->__a[$offset]);
 	}
 
 	function offsetGet($offset) {
-		if(isset($this->a[$offset])) return $this->a[$offset];
+		if(isset($this->__a[$offset])) return $this->__a[$offset];
 		return null;
 	}
 
 	function offsetSet($offset, $value) {
 		if($this->length <= $offset) {
-			$this->a = array_merge($this->a, array_fill(0, $offset+1-$this->length, null));
+			$this->__a = array_merge($this->__a, array_fill(0, $offset+1-$this->length, null));
 			$this->length = $offset+1;
 		}
-		return $this->a[$offset] = $value;
+		return $this->__a[$offset] = $value;
 	}
 
 	function offsetUnset($offset) {
@@ -559,17 +554,17 @@ class _hx_array_iterator {
 	private $a;
 	private $i;
 	public function __construct($a) {
-		$this->a = $a;
+		$this->__a = $a;
 		$this->i = 0;
 	}
 
 	public function next() {
 		if(!$this->hasNext()) return null;
-		return $this->a[$this->i++];
+		return $this->__a[$this->i++];
 	}
 
 	public function hasNext() {
-		return $this->i < count($this->a);
+		return $this->i < count($this->__a);
 	}
 }
 

+ 5 - 0
std/php/NativeArray.hx

@@ -0,0 +1,5 @@
+package php;
+
+extern class NativeArray {
+
+}

+ 1 - 6
tests/unit/TestBasetypes.hx

@@ -8,11 +8,6 @@ class TestBasetypes extends Test {
 		eq( a[0], 1 );
 		eq( a[2], 3 );
 
-		#if php
-		assert();
-		return;
-		#end
-
 		eq( a[3], null );
 		eq( a[1000], null );
 		eq( a[-1], null );
@@ -23,7 +18,7 @@ class TestBasetypes extends Test {
 		unspec(function() String.fromCharCode(0));
 		unspec(function() String.fromCharCode(-1));
 		unspec(function() String.fromCharCode(256));
-		eq( null + "x" , "nullx" );
+		eq( null + "x", "nullx" );
 		eq( "x" + null, "xnull" );
 	}
 

+ 0 - 19
tests/unit/TestLocals.hx

@@ -56,11 +56,6 @@ class TestLocals extends Test {
 	}
 
 	function testCapture() {
-		#if php
-		assert();
-		return;
-		#end
-
 		// read
 		var funs = new Array();
 		for( i in 0...5 )
@@ -99,10 +94,6 @@ class TestLocals extends Test {
 	}
 
 	function testSubCapture() {
-		#if php
-		assert();
-		return;
-		#end
 		var funs = new Array();
 		for( i in 0...5 )
 			funs.push(function() {
@@ -119,11 +110,6 @@ class TestLocals extends Test {
 	}
 
 	function testParallelCapture() {
-		#if php
-		assert();
-		return;
-		#end
-
 		var funs = new Array();
 		for( i in 0...5 ) {
 			if( true ) {
@@ -140,11 +126,6 @@ class TestLocals extends Test {
 	}
 
 	function testPossibleBug() {
-		#if php
-		assert();
-		return;
-		#end
-
 		var funs = new Array();
 		for( i in 0...5 )
 			funs.push(function(i) return i);