Skip to content

Commit

Permalink
[php7] typed and untyped cast
Browse files Browse the repository at this point in the history
  • Loading branch information
RealyUniqueName committed Aug 13, 2016
1 parent 65f3bf9 commit 48a9d20
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 20 deletions.
33 changes: 27 additions & 6 deletions src/generators/genphp7.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

(**
Compatible with PHP 5.4+
*)

open Ast
open Type
open Common
Expand Down Expand Up @@ -98,7 +102,7 @@ let get_full_type_name (type_path:path) =
*)
let rec is_native_exception (target:Type.t) =
match follow target with
| TInst ({ cl_path = path }, _) -> path = native_exception_path
| TInst ({ cl_path = path }, _) when path = native_exception_path -> true
| TInst ({ cl_super = Some (parent, params) }, _) -> is_native_exception (TInst (parent, params))
| _ -> false

Expand Down Expand Up @@ -660,7 +664,7 @@ class virtual type_builder ctx wrapper =
| TArray (target, index) -> self#write_expr_array_access target index pos
| TBinop (operation, expr1, expr2) -> self#write_expr_binop operation expr1 expr2 pos
| TField (expr, access) -> self#write_expr_field expr access pos
| TTypeExpr mtype -> self#write (self#use (get_wrapper mtype)#get_type_path)
| TTypeExpr mtype -> self#write_expr_type mtype pos
| TParenthesis expr ->
self#write "(";
self#write_expr expr;
Expand Down Expand Up @@ -847,7 +851,7 @@ class virtual type_builder ctx wrapper =
begin
self#write "throw (is_object($__hx__throw = ";
self#write_expr expr;
self#write (") && $__hx__throw instanceof \Exception ? $__hx__throw : new " ^ (self#use haxe_exception_path) ^ "($__hx__throw))")
self#write (") && $__hx__throw instanceof \\Exception ? $__hx__throw : new " ^ (self#use haxe_exception_path) ^ "($__hx__throw))")
end
else
begin
Expand Down Expand Up @@ -912,8 +916,25 @@ class virtual type_builder ctx wrapper =
Writes TCast to output buffer
*)
method private write_expr_cast expr (mtype:module_type option) pos =
(* of texpr * module_type option *)
Printf.printf "%s" "dsffs"
match mtype with
| None -> self#write_expr expr
| Some mtype ->
let wrapper = get_wrapper mtype in
let type_name = self#use wrapper#get_type_path in
self#write_expr_type mtype pos;
self#write "->typedCast(";
self#write_expr expr;
self#write ")"
(**
Writes TTypeExpr to output buffer
*)
method private write_expr_type (mtype:module_type) pos =
let type_path = (get_wrapper mtype)#get_type_path in
match expr_hierarchy with
| _ :: { eexpr = TField _ } :: _ -> self#write (self#use type_path)
| _ ->
let type_name = String.escaped (get_full_type_name type_path) in
self#write ((self#use boot_class_path) ^ "::getClass('" ^ type_name ^ "')")
(**
Writes binary operation to output buffer
*)
Expand Down Expand Up @@ -1021,7 +1042,7 @@ class virtual type_builder ctx wrapper =
*)
method private write_expr_object_declaration fields pos =
match fields with
| [] -> self#write "new \StdClass()"
| [] -> self#write "new \\StdClass()"
| _ ->
self#write "(object)[\n";
self#indent_more;
Expand Down
50 changes: 40 additions & 10 deletions std/php7/Boot.hx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ package php7;
/**
Various Haxe->PHP compatibility utilities
**/
@:dox(hide)
@:keep
@:dox(hide)
class Boot {

/**
Expand All @@ -37,10 +37,11 @@ class Boot {
}

/**
Typed cast implementation.
**/
public static function typedCast<T> (expr:Dynamic, className:Class<T>) : T {
return expr;
Get Class<T> instance for PHP fully qualified class name (E.g. '\some\pack\MyClass')
It's always the same instance for the same `phpClassName`
*/
public static function getClass (phpClassName:String) : HxClass {
return HxClass.get(phpClassName);
}

/**
Expand Down Expand Up @@ -70,11 +71,40 @@ class Boot {


/**
Implements Haxe's String interface for PHP
*/
class StringImpl
{
Class<T> implementation for Haxe->PHP internals
*/
@:keep
@:dox(hide)
private class HxClass {
@:protected
static private var classes = new Map<String,HxClass>();

@:protected
var phpClassName : String;

/**
Get `HxClass` instance for specified PHP fully qualified class name (E.g. '\some\pack\MyClass')
*/
public static function get (phpClassName:String) : HxClass {
var cls = classes.get(phpClassName);
if (cls == null) {
cls = new HxClass(phpClassName);
classes.set(phpClassName, cls);
}

return cls;
}

}//class StringImpl
@:protected
private function new (phpClassName:String) : Void {
this.phpClassName = phpClassName;
}

/**
Implementation for `cast(value, Class<Dynamic>)`
@throws HException if `value` cannot be casted to this type
*/
public function tryCast (value:Dynamic) : Dynamic {
throw "Not implemented";
}
}
4 changes: 2 additions & 2 deletions std/php7/_std/haxe/ds/IntMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
package haxe.ds;

@:coreApi class IntMap<T> implements php.IteratorAggregate<T> implements haxe.Constraints.IMap<Int,T> {
@:coreApi class IntMap<T> implements php7.IteratorAggregate<T> implements haxe.Constraints.IMap<Int,T> {
@:analyzer(no_simplification)
private var h : ArrayAccess<Int>;

Expand Down Expand Up @@ -76,7 +76,7 @@ package haxe.ds;
/**
Implement IteratorAggregate for native php iteration
**/
#if php
#if php7
function getIterator() : Iterator<T> {
return iterator();
}
Expand Down
4 changes: 2 additions & 2 deletions std/php7/_std/haxe/ds/StringMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
package haxe.ds;

@:coreApi class StringMap<T> implements php.IteratorAggregate<T> implements haxe.Constraints.IMap<String,T> {
@:coreApi class StringMap<T> implements php7.IteratorAggregate<T> implements haxe.Constraints.IMap<String,T> {
@:analyzer(no_simplification)
private var h : ArrayAccess<T>;

Expand Down Expand Up @@ -76,7 +76,7 @@ package haxe.ds;
/**
Implement IteratorAggregate for native php iteration
**/
#if php
#if php7
function getIterator() : Iterator<T> {
return iterator();
}
Expand Down

0 comments on commit 48a9d20

Please sign in to comment.