Skip to content

Add Typed_array.Bytes module #1609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# dev

## Features/Changes

* Library: new Typed_array.Bytes module.

# 5.8.0 (2024-04-20) - Lille

## Features/Changes
Expand Down
8 changes: 8 additions & 0 deletions lib/js_of_ocaml/js_of_ocaml_stubs.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include <caml/misc.h>

void caml_bytes_of_array () {
caml_fatal_error("Unimplemented Javascript primitive caml_bytes_of_array!");
}

Comment on lines +3 to +6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m a bit lost, can someone explain to me why using the API will not just raise this error?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just to keep the Ocaml bytecode compiler happy. This code is executed instead:

function caml_bytes_of_array (a) {
if(! (a instanceof Uint8Array)) {
a = new Uint8Array(a);
}
return new MlBytes(4,a,a.length);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see, thanks!

void caml_js_error_of_exception () {
caml_fatal_error("Unimplemented Javascript primitive caml_js_error_of_exception!");
}
Expand All @@ -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!");
}
10 changes: 10 additions & 0 deletions lib/js_of_ocaml/typed_array.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions lib/js_of_ocaml/typed_array.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading