Permalink
Browse files

Match HPHPC in terms of varray darray default value text reflection

Summary: as per title

Reviewed By: jamesjwu

Differential Revision: D7325656

fbshipit-source-id: 9392687433c3d7fa2703768e7bbea6c34f85a0c6
  • Loading branch information...
oulgen authored and hhvm-bot committed Mar 20, 2018
1 parent dfd3512 commit 29d40c5839899679adb80d41b27de58648a9e4bb
@@ -17,6 +17,9 @@ module SN = Naming_special_names
module ULS = Unique_list_string
open H
let hack_arr_dv_arrs () =
Hhbc_options.hack_arr_dv_arrs !Hhbc_options.compiler_options
(* Generic helpers *)
let sep pieces = String.concat " " pieces
@@ -1109,6 +1112,15 @@ and string_of_param_default_value ~env expr =
Some (string_of_param_default_value ~env e)
| _ -> None
in
let is_static_expr () =
let env = Option.value ~default:Emit_env.empty env.codegen_env in
Option.is_some @@
Ast_constant_folder.expr_to_opt_typed_value
~allow_maps:true
~restrict_keys:(hack_arr_dv_arrs ())
(Emit_env.get_namespace env)
expr
in
let escape_char_for_printing = function
| '\\' | '$' | '"' -> "\\\\"
| '\n' | '\r' | '\t' -> "\\"
@@ -1270,7 +1282,10 @@ and string_of_param_default_value ~env expr =
let h = string_of_hint ~ns:true h in
e ^ o ^ h
| A.Varray es ->
if env.in_xhp
(* TODO(T27176468):
* Everything should be printed as varray not only static ones *)
if env.in_xhp ||
(Emit_env.is_hh_syntax_enabled () && not (is_static_expr ()))
then begin
let es = List.map (string_of_param_default_value ~env) es in
"varray[" ^ (String.concat ", " es) ^ "]"
@@ -1281,8 +1296,11 @@ and string_of_param_default_value ~env expr =
(p, A.Array (List.mapi (fun i e -> A.AFkvalue (index i, e)) es))
end
| A.Darray es ->
(* TODO(T27176468):
* Everything should be printed as darray not only static ones *)
let es = List.map (fun (e1, e2) -> A.AFkvalue (e1, e2)) es in
if env.in_xhp
if env.in_xhp ||
(Emit_env.is_hh_syntax_enabled () && not (is_static_expr ()))
then "darray[" ^ (string_of_afield_list ~env es) ^ "]"
else string_of_param_default_value ~env @@ (p, A.Array es)
| A.Import (fl, e) ->
@@ -0,0 +1,30 @@
<?hh
class C {
const A = 1;
}
function f(
$p0 = varray[C::A, 1],
$p1 = varray[2, 1, 3],
$p2 = varray[1, 2, 3]
) {}
function g(
$p0 = darray[1 => C::A, 2 => 1],
$p1 = darray[1 => 2, 4 => 1, 3 => 3],
$p2 = darray[1 => 1, "hi" => 2, 5 => 3]
) {}
$f = new ReflectionFunction("f");
$p0 = $f->getParameters()[0]->getDefaultValueText();
$p1 = $f->getParameters()[1]->getDefaultValueText();
$p2 = $f->getParameters()[2]->getDefaultValueText();
var_dump($p0, $p1, $p2);
$g = new ReflectionFunction("g");
$p0 = $g->getParameters()[0]->getDefaultValueText();
$p1 = $g->getParameters()[1]->getDefaultValueText();
$p2 = $g->getParameters()[2]->getDefaultValueText();
var_dump($p0, $p1, $p2);
@@ -0,0 +1,23 @@
string(16) "varray[\C::A, 1]"
string(39) "array (
0 => 2,
1 => 1,
2 => 3,
)"
string(39) "array (
0 => 1,
1 => 2,
2 => 3,
)"
string(26) "darray[1 => \C::A, 2 => 1]"
string(39) "array (
1 => 2,
4 => 1,
3 => 3,
)"
string(42) "array (
1 => 1,
"hi" => 2,
5 => 3,
)"

0 comments on commit 29d40c5

Please sign in to comment.