Skip to content

Commit

Permalink
refactor: external_arg_spec (#1227)
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro authored Nov 18, 2024
1 parent 13dd405 commit 0f1a7a7
Show file tree
Hide file tree
Showing 20 changed files with 477 additions and 438 deletions.
29 changes: 17 additions & 12 deletions jscomp/common/external_arg_spec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,22 @@ type cst =
| Arg_string_lit of string
| Arg_js_literal of string

type label_noname = Arg_label | Arg_empty | Arg_optional
module Arg_label = struct
type t = Arg_label | Arg_empty | Arg_optional
end

type label =
| Obj_label of { name : string }
| Obj_empty
| Obj_optional of { name : string; for_sure_no_nested_option : bool }
module Obj_label = struct
type t =
| Obj_label of { name : string }
| Obj_empty
| Obj_optional of { name : string; for_sure_no_nested_option : bool }

let empty = Obj_empty
let obj name = Obj_label { name }

let optional ~for_sure_no_nested_option name =
Obj_optional { name; for_sure_no_nested_option }
end

(* it will be ignored , side effect will be recorded *)

Expand Down Expand Up @@ -67,13 +77,8 @@ type 'a param = { arg_type : attr; arg_label : 'a }
let cst_obj_literal s = Arg_js_literal s
let cst_int i = Arg_int_lit i
let cst_string s = Arg_string_lit s
let empty_label = Obj_empty
let obj_label name = Obj_label { name }

let optional for_sure_no_nested_option name =
Obj_optional { name; for_sure_no_nested_option }

let empty_kind obj_arg_type =
{ arg_label = empty_label; arg_type = obj_arg_type }
{ arg_label = Obj_label.empty; arg_type = obj_arg_type }

let dummy = { arg_type = Nothing; arg_label = Arg_empty }
let dummy = { arg_type = Nothing; arg_label = Arg_label.Arg_empty }
30 changes: 18 additions & 12 deletions jscomp/common/external_arg_spec.mli
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ type cst = private
| Arg_string_lit of string
| Arg_js_literal of string

type label = private
| Obj_label of { name : string }
| Obj_empty
| Obj_optional of { name : string; for_sure_no_nested_option : bool }
(** it will be ignored , side effect will be recorded *)

type attr =
| Poly_var_string of { descr : (string * string) list }
| Poly_var of { descr : (string * string) list option }
Expand All @@ -46,14 +40,26 @@ type attr =
| Ignore
| Unwrap

type label_noname = Arg_label | Arg_empty | Arg_optional
module Arg_label : sig
type t = Arg_label | Arg_empty | Arg_optional
end

module Obj_label : sig
type t = private
| Obj_label of { name : string }
| Obj_empty
| Obj_optional of { name : string; for_sure_no_nested_option : bool }
(** it will be ignored , side effect will be recorded *)

val empty : t
val obj : string -> t
val optional : for_sure_no_nested_option:bool -> string -> t
end

type 'a param = { arg_type : attr; arg_label : 'a }

val cst_obj_literal : string -> cst
val cst_int : int -> cst
val cst_string : string -> cst
val empty_label : label
val obj_label : string -> label
val optional : bool -> string -> label
val empty_kind : attr -> label param
val dummy : label_noname param
val empty_kind : attr -> Obj_label.t param
val dummy : Arg_label.t param
10 changes: 5 additions & 5 deletions jscomp/common/external_ffi_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type arg_type = External_arg_spec.attr
design a more compact representation so that it is also easy to seralize by
hand *)

type arg_label = External_arg_spec.label
type arg_label = External_arg_spec.Obj_label.t

type external_spec =
| Js_var of {
Expand Down Expand Up @@ -94,15 +94,15 @@ type return_wrapper =
| Return_replaced_with_unit

type params =
| Params of External_arg_spec.label_noname External_arg_spec.param list
| Params of External_arg_spec.Arg_label.t External_arg_spec.param list
| Param_number of int

type t =
| Ffi_mel of params * return_wrapper * external_spec
(** [Ffi_mel(args,return,attr) ]
[return] means return value is unit or not,
[true] means is [unit] *)
| Ffi_obj_create of External_arg_spec.label External_arg_spec.param list
| Ffi_obj_create of External_arg_spec.Obj_label.t External_arg_spec.param list
| Ffi_inline_const of Lam_constant.t
| Ffi_normal
(* When it's normal, it is handled as normal c functional ffi call *)
Expand Down Expand Up @@ -241,7 +241,7 @@ let inline_float_primitive (i : string) : string list =

let ffi_mel =
let rec ffi_mel_aux acc
(params : External_arg_spec.label_noname External_arg_spec.param list) =
(params : External_arg_spec.Arg_label.t External_arg_spec.param list) =
match params with
| { arg_type = Nothing; arg_label = Arg_empty }
(* same as External_arg_spec.dummy*)
Expand All @@ -250,7 +250,7 @@ let ffi_mel =
| _ :: _ -> -1
| [] -> acc
in
fun (params : External_arg_spec.label_noname External_arg_spec.param list)
fun (params : External_arg_spec.Arg_label.t External_arg_spec.param list)
return attr ->
let n = ffi_mel_aux 0 params in
if n < 0 then Ffi_mel (Params params, return, attr)
Expand Down
19 changes: 9 additions & 10 deletions jscomp/common/external_ffi_types.mli
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type external_module_name = {
}

type arg_type = External_arg_spec.attr
type arg_label = External_arg_spec.label
type arg_label = External_arg_spec.Obj_label.t

type external_spec =
| Js_var of {
Expand Down Expand Up @@ -86,17 +86,15 @@ type return_wrapper =
| Return_replaced_with_unit

type params =
| Params of External_arg_spec.label_noname External_arg_spec.param list
| Params of External_arg_spec.Arg_label.t External_arg_spec.param list
| Param_number of int

type t = private
| Ffi_mel of params * return_wrapper * external_spec
| Ffi_obj_create of External_arg_spec.label External_arg_spec.param list
| Ffi_obj_create of External_arg_spec.Obj_label.t External_arg_spec.param list
| Ffi_inline_const of Lam_constant.t
| Ffi_normal
(* When it's normal, it is handled as normal c functional ffi call *)

(* val name_of_ffi : external_spec -> string *)
(* Ffi_normal represents a C functional ffi call *)

val check_ffi : loc:Location.t -> external_spec -> bool
val to_string : t -> string
Expand All @@ -111,18 +109,19 @@ val inline_int64_primitive : int64 -> string list
val inline_float_primitive : string -> string list

val ffi_mel :
External_arg_spec.label_noname External_arg_spec.param list ->
External_arg_spec.Arg_label.t External_arg_spec.param list ->
return_wrapper ->
external_spec ->
t

val ffi_mel_as_prims :
External_arg_spec.label_noname External_arg_spec.param list ->
External_arg_spec.Arg_label.t External_arg_spec.param list ->
return_wrapper ->
external_spec ->
string list

val ffi_obj_create : External_arg_spec.label External_arg_spec.param list -> t
val ffi_obj_create :
External_arg_spec.Obj_label.t External_arg_spec.param list -> t

val ffi_obj_as_prims :
External_arg_spec.label External_arg_spec.param list -> string list
External_arg_spec.Obj_label.t External_arg_spec.param list -> string list
6 changes: 4 additions & 2 deletions jscomp/core/js_cmj_format.ml
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,10 @@ let rec binarySearchAux arr lo hi (key : string) =
let loVal = Array.unsafe_get arr lo in
if loVal.name = key then get_result loVal else not_found key
else binarySearchAux arr lo mid key
else if (* a[lo] =< a[mid] < key <= a[hi] *)
lo = mid then
else if
(* a[lo] =< a[mid] < key <= a[hi] *)
lo = mid
then
let hiVal = Array.unsafe_get arr hi in
if hiVal.name = key then get_result hiVal else not_found key
else binarySearchAux arr mid hi key
Expand Down
3 changes: 1 addition & 2 deletions jscomp/core/lam_analysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,7 @@ let ok_to_inline_fun_when_app (m : Lam.lfunction) (args : Lam.t list) =
|| (args_all_const args && s < 10 && no_side_effects body))

(* TODO: We can relax this a bit later,
but decide whether to inline it later in the call site
*)
but decide whether to inline it later in the call site *)
let safe_to_inline (lam : Lam.t) =
match lam with
| Lfunction _ -> true
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/lam_compile_external_call.ml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ let append_list x xs =
This would not work with [NonNullString]
*)
let ocaml_to_js_eff ~(arg_label : Melange_ffi.External_arg_spec.label_noname)
let ocaml_to_js_eff ~(arg_label : Melange_ffi.External_arg_spec.Arg_label.t)
~(arg_type : Melange_ffi.External_arg_spec.attr) (raw_arg : E.t) :
arg_expression * E.t list =
let arg =
Expand Down Expand Up @@ -154,7 +154,7 @@ let empty_pair = ([], [])
let add_eff eff e = match eff with None -> e | Some v -> E.seq v e

type specs =
Melange_ffi.External_arg_spec.label_noname Melange_ffi.External_arg_spec.param
Melange_ffi.External_arg_spec.Arg_label.t Melange_ffi.External_arg_spec.param
list

type exprs = E.t list
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/lam_compile_external_call.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

val ocaml_to_js_eff :
arg_label:Melange_ffi.External_arg_spec.label_noname ->
arg_label:Melange_ffi.External_arg_spec.Arg_label.t ->
arg_type:Melange_ffi.External_arg_spec.attr ->
J.expression ->
Js_of_lam_variant.arg_expression * J.expression list
(** Compile ocaml external function call to JS IR. *)

val translate_ffi :
Lam_compile_context.t ->
Melange_ffi.External_arg_spec.label_noname Melange_ffi.External_arg_spec.param
Melange_ffi.External_arg_spec.Arg_label.t Melange_ffi.External_arg_spec.param
list ->
Melange_ffi.External_ffi_types.external_spec ->
J.expression list ->
Expand Down
8 changes: 5 additions & 3 deletions jscomp/core/lam_compile_external_obj.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ module S = Js_stmt_make
(* TODO: check stackoverflow *)
let assemble_obj_args
(labels :
Melange_ffi.External_arg_spec.label Melange_ffi.External_arg_spec.param
Melange_ffi.External_arg_spec.Obj_label.t
Melange_ffi.External_arg_spec.param
list) (args : J.expression list) : J.block * J.expression =
let rec aux
(labels :
Melange_ffi.External_arg_spec.label Melange_ffi.External_arg_spec.param
Melange_ffi.External_arg_spec.Obj_label.t
Melange_ffi.External_arg_spec.param
list) args : (Js_op.property_name * E.t) list * J.expression list * _ =
match (labels, args) with
| [], [] -> ([], [], [])
Expand Down Expand Up @@ -109,7 +111,7 @@ let assemble_obj_args
:: List.concat_map
~f:(fun
( (xlabel :
Melange_ffi.External_arg_spec.label
Melange_ffi.External_arg_spec.Obj_label.t
Melange_ffi.External_arg_spec.param),
(arg : J.expression) )
->
Expand Down
3 changes: 2 additions & 1 deletion jscomp/core/lam_compile_external_obj.mli
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
*)

val assemble_obj_args :
Melange_ffi.External_arg_spec.label Melange_ffi.External_arg_spec.param list ->
Melange_ffi.External_arg_spec.Obj_label.t Melange_ffi.External_arg_spec.param
list ->
J.expression list ->
J.block * J.expression
(* It returns a block in cases we need set the property dynamically: we need
Expand Down
6 changes: 3 additions & 3 deletions jscomp/core/lam_ffi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*)
let rec no_auto_uncurried_arg_types
(xs :
Melange_ffi.External_arg_spec.label_noname
Melange_ffi.External_arg_spec.Arg_label.t
Melange_ffi.External_arg_spec.param
list) =
match xs with
Expand All @@ -41,7 +41,7 @@ let rec no_auto_uncurried_arg_types

let rec transform_uncurried_arg_type loc
(arg_types :
Melange_ffi.External_arg_spec.label_noname
Melange_ffi.External_arg_spec.Arg_label.t
Melange_ffi.External_arg_spec.param
list) (args : Lam.t list) =
match (arg_types, args) with
Expand Down Expand Up @@ -74,7 +74,7 @@ let handle_mel_non_obj_ffi =
| Return_unset | Return_identity -> result
in
fun (arg_types :
Melange_ffi.External_arg_spec.label_noname
Melange_ffi.External_arg_spec.Arg_label.t
Melange_ffi.External_arg_spec.param
list) (result_type : Melange_ffi.External_ffi_types.return_wrapper) ffi
args loc prim_name ~dynamic_import ->
Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/lam_ffi.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

val handle_mel_non_obj_ffi :
Melange_ffi.External_arg_spec.label_noname Melange_ffi.External_arg_spec.param
Melange_ffi.External_arg_spec.Arg_label.t Melange_ffi.External_arg_spec.param
list ->
Melange_ffi.External_ffi_types.return_wrapper ->
Melange_ffi.External_ffi_types.external_spec ->
Expand Down
5 changes: 3 additions & 2 deletions jscomp/core/lam_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ type t =
| Pjs_call of {
prim_name : string;
arg_types :
Melange_ffi.External_arg_spec.label_noname
Melange_ffi.External_arg_spec.Arg_label.t
Melange_ffi.External_arg_spec.param
list;
ffi : Melange_ffi.External_ffi_types.external_spec;
dynamic_import : bool;
}
| Pjs_object_create of
Melange_ffi.External_arg_spec.label Melange_ffi.External_arg_spec.param
Melange_ffi.External_arg_spec.Obj_label.t
Melange_ffi.External_arg_spec.param
list
(* Exceptions *)
| Praise
Expand Down
5 changes: 3 additions & 2 deletions jscomp/core/lam_primitive.mli
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ type t =
(* Location.t * [loc] is passed down *)
prim_name : string;
arg_types :
Melange_ffi.External_arg_spec.label_noname
Melange_ffi.External_arg_spec.Arg_label.t
Melange_ffi.External_arg_spec.param
list;
ffi : Melange_ffi.External_ffi_types.external_spec;
dynamic_import : bool;
}
| Pjs_object_create of
Melange_ffi.External_arg_spec.label Melange_ffi.External_arg_spec.param
Melange_ffi.External_arg_spec.Obj_label.t
Melange_ffi.External_arg_spec.param
list
| Praise
| Psequand
Expand Down
4 changes: 2 additions & 2 deletions jscomp/test/dist/jscomp/test/gpr_4442_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions ppx/ast_derive/ast_derive_abstract.ml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ let derive_js_constructor =
let label_name = Melange_ffi.Lam_methname.translate p.txt in
let obj_arg_label =
if is_option then
Melange_ffi.External_arg_spec.optional false label_name
else Melange_ffi.External_arg_spec.obj_label label_name
Melange_ffi.External_arg_spec.Obj_label.optional
~for_sure_no_nested_option:false label_name
else Melange_ffi.External_arg_spec.Obj_label.obj label_name
in
{
Melange_ffi.External_arg_spec.arg_type = Nothing;
Expand Down
Loading

0 comments on commit 0f1a7a7

Please sign in to comment.