Skip to content

Commit

Permalink
Runtime: add caml_bytes_of_uint8_array
Browse files Browse the repository at this point in the history
  • Loading branch information
vouillon committed Jan 16, 2025
1 parent 0cd52e7 commit a21bc84
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/js_of_ocaml/js_of_ocaml_stubs.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <caml/misc.h>

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

void caml_custom_identifier () {
Expand Down
2 changes: 1 addition & 1 deletion lib/js_of_ocaml/typed_array.ml
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ module String = struct
end

module Bytes = struct
external of_uint8Array : uint8Array Js.t -> bytes = "caml_bytes_of_array"
external of_uint8Array : uint8Array Js.t -> bytes = "caml_bytes_of_uint8_array"

external to_uint8Array : bytes -> uint8Array Js.t = "caml_uint8_array_of_bytes"

Expand Down
4 changes: 2 additions & 2 deletions runtime/js/bigstring.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function caml_bigstring_blit_bytes_to_ba(str1, pos1, ba2, pos2, len) {

//Provides: caml_bigstring_blit_ba_to_bytes
//Requires: caml_invalid_argument, caml_array_bound_error
//Requires: caml_blit_bytes, caml_bytes_of_array
//Requires: caml_blit_bytes, caml_bytes_of_uint8_array
//Requires: caml_ml_bytes_length
function caml_bigstring_blit_ba_to_bytes(ba1, pos1, bytes2, pos2, len) {
if (12 !== ba1.kind)
Expand All @@ -121,6 +121,6 @@ function caml_bigstring_blit_ba_to_bytes(ba1, pos1, bytes2, pos2, len) {
caml_array_bound_error();
}
var slice = ba1.data.subarray(ofs1, ofs1 + len);
caml_blit_bytes(caml_bytes_of_array(slice), 0, bytes2, pos2, len);
caml_blit_bytes(caml_bytes_of_uint8_array(slice), 0, bytes2, pos2, len);
return 0;
}
8 changes: 4 additions & 4 deletions runtime/js/fs_fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ MlFakeDevice.prototype.constructor = MlFakeDevice;
//Provides: MlFakeFile
//Requires: MlFile
//Requires: caml_create_bytes, caml_ml_bytes_length, caml_blit_bytes
//Requires: caml_uint8_array_of_bytes, caml_bytes_of_array
//Requires: caml_uint8_array_of_bytes, caml_bytes_of_uint8_array
function MlFakeFile(content) {
this.data = content;
}
Expand All @@ -329,7 +329,7 @@ MlFakeFile.prototype.write = function (offset, buf, pos, len) {
this.data = new_str;
caml_blit_bytes(old_data, 0, this.data, 0, clen);
}
caml_blit_bytes(caml_bytes_of_array(buf), pos, this.data, offset, len);
caml_blit_bytes(caml_bytes_of_uint8_array(buf), pos, this.data, offset, len);
return 0;
};
MlFakeFile.prototype.read = function (offset, buf, pos, len) {
Expand All @@ -346,7 +346,7 @@ MlFakeFile.prototype.read = function (offset, buf, pos, len) {
};

//Provides: MlFakeFd_out
//Requires: MlFakeFile, caml_create_bytes, caml_blit_bytes, caml_bytes_of_array
//Requires: MlFakeFile, caml_create_bytes, caml_blit_bytes, caml_bytes_of_uint8_array
//Requires: caml_raise_sys_error
function MlFakeFd_out(fd, flags) {
MlFakeFile.call(this, caml_create_bytes(0));
Expand Down Expand Up @@ -374,7 +374,7 @@ MlFakeFd_out.prototype.write = function (offset, buf, pos, len) {
// Do not output the last \n if present
// as console logging display a newline at the end
var src = caml_create_bytes(len);
caml_blit_bytes(caml_bytes_of_array(buf), pos, src, 0, len);
caml_blit_bytes(caml_bytes_of_uint8_array(buf), pos, src, 0, len);
this.log(src.toUtf16());
return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions runtime/js/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ function caml_ml_input_block(chanid, ba, i, l) {
}

//Provides: caml_input_value
//Requires: caml_marshal_data_size, caml_input_value_from_bytes, caml_create_bytes, caml_ml_channel_get, caml_bytes_of_array
//Requires: caml_marshal_data_size, caml_input_value_from_bytes, caml_create_bytes, caml_ml_channel_get, caml_bytes_of_uint8_array
//Requires: caml_refill, caml_failwith, caml_raise_end_of_file
//Requires: caml_marshal_header_size
function caml_input_value(chanid) {
Expand All @@ -434,12 +434,12 @@ function caml_input_value(chanid) {
if (r === 0) caml_raise_end_of_file();
else if (r < caml_marshal_header_size)
caml_failwith("input_value: truncated object");
var len = caml_marshal_data_size(caml_bytes_of_array(header), 0);
var len = caml_marshal_data_size(caml_bytes_of_uint8_array(header), 0);
var buf = new Uint8Array(len + caml_marshal_header_size);
buf.set(header, 0);
var r = block(buf, caml_marshal_header_size, len);
if (r < len) caml_failwith("input_value: truncated object " + r + " " + len);
var res = caml_input_value_from_bytes(caml_bytes_of_array(buf), 0);
var res = caml_input_value_from_bytes(caml_bytes_of_uint8_array(buf), 0);
return res;
}

Expand Down
8 changes: 4 additions & 4 deletions runtime/js/marshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,16 +871,16 @@ function caml_output_value_to_string(v, flags) {
}

//Provides: caml_output_value_to_bytes mutable
//Requires: caml_output_val, caml_bytes_of_array
//Requires: caml_output_val, caml_bytes_of_uint8_array
function caml_output_value_to_bytes(v, flags) {
return caml_bytes_of_array(caml_output_val(v, flags));
return caml_bytes_of_uint8_array(caml_output_val(v, flags));
}

//Provides: caml_output_value_to_buffer
//Requires: caml_output_val, caml_failwith, caml_blit_bytes, caml_bytes_of_array
//Requires: caml_output_val, caml_failwith, caml_blit_bytes, caml_bytes_of_uint8_array
function caml_output_value_to_buffer(s, ofs, len, v, flags) {
var t = caml_output_val(v, flags);
if (t.length > len) caml_failwith("Marshal.to_buffer: buffer overflow");
caml_blit_bytes(caml_bytes_of_array(t), 0, s, ofs, t.length);
caml_blit_bytes(caml_bytes_of_uint8_array(t), 0, s, ofs, t.length);
return 0;
}
6 changes: 6 additions & 0 deletions runtime/js/mlBytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,12 @@ function caml_bytes_of_array(a) {
return new MlBytes(4, a, a.length);
}

//Provides: caml_bytes_of_uint8_array
//Requires: MlBytes
function caml_bytes_of_uint8_array(a) {
return new MlBytes(4, a, a.length);
}

//Provides: caml_bytes_compare mutable
//Requires: caml_convert_string_to_bytes
function caml_bytes_compare(s1, s2) {
Expand Down
2 changes: 1 addition & 1 deletion runtime/wasm/bigarray.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2020,7 +2020,7 @@
(i32.wrap_i64 (i64.shr_u (local.get $d) (i64.const 32))))
(ref.i31 (i32.const 0)))

(export "caml_bytes_of_array" (func $caml_string_of_uint8_array))
(export "caml_bytes_of_uint8_array" (func $caml_string_of_uint8_array))
(func $caml_string_of_uint8_array (export "caml_string_of_uint8_array")
(param (ref eq)) (result (ref eq))
;; used to convert a typed array to a string
Expand Down

0 comments on commit a21bc84

Please sign in to comment.