Skip to content

Commit 6ba663e

Browse files
authored
Merge pull request #4598 from BuckleScript/bytes
clean up runtime support: remove legacy caml_char primitives
2 parents f479dcf + b0bc8af commit 6ba663e

39 files changed

+294
-474
lines changed

jscomp/core/lam_dispatch_primitive.ml

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,10 @@ let translate loc (prim_name : string)
293293
| [e0; e1] -> E.float_mul e0 e1
294294
| _ -> assert false
295295
end
296-
| "caml_bytes_equal" ->
297-
call Js_runtime_modules.caml_primitive
296+
| "caml_bytes_compare"
297+
| "caml_bytes_equal"
298+
->
299+
call Js_runtime_modules.bytes
298300
| "caml_int64_succ" ->
299301
E.runtime_call Js_runtime_modules.int64 "succ" args
300302
| "caml_int64_to_string" ->
@@ -524,11 +526,10 @@ let translate loc (prim_name : string)
524526
| "caml_int32_compare"
525527
| "caml_nativeint_compare"
526528
| "caml_float_compare"
527-
| "caml_bytes_compare"
529+
528530
| "caml_string_compare"
529531
->
530532
call Js_runtime_modules.caml_primitive
531-
532533
| "caml_bool_min"
533534
| "caml_int_min"
534535
| "caml_float_min"
@@ -560,17 +561,10 @@ let translate loc (prim_name : string)
560561
call Js_runtime_modules.caml_primitive
561562
| _ -> assert false
562563
end
563-
| "caml_fill_string"
564-
| "caml_fill_bytes"
565-
->
566-
E.runtime_call
567-
Js_runtime_modules.bytes "caml_fill_bytes" args
568-
| "caml_is_printable"
569-
->
570-
call Js_runtime_modules.char
571564
| "caml_string_get"
572565
->
573566
E.runtime_call Js_runtime_modules.string "get" args
567+
| "caml_fill_bytes"
574568
| "bytes_to_string"
575569
| "bytes_of_string"
576570
| "caml_blit_string"

jscomp/ext/js_runtime_modules.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ let array = "Caml_array"
3333
let format = "Caml_format"
3434
let string = "Caml_string"
3535
let bytes = "Caml_bytes"
36-
let char = "Caml_char"
3736
let float = "Caml_float"
3837
let hash_primitive = "Caml_hash_primitive"
3938
let hash = "Caml_hash"

jscomp/main/builtin_cmi_datasets.ml

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

jscomp/main/builtin_cmj_datasets.ml

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

jscomp/others/js_mapperRt.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25+
external (.!()) : int array -> int -> int = "" [@@bs.get_index]
2526

2627
let raiseWhenNotFound x =
2728
if Js.testAny x then raise Not_found
@@ -30,7 +31,7 @@ let raiseWhenNotFound x =
3031
let rec fromIntAux (enum : int) i len xs =
3132
if i = len then None
3233
else
33-
let k = Js.Array2.unsafe_get xs i in
34+
let k = xs.!(i) in
3435
if k = enum then Some i
3536
else fromIntAux enum (i + 1) len xs
3637

@@ -40,7 +41,7 @@ let fromInt len (xs : int array) (enum : int ) : 'variant option =
4041
let rec fromIntAssertAux len (enum : int) i xs =
4142
if i = len then raise Not_found
4243
else
43-
let k = Js.Array2.unsafe_get xs i in
44+
let k = xs.!(i) in
4445
if k = enum then i
4546
else fromIntAssertAux len enum (i + 1) xs
4647

jscomp/others/release.ninja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ build others/js_json.cmj : cc_cmi others/js_json.ml | others/js_array2.cmj other
3030
build others/js_json.cmi : cc others/js_json.mli | others/js_dict.cmi others/js_null.cmi others/js_string.cmj others/js_types.cmi runtime
3131
build others/js_list.cmj : cc_cmi others/js_list.ml | others/js_array2.cmj others/js_list.cmi others/js_vector.cmj runtime
3232
build others/js_list.cmi : cc others/js_list.mli | others/js_vector.cmi runtime
33-
build others/js_mapperRt.cmj : cc_cmi others/js_mapperRt.ml | others/js_array2.cmj others/js_mapperRt.cmi runtime
33+
build others/js_mapperRt.cmj : cc_cmi others/js_mapperRt.ml | others/js_mapperRt.cmi runtime
3434
build others/js_mapperRt.cmi : cc others/js_mapperRt.mli | runtime
3535
build others/js_math.cmi others/js_math.cmj : cc others/js_math.ml | others/js_int.cmj runtime
3636
build others/js_null.cmj : cc_cmi others/js_null.ml | others/js_exn.cmj others/js_null.cmi runtime

jscomp/runtime/caml_bytes.ml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,42 @@ let bytes_of_string s =
141141
*)
142142
done;
143143
res
144+
145+
146+
let rec caml_bytes_compare_aux (s1 : bytes) (s2 : bytes) off len def =
147+
if off < len then
148+
let a, b = Caml_bytes_extern.unsafe_get s1 off, Caml_bytes_extern.unsafe_get s2 off in
149+
if a > b then 1
150+
else if a < b then -1
151+
else caml_bytes_compare_aux s1 s2 (off + 1) len def
152+
else def
153+
154+
(* code path could be using a tuple if we can eliminate the tuple allocation for code below
155+
{[
156+
let (len, v) =
157+
if len1 = len2 then (..,...)
158+
else (.., .)
159+
]}
160+
161+
*)
162+
let caml_bytes_compare (s1 : bytes) (s2 : bytes) : int =
163+
let len1, len2 = Caml_bytes_extern.length s1, Caml_bytes_extern.length s2 in
164+
if len1 = len2 then
165+
caml_bytes_compare_aux s1 s2 0 len1 0
166+
else if len1 < len2 then
167+
caml_bytes_compare_aux s1 s2 0 len1 (-1)
168+
else
169+
caml_bytes_compare_aux s1 s2 0 len2 1
170+
171+
let rec caml_bytes_equal_aux (s1 : bytes) s2 (off : int) len =
172+
if off = len then true
173+
else
174+
let a, b = Caml_bytes_extern.unsafe_get s1 off, Caml_bytes_extern.unsafe_get s2 off in
175+
a = b
176+
&& caml_bytes_equal_aux s1 s2 (off + 1) len
177+
178+
let caml_bytes_equal (s1 : bytes) (s2 : bytes) : bool =
179+
let len1, len2 = Caml_bytes_extern.length s1, Caml_bytes_extern.length s2 in
180+
len1 = len2 &&
181+
caml_bytes_equal_aux s1 s2 0 len1
182+

jscomp/runtime/caml_bytes.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ val caml_blit_string :
3737
int ->
3838
unit
3939
val bytes_of_string : string -> bytes
40+
val caml_bytes_compare: bytes -> bytes -> int
41+
val caml_bytes_equal : bytes -> bytes -> bool

jscomp/runtime/caml_char.ml

Lines changed: 0 additions & 33 deletions
This file was deleted.

jscomp/runtime/caml_format.ml

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,19 @@
2424

2525

2626

27+
external (.![]) : string -> int -> int = "%string_unsafe_get"
28+
external (.!()) : string -> int -> char = "%string_unsafe_get"
2729

30+
let code_0 = "0".![0]
31+
let code_a = "a".![0]
32+
let code_A = "A".![0]
2833

34+
module Caml_char = struct
35+
external code : char -> int = "%identity"
36+
external unsafe_chr : int -> char = "%identity"
37+
end
2938

30-
let caml_failwith s = raise (Failure s)
39+
let failwith s = raise (Failure s)
3140
(* let caml_invalid_argument s= raise (Invalid_argument s ) *)
3241

3342
let (>>>) = Caml_nativeint_extern.shift_right_logical
@@ -40,11 +49,11 @@ let ( *~ ) = Caml_nativeint_extern.mul
4049
let parse_digit c =
4150
match c with
4251
| '0' .. '9'
43-
-> Caml_char.code c - Caml_char.code '0'
52+
-> Caml_char.code c - code_0
4453
| 'A' .. 'Z'
45-
-> Caml_char.code c - (Caml_char.code 'A' - 10)
54+
-> Caml_char.code c - (code_A - 10)
4655
| 'a' .. 'z'
47-
-> Caml_char.code c - (Caml_char.code 'a' - 10 )
56+
-> Caml_char.code c - (code_a - 10 )
4857
| _ -> -1
4958

5059
type of_string_base =
@@ -86,41 +95,39 @@ let parse_sign_and_base (s : string) =
8695

8796

8897
let caml_int_of_string s =
89-
let module String = Caml_string_extern in
9098
let i, sign, hbase = parse_sign_and_base s in
9199
let base = Caml_nativeint_extern.of_int (int_of_string_base hbase) in
92100
let threshold = (-1n >>> 0) in
93101
let len =Caml_string_extern.length s in
94-
let c = if i < len then s.[i] else '\000' in
102+
let c = if i < len then s.!(i) else '\000' in
95103
let d = to_nat (parse_digit c) in
96104
let () =
97105
if d < 0n || d >= base then
98-
caml_failwith "int_of_string" in
106+
failwith "int_of_string" in
99107
(* let () = [%bs.debugger] in *)
100108
let rec aux acc k =
101109
if k = len then acc
102110
else
103-
let a = s.[k] in
111+
let a = s.!(k) in
104112
if a = '_' then aux acc ( k + 1)
105113
else
106114
let v = to_nat (parse_digit a) in
107115
if v < 0n || v >= base then
108-
caml_failwith "int_of_string"
116+
failwith "int_of_string"
109117
else
110118
let acc = base *~ acc +~ v in
111119
if acc > threshold then
112-
caml_failwith "int_of_string"
120+
failwith "int_of_string"
113121
else aux acc ( k + 1)
114122
in
115123
let res = sign *~ aux d (i + 1) in
116124
let or_res = Caml_nativeint_extern.logor res 0n in
117125
(if base = 10n && res <> or_res then
118-
caml_failwith "int_of_string");
126+
failwith "int_of_string");
119127
or_res
120128

121129

122130
let caml_int64_of_string s =
123-
let module String = Caml_string_extern in
124131
let i, sign, hbase = parse_sign_and_base s in
125132
let base = Caml_int64_extern.of_int (int_of_string_base hbase) in
126133
let sign = Caml_int64_extern.of_nativeint sign in
@@ -136,31 +143,31 @@ let caml_int64_of_string s =
136143
9223372036854775807L
137144
in
138145
let len =Caml_string_extern.length s in
139-
let c = if i < len then s.[i] else '\000' in
146+
let c = if i < len then s.!(i) else '\000' in
140147
let d = Caml_int64_extern.of_int (parse_digit c) in
141148
let () =
142149
if d < 0L || d >= base then
143-
caml_failwith "int64_of_string" in
150+
failwith "int64_of_string" in
144151
let (+~) = Caml_int64_extern.add in
145152
let ( *~ ) = Caml_int64_extern.mul in
146153

147154
let rec aux acc k =
148155
if k = len then acc
149156
else
150-
let a = s.[k] in
157+
let a = s.!(k) in
151158
if a = '_' then aux acc ( k + 1)
152159
else
153160
let v = Caml_int64_extern.of_int (parse_digit a) in
154161
if v < 0L || v >= base || acc > threshold then
155-
caml_failwith "int64_of_string"
162+
failwith "int64_of_string"
156163
else
157164
let acc = base *~ acc +~ v in
158165
aux acc ( k + 1)
159166
in
160167
let res = sign *~ aux d (i + 1) in
161168
let or_res = Caml_int64_extern.logor res 0L in
162169
(if base = 10L && res <> or_res then
163-
caml_failwith "int64_of_string");
170+
failwith "int64_of_string");
164171
or_res
165172

166173
type base =
@@ -184,7 +191,7 @@ type fmt = {
184191
mutable conv : string
185192
}
186193

187-
let lowercase c =
194+
let lowercase (c : char) : char =
188195
if (c >= 'A' && c <= 'Z')
189196
|| (c >= '\192' && c <= '\214')
190197
|| (c >= '\216' && c <= '\222')
@@ -220,8 +227,8 @@ let parse_format fmt =
220227
f.width <- 0;
221228
let j = ref i in
222229

223-
while (let w = Caml_char.code fmt.[j.contents] - Caml_char.code '0' in w >=0 && w <= 9 ) do
224-
f.width <- f.width * 10 + Caml_char.code fmt.[j.contents] - Caml_char.code '0';
230+
while (let w = fmt.![j.contents] - code_0 in w >=0 && w <= 9 ) do
231+
f.width <- f.width * 10 + fmt.![j.contents] - code_0;
225232
j.contents <- j.contents + 1
226233
done;
227234
aux f j.contents
@@ -230,8 +237,8 @@ let parse_format fmt =
230237
->
231238
f.prec <- 0;
232239
let j = ref (i + 1 ) in
233-
while (let w = Caml_char.code fmt.[j.contents] - Caml_char.code '0' in w >=0 && w <= 9 ) do
234-
f.prec <- f.prec * 10 + Caml_char.code fmt.[j.contents] - Caml_char.code '0';
240+
while (let w = fmt.![j.contents] - code_0 in w >=0 && w <= 9 ) do
241+
f.prec <- f.prec * 10 + fmt.![j.contents] - code_0;
235242
j.contents <- j.contents + 1;
236243
done;
237244
aux f j.contents

0 commit comments

Comments
 (0)