Skip to content

Commit

Permalink
remove (almost) Caml String/Bytes/Char/Stream
Browse files Browse the repository at this point in the history
  • Loading branch information
cometkim committed Aug 27, 2024
1 parent 4d9ee4b commit 035a175
Show file tree
Hide file tree
Showing 76 changed files with 57 additions and 3,924 deletions.
11 changes: 0 additions & 11 deletions jscomp/core/js_of_lam_string.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ let const_char (i : int) = E.int ~c:i (Int32.of_int @@ i)
(* string [s[i]] expects to return a [ocaml_char] *)
let ref_string e e1 = E.string_index e e1

(* [s[i]] excepts to return a [ocaml_char]
We use normal array for [bytes]
TODO: we can use [Buffer] in the future
*)
let ref_byte e e0 = E.array_index e e0

(* {Bytes.set : bytes -> int -> char -> unit }*)
let set_byte e e0 e1 = E.assign (E.array_index e e0) e1

(**
Note that [String.fromCharCode] also works, but it only
work for small arrays, however, for {bytes_to_string} it is likely the bytes
Expand All @@ -59,5 +50,3 @@ let set_byte e e0 e1 = E.assign (E.array_index e e0) e1
Maxiume call stack size exceeded
]}
*)
let bytes_to_string e =
E.runtime_call Js_runtime_modules.bytes_ "to_string" [ e ]
6 changes: 0 additions & 6 deletions jscomp/core/js_of_lam_string.mli
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,4 @@

val ref_string : J.expression -> J.expression -> J.expression

val ref_byte : J.expression -> J.expression -> J.expression

val set_byte : J.expression -> J.expression -> J.expression -> J.expression

val const_char : int -> J.expression

val bytes_to_string : J.expression -> J.expression
10 changes: 5 additions & 5 deletions jscomp/core/lam_analysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
(* more safe to check if arguments are constant *)
(* non-observable side effect *)
| "?sys_get_argv" (* should be fine *)
| "?string_repeat" | "?make_vect" | "?create_bytes" | "?obj_dup"
| "?make_vect" | "?obj_dup"
| "caml_array_dup" | "?nativeint_add" | "?nativeint_div"
| "?nativeint_mod" | "?nativeint_lsr" | "?nativeint_mul" ),
_ ) ->
Expand All @@ -63,7 +63,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
| Pundefined_to_opt | Pnull_undefined_to_opt | Pjs_fn_make _ | Pjs_fn_make_unit
| Pjs_object_create _ | Pimport
(* TODO: check *)
| Pbytes_to_string | Pmakeblock _
| Pmakeblock _
(* whether it's mutable or not *)
| Pfield _ | Pval_from_option | Pval_from_option_not_nest
(* NOP The compiler already [t option] is the same as t *)
Expand All @@ -81,8 +81,8 @@ let rec no_side_effects (lam : Lam.t) : bool =
| Pandbigint | Porbigint | Pxorbigint | Plslbigint | Pasrbigint
| Pbigintcomp _
(* String operations *)
| Pstringlength | Pstringrefu | Pstringrefs | Pbyteslength | Pbytesrefu
| Pbytesrefs | Pmakearray | Parraylength | Parrayrefu | Parrayrefs
| Pstringlength | Pstringrefu | Pstringrefs
| Pmakearray | 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 All @@ -103,7 +103,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
| Pjs_unsafe_downgrade _ | Pdebugger | Pvoid_run | Pfull_apply
| Pjs_fn_method
(* TODO *)
| Praw_js_code _ | Pbytessetu | Pbytessets
| Praw_js_code _
(* Operations on boxed integers (Nativeint.t, Int32.t, Int64.t) *)
| Parraysets
(* byte swap *)
Expand Down
17 changes: 0 additions & 17 deletions jscomp/core/lam_compile_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -308,24 +308,7 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
*)
| [ range; e ] -> E.is_out (E.offset e off) range
| _ -> assert false)
| Pbytes_to_string ->
Js_of_lam_string.bytes_to_string (Ext_list.singleton_exn args)
| Pstringlength -> E.string_length (Ext_list.singleton_exn args)
| Pbyteslength -> E.bytes_length (Ext_list.singleton_exn args)
(* This should only be Pbyteset(u|s), which in js, is an int array
Bytes is an int array in javascript
*)
| Pbytessetu -> (
match args with
| [ e; e0; e1 ] ->
ensure_value_unit cxt.continuation (Js_of_lam_string.set_byte e e0 e1)
| _ -> assert false)
| Pbytessets -> E.runtime_call Js_runtime_modules.bytes "set" args
| Pbytesrefu -> (
match args with
| [ e; e1 ] -> Js_of_lam_string.ref_byte e e1
| _ -> assert false)
| Pbytesrefs -> E.runtime_call Js_runtime_modules.bytes "get" args
| Pstringrefs -> E.runtime_call Js_runtime_modules.string "get" args
(* For bytes and string, they both return [int] in ocaml
we need tell Pbyteref from Pstringref
Expand Down
7 changes: 0 additions & 7 deletions jscomp/core/lam_convert.ml
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
| Prevapply -> assert false
| Pdirapply -> assert false
| Ploc _ -> assert false (* already compiled away here*)
| Pbytes_to_string (* handled very early *) ->
prim ~primitive:Pbytes_to_string ~args loc
| Pcreate_extension s -> prim ~primitive:(Pcreate_extension s) ~args loc
| Pignore ->
(* Pignore means return unit, it is not an nop *)
Expand Down Expand Up @@ -239,11 +237,6 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
| Pstringrefu -> prim ~primitive:Pstringrefu ~args loc
| Pabsfloat -> assert false
| Pstringrefs -> prim ~primitive:Pstringrefs ~args loc
| Pbyteslength -> prim ~primitive:Pbyteslength ~args loc
| Pbytesrefu -> prim ~primitive:Pbytesrefu ~args loc
| Pbytessetu -> prim ~primitive:Pbytessetu ~args loc
| Pbytesrefs -> prim ~primitive:Pbytesrefs ~args loc
| Pbytessets -> prim ~primitive:Pbytessets ~args loc
| Pisint -> prim ~primitive:Pisint ~args loc
| Pisout -> (
match args with
Expand Down
27 changes: 0 additions & 27 deletions jscomp/core/lam_dispatch_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ let translate loc (prim_name : string) (args : J.expression list) : J.expression
Location.prerr_warning loc Warnings.Bs_polymorphic_comparison;
call Js_runtime_modules.obj_runtime
(* generated by the compiler, not user facing *)
| "caml_bytes_greaterthan" | "caml_bytes_greaterequal" | "caml_bytes_lessthan"
| "caml_bytes_lessequal" | "caml_bytes_compare" | "caml_bytes_equal" ->
call Js_runtime_modules.bytes
| "caml_string_equal" -> (
match args with [ e0; e1 ] -> E.string_equal e0 e1 | _ -> assert false)
| "caml_string_notequal" -> (
Expand Down Expand Up @@ -177,30 +174,6 @@ let translate loc (prim_name : string) (args : J.expression list) : J.expression
call Js_runtime_modules.float
| "?fmod_float" (* float module like js number module *) -> (
match args with [ e0; e1 ] -> E.float_mod e0 e1 | _ -> assert false)
| "?string_repeat" -> (
match args with
| [ n; { expression_desc = Number (Int { i }) } ] -> (
let str = String.make 1 (Char.chr (Int32.to_int i)) in
match n.expression_desc with
| Number (Int { i = 1l }) -> E.str str
| _ ->
E.call
(E.dot (E.str str) "repeat")
[ n ] ~info:Js_call_info.builtin_runtime_call)
| _ -> E.runtime_call Js_runtime_modules.string "make" args)
| "?create_bytes" -> (
(* Bytes.create *)
(* Note that for invalid range, JS raise an Exception RangeError,
here in OCaml it's [Invalid_argument], we have to preserve this semantics.
Also, it's creating a [bytes] which is a js array actually.
*)
match args with
| [ { expression_desc = Number (Int { i; _ }); _ } ] when i < 8l ->
(*Invariants: assuming bytes are [int array]*)
E.array NA
(if i = 0l then []
else Ext_list.init (Int32.to_int i) (fun _ -> E.zero_int_literal))
| _ -> E.runtime_call Js_runtime_modules.bytes "create" args)
(* Note we captured [exception/extension] creation in the early pass, this primitive is
like normal one to set the identifier *)
| "?exn_slot_name" | "?is_extension" -> call Js_runtime_modules.exceptions
Expand Down
12 changes: 0 additions & 12 deletions jscomp/core/lam_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ type record_representation =
(* Inlined record under extension *)

type t =
| Pbytes_to_string
(* Operations on heap blocks *)
| Pmakeblock of int * Lam_tag_info.t * Asttypes.mutable_flag
| Pfield of int * Lam_compat.field_dbg_info
Expand Down Expand Up @@ -105,11 +104,6 @@ type t =
| Pstringrefu
| Pstringrefs
| Pstringadd
| Pbyteslength
| Pbytesrefu
| Pbytessetu
| Pbytesrefs
| Pbytessets
(* Array operations *)
| Pmakearray
| Parraylength
Expand Down Expand Up @@ -185,7 +179,6 @@ let eq_primitive_approx (lhs : t) (rhs : t) =
| Pcreate_extension a -> (
match rhs with Pcreate_extension b -> a = (b : string) | _ -> false)
| Pwrap_exn -> rhs = Pwrap_exn
| Pbytes_to_string -> rhs = Pbytes_to_string
| Praise -> rhs = Praise
| Psequand -> rhs = Psequand
| Psequor -> rhs = Psequor
Expand Down Expand Up @@ -231,11 +224,6 @@ let eq_primitive_approx (lhs : t) (rhs : t) =
| Pstringrefu -> rhs = Pstringrefu
| Pstringrefs -> rhs = Pstringrefs
| Pstringadd -> rhs = Pstringadd
| Pbyteslength -> rhs = Pbyteslength
| Pbytesrefu -> rhs = Pbytesrefu
| Pbytessetu -> rhs = Pbytessetu
| Pbytesrefs -> rhs = Pbytesrefs
| Pbytessets -> rhs = Pbytessets
| Pundefined_to_opt -> rhs = Pundefined_to_opt
| Pnull_to_opt -> rhs = Pnull_to_opt
| Pnull_undefined_to_opt -> rhs = Pnull_undefined_to_opt
Expand Down
6 changes: 0 additions & 6 deletions jscomp/core/lam_primitive.mli
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type record_representation =
(* Inlined record under extension *)

type t =
| Pbytes_to_string
| Pmakeblock of int * Lam_tag_info.t * Asttypes.mutable_flag
| Pfield of int * Lambda.field_dbg_info
| Psetfield of int * Lambda.set_field_dbg_info
Expand Down Expand Up @@ -94,11 +93,6 @@ type t =
| Pstringrefu
| Pstringrefs
| Pstringadd
| Pbyteslength
| Pbytesrefu
| Pbytessetu
| Pbytesrefs
| Pbytessets
(* Array operations *)
| Pmakearray
| Parraylength
Expand Down
6 changes: 0 additions & 6 deletions jscomp/core/lam_print.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ let primitive ppf (prim : Lam_primitive.t) =
| Pcaml_obj_length -> fprintf ppf "#obj_length"
| Pinit_mod -> fprintf ppf "init_mod!"
| Pupdate_mod -> fprintf ppf "update_mod!"
| Pbytes_to_string -> fprintf ppf "bytes_to_string"
| Pjs_apply -> fprintf ppf "#apply"
| Pjs_runtime_apply -> fprintf ppf "#runtime_apply"
| Pjs_unsafe_downgrade { name; setter } ->
Expand Down Expand Up @@ -161,11 +160,6 @@ let primitive ppf (prim : Lam_primitive.t) =
| Pstringlength -> fprintf ppf "string.length"
| Pstringrefu -> fprintf ppf "string.unsafe_get"
| Pstringrefs -> fprintf ppf "string.get"
| Pbyteslength -> fprintf ppf "bytes.length"
| Pbytesrefu -> fprintf ppf "bytes.unsafe_get"
| Pbytessetu -> fprintf ppf "bytes.unsafe_set"
| Pbytesrefs -> fprintf ppf "bytes.get"
| Pbytessets -> fprintf ppf "bytes.set"
| Parraylength -> fprintf ppf "array.length"
| Pmakearray -> fprintf ppf "makearray"
| Parrayrefu -> fprintf ppf "array.unsafe_get"
Expand Down
3 changes: 0 additions & 3 deletions jscomp/ext/js_runtime_modules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ let array = "Caml_array"

let string = "Caml_string"

let bytes = "Caml_bytes"
let bytes_ = "Bytes"

let float = "Caml_float"

let hash_primitive = "Caml_hash_primitive"
Expand Down
6 changes: 1 addition & 5 deletions jscomp/ml/ctype.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1893,11 +1893,7 @@ let is_instantiable env p =


(* PR#7113: -safe-string should be a global property *)
let compatible_paths p1 p2 =
let open Predef in
Path.same p1 p2 ||
Path.same p1 path_bytes && Path.same p2 path_string ||
Path.same p1 path_string && Path.same p2 path_bytes
let compatible_paths p1 p2 = Path.same p1 p2

(* Check for datatypes carefully; see PR#6348 *)
let rec expands_to_datatype env ty =
Expand Down
2 changes: 0 additions & 2 deletions jscomp/ml/lambda.ml
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ type is_safe =

type primitive =
| Pidentity
| Pbytes_to_string
| Pignore
| Prevapply
| Pdirapply
Expand Down Expand Up @@ -221,7 +220,6 @@ type primitive =
| Pbigintcomp of comparison
(* String operations *)
| Pstringlength | Pstringrefu | Pstringrefs
| Pbyteslength | Pbytesrefu | Pbytessetu | Pbytesrefs | Pbytessets
(* Array operations *)
| Pmakearray of Asttypes.mutable_flag
| Parraylength
Expand Down
2 changes: 0 additions & 2 deletions jscomp/ml/lambda.mli
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ type pointer_info =

type primitive =
| Pidentity
| Pbytes_to_string
| Pignore
| Prevapply
| Pdirapply
Expand Down Expand Up @@ -193,7 +192,6 @@ type primitive =
| Pbigintcomp of comparison
(* String operations *)
| Pstringlength | Pstringrefu | Pstringrefs
| Pbyteslength | Pbytesrefu | Pbytessetu | Pbytesrefs | Pbytessets
(* Array operations *)
| Pmakearray of mutable_flag
| Parraylength
Expand Down
6 changes: 1 addition & 5 deletions jscomp/ml/predef.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ let ident_create_predef_exn = wrap Ident.create_predef_exn

let ident_int = ident_create "int"
and ident_char = ident_create "char"
and ident_bytes = ident_create "bytes"
and ident_float = ident_create "float"
and ident_bool = ident_create "bool"
and ident_unit = ident_create "unit"
Expand Down Expand Up @@ -75,7 +74,6 @@ let type_is_builtin_path_but_option (p : Path.t) : test =

let path_int = Pident ident_int
and path_char = Pident ident_char
and path_bytes = Pident ident_bytes
and path_float = Pident ident_float
and path_bool = Pident ident_bool
and path_unit = Pident ident_unit
Expand All @@ -101,7 +99,6 @@ and path_uncurried = Pident ident_uncurried

let type_int = newgenty (Tconstr(path_int, [], ref Mnil))
and type_char = newgenty (Tconstr(path_char, [], ref Mnil))
and type_bytes = newgenty (Tconstr(path_bytes, [], ref Mnil))
and type_float = newgenty (Tconstr(path_float, [], ref Mnil))
and type_bool = newgenty (Tconstr(path_bool, [], ref Mnil))
and type_unit = newgenty (Tconstr(path_unit, [], ref Mnil))
Expand Down Expand Up @@ -325,12 +322,11 @@ let common_initial_env add_type add_extension empty_env =

let build_initial_env add_type add_exception empty_env =
let common = common_initial_env add_type add_exception empty_env in
let res = add_type ident_bytes decl_abstr common in
let decl_type_char =
{decl_abstr with
type_manifest = Some type_int;
type_private = Private} in
add_type ident_char decl_type_char res
add_type ident_char decl_type_char common


let builtin_values =
Expand Down
2 changes: 0 additions & 2 deletions jscomp/ml/predef.mli
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ open Types
val type_int: type_expr
val type_char: type_expr
val type_string: type_expr
val type_bytes: type_expr
val type_float: type_expr
val type_bool: type_expr
val type_unit: type_expr
Expand All @@ -40,7 +39,6 @@ val type_floatarray:type_expr
val path_int: Path.t
val path_char: Path.t
val path_string: Path.t
val path_bytes: Path.t
val path_float: Path.t
val path_bool: Path.t
val path_unit: Path.t
Expand Down
13 changes: 0 additions & 13 deletions jscomp/ml/printlambda.ml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ let print_taginfo ppf = function

let primitive ppf = function
| Pidentity -> fprintf ppf "id"
| Pbytes_to_string -> fprintf ppf "bytes_to_string"
| Pignore -> fprintf ppf "ignore"
| Prevapply -> fprintf ppf "revapply"
| Pdirapply -> fprintf ppf "dirapply"
Expand Down Expand Up @@ -197,12 +196,6 @@ let primitive ppf = function
| Pstringlength -> fprintf ppf "string.length"
| Pstringrefu -> fprintf ppf "string.unsafe_get"
| Pstringrefs -> fprintf ppf "string.get"
| Pbyteslength -> fprintf ppf "bytes.length"
| Pbytesrefu -> fprintf ppf "bytes.unsafe_get"
| Pbytessetu -> fprintf ppf "bytes.unsafe_set"
| Pbytesrefs -> fprintf ppf "bytes.get"
| Pbytessets -> fprintf ppf "bytes.set"

| Parraylength -> fprintf ppf "array.length"
| Pmakearray Mutable -> fprintf ppf "makearray"
| Pmakearray Immutable -> fprintf ppf "makearray_imm"
Expand Down Expand Up @@ -242,7 +235,6 @@ let primitive ppf = function
| Pcreate_extension s -> fprintf ppf "extension[%s]" s
let name_of_primitive = function
| Pidentity -> "Pidentity"
| Pbytes_to_string -> "Pbytes_to_string"
| Pignore -> "Pignore"
| Prevapply -> "Prevapply"
| Pdirapply -> "Pdirapply"
Expand Down Expand Up @@ -298,11 +290,6 @@ let name_of_primitive = function
| Pstringlength -> "Pstringlength"
| Pstringrefu -> "Pstringrefu"
| Pstringrefs -> "Pstringrefs"
| Pbyteslength -> "Pbyteslength"
| Pbytesrefu -> "Pbytesrefu"
| Pbytessetu -> "Pbytessetu"
| Pbytesrefs -> "Pbytesrefs"
| Pbytessets -> "Pbytessets"
| Parraylength -> "Parraylength"
| Pmakearray _-> "Pmakearray"
| Parrayrefu -> "Parrayrefu"
Expand Down
Loading

0 comments on commit 035a175

Please sign in to comment.