فهرست منبع

[php] make Array mimic native array for 3rd-party php code (closes #9377)

Aleksandr Kuzmenko 5 سال پیش
والد
کامیت
0741f6e18a
3فایلهای تغییر یافته به همراه31 افزوده شده و 13 حذف شده
  1. 1 6
      std/php/IteratorAggregate.hx
  2. 1 1
      std/php/JsonSerializable.hx
  3. 29 6
      std/php/_std/Array.hx

+ 1 - 6
std/php/IteratorAggregate.hx

@@ -27,10 +27,5 @@ package php;
 **/
 @:native('IteratorAggregate')
 extern interface IteratorAggregate<T> extends Traversable {
-	/**
-		This method is not public to not induce Haxe users to use it ;)
-		Use iterator() instead.
-	**/
-	private function getIterator():Traversable;
-
+	function getIterator():Traversable;
 }

+ 1 - 1
std/php/JsonSerializable.hx

@@ -27,5 +27,5 @@ package php;
 **/
 @:native('JsonSerializable')
 extern interface JsonSerializable<T> {
-	private function jsonSerialize():T;
+	function jsonSerialize():T;
 }

+ 29 - 6
std/php/_std/Array.hx

@@ -28,7 +28,7 @@ import haxe.iterators.ArrayKeyValueIterator;
 using php.Global;
 
 @:coreApi
-final class Array<T> implements ArrayAccess<Int, T> implements IteratorAggregate<T> implements JsonSerializable<NativeIndexedArray<T>> {
+final class Array<T> implements ArrayAccess<Int, T> implements IteratorAggregate<T> implements Countable implements JsonSerializable<NativeIndexedArray<T>> {
 	public var length(default, null):Int;
 
 	var arr:NativeIndexedArray<T>;
@@ -206,12 +206,12 @@ final class Array<T> implements ArrayAccess<Int, T> implements IteratorAggregate
 		length = len;
 	}
 
-	@:noCompletion
+	@:noCompletion @:keep
 	function offsetExists(offset:Int):Bool {
 		return offset < length;
 	}
 
-	@:noCompletion
+	@:noCompletion @:keep
 	function offsetGet(offset:Int):Ref<T> {
 		try {
 			return arr[offset];
@@ -220,7 +220,7 @@ final class Array<T> implements ArrayAccess<Int, T> implements IteratorAggregate
 		}
 	}
 
-	@:noCompletion
+	@:noCompletion @:keep
 	function offsetSet(offset:Int, value:T):Void {
 		if (length <= offset) {
 			for(i in length...offset + 1) {
@@ -232,7 +232,7 @@ final class Array<T> implements ArrayAccess<Int, T> implements IteratorAggregate
 		Syntax.code("return {0}", value);
 	}
 
-	@:noCompletion
+	@:noCompletion @:keep
 	function offsetUnset(offset:Int):Void {
 		if (offset >= 0 && offset < length) {
 			Global.array_splice(arr, offset, 1);
@@ -245,6 +245,12 @@ final class Array<T> implements ArrayAccess<Int, T> implements IteratorAggregate
 		return new NativeArrayIterator(arr);
 	}
 
+	@:noCompletion @:keep
+	@:native('count') //to not interfere with `Lambda.count`
+	private function _hx_count():Int {
+		return length;
+	}
+
 	@:noCompletion @:keep
 	function jsonSerialize():NativeIndexedArray<T> {
 		return arr;
@@ -259,7 +265,8 @@ final class Array<T> implements ArrayAccess<Int, T> implements IteratorAggregate
 }
 
 /**
-	This one is required for `Array`
+	Following interfaces are required to make `Array` mimic native arrays for usage
+	from a 3rd party PHP code.
 **/
 @:native('ArrayAccess')
 private extern interface ArrayAccess<K, V> {
@@ -267,4 +274,20 @@ private extern interface ArrayAccess<K, V> {
 	private function offsetGet(offset:K):V;
 	private function offsetSet(offset:K, value:V):Void;
 	private function offsetUnset(offset:K):Void;
+}
+
+@:native('JsonSerializable')
+private extern interface JsonSerializable<T> {
+	private function jsonSerialize():T;
+}
+
+@:native('IteratorAggregate')
+private extern interface IteratorAggregate<T> extends Traversable {
+	private function getIterator():Traversable;
+}
+
+@:native('Countable')
+private extern interface Countable {
+	@:native('count') //to not interfere with `Lambda.count`
+	private function _hx_count():Int;
 }