Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[php] Implement native iterator on arrays #8821

Merged
merged 4 commits into from
Sep 18, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion std/php/_std/Array.hx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import php.*;
using php.Global;

@:coreApi
final class Array<T> implements ArrayAccess<Int, T> {
final class Array<T> implements ArrayAccess<Int, T> implements NativeIterator<Int, T> {
public var length(default, null):Int;

var arr:NativeIndexedArray<T>;
Expand Down Expand Up @@ -228,6 +228,31 @@ final class Array<T> implements ArrayAccess<Int, T> {
}
}

@:noCompletion @:keep
function current():T {
return Global.current(arr);
}

@:noCompletion @:keep
function key():Int {
return Global.key(arr);
}

@:noCompletion @:keep
function next():Void {
Global.next(arr);
}

@:noCompletion @:keep
function rewind():Void {
Global.reset(arr);
}

@:noCompletion @:keep
function valid():Bool {
return Global.key(arr) != null;
}

static function wrap<T>(arr:NativeIndexedArray<T>):Array<T> {
var a = new Array();
a.arr = arr;
Expand Down Expand Up @@ -273,3 +298,12 @@ private extern interface ArrayAccess<K, V> {
private function offsetSet(offset:K, value:V):Void;
private function offsetUnset(offset:K):Void;
}

@:native('Iterator')
private extern interface NativeIterator<K, V> {
benmerckx marked this conversation as resolved.
Show resolved Hide resolved
private function current():V;
private function key():K;
private function next():Void;
private function rewind():Void;
private function valid():Bool;
}