Skip to content

Commit

Permalink
[php7] Merge Array & NativeArray implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
RealyUniqueName committed Aug 20, 2016
2 parents 09f4c9e + e0388b9 commit 09f84aa
Show file tree
Hide file tree
Showing 10 changed files with 544 additions and 36 deletions.
32 changes: 23 additions & 9 deletions src/generators/genphp7.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let haxe_exception_path = (["php7"], "HException")
(**
Type path of `php7.Boot`
*)
let boot_class_path = (["php7"], "Boot")
let boot_type_path = (["php7"], "Boot")
(**
Type path of the base class for all enums: `php7.Boot.HxEnum`
*)
Expand All @@ -41,6 +41,11 @@ let hxenum_type_path = (["php7"; "_Boot"], "HxEnum")
*)
let hxclass_type_path = (["php7"; "_Boot"], "HxClass")

(**
Type path of the implementation class for `Array<T>`
*)
let array_type_path = ([], "Array")

(**
Resolve real type (bypass abstracts and typedefs)
*)
Expand Down Expand Up @@ -919,23 +924,32 @@ class virtual type_builder ctx wrapper =
*)
method private write_expr_array_decl exprs =
match exprs with
| [] -> self#write "[]"
| [] -> self#write ("new " ^ (self#use array_type_path) ^ "()")
| [expr] ->
self#write "[";
self#write ((self#use array_type_path) ^ "::wrap([");
self#write_expr expr;
self#write "]"
self#write "])"
| _ ->
self#write "[\n";
self#write ((self#use array_type_path) ^ "::wrap([\n");
self#indent_more;
List.iter (fun expr -> self#write_array_item expr) exprs;
self#indent_less;
self#write_indentation;
self#write "]"
self#write "])"
(**
Writes TArray to output buffer
*)
method private write_expr_array_access target index =
self#write_expr target;
(match follow target.etype with
| TInst ({ cl_path = path }, _) when path = array_type_path ->
(match expr_hierarchy with
| _ :: { eexpr = TBinop (OpAssign, _, _) } :: _ -> ()
| _ :: { eexpr = TBinop (OpAssignOp _, _, _) } :: _ -> ()
| _ -> self#write "->arr"
)
| _ -> ()
);
self#write "[";
self#write_expr index;
self#write "]"
Expand Down Expand Up @@ -1181,13 +1195,13 @@ class virtual type_builder ctx wrapper =
| _ :: { eexpr = TField _ } :: _ -> self#write (self#use type_path)
| _ ->
let type_name = get_full_type_name ~escape:true type_path in
self#write ((self#use boot_class_path) ^ "::getClass('" ^ type_name ^ "')")
self#write ((self#use boot_type_path) ^ "::getClass('" ^ type_name ^ "')")
(**
Writes binary operation to output buffer
*)
method private write_expr_binop operation expr1 expr2 =
let write_shiftRightUnsigned () =
self#write ((self#use boot_class_path) ^ "::shiftRightUnsigned(");
self#write ((self#use boot_type_path) ^ "::shiftRightUnsigned(");
self#write_expr expr1;
self#write ", ";
self#write_expr expr2;
Expand Down Expand Up @@ -1641,7 +1655,7 @@ class class_builder ctx (cls:tclass) =
at_least_one_field_written := true;
self#write_field is_static field
in
if boot_class_path = self#get_type_path then begin
if boot_type_path = self#get_type_path then begin
self#write_php_prefix ();
at_least_one_field_written := true
end;
Expand Down
13 changes: 13 additions & 0 deletions std/php7/ArrayAccess.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package php7;

/**
Native PHP interface.
@see http://php.net/manual/en/class.arrayaccess.php
**/
@:native('ArrayAccess')
extern interface ArrayAccess<K,V> {
function offsetExists( offset:K ) : Bool;
function offsetGet( offset:K ) : V;
function offsetSet( offset:K, value:V ) : Void;
function offsetUnset( offset:K ) : Void;
}
52 changes: 31 additions & 21 deletions std/php7/Const.hx
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
package php7;

/**
This class contains externs for native PHP constants defined in global namespace.
For native PHP functions in global namespace see `php7.Global`.
This class contains externs for native PHP constants defined in global namespace.
For native PHP functions in global namespace see `php7.Global`.
**/
@:phpGlobal
extern class Const {
/**
@see http://php.net/manual/en/errorfunc.constants.php
**/
static var E_ERROR : Int;
static var E_WARNING : Int;
static var E_PARSE : Int;
static var E_NOTICE : Int;
static var E_CORE_ERROR : Int;
static var E_CORE_WARNING : Int;
static var E_COMPILE_ERROR : Int;
static var E_COMPILE_WARNING : Int;
static var E_USER_ERROR : Int;
static var E_USER_WARNING : Int;
static var E_USER_NOTICE : Int;
static var E_STRICT : Int;
static var E_RECOVERABLE_ERROR : Int;
static var E_DEPRECATED : Int;
static var E_USER_DEPRECATED : Int;
static var E_ALL : Int;
/**
@see http://php.net/manual/en/errorfunc.constants.php
**/
static var E_ERROR : Int;
static var E_WARNING : Int;
static var E_PARSE : Int;
static var E_NOTICE : Int;
static var E_CORE_ERROR : Int;
static var E_CORE_WARNING : Int;
static var E_COMPILE_ERROR : Int;
static var E_COMPILE_WARNING : Int;
static var E_USER_ERROR : Int;
static var E_USER_WARNING : Int;
static var E_USER_NOTICE : Int;
static var E_STRICT : Int;
static var E_RECOVERABLE_ERROR : Int;
static var E_DEPRECATED : Int;
static var E_USER_DEPRECATED : Int;
static var E_ALL : Int;
/**
@see http://php.net/manual/en/function.count.php
**/
static var COUNT_NORMAL : Int;
static var COUNT_RECURSIVE : Int;
/**
@see http://php.net/manual/en/function.array-filter.php
**/
static var ARRAY_FILTER_USE_KEY : Int;
static var ARRAY_FILTER_USE_BOTH : Int;
}
132 changes: 131 additions & 1 deletion std/php7/Global.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package php7;


import haxe.extern.EitherType;
import haxe.extern.Rest;

/**
This class contains externs for native PHP functions defined in global namespace.
Expand Down Expand Up @@ -82,4 +83,133 @@ extern class Global {
**/
@:overload(function(original:String,alias:String):Bool {})
static function class_alias( original:String, alias:String, autoload:Bool ) : Bool ;

/**
@see http://php.net/manual/en/function.count.php
**/
@:overload(function(array:NativeArray):Int {})
static function count( array:NativeArray, mode:Int ) : Int ;

/**
@see http://php.net/manual/en/function.array-filter.php
**/
@:overload(function(array:NativeArray):NativeArray {})
@:overload(function(array:NativeArray,callback:Dynamic->Bool):NativeArray {})
static function array_filter( array:NativeArray, callback:Dynamic->?Dynamic->Bool, flag:Int ) : NativeArray ;

/**
@see http://php.net/manual/en/function.implode.php
**/
static function implode( glue:String = "", array:NativeArray ) : String ;

/**
@see http://php.net/manual/en/function.array-map.php
**/
static function array_map( callback:Dynamic->Dynamic, array:Rest<NativeArray> ) : NativeArray ;

/**
@see http://php.net/manual/en/function.array-merge.php
**/
static function array_merge( array:Rest<NativeArray> ) : NativeArray ;

/**
@see http://php.net/manual/en/function.array-pop.php
**/
static function array_pop( array:NativeArray ) : Dynamic;

/**
@see http://php.net/manual/en/function.array-push.php
**/
static function array_push( array:NativeArray, value:Rest<Dynamic> ) : Int ;

/**
@see http://php.net/manual/en/function.array-reverse.php
**/
@:overload(function(array:NativeArray):NativeArray {})
static function array_reverse( array:NativeArray, preserve_keys:Bool ) : NativeArray ;

/**
@see http://php.net/manual/en/function.array-search.php
**/
@:overload(function(needle:Dynamic,haystack:NativeArray):Null<EitherType<String,Int>> {})
static function array_search( needle:Dynamic, haystack:NativeArray, strict:Bool ) : EitherType<Bool,EitherType<String,Int>> ;

/**
@see http://php.net/manual/en/function.array-shift.php
**/
static function array_shift( array:NativeArray ) : Dynamic ;

/**
@see http://php.net/manual/en/function.array-slice.php
**/
@:overload(function(array:NativeArray,offset:Int):NativeArray {})
@:overload(function(array:NativeArray,offset:Int,length:Int):NativeArray {})
static function array_slice( array:NativeArray, offset:Int, length:Int, preserve_keys:Bool ) : NativeArray ;

/**
@see http://php.net/manual/en/function.array-splice.php
**/
@:overload(function(array:NativeArray,offset:Int):NativeArray {})
@:overload(function(array:NativeArray,offset:Int,length:Int):NativeArray {})
static function array_splice( array:NativeArray, offset:Int, lenght:Int, replacement:Dynamic ) : NativeArray ;

/**
@see http://php.net/manual/en/function.array-unshift.php
**/
static function array_unshift( arr:NativeArray, value:Rest<Dynamic> ) : Int ;

/**
@see http://php.net/manual/en/function.array-values.php
**/
static function array_values( arr:NativeArray ) : NativeIndexedArray<Dynamic> ;

/**
@see http://php.net/manual/en/function.array-keys.php
**/
static function array_keys( arr:NativeArray ) : NativeIndexedArray<EitherType<String,Int>> ;

/**
@see http://php.net/manual/en/function.array-fill.php
**/
static function array_fill( start_index:Int, num:Int, value:Dynamic ) : NativeArray ;

/**
@see http://php.net/manual/en/function.usort.php
**/
static function usort( array:NativeArray, value_compare_func:Dynamic->Dynamic->Int ) : Bool ;

/**
@see http://php.net/manual/en/function.reset.php
**/
static function reset( array:NativeArray ) : Dynamic;

/**
@see http://php.net/manual/en/function.current.php
**/
static function current( array:NativeArray ) : Dynamic;

/**
@see http://php.net/manual/en/function.next.php
**/
static function next( array:NativeArray ) : Dynamic;

/**
@see http://php.net/manual/en/function.prev.php
**/
static function prev( array:NativeArray ) : Dynamic;

/**
@see http://php.net/manual/en/function.end.php
**/
static function end( array:NativeArray ) : Dynamic;

/**
@see http://php.net/manual/en/function.key.php
**/
static function key( array:NativeArray ) : EitherType<String,Int>;

/**
@see http://php.net/manual/en/function.each.php
**/
static function each( array:NativeArray ) : NativeArray;
}
6 changes: 3 additions & 3 deletions std/php7/Lib.hx
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ class Lib {
return untyped __call__("fpassthru", __call__("fopen", file, "r"));
}

public static function toPhpArray(a : Array<Dynamic>) : NativeArray {
throw "Not implemented";
public static inline function toPhpArray(a : Array<Dynamic>) : NativeArray {
return @:privateAccess a.arr;
}

public static inline function toHaxeArray(a : NativeArray) : Array<Dynamic> {
throw "Not implemented";
return @:privateAccess Array.wrap(a);
}

public static function hashOfAssociativeArray<T>(arr : NativeArray) : Map<String,T> {
Expand Down
42 changes: 40 additions & 2 deletions std/php7/NativeArray.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,44 @@
*/
package php7;

extern class NativeArray implements ArrayAccess<Dynamic> {
import haxe.extern.EitherType;

}
using php7.Global;


/**
Native PHP array.
**/
@:coreType @:runtimeValue abstract NativeArray {
public inline function new()
this = untyped __php__("[]");

@:arrayAccess function get(key:Scalar):Dynamic;
@:arrayAccess function set(key:Scalar, val:Dynamic):Dynamic;

public inline function iterator()
return new NativeArrayIterator(this);
}

/**
Allows iterating over native PHP array with Haxe for-loop
**/
private class NativeArrayIterator {
var arr:NativeArray;
var hasMore:Bool;

public inline function new( a:NativeArray ) {
arr = a;
hasMore = (arr.reset() != false);
}

public inline function hasNext() : Bool {
return hasMore;
}

public inline function next() : Dynamic {
var result = arr.current();
hasMore = (arr.next() != false);
return result;
}
}
Loading

0 comments on commit 09f84aa

Please sign in to comment.