From 4fa380aee61aa7ba1ba8b8e052ebe438d5b18875 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Mon, 11 Sep 2023 23:44:49 -0700 Subject: [PATCH] fix: add missing Caml_bytes.set{32,64}u (#736) --- jscomp/runtime/caml_bytes.ml | 18 ++++++++++++------ jscomp/runtime/caml_bytes.mli | 2 ++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/jscomp/runtime/caml_bytes.ml b/jscomp/runtime/caml_bytes.ml index 985dbc011f..f13f6cd64b 100644 --- a/jscomp/runtime/caml_bytes.ml +++ b/jscomp/runtime/caml_bytes.ml @@ -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 @@ -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 @@ -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 diff --git a/jscomp/runtime/caml_bytes.mli b/jscomp/runtime/caml_bytes.mli index 97ad90cdc8..0d194092fc 100644 --- a/jscomp/runtime/caml_bytes.mli +++ b/jscomp/runtime/caml_bytes.mli @@ -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