diff --git a/CHANGES.md b/CHANGES.md index 271d9231fc..3890f9ff65 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,9 @@ +# dev + +## Features/Changes + +* Library: new Typed_array.Bytes module. + # 5.8.0 (2024-04-20) - Lille ## Features/Changes diff --git a/lib/js_of_ocaml/js_of_ocaml_stubs.c b/lib/js_of_ocaml/js_of_ocaml_stubs.c index c93129471c..36f5cf8bfa 100644 --- a/lib/js_of_ocaml/js_of_ocaml_stubs.c +++ b/lib/js_of_ocaml/js_of_ocaml_stubs.c @@ -1,5 +1,9 @@ #include +void caml_bytes_of_array () { + caml_fatal_error("Unimplemented Javascript primitive caml_bytes_of_array!"); +} + void caml_js_error_of_exception () { caml_fatal_error("Unimplemented Javascript primitive caml_js_error_of_exception!"); } @@ -20,6 +24,10 @@ void caml_js_on_ie () { caml_fatal_error("Unimplemented Javascript primitive caml_js_on_ie!"); } +void caml_uint8_array_of_bytes () { + caml_fatal_error("Unimplemented Javascript primitive caml_uint8_array_of_bytes!"); +} + void caml_xmlhttprequest_create () { caml_fatal_error("Unimplemented Javascript primitive caml_xmlhttprequest_create!"); } diff --git a/lib/js_of_ocaml/typed_array.ml b/lib/js_of_ocaml/typed_array.ml index 1e42b79f5d..f0bd32f2aa 100644 --- a/lib/js_of_ocaml/typed_array.ml +++ b/lib/js_of_ocaml/typed_array.ml @@ -261,3 +261,13 @@ module String = struct let uint8 = new%js uint8Array_fromBuffer ab in of_uint8Array uint8 end + +module Bytes = struct + external of_uint8Array : uint8Array Js.t -> bytes = "caml_bytes_of_array" + + external to_uint8Array : bytes -> uint8Array Js.t = "caml_uint8_array_of_bytes" + + let of_arrayBuffer ab = + let uint8 = new%js uint8Array_fromBuffer ab in + of_uint8Array uint8 +end diff --git a/lib/js_of_ocaml/typed_array.mli b/lib/js_of_ocaml/typed_array.mli index b85c784af4..21c80fb6fd 100644 --- a/lib/js_of_ocaml/typed_array.mli +++ b/lib/js_of_ocaml/typed_array.mli @@ -254,3 +254,20 @@ module String : sig val of_uint8Array : uint8Array Js.t -> string end + +module Bytes : sig + val of_uint8Array : uint8Array Js.t -> bytes + (** This efficiently converts a typed array to [bytes] because it will usually + not copy its input. + + Modifying its input may also modify its output, and vice versa when + modifying its output. This is not a guarantee, however, since certain + [bytes] operations may require the runtime to make a copy. One should not + use this on input that is sensitive to modification. *) + + val to_uint8Array : bytes -> uint8Array Js.t + (** See the words of caution for {!of_uint8Array}. *) + + val of_arrayBuffer : arrayBuffer Js.t -> bytes + (** See the words of caution for {!of_uint8Array}. *) +end