From 5e2b223da586f0884e5e909f1f4145fedf0f416b Mon Sep 17 00:00:00 2001 From: John <37978984+johnridesabike@users.noreply.github.com> Date: Thu, 2 May 2024 18:26:36 -0400 Subject: [PATCH 1/4] Add Typed_array.Bytes module --- CHANGES.md | 6 ++++++ lib/js_of_ocaml/js_of_ocaml_stubs.c | 8 ++++++++ lib/js_of_ocaml/typed_array.ml | 10 ++++++++++ lib/js_of_ocaml/typed_array.mli | 8 ++++++++ 4 files changed, 32 insertions(+) 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..0fc3b0f5fa 100644 --- a/lib/js_of_ocaml/typed_array.mli +++ b/lib/js_of_ocaml/typed_array.mli @@ -254,3 +254,11 @@ module String : sig val of_uint8Array : uint8Array Js.t -> string end + +module Bytes : sig + val of_arrayBuffer : arrayBuffer Js.t -> bytes + + val of_uint8Array : uint8Array Js.t -> bytes + + val to_uint8Array : bytes -> uint8Array Js.t +end From 28e2293bd04a47fa0ee31d498a6071d7b951b44f Mon Sep 17 00:00:00 2001 From: John <37978984+johnridesabike@users.noreply.github.com> Date: Fri, 3 May 2024 07:26:07 -0400 Subject: [PATCH 2/4] Add documentation about modification behavior --- lib/js_of_ocaml/typed_array.mli | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/js_of_ocaml/typed_array.mli b/lib/js_of_ocaml/typed_array.mli index 0fc3b0f5fa..bc1b6d77cf 100644 --- a/lib/js_of_ocaml/typed_array.mli +++ b/lib/js_of_ocaml/typed_array.mli @@ -256,6 +256,13 @@ module String : sig end module Bytes : sig + (** These functions offer an efficient way to convert typed arrays to and from + [bytes] because they will not usually copy their input. + + Modifying their input may also modify their corresponding output, and vice + versa when modifying their output. This is not a guarantee, however, since + certain [bytes] operations may require the runtime to make a copy. *) + val of_arrayBuffer : arrayBuffer Js.t -> bytes val of_uint8Array : uint8Array Js.t -> bytes From d9cf0a8722c99409f9a37f38adb50424d46b85d9 Mon Sep 17 00:00:00 2001 From: John <37978984+johnridesabike@users.noreply.github.com> Date: Fri, 3 May 2024 07:36:21 -0400 Subject: [PATCH 3/4] Update documentation --- lib/js_of_ocaml/typed_array.mli | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/js_of_ocaml/typed_array.mli b/lib/js_of_ocaml/typed_array.mli index bc1b6d77cf..2cd68c7b0f 100644 --- a/lib/js_of_ocaml/typed_array.mli +++ b/lib/js_of_ocaml/typed_array.mli @@ -256,16 +256,18 @@ module String : sig end module Bytes : sig - (** These functions offer an efficient way to convert typed arrays to and from - [bytes] because they will not usually copy their input. - - Modifying their input may also modify their corresponding output, and vice - versa when modifying their output. This is not a guarantee, however, since - certain [bytes] operations may require the runtime to make a copy. *) - - val of_arrayBuffer : arrayBuffer Js.t -> bytes - val of_uint8Array : uint8Array Js.t -> bytes + (** This offers an efficient way to convert typed arrays to [bytes] because it + will not usually 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 on inputs that are 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 From df2eacb8be3923840c87f25b911722a4e2a10f36 Mon Sep 17 00:00:00 2001 From: John <37978984+johnridesabike@users.noreply.github.com> Date: Fri, 3 May 2024 07:40:45 -0400 Subject: [PATCH 4/4] Update documentation --- lib/js_of_ocaml/typed_array.mli | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/js_of_ocaml/typed_array.mli b/lib/js_of_ocaml/typed_array.mli index 2cd68c7b0f..21c80fb6fd 100644 --- a/lib/js_of_ocaml/typed_array.mli +++ b/lib/js_of_ocaml/typed_array.mli @@ -257,13 +257,13 @@ end module Bytes : sig val of_uint8Array : uint8Array Js.t -> bytes - (** This offers an efficient way to convert typed arrays to [bytes] because it - will not usually copy its input. + (** 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 on inputs that are sensitive to modification. *) + use this on input that is sensitive to modification. *) val to_uint8Array : bytes -> uint8Array Js.t (** See the words of caution for {!of_uint8Array}. *)