Skip to content

Commit 6d36a98

Browse files
committed
clean up ffi handling
ideally, we should branch bs.obj and normal FFI as early as we can
1 parent ed6a395 commit 6d36a98

8 files changed

+69
-59
lines changed

jscomp/core/js_of_lam_variant.ml

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,23 @@ 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 : (Ast_compatible.hash_label * string) list option) : E.t =
35-
match dispatches with
36-
| None -> arg
37-
| Some dispatches ->
38-
if arg == E.undefined then E.undefined
39-
else
40-
match arg.expression_desc with
41-
| Str (_,s) ->
42-
let s =
43-
(Ext_list.assoc_by_string dispatches s None) in
44-
E.str s
45-
| _ ->
46-
E.of_block
47-
[(S.string_switch arg
48-
(Ext_list.map dispatches (fun (i,r) ->
49-
{J.switch_case = i ;
50-
switch_body = [S.return_stmt (E.str r)];
51-
should_break = false; (* FIXME: if true, still print break*)
52-
comment = None;
53-
})))]
34+
let eval (arg : J.expression) (dispatches : (Ast_compatible.hash_label * string) list ) : E.t =
35+
if arg == E.undefined then E.undefined
36+
else
37+
match arg.expression_desc with
38+
| Str (_,s) ->
39+
let s =
40+
(Ext_list.assoc_by_string dispatches s None) in
41+
E.str s
42+
| _ ->
43+
E.of_block
44+
[(S.string_switch arg
45+
(Ext_list.map dispatches (fun (i,r) ->
46+
{J.switch_case = i ;
47+
switch_body = [S.return_stmt (E.str r)];
48+
should_break = false; (* FIXME: if true, still print break*)
49+
comment = None;
50+
})))]
5451

5552
(** invariant: optional is not allowed in this case *)
5653
(** arg is a polyvar *)

jscomp/core/js_of_lam_variant.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type arg_expression =
3131

3232
val eval :
3333
J.expression ->
34-
(Ast_compatible.hash_label * string) list option
34+
(Ast_compatible.hash_label * string) list
3535
-> J.expression
3636

3737
val eval_as_event :

jscomp/core/lam_compile_external_call.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ let ocaml_to_js_eff
118118
[]
119119
else
120120
[arg])
121-
| Poly_var {has_payload = false ; descr = dispatches} ->
122-
Splice1 (Js_of_lam_variant.eval arg dispatches),[]
123-
| Poly_var {has_payload = true ; descr = dispatches} ->
124-
Js_of_lam_variant.eval_as_event arg dispatches,[]
121+
| Poly_var_string {descr } ->
122+
Splice1 (Js_of_lam_variant.eval arg descr),[]
123+
| Poly_var {descr} ->
124+
Js_of_lam_variant.eval_as_event arg descr,[]
125125
(* FIXME: encode invariant below in the signature*)
126126
(* length of 2
127127
- the poly var tag

jscomp/main/builtin_cmi_datasets.ml

Lines changed: 12 additions & 12 deletions
Large diffs are not rendered by default.

jscomp/syntax/ast_external_process.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ let process_obj
437437
{obj_arg_label = External_arg_spec.obj_label s; obj_arg_type},
438438
{param_type with ty = new_ty}::arg_types,
439439
(({Asttypes.txt = name; loc}, [], Ast_literal.type_int ~loc ()) :: result_types)
440-
| Poly_var { has_payload = false ; _} ->
440+
| Poly_var_string _ ->
441441
let s = Lam_methname.translate name in
442442
{obj_arg_label = External_arg_spec.obj_label s; obj_arg_type},
443443
{param_type with ty = new_ty }::arg_types,
@@ -446,7 +446,7 @@ let process_obj
446446
Location.raise_errorf ~loc
447447
"The combination of [@@bs.obj], [@@bs.uncurry] is not supported yet"
448448
| Extern_unit -> assert false
449-
| Poly_var { has_payload = true ; _}
449+
| Poly_var _
450450
->
451451
Location.raise_errorf ~loc
452452
"bs.obj label %s does not support such arg type" name
@@ -470,7 +470,7 @@ let process_obj
470470
{obj_arg_label = External_arg_spec.optional s ; obj_arg_type },
471471
param_type :: arg_types,
472472
(({Asttypes.txt = name; loc}, [], Ast_comb.to_undefined_type loc @@ Ast_literal.type_int ~loc ()) :: result_types)
473-
| Poly_var {has_payload = false ; _} ->
473+
| Poly_var_string _ ->
474474
let s = Lam_methname.translate name in
475475
{obj_arg_label = External_arg_spec.optional s ; obj_arg_type },
476476
param_type::arg_types,
@@ -482,7 +482,7 @@ let process_obj
482482
Location.raise_errorf ~loc
483483
"The combination of [@@bs.obj], [@@bs.uncurry] is not supported yet"
484484
| Extern_unit -> assert false
485-
| Poly_var {has_payload = true; _}
485+
| Poly_var _
486486
->
487487
Location.raise_errorf ~loc
488488
"bs.obj label %s does not support such arg type" name
@@ -896,7 +896,7 @@ let handle_attributes
896896
| Optional s ->
897897
let arg_type = get_opt_arg_type ~nolabel:false ty in
898898
begin match arg_type with
899-
| Poly_var {has_payload = true; _} ->
899+
| Poly_var _ ->
900900
(* ?x:([`x of int ] [@bs.string]) does not make sense *)
901901
Location.raise_errorf
902902
~loc

jscomp/syntax/ast_polyvar.ml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,15 @@ let map_row_fields_into_strings ptyp_loc
112112
| `NonNull ->
113113
let has_payload = case = `NonNull in
114114
let descr = if !has_bs_as then Some result else None in
115-
if not has_payload && descr = None then begin
116-
Location.prerr_warning ptyp_loc (Bs_ffi_warning "bs.string is redundant here, you can safely remove it")
117-
end;
118-
External_arg_spec.Poly_var
119-
{has_payload ;
120-
descr }
115+
match has_payload, descr with
116+
| false, None ->
117+
Location.prerr_warning ptyp_loc (Bs_ffi_warning "bs.string is redundant here, you can safely remove it");
118+
Nothing
119+
| false , Some descr ->
120+
External_arg_spec.Poly_var_string {descr }
121+
| true, _ ->
122+
External_arg_spec.Poly_var
123+
{ descr }
121124

122125
let is_enum row_fields =
123126
List.for_all (fun (x : Parsetree.row_field) ->

jscomp/syntax/external_arg_spec.ml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,22 @@ type label =
4646
(* it will be ignored , side effect will be recorded *)
4747

4848

49-
49+
(* This type is used to give some meta info on each argument *)
5050
type attr =
51-
| Poly_var of {
52-
has_payload : bool ;
51+
| Poly_var_string of {
5352
descr :
5453
(Ast_compatible.hash_label * string) list
55-
option
56-
}
54+
(* introduced by attributes bs.string
55+
and bs.as
56+
*)
57+
}
58+
| Poly_var of {
59+
descr :
60+
(Ast_compatible.hash_label * string) list option
61+
(* introduced by attributes bs.string
62+
and bs.as
63+
*)
64+
}
5765
(* `a does not have any value*)
5866
| Int of (Ast_compatible.hash_label * int ) list (* ([`a | `b ] [@bs.int])*)
5967
| Arg_cst of cst

jscomp/syntax/external_arg_spec.mli

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ type label = private
4141

4242

4343
type attr =
44-
| Poly_var of {
45-
has_payload : bool ;
46-
descr :
47-
(Ast_compatible.hash_label * string)
48-
list option
49-
}
44+
| Poly_var_string of {
45+
descr :
46+
(Ast_compatible.hash_label * string) list
47+
}
48+
| Poly_var of {
49+
descr :
50+
(Ast_compatible.hash_label * string) list option
51+
}
5052
| Int of (Ast_compatible.hash_label * int ) list (* ([`a | `b ] [@bs.int])*)
5153
| Arg_cst of cst
5254
| Fn_uncurry_arity of int (* annotated with [@bs.uncurry ] or [@bs.uncurry 2]*)

0 commit comments

Comments
 (0)