Skip to content

Commit

Permalink
[php7] typed cast
Browse files Browse the repository at this point in the history
  • Loading branch information
RealyUniqueName committed Aug 13, 2016
1 parent 48a9d20 commit b99cfa3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 18 deletions.
23 changes: 9 additions & 14 deletions src/generators/genphp7.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1208,43 +1208,38 @@ class class_builder ctx (cls:tclass) =
E.g. for "class SomeClass { <BODY> }" writes <BODY> part.
*)
method private write_body =
let empty_lines_before_var = ref false (* write empty lines to visually separate groups of fields *)
and empty_lines_before_method = ref false in
let at_least_one_field_written = ref false in
let write_if_constant _ field =
match field.cf_kind with
| Var { v_read = AccInline; v_write = AccNever } -> self#write_field true field
| Var { v_read = AccInline; v_write = AccNever } ->
at_least_one_field_written := true;
self#write_field true field
| _ -> ()
and write_if_method is_static _ field =
match field.cf_kind with
| Var _ -> ()
| Method _ ->
if !empty_lines_before_method then
self#write_empty_lines
else
empty_lines_before_method := true;
if !at_least_one_field_written then self#write_empty_lines;
at_least_one_field_written := true;
self#write_field is_static field
and write_if_var is_static _ field =
match field.cf_kind with
| Var { v_read = AccInline; v_write = AccNever } -> ()
| Method _ -> ()
| Var _ ->
if !empty_lines_before_var then begin
self#write_empty_lines;
empty_lines_before_var := false
end;
at_least_one_field_written := true;
self#write_field is_static field
in
if not cls.cl_interface then begin
(* Inlined statc vars (constants) *)
PMap.iter (write_if_constant) cls.cl_statics;
empty_lines_before_var := true;
if !at_least_one_field_written then self#write_empty_lines;
(* Statc vars *)
PMap.iter (write_if_var true) cls.cl_statics;
empty_lines_before_var := true;
if !at_least_one_field_written then self#write_empty_lines;
(* instance vars *)
PMap.iter (write_if_var false) cls.cl_fields
end;
empty_lines_before_method := not !empty_lines_before_var;
(* Statc methods *)
PMap.iter (write_if_method true) cls.cl_statics;
(* Constructor *)
Expand Down
54 changes: 50 additions & 4 deletions std/php7/Boot.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
package php7;

using StringTools;

/**
Various Haxe->PHP compatibility utilities
Expand Down Expand Up @@ -85,7 +86,7 @@ private class HxClass {
/**
Get `HxClass` instance for specified PHP fully qualified class name (E.g. '\some\pack\MyClass')
*/
public static function get (phpClassName:String) : HxClass {
public static function get( phpClassName:String ) : HxClass {
var cls = classes.get(phpClassName);
if (cls == null) {
cls = new HxClass(phpClassName);
Expand All @@ -96,15 +97,60 @@ private class HxClass {
}

@:protected
private function new (phpClassName:String) : Void {
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";
public function tryCast( value:Dynamic ) : Dynamic {
switch (phpClassName) {
case '\\Int':
if (untyped __php__("is_int($value) || is_float($value)")) {
return untyped __php__("(int)$value");
}
case '\\Float':
if (untyped __php__("is_int($value) || is_float($value)")) {
return untyped __php__("(float)$value");
}
case '\\Bool':
if (untyped __php__("is_bool($value)")) {
return value;
}
case '\\String':
if (untyped __php__("is_string($value)")) {
return value;
}
case '\\php7\\NativeArray':
if (untyped __php__("is_array($value)")) {
return value;
}
case _:
if (untyped __php__("is_object($value) && $value instanceof $this->phpClassName")) {
return value;
}
}
throw 'Cannot cast ' + Std.string(value) + ' to ' + toString();
}

/**
Get string representation of this `Class`
*/
public function toString() : String {
return __toString();
}

/**
PHP magic method to get string representation of this `Class`
*/
public function __toString() : String {
var haxeType = phpClassName.replace('\\', '.');
if (haxeType.charAt(0) == '.') {
haxeType = haxeType.substr(1);
}

return haxeType;
}
}

0 comments on commit b99cfa3

Please sign in to comment.