Skip to content

Commit 77b1759

Browse files
committed
move bytes runtime support from caml_primitive to bytes
1 parent bf96a2c commit 77b1759

15 files changed

+180
-172
lines changed

jscomp/core/lam_dispatch_primitive.ml

Lines changed: 5 additions & 4 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"

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_primitive.ml

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -48,43 +48,6 @@ let caml_string_compare (s1 : string) (s2 : string) : int =
4848
else if s1 < s2 then -1
4949
else 1
5050

51-
let rec caml_bytes_compare_aux (s1 : bytes) (s2 : bytes) off len def =
52-
if off < len then
53-
let a, b = Caml_bytes_extern.unsafe_get s1 off, Caml_bytes_extern.unsafe_get s2 off in
54-
if a > b then 1
55-
else if a < b then -1
56-
else caml_bytes_compare_aux s1 s2 (off + 1) len def
57-
else def
58-
59-
(* code path could be using a tuple if we can eliminate the tuple allocation for code below
60-
{[
61-
let (len, v) =
62-
if len1 = len2 then (..,...)
63-
else (.., .)
64-
]}
65-
66-
*)
67-
let caml_bytes_compare (s1 : bytes) (s2 : bytes) : int =
68-
let len1, len2 = Caml_bytes_extern.length s1, Caml_bytes_extern.length s2 in
69-
if len1 = len2 then
70-
caml_bytes_compare_aux s1 s2 0 len1 0
71-
else if len1 < len2 then
72-
caml_bytes_compare_aux s1 s2 0 len1 (-1)
73-
else
74-
caml_bytes_compare_aux s1 s2 0 len2 1
75-
76-
let rec caml_bytes_equal_aux (s1 : bytes) s2 (off : int) len =
77-
if off = len then true
78-
else
79-
let a, b = Caml_bytes_extern.unsafe_get s1 off, Caml_bytes_extern.unsafe_get s2 off in
80-
a = b
81-
&& caml_bytes_equal_aux s1 s2 (off + 1) len
82-
83-
let caml_bytes_equal (s1 : bytes) (s2 : bytes) : bool =
84-
let len1, len2 = Caml_bytes_extern.length s1, Caml_bytes_extern.length s2 in
85-
len1 = len2 &&
86-
caml_bytes_equal_aux s1 s2 0 len1
87-
8851

8952
type 'a selector = 'a -> 'a -> 'a
9053

jscomp/runtime/caml_primitive.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
type 'a selector = 'a -> 'a -> 'a
3232

3333

34-
val caml_bytes_compare: bytes -> bytes -> int
35-
val caml_bytes_equal : bytes -> bytes -> bool
34+
35+
3636
val caml_int_compare : int -> int -> int
3737
val caml_bool_compare : bool -> bool -> int
3838
val caml_float_compare : float -> float -> int

jscomp/runtime/release.ninja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ build runtime/caml_option.cmj : cc_cmi runtime/caml_option.ml | runtime/caml_opt
4747
build runtime/caml_option.cmi : cc runtime/caml_option.mli | runtime/bs_stdlib_mini.cmi runtime/caml_undefined_extern.cmj runtime/js.cmi runtime/js.cmj
4848
build runtime/caml_parser.cmj : cc_cmi runtime/caml_parser.ml | runtime/caml_parser.cmi
4949
build runtime/caml_parser.cmi : cc runtime/caml_parser.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
50-
build runtime/caml_primitive.cmj : cc_cmi runtime/caml_primitive.ml | runtime/caml_bytes_extern.cmj runtime/caml_primitive.cmi
50+
build runtime/caml_primitive.cmj : cc_cmi runtime/caml_primitive.ml | runtime/caml_primitive.cmi
5151
build runtime/caml_primitive.cmi : cc runtime/caml_primitive.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
5252
build runtime/caml_splice_call.cmj : cc_cmi runtime/caml_splice_call.ml | runtime/caml_splice_call.cmi
5353
build runtime/caml_splice_call.cmi : cc runtime/caml_splice_call.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110895,8 +110895,10 @@ let translate loc (prim_name : string)
110895110895
| [e0; e1] -> E.float_mul e0 e1
110896110896
| _ -> assert false
110897110897
end
110898-
| "caml_bytes_equal" ->
110899-
call Js_runtime_modules.caml_primitive
110898+
| "caml_bytes_compare"
110899+
| "caml_bytes_equal"
110900+
->
110901+
call Js_runtime_modules.bytes
110900110902
| "caml_int64_succ" ->
110901110903
E.runtime_call Js_runtime_modules.int64 "succ" args
110902110904
| "caml_int64_to_string" ->
@@ -111126,11 +111128,10 @@ let translate loc (prim_name : string)
111126111128
| "caml_int32_compare"
111127111129
| "caml_nativeint_compare"
111128111130
| "caml_float_compare"
111129-
| "caml_bytes_compare"
111131+
111130111132
| "caml_string_compare"
111131111133
->
111132111134
call Js_runtime_modules.caml_primitive
111133-
111134111135
| "caml_bool_min"
111135111136
| "caml_int_min"
111136111137
| "caml_float_min"

lib/4.06.1/unstable/js_refmt_compiler.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110895,8 +110895,10 @@ let translate loc (prim_name : string)
110895110895
| [e0; e1] -> E.float_mul e0 e1
110896110896
| _ -> assert false
110897110897
end
110898-
| "caml_bytes_equal" ->
110899-
call Js_runtime_modules.caml_primitive
110898+
| "caml_bytes_compare"
110899+
| "caml_bytes_equal"
110900+
->
110901+
call Js_runtime_modules.bytes
110900110902
| "caml_int64_succ" ->
110901110903
E.runtime_call Js_runtime_modules.int64 "succ" args
110902110904
| "caml_int64_to_string" ->
@@ -111126,11 +111128,10 @@ let translate loc (prim_name : string)
111126111128
| "caml_int32_compare"
111127111129
| "caml_nativeint_compare"
111128111130
| "caml_float_compare"
111129-
| "caml_bytes_compare"
111131+
111130111132
| "caml_string_compare"
111131111133
->
111132111134
call Js_runtime_modules.caml_primitive
111133-
111134111135
| "caml_bool_min"
111135111136
| "caml_int_min"
111136111137
| "caml_float_min"

lib/4.06.1/whole_compiler.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392987,8 +392987,10 @@ let translate loc (prim_name : string)
392987392987
| [e0; e1] -> E.float_mul e0 e1
392988392988
| _ -> assert false
392989392989
end
392990-
| "caml_bytes_equal" ->
392991-
call Js_runtime_modules.caml_primitive
392990+
| "caml_bytes_compare"
392991+
| "caml_bytes_equal"
392992+
->
392993+
call Js_runtime_modules.bytes
392992392994
| "caml_int64_succ" ->
392993392995
E.runtime_call Js_runtime_modules.int64 "succ" args
392994392996
| "caml_int64_to_string" ->
@@ -393218,11 +393220,10 @@ let translate loc (prim_name : string)
393218393220
| "caml_int32_compare"
393219393221
| "caml_nativeint_compare"
393220393222
| "caml_float_compare"
393221-
| "caml_bytes_compare"
393223+
393222393224
| "caml_string_compare"
393223393225
->
393224393226
call Js_runtime_modules.caml_primitive
393225-
393226393227
| "caml_bool_min"
393227393228
| "caml_int_min"
393228393229
| "caml_float_min"

lib/es6/bytes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ function rcontains_from(s, i, c) {
569569
}
570570
}
571571

572-
var compare = Caml_primitive.caml_bytes_compare;
572+
var compare = Caml_bytes.caml_bytes_compare;
573573

574574
function uppercase(s) {
575575
return map(Char.uppercase, s);
@@ -587,7 +587,7 @@ function uncapitalize(s) {
587587
return apply1(Char.lowercase, s);
588588
}
589589

590-
var equal = Caml_primitive.caml_bytes_equal;
590+
var equal = Caml_bytes.caml_bytes_equal;
591591

592592
var unsafe_to_string = Caml_bytes.bytes_to_string;
593593

0 commit comments

Comments
 (0)