Skip to content

Commit

Permalink
Runtime: add caml_string_of_uint8_array
Browse files Browse the repository at this point in the history
  • Loading branch information
vouillon committed Jan 16, 2025
1 parent 1f2bd2e commit 0cd52e7
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 34 deletions.
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 @@ -266,7 +266,7 @@ module Bigstring = struct
end

module String = struct
external of_uint8Array : uint8Array Js.t -> string = "caml_string_of_array"
external of_uint8Array : uint8Array Js.t -> string = "caml_string_of_uint8_array"

let of_arrayBuffer ab =
let uint8 = new%js uint8Array_fromBuffer ab in
Expand Down
8 changes: 4 additions & 4 deletions lib/runtime/js_of_ocaml_runtime_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,6 @@ void caml_read_file_content () {
caml_fatal_error("Unimplemented Javascript primitive caml_read_file_content!");
}

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

void caml_string_of_jsbytes () {
caml_fatal_error("Unimplemented Javascript primitive caml_string_of_jsbytes!");
}
Expand All @@ -268,6 +264,10 @@ void caml_string_of_jsstring () {
caml_fatal_error("Unimplemented Javascript primitive caml_string_of_jsstring!");
}

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

void caml_unmount () {
caml_fatal_error("Unimplemented Javascript primitive caml_unmount!");
}
Expand Down
2 changes: 1 addition & 1 deletion lib/runtime/jsoo_runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ module Typed_array = struct
external of_uint8Array : uint8Array -> t = "bigstring_of_typed_array"
end

external of_uint8Array : uint8Array -> string = "caml_string_of_array"
external of_uint8Array : uint8Array -> string = "caml_string_of_uint8_array"
end

module Int64 = struct
Expand Down
4 changes: 2 additions & 2 deletions runtime/js/blake2.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,12 @@ function caml_blake2_create(hashlen, key) {
}

//Provides: caml_blake2_final
//Requires: caml_string_of_array
//Requires: caml_string_of_uint8_array
//Requires: blake2b
//Version: >= 5.2
function caml_blake2_final(ctx, hashlen) {
var r = blake2b.Final(ctx);
return caml_string_of_array(r);
return caml_string_of_uint8_array(r);
}

//Provides: caml_blake2_update
Expand Down
4 changes: 2 additions & 2 deletions runtime/js/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ function jsoo_create_file(name, content) {
}

//Provides: caml_read_file_content
//Requires: resolve_fs_device, caml_raise_no_such_file, caml_string_of_array
//Requires: resolve_fs_device, caml_raise_no_such_file, caml_string_of_uint8_array
//Requires: caml_string_of_jsbytes, caml_jsbytes_of_string
function caml_read_file_content(name) {
var name = typeof name === "string" ? caml_string_of_jsbytes(name) : name;
Expand All @@ -348,7 +348,7 @@ function caml_read_file_content(name) {
var len = file.length();
var buf = new Uint8Array(len);
file.read(0, buf, 0, len);
return caml_string_of_array(buf);
return caml_string_of_uint8_array(buf);
}
caml_raise_no_such_file(caml_jsbytes_of_string(name));
}
6 changes: 2 additions & 4 deletions runtime/js/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,12 +700,10 @@ function caml_ml_pos_out_64(chanid) {
}

//Provides: caml_ml_output_int
//Requires: caml_ml_output
//Requires: caml_string_of_array
//Requires: caml_ml_output_ta
function caml_ml_output_int(chanid, i) {
var arr = [(i >> 24) & 0xff, (i >> 16) & 0xff, (i >> 8) & 0xff, i & 0xff];
var s = caml_string_of_array(arr);
caml_ml_output(chanid, s, 0, 4);
caml_ml_output_ta(chanid, new Uint8Array(arr), 0, 4);
return 0;
}

Expand Down
17 changes: 7 additions & 10 deletions runtime/js/marshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var caml_marshal_constants = {
};

//Provides: UInt8ArrayReader
//Requires: caml_string_of_array, caml_jsbytes_of_string
//Requires: caml_string_of_uint8_array, caml_jsbytes_of_string
function UInt8ArrayReader(s, i) {
this.s = s;
this.i = i;
Expand Down Expand Up @@ -86,7 +86,7 @@ UInt8ArrayReader.prototype = {
readstr: function (len) {
var i = this.i;
this.i = i + len;
return caml_string_of_array(this.s.subarray(i, i + len));
return caml_string_of_uint8_array(this.s.subarray(i, i + len));
},
readuint8array: function (len) {
var i = this.i;
Expand Down Expand Up @@ -161,7 +161,7 @@ MlStringReader.prototype = {
};

//Provides: BigStringReader
//Requires: caml_string_of_array, caml_ba_get_1
//Requires: caml_string_of_uint8_array, caml_ba_get_1
function BigStringReader(bs, i) {
this.s = bs;
this.i = i;
Expand Down Expand Up @@ -210,12 +210,9 @@ BigStringReader.prototype = {
},
readstr: function (len) {
var i = this.i;
var arr = new Array(len);
for (var j = 0; j < len; j++) {
arr[j] = caml_ba_get_1(this.s, i + j);
}
var offset = this.offset(i);
this.i = i + len;
return caml_string_of_array(arr);
return caml_string_of_uint8_array(this.s.data.subarray(offset, offset + len));
},
readuint8array: function (len) {
var i = this.i;
Expand Down Expand Up @@ -868,9 +865,9 @@ var caml_output_val = (function () {
})();

//Provides: caml_output_value_to_string mutable
//Requires: caml_output_val, caml_string_of_array
//Requires: caml_output_val, caml_string_of_uint8_array
function caml_output_value_to_string(v, flags) {
return caml_string_of_array(caml_output_val(v, flags));
return caml_string_of_uint8_array(caml_output_val(v, flags));
}

//Provides: caml_output_value_to_bytes mutable
Expand Down
8 changes: 4 additions & 4 deletions runtime/js/md5.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

//Provides: caml_md5_chan
//Requires: caml_string_of_array
//Requires: caml_string_of_uint8_array
//Requires: caml_raise_end_of_file, caml_ml_input_block
//Requires: caml_MD5Init, caml_MD5Update, caml_MD5Final
function caml_md5_chan(chanid, toread) {
Expand All @@ -43,7 +43,7 @@ function caml_md5_chan(chanid, toread) {
toread -= read;
}
}
return caml_string_of_array(caml_MD5Final(ctx));
return caml_string_of_uint8_array(caml_MD5Final(ctx));
}

//Provides: caml_md5_string
Expand Down Expand Up @@ -224,11 +224,11 @@ function caml_MD5Final(ctx) {
}

//Provides: caml_md5_bytes
//Requires: caml_uint8_array_of_bytes, caml_string_of_array
//Requires: caml_uint8_array_of_bytes, caml_string_of_uint8_array
//Requires: caml_MD5Init, caml_MD5Update, caml_MD5Final
function caml_md5_bytes(s, ofs, len) {
var ctx = caml_MD5Init();
var a = caml_uint8_array_of_bytes(s);
caml_MD5Update(ctx, a.subarray(ofs, ofs + len), len);
return caml_string_of_array(caml_MD5Final(ctx));
return caml_string_of_uint8_array(caml_MD5Final(ctx));
}
6 changes: 6 additions & 0 deletions runtime/js/mlBytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,12 @@ function caml_string_of_array(a) {
return caml_string_of_jsbytes(caml_subarray_to_jsbytes(a, 0, a.length));
}

//Provides: caml_string_of_uint8_array
//Requires: caml_sub_uint8_array_to_jsbytes, caml_string_of_jsbytes
function caml_string_of_uint8_array(a) {
return caml_string_of_jsbytes(caml_sub_uint8_array_to_jsbytes(a, 0, a.length));
}

//Provides: caml_bytes_of_array
//Requires: MlBytes
function caml_bytes_of_array(a) {
Expand Down
8 changes: 4 additions & 4 deletions runtime/js/toplevel.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,20 @@ function jsoo_toplevel_init_reloc(f) {

//Provides: caml_reify_bytecode
//Requires: caml_callback
//Requires: caml_string_of_array, caml_ba_to_typed_array
//Requires: caml_string_of_uint8_array, caml_ba_to_typed_array
//Requires: jsoo_toplevel_compile, caml_failwith
//Version: >= 5.2
function caml_reify_bytecode(code, debug, _digest) {
if (!jsoo_toplevel_compile) {
caml_failwith("Toplevel not initialized (jsoo_toplevel_compile)");
}
code = caml_string_of_array(caml_ba_to_typed_array(code));
code = caml_string_of_uint8_array(caml_ba_to_typed_array(code));
return [0, 0, caml_callback(jsoo_toplevel_compile, [code, debug])];
}

//Provides: caml_reify_bytecode
//Requires: caml_callback
//Requires: caml_string_of_array, caml_uint8_array_of_bytes
//Requires: caml_string_of_uint8_array, caml_uint8_array_of_bytes
//Requires: jsoo_toplevel_compile, caml_failwith
//Version: < 5.2
function caml_reify_bytecode(code, debug, _digest) {
Expand All @@ -124,7 +124,7 @@ function caml_reify_bytecode(code, debug, _digest) {
code.set(all[i], len);
len += all[i].length;
}
code = caml_string_of_array(code);
code = caml_string_of_uint8_array(code);
return [0, 0, caml_callback(jsoo_toplevel_compile, [code, debug])];
}

Expand Down
4 changes: 2 additions & 2 deletions runtime/wasm/bigarray.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2020,8 +2020,8 @@
(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_array))
(func $caml_string_of_array (export "caml_string_of_array")
(export "caml_bytes_of_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
(local $a (ref extern)) (local $len i32)
Expand Down

0 comments on commit 0cd52e7

Please sign in to comment.