Skip to content

Commit 1331b93

Browse files
Lib: Add Typed_array.Bytes module (#1609)
1 parent bfbeb69 commit 1331b93

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# dev
2+
3+
## Features/Changes
4+
5+
* Library: new Typed_array.Bytes module.
6+
17
# 5.8.0 (2024-04-20) - Lille
28

39
## Features/Changes

lib/js_of_ocaml/js_of_ocaml_stubs.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#include <caml/misc.h>
22

3+
void caml_bytes_of_array () {
4+
caml_fatal_error("Unimplemented Javascript primitive caml_bytes_of_array!");
5+
}
6+
37
void caml_js_error_of_exception () {
48
caml_fatal_error("Unimplemented Javascript primitive caml_js_error_of_exception!");
59
}
@@ -20,6 +24,10 @@ void caml_js_on_ie () {
2024
caml_fatal_error("Unimplemented Javascript primitive caml_js_on_ie!");
2125
}
2226

27+
void caml_uint8_array_of_bytes () {
28+
caml_fatal_error("Unimplemented Javascript primitive caml_uint8_array_of_bytes!");
29+
}
30+
2331
void caml_xmlhttprequest_create () {
2432
caml_fatal_error("Unimplemented Javascript primitive caml_xmlhttprequest_create!");
2533
}

lib/js_of_ocaml/typed_array.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,13 @@ module String = struct
261261
let uint8 = new%js uint8Array_fromBuffer ab in
262262
of_uint8Array uint8
263263
end
264+
265+
module Bytes = struct
266+
external of_uint8Array : uint8Array Js.t -> bytes = "caml_bytes_of_array"
267+
268+
external to_uint8Array : bytes -> uint8Array Js.t = "caml_uint8_array_of_bytes"
269+
270+
let of_arrayBuffer ab =
271+
let uint8 = new%js uint8Array_fromBuffer ab in
272+
of_uint8Array uint8
273+
end

lib/js_of_ocaml/typed_array.mli

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,20 @@ module String : sig
254254

255255
val of_uint8Array : uint8Array Js.t -> string
256256
end
257+
258+
module Bytes : sig
259+
val of_uint8Array : uint8Array Js.t -> bytes
260+
(** This efficiently converts a typed array to [bytes] because it will usually
261+
not copy its input.
262+
263+
Modifying its input may also modify its output, and vice versa when
264+
modifying its output. This is not a guarantee, however, since certain
265+
[bytes] operations may require the runtime to make a copy. One should not
266+
use this on input that is sensitive to modification. *)
267+
268+
val to_uint8Array : bytes -> uint8Array Js.t
269+
(** See the words of caution for {!of_uint8Array}. *)
270+
271+
val of_arrayBuffer : arrayBuffer Js.t -> bytes
272+
(** See the words of caution for {!of_uint8Array}. *)
273+
end

0 commit comments

Comments
 (0)