Skip to content

Commit

Permalink
fix: add missing Caml_bytes.set{32,64}u (#736)
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro authored Sep 12, 2023
1 parent d717d5b commit 4fa380a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 12 additions & 6 deletions jscomp/runtime/caml_bytes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,7 @@ let set16 b idx newval =
raise (Invalid_argument "index out of bounds");
set16u b idx newval

let set32 str idx newval =
if idx < 0 || idx + 3 >= length str then
raise (Invalid_argument "index out of bounds");
let set32u str idx newval =
let b4 = 0xFFl &~ (newval >>~ 24) in
let b3 = 0xFFl &~ (newval >>~ 16) in
let b2 = 0xFFl &~ (newval >>~ 8) in
Expand All @@ -299,10 +297,13 @@ let set32 str idx newval =
str.![idx + 2] <- unsafe_code (Caml_int32_extern.to_int b3);
str.![idx + 3] <- unsafe_code (Caml_int32_extern.to_int b4)

let set64 str idx newval =
let newval = Caml_int64.unsafe_of_int64 newval in
if idx < 0 || idx + 7 >= length str then
let set32 str idx newval =
if idx < 0 || idx + 3 >= length str then
raise (Invalid_argument "index out of bounds");
set32u str idx newval

let set64u str idx newval =
let newval = Caml_int64.unsafe_of_int64 newval in
let b8 = 0xFF land Caml_int64.to_int32 (newval >>>~ 56) in
let b7 = 0xFF land Caml_int64.to_int32 (newval >>>~ 48) in
let b6 = 0xFF land Caml_int64.to_int32 (newval >>>~ 40) in
Expand All @@ -319,3 +320,8 @@ let set64 str idx newval =
str.![idx + 5] <- unsafe_code b6;
str.![idx + 6] <- unsafe_code b7;
str.![idx + 7] <- unsafe_code b8

let set64 str idx newval =
if idx < 0 || idx + 7 >= length str then
raise (Invalid_argument "index out of bounds");
set64u str idx newval
2 changes: 2 additions & 0 deletions jscomp/runtime/caml_bytes.mli
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ val get32 : bytes -> int -> int32
val get64 : bytes -> int -> int64
val set16u : bytes -> int -> int -> unit
val set16 : bytes -> int -> int -> unit
val set32u : bytes -> int -> int32 -> unit
val set32 : bytes -> int -> int32 -> unit
val set64u : bytes -> int -> int64 -> unit
val set64 : bytes -> int -> int64 -> unit

0 comments on commit 4fa380a

Please sign in to comment.