From 0cd52e7abe4e7605cba6c2e12e9ae5c763af84f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Vouillon?= Date: Thu, 16 Jan 2025 12:12:34 +0100 Subject: [PATCH] Runtime: add caml_string_of_uint8_array --- lib/js_of_ocaml/typed_array.ml | 2 +- lib/runtime/js_of_ocaml_runtime_stubs.c | 8 ++++---- lib/runtime/jsoo_runtime.ml | 2 +- runtime/js/blake2.js | 4 ++-- runtime/js/fs.js | 4 ++-- runtime/js/io.js | 6 ++---- runtime/js/marshal.js | 17 +++++++---------- runtime/js/md5.js | 8 ++++---- runtime/js/mlBytes.js | 6 ++++++ runtime/js/toplevel.js | 8 ++++---- runtime/wasm/bigarray.wat | 4 ++-- 11 files changed, 35 insertions(+), 34 deletions(-) diff --git a/lib/js_of_ocaml/typed_array.ml b/lib/js_of_ocaml/typed_array.ml index 75afe885f6..98def83222 100644 --- a/lib/js_of_ocaml/typed_array.ml +++ b/lib/js_of_ocaml/typed_array.ml @@ -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 diff --git a/lib/runtime/js_of_ocaml_runtime_stubs.c b/lib/runtime/js_of_ocaml_runtime_stubs.c index 559cc91fd1..244378350f 100644 --- a/lib/runtime/js_of_ocaml_runtime_stubs.c +++ b/lib/runtime/js_of_ocaml_runtime_stubs.c @@ -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!"); } @@ -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!"); } diff --git a/lib/runtime/jsoo_runtime.ml b/lib/runtime/jsoo_runtime.ml index af0457de38..ffde82958f 100644 --- a/lib/runtime/jsoo_runtime.ml +++ b/lib/runtime/jsoo_runtime.ml @@ -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 diff --git a/runtime/js/blake2.js b/runtime/js/blake2.js index e7661230cb..a13531eadb 100644 --- a/runtime/js/blake2.js +++ b/runtime/js/blake2.js @@ -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 diff --git a/runtime/js/fs.js b/runtime/js/fs.js index 9406743bb9..089fe00a87 100644 --- a/runtime/js/fs.js +++ b/runtime/js/fs.js @@ -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; @@ -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)); } diff --git a/runtime/js/io.js b/runtime/js/io.js index f8226aef1a..511825cde7 100644 --- a/runtime/js/io.js +++ b/runtime/js/io.js @@ -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; } diff --git a/runtime/js/marshal.js b/runtime/js/marshal.js index 33676514bb..d3a0a9cf83 100644 --- a/runtime/js/marshal.js +++ b/runtime/js/marshal.js @@ -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; @@ -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; @@ -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; @@ -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; @@ -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 diff --git a/runtime/js/md5.js b/runtime/js/md5.js index 8c026b42b3..950cf3a406 100644 --- a/runtime/js/md5.js +++ b/runtime/js/md5.js @@ -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) { @@ -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 @@ -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)); } diff --git a/runtime/js/mlBytes.js b/runtime/js/mlBytes.js index 5500518f93..c5277298a0 100644 --- a/runtime/js/mlBytes.js +++ b/runtime/js/mlBytes.js @@ -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) { diff --git a/runtime/js/toplevel.js b/runtime/js/toplevel.js index 9d54018f86..2e4547ee4e 100644 --- a/runtime/js/toplevel.js +++ b/runtime/js/toplevel.js @@ -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) { @@ -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])]; } diff --git a/runtime/wasm/bigarray.wat b/runtime/wasm/bigarray.wat index 812055eca7..c4b480b5bb 100644 --- a/runtime/wasm/bigarray.wat +++ b/runtime/wasm/bigarray.wat @@ -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)