Skip to content

Commit

Permalink
define as primitive
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Sep 12, 2024
1 parent f373aed commit 0f63b09
Show file tree
Hide file tree
Showing 15 changed files with 21 additions and 7 deletions.
4 changes: 4 additions & 0 deletions jscomp/core/js_of_lam_array.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ module E = Js_exp_make
(* Parrayref(u|s) *)
let make_array mt args = E.array mt args

let spread_array args = match args with
| [e] -> {e with J.expression_desc = Spread e}
| _ -> assert false

let set_array e e0 e1 = E.assign (E.array_index e e0) e1

let ref_array e e0 = E.array_index e e0
3 changes: 3 additions & 0 deletions jscomp/core/js_of_lam_array.mli
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
val make_array : J.mutable_flag -> J.expression list -> J.expression
(** create an array *)

val spread_array : J.expression list -> J.expression
(** spread an array *)

val set_array : J.expression -> J.expression -> J.expression -> J.expression
(** Here we don't care about [array_kind],
In the future, we might used TypedArray for FloatArray
Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/lam_analysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
| Pbigintcomp _
(* String operations *)
| Pstringlength | Pstringrefu | Pstringrefs | Pbyteslength | Pbytesrefu
| Pbytesrefs | Pmakearray | Parraylength | Parrayrefu | Parrayrefs
| Pbytesrefs | Pmakearray | Parrayspread | Parraylength | Parrayrefu | Parrayrefs
(* Test if the argument is a block or an immediate integer *)
| Pisint | Pis_poly_var_block
(* Test if the (integer) argument is outside an interval *)
Expand Down
1 change: 1 addition & 0 deletions jscomp/core/lam_compile_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
| Parrayrefs -> E.runtime_call Js_runtime_modules.array "get" args
| Parraysets -> E.runtime_call Js_runtime_modules.array "set" args
| Pmakearray -> Js_of_lam_array.make_array Mutable args
| Parrayspread -> Js_of_lam_array.spread_array args
| Parraysetu -> (
match args with
(* wrong*)
Expand Down
1 change: 1 addition & 0 deletions jscomp/core/lam_convert.ml
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
| Poffsetref x -> prim ~primitive:(Poffsetref x) ~args loc
| Pfloatcomp x -> prim ~primitive:(Pfloatcomp x) ~args loc
| Pmakearray _mutable_flag (*FIXME*) -> prim ~primitive:Pmakearray ~args loc
| Parrayspread -> prim ~primitive:Parrayspread ~args loc
| Parraylength -> prim ~primitive:Parraylength ~args loc
| Parrayrefu -> prim ~primitive:Parrayrefu ~args loc
| Parraysetu -> prim ~primitive:Parraysetu ~args loc
Expand Down
4 changes: 0 additions & 4 deletions jscomp/core/lam_dispatch_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,6 @@ let translate loc (prim_name : string) (args : J.expression list) : J.expression
match args with
| [e] -> {e with expression_desc = Await e}
| _ -> assert false)
| "?array_spread" -> (
match args with
| [e] -> {e with expression_desc = Spread e}
| _ -> assert false)
| "?create_dict" -> (
match args with
| [{expression_desc = Array (items, _)}] ->
Expand Down
2 changes: 2 additions & 0 deletions jscomp/core/lam_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ type t =
| Pbytessets
(* Array operations *)
| Pmakearray
| Parrayspread
| Parraylength
| Parrayrefu
| Parraysetu
Expand Down Expand Up @@ -307,6 +308,7 @@ let eq_primitive_approx (lhs : t) (rhs : t) =
| Poffsetint i0 -> ( match rhs with Poffsetint i1 -> i0 = i1 | _ -> false)
| Poffsetref i0 -> ( match rhs with Poffsetref i1 -> i0 = i1 | _ -> false)
| Pmakearray -> rhs = Pmakearray
| Parrayspread -> rhs = Parrayspread
| Parraylength -> rhs = Parraylength
| Parrayrefu -> rhs = Parrayrefu
| Parraysetu -> rhs = Parraysetu
Expand Down
1 change: 1 addition & 0 deletions jscomp/core/lam_primitive.mli
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type t =
| Pbytessets
(* Array operations *)
| Pmakearray
| Parrayspread
| Parraylength
| Parrayrefu
| Parraysetu
Expand Down
1 change: 1 addition & 0 deletions jscomp/core/lam_print.ml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ let primitive ppf (prim : Lam_primitive.t) =
| Pbytessets -> fprintf ppf "bytes.set"
| Parraylength -> fprintf ppf "array.length"
| Pmakearray -> fprintf ppf "makearray"
| Parrayspread -> fprintf ppf "array.spread"
| Parrayrefu -> fprintf ppf "array.unsafe_get"
| Parraysetu -> fprintf ppf "array.unsafe_set"
| Parrayrefs -> fprintf ppf "array.get"
Expand Down
1 change: 1 addition & 0 deletions jscomp/ml/lambda.ml
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ type primitive =
| Pbyteslength | Pbytesrefu | Pbytessetu | Pbytesrefs | Pbytessets
(* Array operations *)
| Pmakearray of Asttypes.mutable_flag
| Parrayspread
| Parraylength
| Parrayrefu
| Parraysetu
Expand Down
1 change: 1 addition & 0 deletions jscomp/ml/lambda.mli
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ type primitive =
| Pbyteslength | Pbytesrefu | Pbytessetu | Pbytesrefs | Pbytessets
(* Array operations *)
| Pmakearray of mutable_flag
| Parrayspread
| Parraylength
| Parrayrefu
| Parraysetu
Expand Down
2 changes: 2 additions & 0 deletions jscomp/ml/printlambda.ml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ let primitive ppf = function
| Parraylength -> fprintf ppf "array.length"
| Pmakearray Mutable -> fprintf ppf "makearray"
| Pmakearray Immutable -> fprintf ppf "makearray_imm"
| Parrayspread -> fprintf ppf "array.spread"
| Parrayrefu -> fprintf ppf "array.unsafe_get"
| Parraysetu -> fprintf ppf "array.unsafe_set"
| Parrayrefs -> fprintf ppf "array.get"
Expand Down Expand Up @@ -316,6 +317,7 @@ let name_of_primitive = function
| Pbytessets -> "Pbytessets"
| Parraylength -> "Parraylength"
| Pmakearray _-> "Pmakearray"
| Parrayspread -> "Parrayspread"
| Parrayrefu -> "Parrayrefu"
| Parraysetu -> "Parraysetu"
| Parrayrefs -> "Parrayrefs"
Expand Down
1 change: 1 addition & 0 deletions jscomp/ml/translcore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ let primitives_table =
("%array_safe_set", Parraysets);
("%array_unsafe_get", Parrayrefu);
("%array_unsafe_set", Parraysetu);
("%array_spread", Parrayspread);
("%floatarray_length", Parraylength);
("%floatarray_safe_get", Parrayrefs);
("%floatarray_safe_set", Parraysets);
Expand Down
2 changes: 1 addition & 1 deletion jscomp/runtime/caml_array.res
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,4 @@ let blit = (a1, i1, a2, i2, len) =>
}
}

external unsafe_spread: array<'a> => 'a = "?array_spread"
external unsafe_spread: array<'a> => 'a = "%array_spread"
2 changes: 1 addition & 1 deletion jscomp/runtime/caml_array.resi
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ let get: (array<'a>, int) => 'a

let set: (array<'a>, int, 'a) => unit

external unsafe_spread: array<'a> => 'a = "?array_spread"
external unsafe_spread: array<'a> => 'a = "%array_spread"

0 comments on commit 0f63b09

Please sign in to comment.