Skip to content

Commit 24e171b

Browse files
authored
Merge pull request #4558 from BuckleScript/string_poly_var
first class string types
2 parents 86a9cfe + 8dde117 commit 24e171b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+8969
-8963
lines changed

jscomp/core/js_dump.ml

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ let exp_need_paren (e : J.expression) =
167167

168168
| Raw_js_code {code_info = Exp _}
169169
| Fun _
170-
| Caml_block (_,_,_, (Blk_record _ | Blk_module _ | Blk_poly_var _ | Blk_extension | Blk_record_ext _ | Blk_record_inlined _ | Blk_constructor _ ))
170+
| Caml_block (_,_,_, (Blk_record _ | Blk_module _ | Blk_poly_var | Blk_extension | Blk_record_ext _ | Blk_record_inlined _ | Blk_constructor _ ))
171171
| Object _ -> true
172172
| Raw_js_code {code_info = Stmt _ }
173173
| Length _
@@ -823,23 +823,17 @@ and expression_desc cxt ~(level:int) f x : cxt =
823823
expression_desc cxt ~level f (Object
824824
((Ext_list.combine_array fields el (fun i -> Js_op.Lit i))))
825825

826-
| Caml_block(el,_,_, Blk_poly_var name) ->
826+
| Caml_block(el,_,_, Blk_poly_var ) ->
827827
begin match el with
828-
| [hash;value] ->
828+
| [{expression_desc = Str (_,name)};value] ->
829829
expression_desc
830830
cxt
831831
~level
832832
f
833-
(Object
834-
((Js_op.Lit Literals.polyvar_hash,
835-
if !Js_config.debug then hash
836-
else {hash with comment = Some name}
837-
) ::
838-
(Js_op.Lit Literals.polyvar_value, value) ::
839-
if !Js_config.debug then
840-
[name_symbol, E.str name]
841-
else []
842-
)
833+
(Object [
834+
Js_op.Lit Literals.polyvar_hash, E.str name;
835+
Lit Literals.polyvar_value, value
836+
]
843837
)
844838
| _ -> assert false
845839
end

jscomp/core/js_exp_make.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ let dummy_obj ?comment (info : Lam_tag_info.t) : t =
255255
| Blk_module _
256256
| Blk_constructor _
257257
| Blk_record_inlined _
258-
| Blk_poly_var _
258+
| Blk_poly_var
259259
| Blk_extension
260260
| Blk_record_ext _
261261
->
@@ -856,6 +856,8 @@ let string_equal ?comment (e0 : t) (e1 : t) : t =
856856
let is_type_number ?comment (e : t) : t =
857857
string_equal ?comment (typeof e) (str "number")
858858

859+
let is_type_string ?comment (e : t) : t =
860+
string_equal ?comment (typeof e) (str "string")
859861

860862
(* we are calling [Caml_primitive.primitive_name], since it's under our
861863
control, we should make it follow the javascript name convention, and

jscomp/core/js_exp_make.mli

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,12 @@ val int_equal : ?comment:string -> t -> t -> t
260260
val string_equal : ?comment:string -> t -> t -> t
261261
val eq_null_undefined_boolean: ?comment:string -> t -> t -> t
262262
val neq_null_undefined_boolean: ?comment:string -> t -> t -> t
263-
val is_type_number : ?comment:string -> t -> t
263+
264+
val is_type_number :
265+
?comment:string -> t -> t
266+
val is_type_string :
267+
?comment:string -> t -> t
268+
264269
val typeof : ?comment:string -> t -> t
265270

266271
val to_int32 : ?comment:string -> t -> t

jscomp/core/js_of_lam_variant.ml

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ type arg_expression =
3131
| Splice2 of E.t * E.t
3232

3333
(* we need destruct [undefined] when input is optional *)
34-
let eval (arg : J.expression) (dispatches : (int * string) list ) : E.t =
34+
let eval (arg : J.expression) (dispatches : (Ast_compatible.hash_label * string) list ) : E.t =
3535
if arg == E.undefined then E.undefined else
3636
match arg.expression_desc with
37-
| Number (Int {i} | Uint i) ->
38-
E.str (Ext_list.assoc_by_int dispatches (Int32.to_int i) None)
37+
| Str (_,s) ->
38+
E.str (Ext_list.assoc_by_string dispatches s None)
3939
| _ ->
4040
E.of_block
41-
[(S.int_switch arg
41+
[(S.string_switch arg
4242
(Ext_list.map dispatches (fun (i,r) ->
4343
{J.switch_case = i ;
4444
switch_body = [S.return_stmt (E.str r)];
@@ -48,17 +48,16 @@ let eval (arg : J.expression) (dispatches : (int * string) list ) : E.t =
4848

4949
(** invariant: optional is not allowed in this case *)
5050
(** arg is a polyvar *)
51-
let eval_as_event (arg : J.expression) (dispatches : (int * string) list ) =
51+
let eval_as_event (arg : J.expression) (dispatches : (Ast_compatible.hash_label * string) list ) =
5252
match arg.expression_desc with
53-
| Array ([{expression_desc = Number (Int {i} | Uint i)}; cb], _)
54-
| Caml_block([{expression_desc = Number (Int {i} | Uint i)}; cb], _, _, _)
55-
-> (* FIXME - to polyvar*)
56-
let v = Ext_list.assoc_by_int dispatches (Int32.to_int i) None in
53+
| Caml_block([{expression_desc = Str(_,s)}; cb], _, _, Blk_poly_var ) when Js_analyzer.no_side_effect_expression cb
54+
->
55+
let v = Ext_list.assoc_by_string dispatches s None in
5756
Splice2(E.str v , cb )
5857
| _ ->
5958
Splice2
6059
(E.of_block
61-
[(S.int_switch (E.poly_var_tag_access arg)
60+
[(S.string_switch (E.poly_var_tag_access arg)
6261
(Ext_list.map dispatches (fun (i,r) ->
6362
{J.switch_case = i ;
6463
switch_body = [S.return_stmt (E.str r)];
@@ -80,14 +79,14 @@ let eval_as_event (arg : J.expression) (dispatches : (int * string) list ) =
8079
*)
8180

8281
(* we need destruct [undefined] when input is optional *)
83-
let eval_as_int (arg : J.expression) (dispatches : (int * int) list ) : E.t =
82+
let eval_as_int (arg : J.expression) (dispatches : (Ast_compatible.hash_label * int) list ) : E.t =
8483
if arg == E.undefined then E.undefined else
8584
match arg.expression_desc with
86-
| Number (Int {i} | Uint i) ->
87-
E.int (Int32.of_int (Ext_list.assoc_by_int dispatches (Int32.to_int i) None))
85+
| Str(_,i) ->
86+
E.int (Int32.of_int (Ext_list.assoc_by_string dispatches i None))
8887
| _ ->
8988
E.of_block
90-
[(S.int_switch arg
89+
[(S.string_switch arg
9190
(Ext_list.map dispatches (fun (i,r) ->
9291
{J.switch_case = i ;
9392
switch_body = [S.return_stmt (E.int (Int32.of_int r))];

jscomp/core/js_of_lam_variant.mli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ type arg_expression =
3030
| Splice2 of J.expression * J.expression
3131

3232
val eval :
33-
J.expression -> (int * string) list -> J.expression
33+
J.expression -> (Ast_compatible.hash_label * string) list -> J.expression
3434
val eval_as_event :
35-
J.expression -> (int * string) list -> arg_expression
35+
J.expression -> (Ast_compatible.hash_label * string) list -> arg_expression
3636
val eval_as_int :
37-
J.expression -> (int * int) list -> J.expression
37+
J.expression -> (Ast_compatible.hash_label * int) list -> J.expression
3838
val eval_as_unwrap : J.expression -> J.expression

jscomp/core/lam_analysis.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
132132
| Parrayrefs
133133
(* Test if the argument is a block or an immediate integer *)
134134
| Pisint
135+
| Pis_poly_var_const
135136
(* Test if the (integer) argument is outside an interval *)
136137
| Pisout
137138
| Pbintofint _

jscomp/core/lam_compile_const.ml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,12 @@ and translate (x : Lam_constant.t ) : J.expression =
8989

9090

9191
| Const_pointer (c,pointer_info) ->
92-
E.int ?comment:(Lam_compile_util.comment_of_pointer_info pointer_info)
93-
(Int32.of_int c )
94-
92+
begin match pointer_info with
93+
| Pt_variant {name} -> E.str name
94+
| _ ->
95+
E.int ?comment:(Lam_compile_util.comment_of_pointer_info pointer_info)
96+
(Int32.of_int c )
97+
end
9598
| Const_block(tag, tag_info, xs ) ->
9699
Js_of_lam_block.make_block NA tag_info
97100
(E.small_int tag) (Ext_list.map xs translate)

jscomp/core/lam_compile_primitive.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,8 @@ let translate loc
579579
(** FIXME, this can be removed later *)
580580
| Pisint ->
581581
E.is_type_number (Ext_list.singleton_exn args)
582+
| Pis_poly_var_const ->
583+
E.is_type_string (Ext_list.singleton_exn args)
582584
| Pctconst ct ->
583585
(match ct with
584586
| Big_endian -> E.bool Sys.big_endian

jscomp/core/lam_compile_util.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ let comment_of_tag_info (x : Lam_tag_info.t) =
4545
| Blk_constructor {name = n} -> Some n
4646
| Blk_tuple -> Some "tuple"
4747
| Blk_class -> Some "class"
48-
| Blk_poly_var _ -> None
48+
| Blk_poly_var -> None
4949
| Blk_record _ -> None
5050
| Blk_record_inlined {name = ctor} -> Some ctor
5151
| Blk_record_ext _ -> None

jscomp/core/lam_constant_convert.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@ let rec convert_constant ( const : Lambda.structured_constant) : Lam_constant.t
7272
let t : Lam_tag_info.t = Blk_array in
7373
Const_block (i,t, Ext_list.map xs convert_constant )
7474
| Blk_poly_var s ->
75-
let t : Lam_tag_info.t = Blk_poly_var s in
76-
Const_block (i,t, Ext_list.map xs convert_constant )
75+
begin match xs with
76+
| [_; value] ->
77+
let t : Lam_tag_info.t = Blk_poly_var in
78+
Const_block (i,t, [Const_string s; convert_constant value] )
79+
| _ -> assert false
80+
end
7781
| Blk_record s ->
7882
let t : Lam_tag_info.t = Blk_record s in
7983
Const_block (i,t, Ext_list.map xs convert_constant )

0 commit comments

Comments
 (0)