Skip to content

Commit 8b9cbfc

Browse files
authored
Merge pull request #4600 from BuckleScript/fix_4599
fix #4599
2 parents 7f8b420 + 5eb6786 commit 8b9cbfc

25 files changed

+132
-160
lines changed

jscomp/core/lam_compile_primitive.ml

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -492,25 +492,22 @@ let translate loc
492492
Bytes is an int array in javascript
493493
*)
494494
| Pbytessetu
495-
| Pbytessets ->
495+
->
496496
(match args with
497497
| [e;e0;e1] -> ensure_value_unit cxt.continuation
498498
(Js_of_lam_string.set_byte e e0 e1)
499499
| _ -> assert false)
500+
| Pbytessets ->
501+
E.runtime_call Js_runtime_modules.bytes "set" args
500502
| Pbytesrefu ->
501503
(match args with
502504
| [e;e1] -> Js_of_lam_string.ref_byte e e1
503505
| _ -> assert false)
504-
505-
506506
| Pbytesrefs ->
507-
begin match args with
508-
| [e ; e1] ->
509-
if !Clflags.fast then
510-
Js_of_lam_string.ref_byte e e1
511-
else E.runtime_call Js_runtime_modules.bytes "get" args
512-
| _ -> assert false
513-
end
507+
E.runtime_call Js_runtime_modules.bytes "get" args
508+
| Pstringrefs ->
509+
E.runtime_call Js_runtime_modules.string "get" args
510+
514511
(* For bytes and string, they both return [int] in ocaml
515512
we need tell Pbyteref from Pstringref
516513
1. Pbyteref -> a[i]
@@ -521,19 +518,8 @@ let translate loc
521518
| [e;e1] -> Js_of_lam_string.ref_string e e1
522519
| _ -> assert false
523520
end
524-
525-
| Pstringrefs ->
526-
begin match args with
527-
| [e;e1] ->
528-
if !Clflags.fast then
529-
Js_of_lam_string.ref_string e e1
530-
else
531-
E.runtime_call Js_runtime_modules.string "get" args
532-
| _ -> assert false
533-
end
534521
(** only when Lapply -> expand = true*)
535522
| Praise -> assert false (* handled before here *)
536-
537523
(* Runtime encoding relevant *)
538524
| Parraylength ->
539525
E.array_length (Ext_list.singleton_exn args)

jscomp/runtime/caml_bytes.ml

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,28 @@
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-
open Caml_bytes_extern
25+
26+
external new_uninitialized : int -> bytes = "Array" [@@bs.new]
27+
28+
external (.![]) : bytes -> int -> char = "%bytes_unsafe_get"
29+
external (.![]<-) : bytes -> int -> char -> unit = "%bytes_unsafe_set"
30+
external length : bytes -> int = "%bytes_length"
31+
32+
33+
let set s i ch =
34+
if i < 0 || i >= length s then
35+
raise (Invalid_argument "index out of bounds")
36+
else s.![i] <- ch
2637

2738
let get s i =
2839
if i < 0 || i >= length s then
2940
raise (Invalid_argument "index out of bounds")
30-
else unsafe_get s i
41+
else s.![i]
3142

3243
let caml_fill_bytes (s : bytes) i l (c : char) =
3344
if l > 0 then
3445
for k = i to l + i - 1 do
35-
unsafe_set s k c
46+
s.![k] <- c
3647
done
3748

3849
let caml_create_bytes len : bytes =
@@ -41,7 +52,7 @@ let caml_create_bytes len : bytes =
4152
else
4253
let result = new_uninitialized len in
4354
for i = 0 to len - 1 do
44-
unsafe_set result i '\000'
55+
result.![i] <- '\000'
4556
done ;
4657
result
4758

@@ -53,14 +64,14 @@ let copyWithin (s1 : bytes) i1 i2 len =
5364
let range_b = len - 1 in
5465
let range = if range_a > range_b then range_b else range_a in
5566
for j = range downto 0 do
56-
unsafe_set s1 (i2 + j) (unsafe_get s1 (i1 + j))
67+
s1.![i2 + j] <- s1.![i1 + j]
5768
done
5869
else if i1 > i2 then
5970
let range_a = length s1 - i1 - 1 in
6071
let range_b = len - 1 in
6172
let range = if range_a > range_b then range_b else range_a in
6273
for k = 0 to range do
63-
unsafe_set s1 (i2 + k) (unsafe_get s1 (i1 + k))
74+
s1.![i2 + k] <- s1.![i1 + k]
6475
done
6576

6677
(* TODO: when the compiler could optimize small function calls,
@@ -74,20 +85,20 @@ let caml_blit_bytes (s1:bytes) i1 (s2:bytes) i2 len =
7485
let off1 = length s1 - i1 in
7586
if len <= off1 then
7687
for i = 0 to len - 1 do
77-
unsafe_set s2 (i2 + i) (unsafe_get s1 (i1 + i))
88+
s2.![i2 + i] <- s1.![i1 + i]
7889
done
7990
else
8091
begin
8192
for i = 0 to off1 - 1 do
82-
unsafe_set s2 (i2 + i) (unsafe_get s1 (i1 + i))
93+
s2.![i2 + i] <- s1.![i1 + i]
8394
done;
8495
for i = off1 to len - 1 do
85-
unsafe_set s2 (i2 + i) '\000'
96+
s2.![i2 + i] <- '\000'
8697
done
8798
end
8899

89100
external to_int_array : bytes -> int array = "%identity"
90-
let string_of_large_bytes bytes i len =
101+
let string_of_large_bytes (bytes : bytes) i len =
91102
let s = ref "" in
92103
let s_len = ref len in
93104
let seg = 1024 in
@@ -118,15 +129,15 @@ let caml_blit_string (s1 : string) i1 (s2 : bytes) i2 (len : int ) =
118129
let off1 = Caml_string_extern.length s1 - i1 in
119130
if len <= off1 then
120131
for i = 0 to len - 1 do
121-
unsafe_set s2 (i2 + i) (Caml_string_extern.unsafe_get s1 (i1 + i))
132+
s2.![i2 + i] <- Caml_string_extern.unsafe_get s1 (i1 + i)
122133
done
123134
else
124135
begin
125136
for i = 0 to off1 - 1 do
126-
unsafe_set s2 (i2 + i) (Caml_string_extern.unsafe_get s1 (i1 + i))
137+
s2.![i2 + i] <- Caml_string_extern.unsafe_get s1 (i1 + i)
127138
done;
128139
for i = off1 to len - 1 do
129-
unsafe_set s2 (i2 + i) '\000'
140+
s2.![i2 + i] <- '\000'
130141
done
131142
end
132143

@@ -135,7 +146,7 @@ let bytes_of_string s =
135146
let len = Caml_string_extern.length s in
136147
let res = new_uninitialized len in
137148
for i = 0 to len - 1 do
138-
unsafe_set res i (Caml_string_extern.unsafe_get s i)
149+
res.![i] <- Caml_string_extern.unsafe_get s i
139150
(* Note that when get a char and convert it to int immedately, should be optimized
140151
should be [s.charCodeAt[i]]
141152
*)
@@ -145,7 +156,7 @@ let bytes_of_string s =
145156

146157
let rec caml_bytes_compare_aux (s1 : bytes) (s2 : bytes) off len def =
147158
if off < len then
148-
let a, b = Caml_bytes_extern.unsafe_get s1 off, Caml_bytes_extern.unsafe_get s2 off in
159+
let a, b = s1.![off], s2.![off] in
149160
if a > b then 1
150161
else if a < b then -1
151162
else caml_bytes_compare_aux s1 s2 (off + 1) len def
@@ -160,7 +171,7 @@ let bytes_of_string s =
160171
161172
*)
162173
let caml_bytes_compare (s1 : bytes) (s2 : bytes) : int =
163-
let len1, len2 = Caml_bytes_extern.length s1, Caml_bytes_extern.length s2 in
174+
let len1, len2 = length s1, length s2 in
164175
if len1 = len2 then
165176
caml_bytes_compare_aux s1 s2 0 len1 0
166177
else if len1 < len2 then
@@ -171,12 +182,12 @@ let caml_bytes_compare (s1 : bytes) (s2 : bytes) : int =
171182
let rec caml_bytes_equal_aux (s1 : bytes) s2 (off : int) len =
172183
if off = len then true
173184
else
174-
let a, b = Caml_bytes_extern.unsafe_get s1 off, Caml_bytes_extern.unsafe_get s2 off in
185+
let a, b = s1.![off], s2.![off] in
175186
a = b
176187
&& caml_bytes_equal_aux s1 s2 (off + 1) len
177188

178189
let caml_bytes_equal (s1 : bytes) (s2 : bytes) : bool =
179-
let len1, len2 = Caml_bytes_extern.length s1, Caml_bytes_extern.length s2 in
190+
let len1, len2 = length s1, length s2 in
180191
len1 = len2 &&
181192
caml_bytes_equal_aux s1 s2 0 len1
182193

jscomp/runtime/caml_bytes.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
val caml_create_bytes : int -> bytes
2727
val caml_fill_bytes : bytes -> int -> int -> char -> unit
2828
val get : bytes -> int -> char
29+
val set :
30+
bytes ->
31+
int ->
32+
char ->
33+
unit
2934
val bytes_to_string : bytes -> string
3035
val caml_blit_bytes : bytes -> int -> bytes -> int -> int -> unit
3136

jscomp/runtime/caml_bytes_extern.ml

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

jscomp/runtime/release.ninja

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ build runtime/js.cmj runtime/js.cmi : cc runtime/js.ml
1515
bsc_flags = $bsc_no_open_flags
1616
build runtime/caml_array.cmj : cc_cmi runtime/caml_array.ml | runtime/caml_array.cmi runtime/caml_array_extern.cmj
1717
build runtime/caml_array.cmi : cc runtime/caml_array.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
18-
build runtime/caml_bytes.cmj : cc_cmi runtime/caml_bytes.ml | runtime/caml_bytes.cmi runtime/caml_bytes_extern.cmj runtime/caml_string_extern.cmj
18+
build runtime/caml_bytes.cmj : cc_cmi runtime/caml_bytes.ml | runtime/caml_bytes.cmi runtime/caml_string_extern.cmj
1919
build runtime/caml_bytes.cmi : cc runtime/caml_bytes.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
2020
build runtime/caml_float.cmj : cc_cmi runtime/caml_float.ml | runtime/caml_float.cmi runtime/caml_float_extern.cmj
2121
build runtime/caml_float.cmi : cc runtime/caml_float.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
@@ -56,7 +56,6 @@ build runtime/caml_string.cmi : cc runtime/caml_string.mli | runtime/bs_stdlib_m
5656
build runtime/caml_sys.cmj : cc_cmi runtime/caml_sys.ml | runtime/caml_array_extern.cmj runtime/caml_nativeint_extern.cmj runtime/caml_sys.cmi runtime/caml_undefined_extern.cmj runtime/js.cmj
5757
build runtime/caml_sys.cmi : cc runtime/caml_sys.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
5858
build runtime/caml_array_extern.cmi runtime/caml_array_extern.cmj : cc runtime/caml_array_extern.ml | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
59-
build runtime/caml_bytes_extern.cmi runtime/caml_bytes_extern.cmj : cc runtime/caml_bytes_extern.ml | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
6059
build runtime/caml_exceptions.cmi runtime/caml_exceptions.cmj : cc runtime/caml_exceptions.ml | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
6160
build runtime/caml_external_polyfill.cmi runtime/caml_external_polyfill.cmj : cc runtime/caml_external_polyfill.ml | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
6261
build runtime/caml_float_extern.cmi runtime/caml_float_extern.cmj : cc runtime/caml_float_extern.ml | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
@@ -68,4 +67,4 @@ build runtime/caml_oo_curry.cmi runtime/caml_oo_curry.cmj : cc runtime/caml_oo_c
6867
build runtime/caml_string_extern.cmi runtime/caml_string_extern.cmj : cc runtime/caml_string_extern.ml | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
6968
build runtime/caml_undefined_extern.cmi runtime/caml_undefined_extern.cmj : cc runtime/caml_undefined_extern.ml | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
7069
build runtime/curry.cmi runtime/curry.cmj : cc runtime/curry.ml | runtime/bs_stdlib_mini.cmi runtime/caml_array.cmj runtime/caml_array_extern.cmj runtime/js.cmi runtime/js.cmj
71-
build runtime : phony runtime/bs_stdlib_mini.cmi runtime/js.cmj runtime/js.cmi runtime/caml_array.cmi runtime/caml_array.cmj runtime/caml_bytes.cmi runtime/caml_bytes.cmj runtime/caml_float.cmi runtime/caml_float.cmj runtime/caml_format.cmi runtime/caml_format.cmj runtime/caml_gc.cmi runtime/caml_gc.cmj runtime/caml_hash.cmi runtime/caml_hash.cmj runtime/caml_hash_primitive.cmi runtime/caml_hash_primitive.cmj runtime/caml_int32.cmi runtime/caml_int32.cmj runtime/caml_int64.cmi runtime/caml_int64.cmj runtime/caml_io.cmi runtime/caml_io.cmj runtime/caml_lexer.cmi runtime/caml_lexer.cmj runtime/caml_md5.cmi runtime/caml_md5.cmj runtime/caml_module.cmi runtime/caml_module.cmj runtime/caml_obj.cmi runtime/caml_obj.cmj runtime/caml_oo.cmi runtime/caml_oo.cmj runtime/caml_option.cmi runtime/caml_option.cmj runtime/caml_parser.cmi runtime/caml_parser.cmj runtime/caml_primitive.cmi runtime/caml_primitive.cmj runtime/caml_splice_call.cmi runtime/caml_splice_call.cmj runtime/caml_string.cmi runtime/caml_string.cmj runtime/caml_sys.cmi runtime/caml_sys.cmj runtime/caml_array_extern.cmi runtime/caml_array_extern.cmj runtime/caml_bytes_extern.cmi runtime/caml_bytes_extern.cmj runtime/caml_exceptions.cmi runtime/caml_exceptions.cmj runtime/caml_external_polyfill.cmi runtime/caml_external_polyfill.cmj runtime/caml_float_extern.cmi runtime/caml_float_extern.cmj runtime/caml_int32_extern.cmi runtime/caml_int32_extern.cmj runtime/caml_int64_extern.cmi runtime/caml_int64_extern.cmj runtime/caml_js_exceptions.cmi runtime/caml_js_exceptions.cmj runtime/caml_nativeint_extern.cmi runtime/caml_nativeint_extern.cmj runtime/caml_oo_curry.cmi runtime/caml_oo_curry.cmj runtime/caml_string_extern.cmi runtime/caml_string_extern.cmj runtime/caml_undefined_extern.cmi runtime/caml_undefined_extern.cmj runtime/curry.cmi runtime/curry.cmj
70+
build runtime : phony runtime/bs_stdlib_mini.cmi runtime/js.cmj runtime/js.cmi runtime/caml_array.cmi runtime/caml_array.cmj runtime/caml_bytes.cmi runtime/caml_bytes.cmj runtime/caml_float.cmi runtime/caml_float.cmj runtime/caml_format.cmi runtime/caml_format.cmj runtime/caml_gc.cmi runtime/caml_gc.cmj runtime/caml_hash.cmi runtime/caml_hash.cmj runtime/caml_hash_primitive.cmi runtime/caml_hash_primitive.cmj runtime/caml_int32.cmi runtime/caml_int32.cmj runtime/caml_int64.cmi runtime/caml_int64.cmj runtime/caml_io.cmi runtime/caml_io.cmj runtime/caml_lexer.cmi runtime/caml_lexer.cmj runtime/caml_md5.cmi runtime/caml_md5.cmj runtime/caml_module.cmi runtime/caml_module.cmj runtime/caml_obj.cmi runtime/caml_obj.cmj runtime/caml_oo.cmi runtime/caml_oo.cmj runtime/caml_option.cmi runtime/caml_option.cmj runtime/caml_parser.cmi runtime/caml_parser.cmj runtime/caml_primitive.cmi runtime/caml_primitive.cmj runtime/caml_splice_call.cmi runtime/caml_splice_call.cmj runtime/caml_string.cmi runtime/caml_string.cmj runtime/caml_sys.cmi runtime/caml_sys.cmj runtime/caml_array_extern.cmi runtime/caml_array_extern.cmj runtime/caml_exceptions.cmi runtime/caml_exceptions.cmj runtime/caml_external_polyfill.cmi runtime/caml_external_polyfill.cmj runtime/caml_float_extern.cmi runtime/caml_float_extern.cmj runtime/caml_int32_extern.cmi runtime/caml_int32_extern.cmj runtime/caml_int64_extern.cmi runtime/caml_int64_extern.cmj runtime/caml_js_exceptions.cmi runtime/caml_js_exceptions.cmj runtime/caml_nativeint_extern.cmi runtime/caml_nativeint_extern.cmj runtime/caml_oo_curry.cmi runtime/caml_oo_curry.cmj runtime/caml_string_extern.cmi runtime/caml_string_extern.cmj runtime/caml_undefined_extern.cmi runtime/caml_undefined_extern.cmj runtime/curry.cmi runtime/curry.cmj

jscomp/test/bytes_split_gpr_743_test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ var b = [
3838
0
3939
];
4040

41-
b[0] = /* "a" */97;
41+
Caml_bytes.set(b, 0, /* "a" */97);
4242

43-
b[1] = /* "b" */98;
43+
Caml_bytes.set(b, 1, /* "b" */98);
4444

45-
b[2] = /* "c" */99;
45+
Caml_bytes.set(b, 2, /* "c" */99);
4646

4747
Bytes.blit(b, 0, b, 1, 2);
4848

@@ -61,11 +61,11 @@ var b$1 = [
6161
0
6262
];
6363

64-
b$1[0] = /* "a" */97;
64+
Caml_bytes.set(b$1, 0, /* "a" */97);
6565

66-
b$1[1] = /* "b" */98;
66+
Caml_bytes.set(b$1, 1, /* "b" */98);
6767

68-
b$1[2] = /* "c" */99;
68+
Caml_bytes.set(b$1, 2, /* "c" */99);
6969

7070
Bytes.blit(b$1, 1, b$1, 0, 2);
7171

jscomp/test/gray_code_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var Bytes = require("../../lib/js/bytes.js");
4+
var Caml_bytes = require("../../lib/js/caml_bytes.js");
45

56
function gray_encode(b) {
67
return b ^ (b >>> 1);
@@ -29,7 +30,7 @@ function bool_string(len, n) {
2930
var n$1 = _n;
3031
var i = _i;
3132
if ((n$1 & 1) === 1) {
32-
s[i] = /* "1" */49;
33+
Caml_bytes.set(s, i, /* "1" */49);
3334
}
3435
if (i <= 0) {
3536
return s;

jscomp/test/ocaml_parsetree_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12018,7 +12018,7 @@ function remove_underscores(s) {
1201812018
}
1201912019
var c = Caml_string.get(s, src);
1202012020
if (c !== 95) {
12021-
b[dst] = c;
12021+
Caml_bytes.set(b, dst, c);
1202212022
_dst = dst + 1 | 0;
1202312023
_src = src + 1 | 0;
1202412024
continue ;

jscomp/test/ocaml_re_test.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,9 +1781,8 @@ function is_charset(_param) {
17811781
function split(s, cm) {
17821782
var _t = s;
17831783
var f = function (i, j) {
1784-
cm[i] = /* "\001" */1;
1785-
cm[j + 1 | 0] = /* "\001" */1;
1786-
1784+
Caml_bytes.set(cm, i, /* "\001" */1);
1785+
return Caml_bytes.set(cm, j + 1 | 0, /* "\001" */1);
17871786
};
17881787
while(true) {
17891788
var t = _t;
@@ -1902,14 +1901,14 @@ function flatten_cmap(cm) {
19021901
var c = Caml_bytes.caml_create_bytes(256);
19031902
var col_repr = Caml_bytes.caml_create_bytes(256);
19041903
var v = 0;
1905-
c[0] = /* "\000" */0;
1906-
col_repr[0] = /* "\000" */0;
1904+
Caml_bytes.set(c, 0, /* "\000" */0);
1905+
Caml_bytes.set(col_repr, 0, /* "\000" */0);
19071906
for(var i = 1; i <= 255; ++i){
19081907
if (Caml_bytes.get(cm, i) !== /* "\000" */0) {
19091908
v = v + 1 | 0;
19101909
}
1911-
c[i] = Char.chr(v);
1912-
col_repr[v] = Char.chr(i);
1910+
Caml_bytes.set(c, i, Char.chr(v));
1911+
Caml_bytes.set(col_repr, v, Char.chr(i));
19131912
}
19141913
return [
19151914
c,

jscomp/test/ocaml_typedtree_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23255,7 +23255,7 @@ function remove_underscores(s) {
2325523255
}
2325623256
var c = Caml_string.get(s, src);
2325723257
if (c !== 95) {
23258-
b[dst] = c;
23258+
Caml_bytes.set(b, dst, c);
2325923259
_dst = dst + 1 | 0;
2326023260
_src = src + 1 | 0;
2326123261
continue ;

0 commit comments

Comments
 (0)