Skip to content

Commit 9f61c3f

Browse files
authored
Merge pull request #4578 from BuckleScript/ffi_clean_up
ffi clean up
2 parents 2c44b73 + c1d5581 commit 9f61c3f

14 files changed

+316
-498
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_attributes.ml

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -174,31 +174,6 @@ let process_derive_type (attrs : t) : derive_attr * t =
174174
st, attr::acc
175175
)
176176

177-
(* let iter_process_derive_type (attrs : t) =
178-
let st = ref {explict_nonrec = false; bs_deriving = None } in
179-
Ext_list.iter attrs
180-
(fun ({txt ; loc}, payload as attr) ->
181-
match txt with
182-
| "bs.deriving"
183-
->
184-
let ost = !st in
185-
(match ost with
186-
| {bs_deriving = None } ->
187-
Bs_ast_invariant.mark_used_bs_attribute attr ;
188-
st :=
189-
{ost with
190-
bs_deriving = Some
191-
(Ast_payload.ident_or_record_as_config loc payload)}
192-
| {bs_deriving = Some _} ->
193-
Bs_syntaxerr.err loc Duplicated_bs_deriving)
194-
195-
| "nonrec" ->
196-
st :=
197-
{ !st with explict_nonrec = true }
198-
(* non bs attribute, no need to mark its use *)
199-
| _ -> ()
200-
) ;
201-
!st *)
202177

203178

204179
(* duplicated [bs.uncurry] [bs.string] not allowed,
@@ -253,26 +228,6 @@ let iter_process_bs_string_as (attrs : t) : string option =
253228
) ;
254229
!st
255230

256-
let iter_process_bs_string_as_ast (attrs : t) : Parsetree.expression option =
257-
let st = ref None in
258-
Ext_list.iter attrs
259-
(fun
260-
(({txt ; loc}, payload ) as attr ) ->
261-
match txt with
262-
| "bs.as"
263-
->
264-
if !st = None then
265-
match Ast_payload.is_single_string_as_ast payload with
266-
| None ->
267-
Bs_syntaxerr.err loc Expect_string_literal
268-
| Some _ as v ->
269-
Bs_ast_invariant.mark_used_bs_attribute attr ;
270-
st:= v
271-
else
272-
Bs_syntaxerr.err loc Duplicated_bs_as
273-
| _ -> ()
274-
) ;
275-
!st
276231

277232
let has_bs_optional (attrs : t) : bool =
278233
Ext_list.exists attrs (fun

jscomp/syntax/ast_attributes.mli

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ val iter_process_bs_string_int_unwrap_uncurry :
7171
val iter_process_bs_string_as :
7272
t -> string option
7373

74-
val iter_process_bs_string_as_ast :
75-
t ->
76-
Parsetree.expression option
74+
7775

7876
val has_bs_optional :
7977
t -> bool

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)