Skip to content

Commit e3f7aa1

Browse files
johnridesabikeOlivierNicole
authored andcommitted
Lib: Add Typed_array.Bytes module (#1609)
1 parent 4ce9356 commit e3f7aa1

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

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 <stdlib.h>
22
#include <stdio.h>
3+
void caml_bytes_of_array () {
4+
fprintf(stderr, "Unimplemented Javascript primitive caml_bytes_of_array!\n");
5+
exit(1);
6+
}
37
void caml_custom_identifier () {
48
fprintf(stderr, "Unimplemented Javascript primitive caml_custom_identifier!\n");
59
exit(1);
@@ -24,6 +28,10 @@ void caml_js_on_ie () {
2428
fprintf(stderr, "Unimplemented Javascript primitive caml_js_on_ie!\n");
2529
exit(1);
2630
}
31+
void caml_uint8_array_of_bytes () {
32+
fprintf(stderr, "Unimplemented Javascript primitive caml_uint8_array_of_bytes!\n");
33+
exit(1);
34+
}
2735
void caml_xmlhttprequest_create () {
2836
fprintf(stderr, "Unimplemented Javascript primitive caml_xmlhttprequest_create!\n");
2937
exit(1);

lib/js_of_ocaml/typed_array.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,13 @@ module String = struct
277277
let uint8 = new%js uint8Array_fromBuffer ab in
278278
of_uint8Array uint8
279279
end
280+
281+
module Bytes = struct
282+
external of_uint8Array : uint8Array Js.t -> bytes = "caml_bytes_of_array"
283+
284+
external to_uint8Array : bytes -> uint8Array Js.t = "caml_uint8_array_of_bytes"
285+
286+
let of_arrayBuffer ab =
287+
let uint8 = new%js uint8Array_fromBuffer ab in
288+
of_uint8Array uint8
289+
end

lib/js_of_ocaml/typed_array.mli

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

274274
val of_uint8Array : uint8Array Js.t -> string
275275
end
276+
277+
module Bytes : sig
278+
val of_uint8Array : uint8Array Js.t -> bytes
279+
(** This efficiently converts a typed array to [bytes] because it will usually
280+
not copy its input.
281+
282+
Modifying its input may also modify its output, and vice versa when
283+
modifying its output. This is not a guarantee, however, since certain
284+
[bytes] operations may require the runtime to make a copy. One should not
285+
use this on input that is sensitive to modification. *)
286+
287+
val to_uint8Array : bytes -> uint8Array Js.t
288+
(** See the words of caution for {!of_uint8Array}. *)
289+
290+
val of_arrayBuffer : arrayBuffer Js.t -> bytes
291+
(** See the words of caution for {!of_uint8Array}. *)
292+
end

0 commit comments

Comments
 (0)