diff --git a/runtime/js/array.js b/runtime/js/array.js index 76e0773a0c..6bf183eea2 100644 --- a/runtime/js/array.js +++ b/runtime/js/array.js @@ -17,8 +17,10 @@ ///////////// Array +import { caml_array_bound_error } from './fail.js'; + //Provides: caml_array_sub mutable -function caml_array_sub(a, i, len) { +export function caml_array_sub(a, i, len) { var a2 = new Array(len + 1); a2[0] = 0; for (var i2 = 1, i1 = i + 1; i2 <= len; i2++, i1++) { @@ -28,21 +30,19 @@ function caml_array_sub(a, i, len) { } //Provides: caml_floatarray_sub mutable -//Requires: caml_array_sub //Version: >= 5.3 -function caml_floatarray_sub(a, i, len) { +export function caml_floatarray_sub(a, i, len) { return caml_array_sub(a, i, len); } //Provides: caml_uniform_array_sub mutable -//Requires: caml_array_sub //Version: >= 5.3 -function caml_uniform_array_sub(a, i, len) { +export function caml_uniform_array_sub(a, i, len) { return caml_array_sub(a, i, len); } //Provides: caml_array_append mutable -function caml_array_append(a1, a2) { +export function caml_array_append(a1, a2) { var l1 = a1.length, l2 = a2.length; var l = l1 + l2 - 1; @@ -56,21 +56,19 @@ function caml_array_append(a1, a2) { } //Provides: caml_floatarray_append mutable -//Requires: caml_array_append //Version: >= 5.3 -function caml_floatarray_append(a1, a2) { +export function caml_floatarray_append(a1, a2) { return caml_array_append(a1, a2); } //Provides: caml_uniform_array_append mutable -//Requires: caml_array_append //Version: >= 5.3 -function caml_uniform_array_append(a1, a2) { +export function caml_uniform_array_append(a1, a2) { return caml_array_append(a1, a2); } //Provides: caml_array_concat mutable -function caml_array_concat(l) { +export function caml_array_concat(l) { var a = [0]; while (l !== 0) { var b = l[1]; @@ -82,7 +80,7 @@ function caml_array_concat(l) { //Provides: caml_floatarray_concat mutable //Version: >= 5.4 -function caml_floatarray_concat(l) { +export function caml_floatarray_concat(l) { var a = [0]; while (l !== 0) { var b = l[1]; @@ -94,7 +92,7 @@ function caml_floatarray_concat(l) { //Provides: caml_uniform_array_concat mutable //Version: >= 5.4 -function caml_uniform_array_concat(l) { +export function caml_uniform_array_concat(l) { var a = [0]; while (l !== 0) { var b = l[1]; @@ -105,7 +103,7 @@ function caml_uniform_array_concat(l) { } //Provides: caml_array_blit -function caml_array_blit(a1, i1, a2, i2, len) { +export function caml_array_blit(a1, i1, a2, i2, len) { if (i2 <= i1) { for (var j = 1; j <= len; j++) a2[i2 + j] = a1[i1 + j]; } else { @@ -115,42 +113,38 @@ function caml_array_blit(a1, i1, a2, i2, len) { } //Provides: caml_floatarray_blit -//Requires: caml_array_blit -function caml_floatarray_blit(a1, i1, a2, i2, len) { +export function caml_floatarray_blit(a1, i1, a2, i2, len) { return caml_array_blit(a1, i1, a2, i2, len); } //Provides: caml_uniform_array_blit -//Requires: caml_array_blit //Version: >= 5.3 -function caml_uniform_array_blit(a1, i1, a2, i2, len) { +export function caml_uniform_array_blit(a1, i1, a2, i2, len) { return caml_array_blit(a1, i1, a2, i2, len); } ///////////// Pervasive //Provides: caml_array_set (mutable, const, mutable) -//Requires: caml_array_bound_error //Alias: caml_array_set_float //Alias: caml_floatarray_set //Alias: caml_array_set_addr -function caml_array_set(array, index, newval) { +export function caml_array_set(array, index, newval) { if (index < 0 || index >= array.length - 1) caml_array_bound_error(); array[index + 1] = newval; return 0; } //Provides: caml_array_get mutable (mutable, const) -//Requires: caml_array_bound_error //Alias: caml_array_get_float //Alias: caml_floatarray_get //Alias: caml_array_get_addr -function caml_array_get(array, index) { +export function caml_array_get(array, index) { if (index < 0 || index >= array.length - 1) caml_array_bound_error(); return array[index + 1]; } //Provides: caml_array_fill -function caml_array_fill(array, ofs, len, v) { +export function caml_array_fill(array, ofs, len, v) { for (var i = 0; i < len; i++) { array[ofs + i + 1] = v; } @@ -158,38 +152,33 @@ function caml_array_fill(array, ofs, len, v) { } //Provides: caml_floatarray_fill -//Requires: caml_array_fill //Version: >= 5.3 -function caml_floatarray_fill(array, ofs, len, v) { +export function caml_floatarray_fill(array, ofs, len, v) { return caml_array_fill(array, ofs, len, v); } //Provides: caml_floatarray_fill_unboxed -//Requires: caml_array_fill //Version: >= 5.3 -function caml_floatarray_fill_unboxed(array, ofs, len, v) { +export function caml_floatarray_fill_unboxed(array, ofs, len, v) { return caml_array_fill(array, ofs, len, v); } //Provides: caml_uniform_array_fill -//Requires: caml_array_fill //Version: >= 5.3 -function caml_uniform_array_fill(array, ofs, len, v) { +export function caml_uniform_array_fill(array, ofs, len, v) { return caml_array_fill(array, ofs, len, v); } //Provides: caml_check_bound (mutable, const) -//Requires: caml_array_bound_error //Alias: caml_check_bound_gen //Alias: caml_check_bound_float -function caml_check_bound(array, index) { +export function caml_check_bound(array, index) { if (index >>> 0 >= array.length - 1) caml_array_bound_error(); return array; } //Provides: caml_array_make const (const, mutable) -//Requires: caml_array_bound_error -function caml_array_make(len, init) { +export function caml_array_make(len, init) { if (len >>> 0 >= ((0x7fffffff / 4) | 0)) caml_array_bound_error(); var len = (len + 1) | 0; var b = new Array(len); @@ -199,14 +188,12 @@ function caml_array_make(len, init) { } //Provides: caml_make_vect const (const, mutable) -//Requires: caml_array_make -function caml_make_vect(len, init) { +export function caml_make_vect(len, init) { return caml_array_make(len, init); } //Provides: caml_make_float_vect const (const) -//Requires: caml_array_bound_error -function caml_make_float_vect(len) { +export function caml_make_float_vect(len) { if (len >>> 0 >= ((0x7fffffff / 8) | 0)) caml_array_bound_error(); var len = (len + 1) | 0; var b = new Array(len); @@ -216,9 +203,8 @@ function caml_make_float_vect(len) { } //Provides: caml_array_create_float const (const) -//Requires: caml_array_bound_error //Version: >= 5.3 -function caml_array_create_float(len) { +export function caml_array_create_float(len) { if (len >>> 0 >= ((0x7fffffff / 8) | 0)) caml_array_bound_error(); var len = (len + 1) | 0; var b = new Array(len); @@ -227,8 +213,7 @@ function caml_array_create_float(len) { return b; } //Provides: caml_floatarray_create const (const) -//Requires: caml_array_bound_error -function caml_floatarray_create(len) { +export function caml_floatarray_create(len) { if (len >>> 0 >= ((0x7fffffff / 8) | 0)) caml_array_bound_error(); var len = (len + 1) | 0; var b = new Array(len); @@ -238,9 +223,8 @@ function caml_floatarray_create(len) { } //Provides: caml_floatarray_make const (const) -//Requires: caml_array_bound_error //Version: >= 5.3 -function caml_floatarray_make(len, init) { +export function caml_floatarray_make(len, init) { if (len >>> 0 >= ((0x7fffffff / 8) | 0)) caml_array_bound_error(); var len = (len + 1) | 0; var b = new Array(len); @@ -250,15 +234,13 @@ function caml_floatarray_make(len, init) { } //Provides: caml_floatarray_make_unboxed const (const) -//Requires: caml_floatarray_make //Version: >= 5.3 -function caml_floatarray_make_unboxed(len, init) { +export function caml_floatarray_make_unboxed(len, init) { return caml_floatarray_make(len, init); } //Provides: caml_uniform_array_make const (const) -//Requires: caml_array_make //Version: >= 5.3 -function caml_uniform_array_make(len, init) { +export function caml_uniform_array_make(len, init) { return caml_array_make(len, init); } diff --git a/runtime/js/backtrace.js b/runtime/js/backtrace.js index 76359336f1..58a568ade0 100644 --- a/runtime/js/backtrace.js +++ b/runtime/js/backtrace.js @@ -15,9 +15,11 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +import { caml_failwith, caml_invalid_argument } from './fail.js'; +import { jsoo_sys_getenv } from './sys.js'; + //Provides: caml_record_backtrace_env_flag -//Requires: jsoo_sys_getenv -var caml_record_backtrace_env_flag = FLAG("with-js-error"); +export let caml_record_backtrace_env_flag = FLAG("with-js-error"); (function () { var r = jsoo_sys_getenv("OCAMLRUNPARAM"); @@ -35,60 +37,55 @@ var caml_record_backtrace_env_flag = FLAG("with-js-error"); })(); //Provides: caml_record_backtrace_runtime_flag -//Requires: caml_record_backtrace_env_flag -var caml_record_backtrace_runtime_flag = caml_record_backtrace_env_flag; +export let caml_record_backtrace_runtime_flag = caml_record_backtrace_env_flag; //Provides: caml_ml_debug_info_status const -function caml_ml_debug_info_status() { +export function caml_ml_debug_info_status() { return 0; } //Provides: caml_backtrace_status -//Requires: caml_record_backtrace_runtime_flag -function caml_backtrace_status(_unit) { +export function caml_backtrace_status(_unit) { return caml_record_backtrace_runtime_flag ? 1 : 0; } //Provides: caml_get_exception_backtrace const -function caml_get_exception_backtrace() { +export function caml_get_exception_backtrace() { return 0; } //Provides: caml_get_exception_raw_backtrace const -function caml_get_exception_raw_backtrace(_unit) { +export function caml_get_exception_raw_backtrace(_unit) { return [0]; } //Provides: caml_record_backtrace -//Requires: caml_record_backtrace_runtime_flag -function caml_record_backtrace(b) { +export function caml_record_backtrace(b) { caml_record_backtrace_runtime_flag = b; return 0; } //Provides: caml_convert_raw_backtrace const -function caml_convert_raw_backtrace() { +export function caml_convert_raw_backtrace() { return [0]; } //Provides: caml_raw_backtrace_length -function caml_raw_backtrace_length() { +export function caml_raw_backtrace_length() { return 0; } //Provides: caml_raw_backtrace_next_slot -function caml_raw_backtrace_next_slot(_slot) { +export function caml_raw_backtrace_next_slot(_slot) { return 0; } //Provides: caml_raw_backtrace_slot -//Requires: caml_invalid_argument -function caml_raw_backtrace_slot(_bt, _idx) { +export function caml_raw_backtrace_slot(_bt, _idx) { caml_invalid_argument("Printexc.get_raw_backtrace_slot: index out of bounds"); } //Provides: caml_restore_raw_backtrace -function caml_restore_raw_backtrace(_exn, _bt) { +export function caml_restore_raw_backtrace(_exn, _bt) { return 0; } //Provides: caml_get_current_callstack const -function caml_get_current_callstack() { +export function caml_get_current_callstack() { return [0]; } //Provides: caml_convert_raw_backtrace_slot -//Requires: caml_failwith -function caml_convert_raw_backtrace_slot(_rbt) { +export function caml_convert_raw_backtrace_slot(_rbt) { caml_failwith("caml_convert_raw_backtrace_slot"); } diff --git a/runtime/js/bigarray.js b/runtime/js/bigarray.js index 16a479a331..1a5f47bf4d 100644 --- a/runtime/js/bigarray.js +++ b/runtime/js/bigarray.js @@ -24,14 +24,19 @@ // - sub/slice/reshape // - retain fast path for 1d array access +import { caml_array_bound_error, caml_failwith, caml_invalid_argument } from './fail.js'; +import { caml_hash_mix_float, caml_hash_mix_int } from './hash.js'; +import { caml_int32_bits_of_float, caml_int32_float_of_bits, caml_int64_bits_of_float, caml_int64_float_of_bits } from './ieee_754.js'; +import { caml_int64_create_lo_hi, caml_int64_hi32, caml_int64_lo32, caml_int64_of_bytes, caml_int64_to_bytes } from './int64.js'; +import { caml_js_from_array } from './jslib.js'; + //Provides: caml_ba_init const -function caml_ba_init() { +export function caml_ba_init() { return 0; } //Provides: caml_ba_get_size -//Requires: caml_invalid_argument -function caml_ba_get_size(dims) { +export function caml_ba_get_size(dims) { var n_dims = dims.length; var size = 1; for (var i = 0; i < n_dims; i++) { @@ -43,7 +48,7 @@ function caml_ba_get_size(dims) { } //Provides: caml_unpackFloat16 -var caml_unpackFloat16 = (function () { +export let caml_unpackFloat16 = (function () { var pow = Math.pow; var EXP_MASK16 = 31; // 2 ** 5 - 1 @@ -73,7 +78,7 @@ var caml_unpackFloat16 = (function () { })(); //Provides: caml_packFloat16 -var caml_packFloat16 = (function () { +export let caml_packFloat16 = (function () { const INVERSE_OF_EPSILON = 1 / Number.EPSILON; function roundTiesToEven(num) { @@ -181,7 +186,7 @@ var caml_packFloat16 = (function () { })(); //Provides: caml_ba_get_size_per_element -function caml_ba_get_size_per_element(kind) { +export function caml_ba_get_size_per_element(kind) { switch (kind) { case 7: case 10: @@ -193,9 +198,7 @@ function caml_ba_get_size_per_element(kind) { } //Provides: caml_ba_create_buffer -//Requires: caml_ba_get_size_per_element -//Requires: caml_invalid_argument -function caml_ba_create_buffer(kind, size) { +export function caml_ba_create_buffer(kind, size) { var view; switch (kind) { case 0: @@ -247,13 +250,10 @@ function caml_ba_create_buffer(kind, size) { } //Provides: caml_ba_custom_name -var caml_ba_custom_name = "_bigarr02"; +export let caml_ba_custom_name = "_bigarr02"; //Provides: Ml_Bigarray -//Requires: caml_array_bound_error, caml_invalid_argument, caml_ba_custom_name -//Requires: caml_int64_create_lo_hi, caml_int64_hi32, caml_int64_lo32 -//Requires: caml_packFloat16, caml_unpackFloat16 -class Ml_Bigarray { +export class Ml_Bigarray { constructor(kind, layout, dims, buffer) { this.kind = kind; this.layout = layout; @@ -431,8 +431,7 @@ class Ml_Bigarray { } //Provides: Ml_Bigarray_c_1_1 -//Requires: Ml_Bigarray, caml_array_bound_error, caml_invalid_argument -class Ml_Bigarray_c_1_1 extends Ml_Bigarray { +export class Ml_Bigarray_c_1_1 extends Ml_Bigarray { offset(arg) { if (typeof arg !== "number") { if (Array.isArray(arg) && arg.length === 1) arg = arg[0]; @@ -458,14 +457,12 @@ class Ml_Bigarray_c_1_1 extends Ml_Bigarray { } //Provides: caml_ba_compare -function caml_ba_compare(a, b, total) { +export function caml_ba_compare(a, b, total) { return a.compare(b, total); } //Provides: caml_ba_create_unsafe -//Requires: Ml_Bigarray, Ml_Bigarray_c_1_1, caml_ba_get_size, caml_ba_get_size_per_element -//Requires: caml_invalid_argument -function caml_ba_create_unsafe(kind, layout, dims, data) { +export function caml_ba_create_unsafe(kind, layout, dims, data) { var size_per_element = caml_ba_get_size_per_element(kind); if (caml_ba_get_size(dims) * size_per_element !== data.length) { caml_invalid_argument("length doesn't match dims"); @@ -482,18 +479,14 @@ function caml_ba_create_unsafe(kind, layout, dims, data) { } //Provides: caml_ba_create -//Requires: caml_js_from_array -//Requires: caml_ba_get_size, caml_ba_create_unsafe -//Requires: caml_ba_create_buffer -function caml_ba_create(kind, layout, dims_ml) { +export function caml_ba_create(kind, layout, dims_ml) { var dims = caml_js_from_array(dims_ml); var data = caml_ba_create_buffer(kind, caml_ba_get_size(dims)); return caml_ba_create_unsafe(kind, layout, dims, data); } //Provides: caml_ba_change_layout -//Requires: caml_ba_create_unsafe -function caml_ba_change_layout(ba, layout) { +export function caml_ba_change_layout(ba, layout) { if (ba.layout === layout) return ba; var new_dims = []; for (var i = 0; i < ba.dims.length; i++) @@ -502,55 +495,49 @@ function caml_ba_change_layout(ba, layout) { } //Provides: caml_ba_kind -function caml_ba_kind(ba) { +export function caml_ba_kind(ba) { return ba.kind; } //Provides: caml_ba_layout -function caml_ba_layout(ba) { +export function caml_ba_layout(ba) { return ba.layout; } //Provides: caml_ba_num_dims -function caml_ba_num_dims(ba) { +export function caml_ba_num_dims(ba) { return ba.dims.length; } //Provides: caml_ba_dim -//Requires: caml_invalid_argument -function caml_ba_dim(ba, i) { +export function caml_ba_dim(ba, i) { if (i < 0 || i >= ba.dims.length) caml_invalid_argument("Bigarray.dim"); return ba.dims[i]; } //Provides: caml_ba_dim_1 -//Requires: caml_ba_dim -function caml_ba_dim_1(ba) { +export function caml_ba_dim_1(ba) { return caml_ba_dim(ba, 0); } //Provides: caml_ba_dim_2 -//Requires: caml_ba_dim -function caml_ba_dim_2(ba) { +export function caml_ba_dim_2(ba) { return caml_ba_dim(ba, 1); } //Provides: caml_ba_dim_3 -//Requires: caml_ba_dim -function caml_ba_dim_3(ba) { +export function caml_ba_dim_3(ba) { return caml_ba_dim(ba, 2); } //Provides: caml_ba_get_generic -//Requires: caml_js_from_array -function caml_ba_get_generic(ba, i) { +export function caml_ba_get_generic(ba, i) { var ofs = ba.offset(caml_js_from_array(i)); return ba.get(ofs); } //Provides: caml_ba_uint8_get16 -//Requires: caml_array_bound_error -function caml_ba_uint8_get16(ba, i0) { +export function caml_ba_uint8_get16(ba, i0) { var ofs = ba.offset(i0); if (ofs + 1 >= ba.data.length) caml_array_bound_error(); var b1 = ba.get(ofs); @@ -559,8 +546,7 @@ function caml_ba_uint8_get16(ba, i0) { } //Provides: caml_ba_uint8_get32 -//Requires: caml_array_bound_error -function caml_ba_uint8_get32(ba, i0) { +export function caml_ba_uint8_get32(ba, i0) { var ofs = ba.offset(i0); if (ofs + 3 >= ba.data.length) caml_array_bound_error(); var b1 = ba.get(ofs + 0); @@ -571,8 +557,7 @@ function caml_ba_uint8_get32(ba, i0) { } //Provides: caml_ba_uint8_get64 -//Requires: caml_array_bound_error, caml_int64_of_bytes -function caml_ba_uint8_get64(ba, i0) { +export function caml_ba_uint8_get64(ba, i0) { var ofs = ba.offset(i0); if (ofs + 7 >= ba.data.length) caml_array_bound_error(); var b1 = ba.get(ofs + 0); @@ -587,30 +572,28 @@ function caml_ba_uint8_get64(ba, i0) { } //Provides: caml_ba_get_1 -function caml_ba_get_1(ba, i0) { +export function caml_ba_get_1(ba, i0) { return ba.get(ba.offset(i0)); } //Provides: caml_ba_get_2 -function caml_ba_get_2(ba, i0, i1) { +export function caml_ba_get_2(ba, i0, i1) { return ba.get(ba.offset([i0, i1])); } //Provides: caml_ba_get_3 -function caml_ba_get_3(ba, i0, i1, i2) { +export function caml_ba_get_3(ba, i0, i1, i2) { return ba.get(ba.offset([i0, i1, i2])); } //Provides: caml_ba_set_generic -//Requires: caml_js_from_array -function caml_ba_set_generic(ba, i, v) { +export function caml_ba_set_generic(ba, i, v) { ba.set(ba.offset(caml_js_from_array(i)), v); return 0; } //Provides: caml_ba_uint8_set16 -//Requires: caml_array_bound_error -function caml_ba_uint8_set16(ba, i0, v) { +export function caml_ba_uint8_set16(ba, i0, v) { var ofs = ba.offset(i0); if (ofs + 1 >= ba.data.length) caml_array_bound_error(); ba.set(ofs + 0, v & 0xff); @@ -619,8 +602,7 @@ function caml_ba_uint8_set16(ba, i0, v) { } //Provides: caml_ba_uint8_set32 -//Requires: caml_array_bound_error -function caml_ba_uint8_set32(ba, i0, v) { +export function caml_ba_uint8_set32(ba, i0, v) { var ofs = ba.offset(i0); if (ofs + 3 >= ba.data.length) caml_array_bound_error(); ba.set(ofs + 0, v & 0xff); @@ -631,8 +613,7 @@ function caml_ba_uint8_set32(ba, i0, v) { } //Provides: caml_ba_uint8_set64 -//Requires: caml_array_bound_error, caml_int64_to_bytes -function caml_ba_uint8_set64(ba, i0, v) { +export function caml_ba_uint8_set64(ba, i0, v) { var ofs = ba.offset(i0); if (ofs + 7 >= ba.data.length) caml_array_bound_error(); var v = caml_int64_to_bytes(v); @@ -641,32 +622,31 @@ function caml_ba_uint8_set64(ba, i0, v) { } //Provides: caml_ba_set_1 -function caml_ba_set_1(ba, i0, v) { +export function caml_ba_set_1(ba, i0, v) { ba.set(ba.offset(i0), v); return 0; } //Provides: caml_ba_set_2 -function caml_ba_set_2(ba, i0, i1, v) { +export function caml_ba_set_2(ba, i0, i1, v) { ba.set(ba.offset([i0, i1]), v); return 0; } //Provides: caml_ba_set_3 -function caml_ba_set_3(ba, i0, i1, i2, v) { +export function caml_ba_set_3(ba, i0, i1, i2, v) { ba.set(ba.offset([i0, i1, i2]), v); return 0; } //Provides: caml_ba_fill -function caml_ba_fill(ba, v) { +export function caml_ba_fill(ba, v) { ba.fill(v); return 0; } //Provides: caml_ba_blit -//Requires: caml_invalid_argument -function caml_ba_blit(src, dst) { +export function caml_ba_blit(src, dst) { if (dst.dims.length !== src.dims.length) caml_invalid_argument("Bigarray.blit: dimension mismatch"); for (var i = 0; i < dst.dims.length; i++) @@ -677,9 +657,7 @@ function caml_ba_blit(src, dst) { } //Provides: caml_ba_sub -//Requires: caml_invalid_argument, caml_ba_create_unsafe, caml_ba_get_size -//Requires: caml_ba_get_size_per_element -function caml_ba_sub(ba, ofs, len) { +export function caml_ba_sub(ba, ofs, len) { var changed_dim; var mul = 1; if (ba.layout === 0) { @@ -702,9 +680,7 @@ function caml_ba_sub(ba, ofs, len) { } //Provides: caml_ba_slice -//Requires: caml_js_from_array, caml_ba_create_unsafe, caml_invalid_argument, caml_ba_get_size -//Requires: caml_ba_get_size_per_element -function caml_ba_slice(ba, vind) { +export function caml_ba_slice(ba, vind) { vind = caml_js_from_array(vind); var num_inds = vind.length; var index = []; @@ -736,8 +712,7 @@ function caml_ba_slice(ba, vind) { } //Provides: caml_ba_reshape -//Requires: caml_js_from_array, caml_invalid_argument, caml_ba_create_unsafe, caml_ba_get_size -function caml_ba_reshape(ba, vind) { +export function caml_ba_reshape(ba, vind) { vind = caml_js_from_array(vind); var new_dim = []; var num_dims = vind.length; @@ -761,10 +736,7 @@ function caml_ba_reshape(ba, vind) { } //Provides: caml_ba_serialize -//Requires: caml_int64_bits_of_float, caml_int64_to_bytes -//Requires: caml_int32_bits_of_float -//Requires: caml_packFloat16 -function caml_ba_serialize(writer, ba, sz) { +export function caml_ba_serialize(writer, ba, sz) { writer.write(32, ba.dims.length); writer.write(32, ba.kind | (ba.layout << 8)); if (ba.caml_custom === "_bigarr02") @@ -848,13 +820,7 @@ function caml_ba_serialize(writer, ba, sz) { } //Provides: caml_ba_deserialize -//Requires: caml_ba_create_unsafe, caml_failwith -//Requires: caml_ba_get_size -//Requires: caml_int64_of_bytes, caml_int64_float_of_bits -//Requires: caml_int32_float_of_bits -//Requires: caml_ba_create_buffer -//Requires: caml_unpackFloat16 -function caml_ba_deserialize(reader, sz, name) { +export function caml_ba_deserialize(reader, sz, name) { var num_dims = reader.read32s(); if (num_dims < 0 || num_dims > 16) caml_failwith("input_value: wrong number of bigarray dimensions"); @@ -966,9 +932,8 @@ function caml_ba_deserialize(reader, sz, name) { } //Provides: caml_ba_create_from -//Requires: caml_ba_create_unsafe, caml_invalid_argument, caml_ba_get_size_per_element //Deprecated: Use [caml_ba_create_unsafe] instead -function caml_ba_create_from(data1, data2, _jstyp, kind, layout, dims) { +export function caml_ba_create_from(data1, data2, _jstyp, kind, layout, dims) { if (data2 || caml_ba_get_size_per_element(kind) === 2) { caml_invalid_argument( "caml_ba_create_from: use return caml_ba_create_unsafe", @@ -978,9 +943,7 @@ function caml_ba_create_from(data1, data2, _jstyp, kind, layout, dims) { } //Provides: caml_ba_hash const -//Requires: caml_ba_get_size, caml_hash_mix_int, caml_hash_mix_float -//Requires: caml_unpackFloat16, caml_hash_mix_float16, caml_hash_mix_float32 -function caml_ba_hash(ba) { +export function caml_ba_hash(ba) { var num_elts = caml_ba_get_size(ba.dims); var h = 0; switch (ba.kind) { @@ -1068,8 +1031,7 @@ function caml_ba_hash(ba) { } //Provides: caml_hash_mix_float16 -//Requires: caml_hash_mix_int -function caml_hash_mix_float16(hash, d) { +export function caml_hash_mix_float16(hash, d) { /* Normalize NaNs */ if ((d & 0x7c00) === 0x7c00 && (d & 0x03ff) !== 0) { d = 0x7c01; @@ -1081,9 +1043,7 @@ function caml_hash_mix_float16(hash, d) { } //Provides: caml_hash_mix_float32 -//Requires: caml_int32_bits_of_float -//Requires: caml_hash_mix_int -function caml_hash_mix_float32(hash, v) { +export function caml_hash_mix_float32(hash, v) { var i = caml_int32_bits_of_float(v); /* Normalize NaNs */ if ((i & 0x7f800000) === 0x7f800000 && (i & 0x7fffff) !== 0) { @@ -1099,13 +1059,12 @@ function caml_hash_mix_float32(hash, v) { } //Provides: caml_ba_to_typed_array mutable -function caml_ba_to_typed_array(ba) { +export function caml_ba_to_typed_array(ba) { return ba.data; } //Provides: caml_ba_kind_of_typed_array mutable -//Requires: caml_invalid_argument -function caml_ba_kind_of_typed_array(ta) { +export function caml_ba_kind_of_typed_array(ta) { var kind; if (ta instanceof Float32Array) kind = 0; else if (ta instanceof Float64Array) kind = 1; @@ -1121,9 +1080,7 @@ function caml_ba_kind_of_typed_array(ta) { } //Provides: caml_ba_from_typed_array mutable -//Requires: caml_ba_kind_of_typed_array -//Requires: caml_ba_create_unsafe -function caml_ba_from_typed_array(ta) { +export function caml_ba_from_typed_array(ta) { var kind = caml_ba_kind_of_typed_array(ta); var ta = /* Needed to avoid unsigned setters overflowing diff --git a/runtime/js/bigstring.js b/runtime/js/bigstring.js index eb87cdb1c5..c90d9c8e31 100644 --- a/runtime/js/bigstring.js +++ b/runtime/js/bigstring.js @@ -1,31 +1,33 @@ ///////// BIGSTRING +import { caml_ba_create_unsafe, caml_ba_get_1 } from './bigarray.js'; +import { caml_array_bound_error, caml_invalid_argument } from './fail.js'; +import { caml_hash_mix_bytes_arr } from './hash.js'; +import { caml_blit_bytes, caml_bytes_of_uint8_array, caml_ml_bytes_length, caml_ml_string_length, caml_uint8_array_of_bytes, caml_uint8_array_of_string } from './mlBytes.js'; + //Provides: caml_hash_mix_bigstring -//Requires: caml_hash_mix_bytes_arr -function caml_hash_mix_bigstring(h, bs) { +export function caml_hash_mix_bigstring(h, bs) { return caml_hash_mix_bytes_arr(h, bs.data); } //Provides: bigstring_to_array_buffer mutable -function bigstring_to_array_buffer(bs) { +export function bigstring_to_array_buffer(bs) { return bs.data.buffer; } //Provides: bigstring_to_typed_array mutable -function bigstring_to_typed_array(bs) { +export function bigstring_to_typed_array(bs) { return bs.data; } //Provides: bigstring_of_array_buffer mutable -//Requires: caml_ba_create_unsafe -function bigstring_of_array_buffer(ab) { +export function bigstring_of_array_buffer(ab) { var ta = new Uint8Array(ab); return caml_ba_create_unsafe(12, 0, [ta.length], ta); } //Provides: bigstring_of_typed_array mutable -//Requires: caml_ba_create_unsafe -function bigstring_of_typed_array(ba) { +export function bigstring_of_typed_array(ba) { var ta = new Uint8Array( ba.buffer, ba.byteOffset, @@ -35,8 +37,7 @@ function bigstring_of_typed_array(ba) { } //Provides: caml_bigstring_memcmp -//Requires: caml_ba_get_1 -function caml_bigstring_memcmp(s1, pos1, s2, pos2, len) { +export function caml_bigstring_memcmp(s1, pos1, s2, pos2, len) { for (var i = 0; i < len; i++) { var a = caml_ba_get_1(s1, pos1 + i); var b = caml_ba_get_1(s2, pos2 + i); @@ -47,8 +48,7 @@ function caml_bigstring_memcmp(s1, pos1, s2, pos2, len) { } //Provides: caml_bigstring_blit_ba_to_ba -//Requires: caml_invalid_argument, caml_array_bound_error -function caml_bigstring_blit_ba_to_ba(ba1, pos1, ba2, pos2, len) { +export function caml_bigstring_blit_ba_to_ba(ba1, pos1, ba2, pos2, len) { if (12 !== ba1.kind) caml_invalid_argument("caml_bigstring_blit_ba_to_ba: kind mismatch"); if (12 !== ba2.kind) @@ -68,9 +68,7 @@ function caml_bigstring_blit_ba_to_ba(ba1, pos1, ba2, pos2, len) { } //Provides: caml_bigstring_blit_string_to_ba -//Requires: caml_invalid_argument, caml_array_bound_error, caml_uint8_array_of_string -//Requires: caml_ml_string_length -function caml_bigstring_blit_string_to_ba(str1, pos1, ba2, pos2, len) { +export function caml_bigstring_blit_string_to_ba(str1, pos1, ba2, pos2, len) { if (12 !== ba2.kind) caml_invalid_argument("caml_bigstring_blit_string_to_ba: kind mismatch"); if (len === 0) return 0; @@ -87,9 +85,7 @@ function caml_bigstring_blit_string_to_ba(str1, pos1, ba2, pos2, len) { } //Provides: caml_bigstring_blit_bytes_to_ba -//Requires: caml_invalid_argument, caml_array_bound_error, caml_uint8_array_of_bytes -//Requires: caml_ml_bytes_length -function caml_bigstring_blit_bytes_to_ba(str1, pos1, ba2, pos2, len) { +export function caml_bigstring_blit_bytes_to_ba(str1, pos1, ba2, pos2, len) { if (12 !== ba2.kind) caml_invalid_argument("caml_bigstring_blit_string_to_ba: kind mismatch"); if (len === 0) return 0; @@ -106,10 +102,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_uint8_array -//Requires: caml_ml_bytes_length -function caml_bigstring_blit_ba_to_bytes(ba1, pos1, bytes2, pos2, len) { +export function caml_bigstring_blit_ba_to_bytes(ba1, pos1, bytes2, pos2, len) { if (12 !== ba1.kind) caml_invalid_argument("caml_bigstring_blit_string_to_ba: kind mismatch"); if (len === 0) return 0; diff --git a/runtime/js/blake2.js b/runtime/js/blake2.js index 4afce141f9..961793e243 100644 --- a/runtime/js/blake2.js +++ b/runtime/js/blake2.js @@ -1,6 +1,8 @@ +import { caml_bytes_of_jsbytes, caml_bytes_of_string, caml_jsbytes_of_string, caml_string_of_jsbytes, caml_string_of_uint8_array, caml_uint8_array_of_bytes, caml_uint8_array_of_string } from './mlBytes.js'; + //Provides: blake2b //Version: >= 5.2 -var blake2b = (function () { +export let blake2b = (function () { // Blake2B in pure Javascript // Adapted from the reference implementation in RFC7693 // Ported to Javascript by DC - https://github.com/dcposch @@ -303,10 +305,8 @@ var blake2b = (function () { })(); //Provides: caml_blake2_create -//Requires: caml_uint8_array_of_string -//Requires: blake2b //Version: >= 5.2 -function caml_blake2_create(hashlen, key) { +export function caml_blake2_create(hashlen, key) { key = caml_uint8_array_of_string(key); if (key.length > 64) { key.subarray(0, 64); @@ -315,20 +315,16 @@ function caml_blake2_create(hashlen, key) { } //Provides: caml_blake2_final -//Requires: caml_string_of_uint8_array -//Requires: blake2b //Version: >= 5.2 -function caml_blake2_final(ctx, _hashlen) { +export function caml_blake2_final(ctx, _hashlen) { // ctx.outlen === hashlen var r = blake2b.Final(ctx); return caml_string_of_uint8_array(r); } //Provides: caml_blake2_update -//Requires: blake2b -//Requires: caml_uint8_array_of_string //Version: >= 5.2, < 5.3 -function caml_blake2_update(ctx, buf, ofs, len) { +export function caml_blake2_update(ctx, buf, ofs, len) { var input = caml_uint8_array_of_string(buf); input = input.subarray(ofs, ofs + len); blake2b.Update(ctx, input); @@ -336,10 +332,8 @@ function caml_blake2_update(ctx, buf, ofs, len) { } //Provides: caml_blake2_update -//Requires: blake2b -//Requires: caml_uint8_array_of_bytes //Version: >= 5.3 -function caml_blake2_update(ctx, buf, ofs, len) { +export function caml_blake2_update(ctx, buf, ofs, len) { var input = caml_uint8_array_of_bytes(buf); input = input.subarray(ofs, ofs + len); blake2b.Update(ctx, input); @@ -347,23 +341,16 @@ function caml_blake2_update(ctx, buf, ofs, len) { } //Provides: caml_blake2_string -//Requires: caml_blake2_create -//Requires: caml_blake2_update -//Requires: caml_blake2_final //Version: >= 5.2, < 5.3 -function caml_blake2_string(hashlen, key, buf, ofs, len) { +export function caml_blake2_string(hashlen, key, buf, ofs, len) { var ctx = caml_blake2_create(hashlen, key); caml_blake2_update(ctx, buf, ofs, len); return caml_blake2_final(ctx, hashlen); } //Provides: caml_blake2_string -//Requires: caml_blake2_create -//Requires: caml_blake2_update -//Requires: caml_blake2_final -//Requires: caml_bytes_of_string //Version: >= 5.3 -function caml_blake2_string(hashlen, key, buf_str, ofs, len) { +export function caml_blake2_string(hashlen, key, buf_str, ofs, len) { var ctx = caml_blake2_create(hashlen, key); var buf = caml_bytes_of_string(buf_str); caml_blake2_update(ctx, buf, ofs, len); @@ -371,47 +358,40 @@ function caml_blake2_string(hashlen, key, buf_str, ofs, len) { } //Provides: caml_blake2_bytes -//Requires: caml_blake2_create -//Requires: caml_blake2_update -//Requires: caml_blake2_final //Version: >= 5.3 -function caml_blake2_bytes(hashlen, key, buf, ofs, len) { +export function caml_blake2_bytes(hashlen, key, buf, ofs, len) { var ctx = caml_blake2_create(hashlen, key); caml_blake2_update(ctx, buf, ofs, len); return caml_blake2_final(ctx, hashlen); } //Provides: blake2_js_for_wasm_create -//Requires: caml_blake2_create, caml_string_of_jsbytes //If: wasm //Version: >= 5.2 -function blake2_js_for_wasm_create(hashlen, key) { +export function blake2_js_for_wasm_create(hashlen, key) { const key_jsoo_string = caml_string_of_jsbytes(key); return caml_blake2_create(hashlen, key_jsoo_string); } //Provides: blake2_js_for_wasm_final -//Requires: caml_blake2_final, caml_jsbytes_of_string //If: wasm //Version: >= 5.2 -function blake2_js_for_wasm_final(ctx, hashlen) { +export function blake2_js_for_wasm_final(ctx, hashlen) { return caml_jsbytes_of_string(caml_blake2_final(ctx, hashlen)); } //Provides: blake2_js_for_wasm_update -//Requires: caml_blake2_update, caml_string_of_jsbytes //If: wasm //Version: >= 5.2, < 5.3 -function blake2_js_for_wasm_update(ctx, buf, ofs, len) { +export function blake2_js_for_wasm_update(ctx, buf, ofs, len) { const buf_jsoo_string = caml_string_of_jsbytes(buf); return caml_blake2_update(ctx, buf_jsoo_string, ofs, len); } //Provides: blake2_js_for_wasm_update -//Requires: caml_blake2_update, caml_bytes_of_jsbytes //If: wasm //Version: >= 5.3 -function blake2_js_for_wasm_update(ctx, buf, ofs, len) { +export function blake2_js_for_wasm_update(ctx, buf, ofs, len) { const buf_jsoo_string = caml_bytes_of_jsbytes(buf); return caml_blake2_update(ctx, buf_jsoo_string, ofs, len); } diff --git a/runtime/js/compare.js b/runtime/js/compare.js index aba227499e..ec624f91f7 100644 --- a/runtime/js/compare.js +++ b/runtime/js/compare.js @@ -15,9 +15,13 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +import { caml_invalid_argument } from './fail.js'; +import { caml_custom_ops } from './marshal.js'; +import { caml_bytes_compare, caml_is_ml_bytes, caml_is_ml_string, caml_jsbytes_of_string, caml_string_compare } from './mlBytes.js'; +import { caml_is_continuation_tag } from './obj.js'; + //Provides: caml_compare_val_tag -//Requires: caml_is_ml_string, caml_is_ml_bytes -function caml_compare_val_tag(a) { +export function caml_compare_val_tag(a) { if (typeof a === "number") return 1000; // int_tag (we use it for all numbers) else if (caml_is_ml_bytes(a)) @@ -47,16 +51,14 @@ function caml_compare_val_tag(a) { } //Provides: caml_compare_val_get_custom -//Requires: caml_custom_ops -function caml_compare_val_get_custom(a) { +export function caml_compare_val_get_custom(a) { return ( caml_custom_ops[a.caml_custom] && caml_custom_ops[a.caml_custom].compare ); } //Provides: caml_compare_val_number_custom -//Requires: caml_compare_val_get_custom -function caml_compare_val_number_custom(num, custom, swap, total) { +export function caml_compare_val_number_custom(num, custom, swap, total) { var comp = caml_compare_val_get_custom(custom); if (comp) { var x = swap > 0 ? comp(custom, num, total) : comp(num, custom, total); @@ -68,12 +70,7 @@ function caml_compare_val_number_custom(num, custom, swap, total) { } //Provides: caml_compare_val (const, const, const) -//Requires: caml_int_compare, caml_string_compare, caml_bytes_compare -//Requires: caml_invalid_argument, caml_compare_val_get_custom, caml_compare_val_tag -//Requires: caml_compare_val_number_custom -//Requires: caml_jsbytes_of_string -//Requires: caml_is_continuation_tag -function caml_compare_val(a, b, total) { +export function caml_compare_val(a, b, total) { var stack = []; for (;;) { if (!(total && a === b)) { @@ -254,15 +251,14 @@ function caml_compare_val(a, b, total) { // May raise //Provides: caml_compare (const, const) -//Requires: caml_compare_val -function caml_compare(a, b) { +export function caml_compare(a, b) { return caml_compare_val(a, b, true); } //Provides: caml_int_compare const //Alias: caml_int32_compare //Alias: caml_nativeint_compare -function caml_int_compare(a, b) { +export function caml_int_compare(a, b) { if (a < b) return -1; if (a === b) return 0; return 1; @@ -270,42 +266,36 @@ function caml_int_compare(a, b) { // May raise //Provides: caml_equal (const, const) -//Requires: caml_compare_val -function caml_equal(x, y) { +export function caml_equal(x, y) { return +(caml_compare_val(x, y, false) === 0); } // May raise //Provides: caml_notequal (const, const) -//Requires: caml_compare_val -function caml_notequal(x, y) { +export function caml_notequal(x, y) { return +(caml_compare_val(x, y, false) !== 0); } // May raise //Provides: caml_greaterequal (const, const) -//Requires: caml_compare_val -function caml_greaterequal(x, y) { +export function caml_greaterequal(x, y) { return +(caml_compare_val(x, y, false) >= 0); } // May raise //Provides: caml_greaterthan (const, const) -//Requires: caml_compare_val -function caml_greaterthan(x, y) { +export function caml_greaterthan(x, y) { return +(caml_compare_val(x, y, false) > 0); } // May raise //Provides: caml_lessequal (const, const) -//Requires: caml_compare_val -function caml_lessequal(x, y) { +export function caml_lessequal(x, y) { return +(caml_compare_val(x, y, false) <= 0); } // May raise //Provides: caml_lessthan (const, const) -//Requires: caml_compare_val -function caml_lessthan(x, y) { +export function caml_lessthan(x, y) { return +(caml_compare_val(x, y, false) < 0); } diff --git a/runtime/js/domain.js b/runtime/js/domain.js index 65823adb86..2aaa91b804 100644 --- a/runtime/js/domain.js +++ b/runtime/js/domain.js @@ -1,45 +1,45 @@ +import { caml_callback } from './jslib.js'; +import { caml_ml_mutex_unlock } from './sync.js'; + //Provides: caml_domain_dls //Version: >= 5 -var caml_domain_dls = [0]; +export let caml_domain_dls = [0]; //Provides: caml_domain_dls_set -//Requires: caml_domain_dls //Version: >= 5 -function caml_domain_dls_set(a) { +export function caml_domain_dls_set(a) { caml_domain_dls = a; } //Provides: caml_domain_dls_compare_and_set -//Requires: caml_domain_dls //Version: >= 5.2 -function caml_domain_dls_compare_and_set(old, n) { +export function caml_domain_dls_compare_and_set(old, n) { if (caml_domain_dls !== old) return 0; caml_domain_dls = n; return 1; } //Provides: caml_domain_dls_get -//Requires: caml_domain_dls //Version: >= 5 -function caml_domain_dls_get(_unit) { +export function caml_domain_dls_get(_unit) { return caml_domain_dls; } //Provides: caml_atomic_load //Version: >= 5 -function caml_atomic_load(ref) { +export function caml_atomic_load(ref) { return ref[1]; } //Provides: caml_atomic_load_field //Version: >= 5.4 -function caml_atomic_load_field(b, i) { +export function caml_atomic_load_field(b, i) { return b[i + 1]; } //Provides: caml_atomic_cas //Version: >= 5 -function caml_atomic_cas(ref, o, n) { +export function caml_atomic_cas(ref, o, n) { if (ref[1] === o) { ref[1] = n; return 1; @@ -49,7 +49,7 @@ function caml_atomic_cas(ref, o, n) { //Provides: caml_atomic_cas_field //Version: >= 5.4 -function caml_atomic_cas_field(b, i, o, n) { +export function caml_atomic_cas_field(b, i, o, n) { if (b[i + 1] === o) { b[i + 1] = n; return 1; @@ -59,7 +59,7 @@ function caml_atomic_cas_field(b, i, o, n) { //Provides: caml_atomic_fetch_add //Version: >= 5 -function caml_atomic_fetch_add(ref, i) { +export function caml_atomic_fetch_add(ref, i) { var old = ref[1]; ref[1] += i; return old; @@ -67,7 +67,7 @@ function caml_atomic_fetch_add(ref, i) { //Provides: caml_atomic_fetch_add_field //Version: >= 5.4 -function caml_atomic_fetch_add_field(b, i, n) { +export function caml_atomic_fetch_add_field(b, i, n) { var old = b[i + 1]; b[i + 1] += n; return old; @@ -75,7 +75,7 @@ function caml_atomic_fetch_add_field(b, i, n) { //Provides: caml_atomic_exchange //Version: >= 5 -function caml_atomic_exchange(ref, v) { +export function caml_atomic_exchange(ref, v) { var r = ref[1]; ref[1] = v; return r; @@ -83,7 +83,7 @@ function caml_atomic_exchange(ref, v) { //Provides: caml_atomic_exchange_field //Version: >= 5.4 -function caml_atomic_exchange_field(b, i, v) { +export function caml_atomic_exchange_field(b, i, v) { var r = b[i + 1]; b[i + 1] = v; return r; @@ -91,41 +91,37 @@ function caml_atomic_exchange_field(b, i, v) { //Provides: caml_atomic_make_contended //Version: >= 5.2 -function caml_atomic_make_contended(a) { +export function caml_atomic_make_contended(a) { return [0, a]; } //Provides: caml_ml_domain_unique_token //Version: >= 5.0, < 5.2 var caml_ml_domain_unique_token_ = [0]; -function caml_ml_domain_unique_token(_unit) { +export function caml_ml_domain_unique_token(_unit) { return caml_ml_domain_unique_token_; } //Provides: caml_recommended_domain_count //Version: >= 5 -function caml_recommended_domain_count(_unit) { +export function caml_recommended_domain_count(_unit) { return 1; } //Provides: caml_ml_domain_index -//Requires: caml_domain_id //Version: >= 5.03 -function caml_ml_domain_index(_unit) { +export function caml_ml_domain_index(_unit) { return caml_domain_id; } //Provides: caml_domain_id //Version: >= 5 -var caml_domain_id = 0; +export let caml_domain_id = 0; //Provides: caml_domain_spawn -//Requires: caml_ml_mutex_unlock -//Requires: caml_domain_id -//Requires: caml_callback //Version: >= 5.2 var caml_domain_latest_idx = 1; -function caml_domain_spawn(f, term_sync) { +export function caml_domain_spawn(f, term_sync) { var id = caml_domain_latest_idx++; var old = caml_domain_id; caml_domain_id = id; @@ -138,12 +134,9 @@ function caml_domain_spawn(f, term_sync) { } //Provides: caml_domain_spawn -//Requires: caml_ml_mutex_unlock -//Requires: caml_domain_id -//Requires: caml_callback //Version: >= 5.0, < 5.2 var caml_domain_latest_idx = 1; -function caml_domain_spawn(f, mutex) { +export function caml_domain_spawn(f, mutex) { var id = caml_domain_latest_idx++; var old = caml_domain_id; caml_domain_id = id; @@ -154,14 +147,13 @@ function caml_domain_spawn(f, mutex) { } //Provides: caml_ml_domain_id -//Requires: caml_domain_id //Version: >= 5.0 -function caml_ml_domain_id(_unit) { +export function caml_ml_domain_id(_unit) { return caml_domain_id; } //Provides: caml_ml_domain_cpu_relax //Version: >= 5 -function caml_ml_domain_cpu_relax(_unit) { +export function caml_ml_domain_cpu_relax(_unit) { return 0; } diff --git a/runtime/js/dynlink.js b/runtime/js/dynlink.js index 70f9473317..a82793f619 100644 --- a/runtime/js/dynlink.js +++ b/runtime/js/dynlink.js @@ -16,6 +16,10 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +import { caml_failwith } from './fail.js'; +import { caml_jsstring_of_string } from './mlBytes.js'; +import { caml_global_data } from './stdlib.js'; + //Provides: get_current_libs var current_libs; function get_current_libs() { @@ -24,9 +28,7 @@ function get_current_libs() { } //Provides: caml_dynlink_open_lib -//Requires: get_current_libs, caml_failwith -//Requires: caml_jsstring_of_string -function caml_dynlink_open_lib(_mode, file) { +export function caml_dynlink_open_lib(_mode, file) { var name = caml_jsstring_of_string(file); console.log("Dynlink: try to open ", name); //caml_failwith("file not found: "+name) @@ -36,17 +38,14 @@ function caml_dynlink_open_lib(_mode, file) { } //Provides: caml_dynlink_close_lib -//Requires: get_current_libs -function caml_dynlink_close_lib(idx) { +export function caml_dynlink_close_lib(idx) { var current_libs = get_current_libs(); current_libs[idx] = null; return 0; } //Provides: caml_dynlink_lookup_symbol -//Requires: get_current_libs -//Requires: caml_jsstring_of_string -function caml_dynlink_lookup_symbol(idx, fun_name) { +export function caml_dynlink_lookup_symbol(idx, fun_name) { var name = caml_jsstring_of_string(fun_name); console.log("Dynlink: looking for symbol", name); var current_libs = get_current_libs(); @@ -56,15 +55,13 @@ function caml_dynlink_lookup_symbol(idx, fun_name) { } //Provides: caml_dynlink_add_primitive -//Requires: caml_global_data -function caml_dynlink_add_primitive(dll_addr) { +export function caml_dynlink_add_primitive(dll_addr) { globalThis.jsoo_runtime[dll_addr.name] = dll_addr.symbol; return caml_global_data.prim_count++; } //Provides: caml_dynlink_get_current_libs -//Requires: get_current_libs -function caml_dynlink_get_current_libs() { +export function caml_dynlink_get_current_libs() { var current_libs = get_current_libs(); var len = current_libs.length; var a = new Array(len); diff --git a/runtime/js/effect.js b/runtime/js/effect.js index b1ac8c5f3c..93cfe51301 100644 --- a/runtime/js/effect.js +++ b/runtime/js/effect.js @@ -45,6 +45,12 @@ The handlers are CPS-transformed functions: they actually take an additional parameter which is the current low-level continuation. */ +import { caml_failwith, caml_raise_constant } from './fail.js'; +import { caml_callback, caml_stack_check_depth, caml_stack_depth, caml_trampoline_return, caml_wrap_exception } from './jslib.js'; +import { caml_string_of_jsbytes } from './mlBytes.js'; +import { caml_fresh_oo_id } from './obj.js'; +import { caml_call_gen, caml_call_gen_cps, caml_named_value } from './stdlib.js'; + //Provides: caml_current_stack //If: effects // This has the shape {k, x, h, e} where @@ -52,19 +58,17 @@ additional parameter which is the current low-level continuation. // - k is the low level continuation // - x is the exception stack // - e is the fiber stack of the parent fiber. -var caml_current_stack = { k: 0, x: 0, h: 0, e: 0 }; +export let caml_current_stack = { k: 0, x: 0, h: 0, e: 0 }; //Provides: caml_push_trap -//Requires: caml_current_stack //If: effects -function caml_push_trap(handler) { +export function caml_push_trap(handler) { caml_current_stack.x = { h: handler, t: caml_current_stack.x }; } //Provides: caml_pop_trap -//Requires: caml_current_stack //If: effects -function caml_pop_trap() { +export function caml_pop_trap() { if (!caml_current_stack.x) return function (x) { throw x; @@ -75,20 +79,17 @@ function caml_pop_trap() { } //Provides: caml_raise_unhandled -//Requires: caml_make_unhandled_effect_exn //If: effects //Version: >= 5.0 -function caml_raise_unhandled(eff) { +export function caml_raise_unhandled(eff) { var exn = caml_make_unhandled_effect_exn(eff); throw exn; } //Provides:caml_resume_stack -//Requires: caml_named_value, caml_raise_constant -//Requires: caml_pop_fiber, caml_current_stack //If: effects //Version: >= 5.0 -function caml_resume_stack(stack, last, k) { +export function caml_resume_stack(stack, last, k) { if (!stack) caml_raise_constant( caml_named_value("Effect.Continuation_already_resumed"), @@ -105,10 +106,9 @@ function caml_resume_stack(stack, last, k) { } //Provides: caml_pop_fiber -//Requires: caml_current_stack //If: effects //Version: >= 5.0 -function caml_pop_fiber() { +export function caml_pop_fiber() { // Move to the parent fiber, returning the parent's low-level continuation var c = caml_current_stack.e; caml_current_stack.e = 0; @@ -117,10 +117,9 @@ function caml_pop_fiber() { } //Provides: caml_make_unhandled_effect_exn -//Requires: caml_named_value, caml_string_of_jsbytes, caml_fresh_oo_id //If: effects //Version: >= 5.0 -function caml_make_unhandled_effect_exn(eff) { +export function caml_make_unhandled_effect_exn(eff) { var exn = caml_named_value("Effect.Unhandled"); if (exn) exn = [0, exn, eff]; else { @@ -134,12 +133,9 @@ function caml_make_unhandled_effect_exn(eff) { } //Provides: caml_perform_effect -//Requires: caml_pop_fiber, caml_stack_check_depth, caml_trampoline_return -//Requires: caml_make_unhandled_effect_exn, caml_current_stack -//Requires: caml_get_cps_fun //If: effects //Version: >= 5.0 -function caml_perform_effect(eff, k0) { +export function caml_perform_effect(eff, k0) { if (caml_current_stack.e === 0) { var exn = caml_make_unhandled_effect_exn(eff); throw exn; @@ -158,13 +154,9 @@ function caml_perform_effect(eff, k0) { } //Provides: caml_reperform_effect -//Requires: caml_pop_fiber, caml_stack_check_depth, caml_trampoline_return -//Requires: caml_make_unhandled_effect_exn, caml_current_stack -//Requires: caml_resume_stack, caml_continuation_use_noexc -//Requires: caml_get_cps_fun //If: effects //Version: >= 5.0 -function caml_reperform_effect(eff, cont, last, k0) { +export function caml_reperform_effect(eff, cont, last, k0) { if (caml_current_stack.e === 0) { var exn = caml_make_unhandled_effect_exn(eff); var stack = caml_continuation_use_noexc(cont); @@ -188,14 +180,14 @@ function caml_reperform_effect(eff, cont, last, k0) { //Provides: caml_get_cps_fun //If: effects //If: !doubletranslate -function caml_get_cps_fun(f) { +export function caml_get_cps_fun(f) { return f; } //Provides: caml_get_cps_fun //If: effects //If: doubletranslate -function caml_get_cps_fun(f) { +export function caml_get_cps_fun(f) { // This function is only used to get the effect handler. If the // effect handler has no CPS function, we know that we can directly // call the direct version instead. @@ -203,8 +195,6 @@ function caml_get_cps_fun(f) { } //Provides: caml_alloc_stack -//Requires: caml_pop_fiber, caml_call_gen, caml_stack_check_depth, caml_trampoline_return -//Requires: caml_call_gen_cps, caml_current_stack //If: effects //Version: >= 5.0 function caml_alloc_stack_call(f, x) { @@ -223,7 +213,7 @@ function caml_alloc_stack_hexn(e) { var f = caml_current_stack.h[2]; return caml_alloc_stack_call(f, e); } -function caml_alloc_stack(hv, hx, hf) { +export function caml_alloc_stack(hv, hx, hf) { var handlers = [0, hv, hx, hf]; return { k: caml_alloc_stack_hval, @@ -236,22 +226,21 @@ function caml_alloc_stack(hv, hx, hf) { //Provides: caml_alloc_stack //If: !effects //Version: >= 5.0 -function caml_alloc_stack(_hv, _hx, _hf) { +export function caml_alloc_stack(_hv, _hx, _hf) { return 0; } //Provides: caml_continuation_use_noexc //Version: >= 5.0 -function caml_continuation_use_noexc(cont) { +export function caml_continuation_use_noexc(cont) { var stack = cont[1]; cont[1] = 0; return stack; } //Provides: caml_continuation_use_and_update_handler_noexc -//Requires: caml_continuation_use_noexc //Version: >= 5.0 -function caml_continuation_use_and_update_handler_noexc( +export function caml_continuation_use_and_update_handler_noexc( cont, hval, hexn, @@ -268,36 +257,35 @@ function caml_continuation_use_and_update_handler_noexc( //Provides: caml_get_continuation_callstack //Version: >= 5.0 -function caml_get_continuation_callstack() { +export function caml_get_continuation_callstack() { return [0]; } //Provides: caml_ml_condition_new //Version: >= 5.0 -function caml_ml_condition_new(_unit) { +export function caml_ml_condition_new(_unit) { return { condition: 1 }; } //Provides: caml_ml_condition_wait //Version: >= 5.0 -function caml_ml_condition_wait(_t, _mutext) { +export function caml_ml_condition_wait(_t, _mutext) { return 0; } //Provides: caml_ml_condition_broadcast //Version: >= 5.0 -function caml_ml_condition_broadcast(_t) { +export function caml_ml_condition_broadcast(_t) { return 0; } //Provides: caml_ml_condition_signal //Version: >= 5.0 -function caml_ml_condition_signal(_t) { +export function caml_ml_condition_signal(_t) { return 0; } //Provides: jsoo_effect_not_supported -//Requires: caml_failwith //!If: effects //Version: >= 5.0 function jsoo_effect_not_supported() { @@ -305,11 +293,10 @@ function jsoo_effect_not_supported() { } //Provides: caml_resume -//Requires:caml_stack_depth, caml_call_gen_cps, caml_current_stack, caml_wrap_exception, caml_resume_stack //If: effects //If: doubletranslate //Version: >= 5.0 -function caml_resume(f, arg, stack, last) { +export function caml_resume(f, arg, stack, last) { var saved_stack_depth = caml_stack_depth; var saved_current_stack = caml_current_stack; try { @@ -349,15 +336,14 @@ function caml_resume(f, arg, stack, last) { //Provides: caml_cps_closure //If: effects //If: doubletranslate -function caml_cps_closure(direct_f, cps_f) { +export function caml_cps_closure(direct_f, cps_f) { direct_f.cps = cps_f; return direct_f; } //Provides: caml_assume_no_perform -//Requires: caml_callback //If: effects //If: !doubletranslate -function caml_assume_no_perform(f) { +export function caml_assume_no_perform(f) { return caml_callback(f, [0]); } diff --git a/runtime/js/fail.js b/runtime/js/fail.js index 1d716f180c..b04af5556c 100644 --- a/runtime/js/fail.js +++ b/runtime/js/fail.js @@ -17,63 +17,58 @@ //Raise exception +import { caml_maybe_attach_backtrace } from './jslib.js'; +import { caml_string_of_jsbytes } from './mlBytes.js'; +import { caml_global_data } from './stdlib.js'; + //Provides: caml_raise_constant (const) -function caml_raise_constant(tag) { +export function caml_raise_constant(tag) { throw tag; } //Provides: caml_raise_with_arg (const, mutable) -//Requires: caml_maybe_attach_backtrace -function caml_raise_with_arg(tag, arg) { +export function caml_raise_with_arg(tag, arg) { throw caml_maybe_attach_backtrace([0, tag, arg]); } //Provides: caml_raise_with_args (const, mutable) -//Requires: caml_maybe_attach_backtrace -function caml_raise_with_args(tag, args) { +export function caml_raise_with_args(tag, args) { throw caml_maybe_attach_backtrace([0, tag].concat(args)); } //Provides: caml_raise_with_string (const, const) -//Requires: caml_raise_with_arg, caml_string_of_jsbytes -function caml_raise_with_string(tag, msg) { +export function caml_raise_with_string(tag, msg) { caml_raise_with_arg(tag, caml_string_of_jsbytes(msg)); } //Provides: caml_failwith (const) -//Requires: caml_raise_with_string, caml_global_data, caml_string_of_jsbytes -function caml_failwith(msg) { +export function caml_failwith(msg) { if (!caml_global_data.Failure) caml_global_data.Failure = [248, caml_string_of_jsbytes("Failure"), -3]; caml_raise_with_string(caml_global_data.Failure, msg); } //Provides: caml_invalid_argument (const) -//Requires: caml_raise_with_string, caml_global_data -function caml_invalid_argument(msg) { +export function caml_invalid_argument(msg) { caml_raise_with_string(caml_global_data.Invalid_argument, msg); } //Provides: caml_raise_end_of_file -//Requires: caml_raise_constant, caml_global_data -function caml_raise_end_of_file() { +export function caml_raise_end_of_file() { caml_raise_constant(caml_global_data.End_of_file); } //Provides: caml_raise_zero_divide -//Requires: caml_raise_constant, caml_global_data -function caml_raise_zero_divide() { +export function caml_raise_zero_divide() { caml_raise_constant(caml_global_data.Division_by_zero); } //Provides: caml_raise_not_found -//Requires: caml_raise_constant, caml_global_data -function caml_raise_not_found() { +export function caml_raise_not_found() { caml_raise_constant(caml_global_data.Not_found); } //Provides: caml_array_bound_error -//Requires: caml_invalid_argument -function caml_array_bound_error() { +export function caml_array_bound_error() { caml_invalid_argument("index out of bounds"); } diff --git a/runtime/js/format.js b/runtime/js/format.js index 40c215310e..9373330752 100644 --- a/runtime/js/format.js +++ b/runtime/js/format.js @@ -17,9 +17,11 @@ ///////////// Format +import { caml_invalid_argument } from './fail.js'; +import { caml_jsbytes_of_string, caml_string_of_jsbytes } from './mlBytes.js'; + //Provides: caml_parse_format -//Requires: caml_jsbytes_of_string, caml_invalid_argument -function caml_parse_format(fmt) { +export function caml_parse_format(fmt) { fmt = caml_jsbytes_of_string(fmt); var len = fmt.length; if (len > 31) caml_invalid_argument("format_int: format too long"); @@ -114,8 +116,7 @@ function caml_parse_format(fmt) { } //Provides: caml_finish_formatting -//Requires: caml_string_of_jsbytes -function caml_finish_formatting(f, rawbuffer) { +export function caml_finish_formatting(f, rawbuffer) { if (f.uppercase) rawbuffer = rawbuffer.toUpperCase(); var len = rawbuffer.length; /* Adjust len to reflect additional chars (sign, etc) */ diff --git a/runtime/js/fs.js b/runtime/js/fs.js index a097d18e0b..4ae062f35d 100644 --- a/runtime/js/fs.js +++ b/runtime/js/fs.js @@ -19,38 +19,40 @@ ///////////// Dummy filesystem +import { caml_failwith } from './fail.js'; +import { MlFakeDevice } from './fs_fake.js'; +import { MlNodeDevice, fs_node_supported, jsoo_is_win32 } from './fs_node.js'; +import { caml_jsstring_of_string, caml_string_of_jsbytes, caml_string_of_jsstring, caml_string_of_uint8_array } from './mlBytes.js'; +import { caml_raise_sys_error } from './sys.js'; +import { caml_raise_system_error } from './unix.js'; + //Provides: caml_trailing_slash -function caml_trailing_slash(name) { +export function caml_trailing_slash(name) { return name.slice(-1) !== "/" ? name + "/" : name; } //Provides: caml_current_dir -//Requires: caml_trailing_slash, fs_node_supported if (fs_node_supported() && globalThis.process && globalThis.process.cwd) var caml_current_dir = globalThis.process.cwd().replace(/\\/g, "/"); else var caml_current_dir = "/static"; caml_current_dir = caml_trailing_slash(caml_current_dir); //Provides: caml_get_root -//Requires: path_is_absolute -function caml_get_root(path) { +export function caml_get_root(path) { var x = path_is_absolute(path); if (!x) return; return x[0] + "/"; } //Provides: caml_root -//Requires: caml_get_root, caml_current_dir, caml_failwith -var caml_root = +export let caml_root = caml_get_root(caml_current_dir) || caml_failwith("unable to compute caml_root"); //Provides: MlFile -function MlFile() {} +export function MlFile() {} //Provides: path_is_absolute -//Requires: fs_node_supported -//Requires: jsoo_is_win32 function make_path_is_absolute() { function posix(path) { if (path.charAt(0) === "/") return ["", path.slice(1)]; @@ -75,12 +77,10 @@ function make_path_is_absolute() { } return jsoo_is_win32 ? win32 : posix; } -var path_is_absolute = make_path_is_absolute(); +export let path_is_absolute = make_path_is_absolute(); //Provides: caml_make_path -//Requires: caml_current_dir -//Requires: caml_jsstring_of_string, path_is_absolute -function caml_make_path(name) { +export function caml_make_path(name) { name = caml_jsstring_of_string(name); if (!path_is_absolute(name)) name = caml_current_dir + name; var comp0 = path_is_absolute(name); @@ -106,8 +106,7 @@ function caml_make_path(name) { } //Provides:jsoo_mount_point -//Requires: MlFakeDevice, MlNodeDevice, caml_root, fs_node_supported -var jsoo_mount_point = []; +export let jsoo_mount_point = []; if (fs_node_supported()) { jsoo_mount_point.push({ path: caml_root, @@ -125,8 +124,7 @@ jsoo_mount_point.push({ }); //Provides:caml_list_mount_point -//Requires: jsoo_mount_point, caml_string_of_jsstring -function caml_list_mount_point() { +export function caml_list_mount_point() { var prev = 0; for (var i = 0; i < jsoo_mount_point.length; i++) { var old = prev; @@ -136,8 +134,7 @@ function caml_list_mount_point() { } //Provides: resolve_fs_device -//Requires: caml_make_path, jsoo_mount_point, caml_raise_sys_error, caml_get_root, MlNodeDevice, caml_trailing_slash, fs_node_supported -function resolve_fs_device(name) { +export function resolve_fs_device(name) { var path = caml_make_path(name); var name = path.join("/"); var name_slash = caml_trailing_slash(name); @@ -171,8 +168,7 @@ function resolve_fs_device(name) { } //Provides: caml_mount_autoload -//Requires: MlFakeDevice, caml_make_path, jsoo_mount_point, caml_trailing_slash -function caml_mount_autoload(name, f) { +export function caml_mount_autoload(name, f) { var path = caml_make_path(name); var name = caml_trailing_slash(path.join("/")); jsoo_mount_point.push({ path: name, device: new MlFakeDevice(name, f) }); @@ -180,8 +176,7 @@ function caml_mount_autoload(name, f) { } //Provides: caml_unmount -//Requires: jsoo_mount_point, caml_make_path, caml_trailing_slash -function caml_unmount(name) { +export function caml_unmount(name) { var path = caml_make_path(name); var name = caml_trailing_slash(path.join("/")); var idx = -1; @@ -192,16 +187,14 @@ function caml_unmount(name) { } //Provides: caml_sys_getcwd -//Requires: caml_current_dir, caml_string_of_jsstring //Alias: caml_unix_getcwd //Alias: unix_getcwd -function caml_sys_getcwd() { +export function caml_sys_getcwd() { return caml_string_of_jsstring(caml_current_dir); } //Provides: caml_sys_chdir -//Requires: caml_current_dir, caml_raise_no_such_file, resolve_fs_device, caml_trailing_slash, caml_jsstring_of_string, caml_raise_system_error -function caml_sys_chdir(dir, raise_unix) { +export function caml_sys_chdir(dir, raise_unix) { var root = resolve_fs_device(dir); if (root.device.is_dir(root.rest)) { if (root.rest) @@ -222,8 +215,7 @@ function caml_sys_chdir(dir, raise_unix) { } //Provides: caml_raise_no_such_file -//Requires: caml_raise_system_error -function caml_raise_no_such_file(name, raise_unix) { +export function caml_raise_no_such_file(name, raise_unix) { caml_raise_system_error( raise_unix, "ENOENT", @@ -233,16 +225,13 @@ function caml_raise_no_such_file(name, raise_unix) { } //Provides: caml_sys_file_exists -//Requires: resolve_fs_device -function caml_sys_file_exists(name) { +export function caml_sys_file_exists(name) { var root = resolve_fs_device(name); return root.device.exists(root.rest); } //Provides: caml_sys_read_directory -//Requires: caml_string_of_jsstring -//Requires: resolve_fs_device -function caml_sys_read_directory(name) { +export function caml_sys_read_directory(name) { var root = resolve_fs_device(name); var a = root.device.readdir(root.rest); var l = new Array(a.length + 1); @@ -252,23 +241,20 @@ function caml_sys_read_directory(name) { } //Provides: caml_sys_remove -//Requires: resolve_fs_device -function caml_sys_remove(name) { +export function caml_sys_remove(name) { var root = resolve_fs_device(name); return root.device.unlink(root.rest); } //Provides: caml_sys_is_directory -//Requires: resolve_fs_device -function caml_sys_is_directory(name) { +export function caml_sys_is_directory(name) { var root = resolve_fs_device(name); var a = root.device.is_dir(root.rest); return a ? 1 : 0; } //Provides: caml_sys_rename -//Requires: caml_failwith, resolve_fs_device -function caml_sys_rename(o, n) { +export function caml_sys_rename(o, n) { var o_root = resolve_fs_device(o); var n_root = resolve_fs_device(n); if (o_root.device !== n_root.device) @@ -278,37 +264,33 @@ function caml_sys_rename(o, n) { } //Provides: caml_sys_mkdir -//Requires: resolve_fs_device -function caml_sys_mkdir(name, perm) { +export function caml_sys_mkdir(name, perm) { var root = resolve_fs_device(name); root.device.mkdir(root.rest, perm); return 0; } //Provides: caml_sys_rmdir -//Requires: resolve_fs_device -function caml_sys_rmdir(name) { +export function caml_sys_rmdir(name) { var root = resolve_fs_device(name); root.device.rmdir(root.rest); return 0; } //Provides: caml_ba_map_file -//Requires: caml_failwith -function caml_ba_map_file(_vfd, _kind, _layout, _shared, _dims, _pos) { +export function caml_ba_map_file(_vfd, _kind, _layout, _shared, _dims, _pos) { // var data = caml_sys_fds[vfd]; caml_failwith("caml_ba_map_file not implemented"); } //Provides: caml_ba_map_file_bytecode -//Requires: caml_ba_map_file -function caml_ba_map_file_bytecode(argv, _argn) { +export function caml_ba_map_file_bytecode(argv, _argn) { // argn === 6 return caml_ba_map_file(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]); } //Provides: jsoo_create_file_extern -function jsoo_create_file_extern(name, content) { +export function jsoo_create_file_extern(name, content) { if (globalThis.jsoo_create_file) globalThis.jsoo_create_file(name, content); else { if (!globalThis.jsoo_fs_tmp) globalThis.jsoo_fs_tmp = []; @@ -318,8 +300,7 @@ function jsoo_create_file_extern(name, content) { } //Provides: caml_fs_init -//Requires: jsoo_create_file -function caml_fs_init() { +export function caml_fs_init() { var tmp = globalThis.jsoo_fs_tmp; if (tmp) { for (var i = 0; i < tmp.length; i++) { @@ -332,8 +313,7 @@ function caml_fs_init() { } //Provides: caml_create_file -//Requires: caml_failwith, resolve_fs_device -function caml_create_file(name, content) { +export function caml_create_file(name, content) { var root = resolve_fs_device(name); if (!root.device.register) caml_failwith("cannot register file"); root.device.register(root.rest, content); @@ -341,17 +321,14 @@ function caml_create_file(name, content) { } //Provides: jsoo_create_file -//Requires: caml_create_file, caml_string_of_jsbytes, caml_string_of_jsstring -function jsoo_create_file(name, content) { +export function jsoo_create_file(name, content) { var name = caml_string_of_jsstring(name); var content = caml_string_of_jsbytes(content); return caml_create_file(name, content); } //Provides: caml_read_file_content -//Requires: resolve_fs_device, caml_raise_no_such_file, caml_string_of_uint8_array -//Requires: caml_string_of_jsstring, caml_jsstring_of_string -function caml_read_file_content(name) { +export function caml_read_file_content(name) { var name = typeof name === "string" ? caml_string_of_jsstring(name) : name; var root = resolve_fs_device(name); if (root.device.exists(root.rest)) { diff --git a/runtime/js/fs_fake.js b/runtime/js/fs_fake.js index f1e2255260..304cde631f 100644 --- a/runtime/js/fs_fake.js +++ b/runtime/js/fs_fake.js @@ -17,14 +17,13 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +import { MlFile, caml_raise_no_such_file } from './fs.js'; +import { caml_blit_bytes, caml_bytes_of_array, caml_bytes_of_jsbytes, caml_bytes_of_string, caml_bytes_of_uint8_array, caml_create_bytes, caml_is_ml_bytes, caml_is_ml_string, caml_ml_bytes_length, caml_string_of_jsbytes, caml_string_of_jsstring, caml_uint8_array_of_bytes } from './mlBytes.js'; +import { caml_raise_sys_error } from './sys.js'; +import { caml_raise_system_error } from './unix.js'; + //Provides: MlFakeDevice -//Requires: MlFakeFile, MlFakeFd, caml_create_bytes -//Requires: caml_raise_sys_error, caml_raise_no_such_file -//Requires: caml_string_of_jsbytes, caml_string_of_jsstring -//Requires: caml_bytes_of_array, caml_bytes_of_string, caml_bytes_of_jsbytes -//Requires: caml_is_ml_bytes, caml_is_ml_string -//Requires: caml_raise_system_error -class MlFakeDevice { +export class MlFakeDevice { constructor(root, f) { this.content = {}; this.root = root; @@ -351,10 +350,7 @@ class 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_uint8_array -class MlFakeFile extends MlFile { +export class MlFakeFile extends MlFile { constructor(content) { super(); this.data = content; @@ -405,9 +401,7 @@ class MlFakeFile extends MlFile { } //Provides: MlFakeFd_out -//Requires: MlFakeFile, caml_create_bytes, caml_blit_bytes, caml_bytes_of_uint8_array -//Requires: caml_raise_system_error -class MlFakeFd_out extends MlFakeFile { +export class MlFakeFd_out extends MlFakeFile { constructor(fd, flags) { super(caml_create_bytes(0)); this.log = function (_s) { @@ -477,9 +471,7 @@ class MlFakeFd_out extends MlFakeFile { } //Provides: MlFakeFd -//Requires: MlFakeFile -//Requires: caml_raise_system_error -class MlFakeFd { +export class MlFakeFd { constructor(name, file, flags) { this.file = file; this.name = name; diff --git a/runtime/js/fs_node.js b/runtime/js/fs_node.js index 2c878a070e..92f22a4f00 100644 --- a/runtime/js/fs_node.js +++ b/runtime/js/fs_node.js @@ -17,26 +17,31 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +import { caml_raise_with_args } from './fail.js'; +import { MlFile } from './fs.js'; +import { caml_int64_of_float } from './int64.js'; +import { caml_bytes_set, caml_string_of_jsstring, caml_uint8_array_of_bytes, caml_uint8_array_of_string } from './mlBytes.js'; +import { caml_named_value } from './stdlib.js'; +import { caml_raise_sys_error } from './sys.js'; +import { caml_raise_system_error, make_unix_err_args } from './unix.js'; + //Provides: jsoo_is_win32 -var jsoo_is_win32 = +export let jsoo_is_win32 = globalThis.Deno?.build?.os === "windows" || globalThis.process?.platform === "win32"; //Provides: fs_node_supported -function fs_node_supported() { +export function fs_node_supported() { return globalThis.process?.versions?.node !== undefined; } //Provides: fs_node_supported //If: browser -function fs_node_supported() { +export function fs_node_supported() { return false; } //Provides: MlNodeDevice -//Requires: MlNodeFd, caml_raise_sys_error, caml_string_of_jsstring -//Requires: caml_raise_nodejs_error, ocaml_stats_from_node_stats -//Requires: jsoo_is_win32 -class MlNodeDevice { +export class MlNodeDevice { constructor(root) { this.fs = require("node:fs"); this.root = root; @@ -325,8 +330,7 @@ class MlNodeDevice { } //Provides: ocaml_stats_from_node_stats -//Requires: caml_int64_of_float -function ocaml_stats_from_node_stats(js_stats, large) { +export function ocaml_stats_from_node_stats(js_stats, large) { /* ===Unix.file_kind=== * type file_kind = * S_REG (** Regular file *) @@ -388,12 +392,10 @@ function ocaml_stats_from_node_stats(js_stats, large) { //Provides: MlNodeDevice //If: browser -class MlNodeDevice {} +export class MlNodeDevice {} //Provides: MlNodeFd -//Requires: MlFile, caml_uint8_array_of_string, caml_uint8_array_of_bytes, caml_bytes_set, caml_raise_sys_error -//Requires: caml_raise_nodejs_error, caml_raise_system_error, ocaml_stats_from_node_stats -class MlNodeFd extends MlFile { +export class MlNodeFd extends MlFile { constructor(fd, flags) { super(); this.fs = require("node:fs"); @@ -561,11 +563,10 @@ class MlNodeFd extends MlFile { //Provides: MlNodeFd //If: browser -class MlNodeFd {} +export class MlNodeFd {} //Provides: caml_sys_open_for_node -//Requires: MlNodeFd -function caml_sys_open_for_node(fd, flags) { +export function caml_sys_open_for_node(fd, flags) { if (flags.altname) { try { var fs = require("node:fs"); @@ -578,14 +579,12 @@ function caml_sys_open_for_node(fd, flags) { //Provides: caml_sys_open_for_node //If: browser -function caml_sys_open_for_node(_fd, _flags) { +export function caml_sys_open_for_node(_fd, _flags) { return null; } //Provides: caml_raise_nodejs_error -//Requires: caml_raise_with_args, make_unix_err_args, caml_named_value -//Requires: caml_raise_sys_error -function caml_raise_nodejs_error(err, raise_unix, cmd) { +export function caml_raise_nodejs_error(err, raise_unix, cmd) { var unix_error = caml_named_value("Unix.Unix_error"); if (raise_unix && unix_error) { var args = make_unix_err_args( diff --git a/runtime/js/gc.js b/runtime/js/gc.js index 183473f4a7..838435baf5 100644 --- a/runtime/js/gc.js +++ b/runtime/js/gc.js @@ -1,58 +1,57 @@ //Provides: caml_gc_minor -function caml_gc_minor(_unit) { +export function caml_gc_minor(_unit) { //available with [node --expose-gc] if (typeof globalThis.gc === "function") globalThis.gc(true); return 0; } //Provides: caml_gc_major -function caml_gc_major(_unit) { +export function caml_gc_major(_unit) { //available with [node --expose-gc] if (typeof globalThis.gc === "function") globalThis.gc(); return 0; } //Provides: caml_gc_full_major -function caml_gc_full_major(_unit) { +export function caml_gc_full_major(_unit) { //available with [node --expose-gc] if (typeof globalThis.gc === "function") globalThis.gc(); return 0; } //Provides: caml_gc_compaction -function caml_gc_compaction(_unit) { +export function caml_gc_compaction(_unit) { return 0; } //Provides: caml_gc_counters -function caml_gc_counters(_unit) { +export function caml_gc_counters(_unit) { return [254, 0, 0, 0]; } //Provides: caml_gc_quick_stat -function caml_gc_quick_stat(_unit) { +export function caml_gc_quick_stat(_unit) { return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; } //Provides: caml_gc_stat -//Requires: caml_gc_quick_stat -function caml_gc_stat(unit) { +export function caml_gc_stat(unit) { return caml_gc_quick_stat(unit); } //Provides: caml_gc_set -function caml_gc_set(_control) { +export function caml_gc_set(_control) { return 0; } //Provides: caml_gc_get -function caml_gc_get(_unit) { +export function caml_gc_get(_unit) { return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; } //Provides: caml_final_register const -function caml_final_register(_f, _x) { +export function caml_final_register(_f, _x) { return 0; } //Provides: caml_final_register_called_without_value var all_finalizers = new globalThis.Set(); -function caml_final_register_called_without_value(cb, a) { +export function caml_final_register_called_without_value(cb, a) { if (globalThis.FinalizationRegistry && a instanceof Object) { var x = new globalThis.FinalizationRegistry(function (x) { all_finalizers.delete(x); @@ -66,67 +65,67 @@ function caml_final_register_called_without_value(cb, a) { } //Provides: caml_final_release const -function caml_final_release(_unit) { +export function caml_final_release(_unit) { return 0; } //Provides: caml_memprof_start -function caml_memprof_start(_rate, _stack_size, _tracker) { +export function caml_memprof_start(_rate, _stack_size, _tracker) { return 0; } //Provides: caml_memprof_stop -function caml_memprof_stop(_unit) { +export function caml_memprof_stop(_unit) { return 0; } //Provides: caml_memprof_discard //Version: >= 5.2 -function caml_memprof_discard(_t) { +export function caml_memprof_discard(_t) { return 0; } //Provides: caml_eventlog_resume //Version: < 5.0 -function caml_eventlog_resume(_unit) { +export function caml_eventlog_resume(_unit) { return 0; } //Provides: caml_eventlog_pause //Version: < 5.0 -function caml_eventlog_pause(_unit) { +export function caml_eventlog_pause(_unit) { return 0; } //Provides: caml_gc_huge_fallback_count //Version: < 5.0 -function caml_gc_huge_fallback_count(_unit) { +export function caml_gc_huge_fallback_count(_unit) { return 0; } //Provides: caml_gc_major_slice -function caml_gc_major_slice(_work) { +export function caml_gc_major_slice(_work) { return 0; } //Provides: caml_gc_minor_words -function caml_gc_minor_words(_unit) { +export function caml_gc_minor_words(_unit) { return 0; } //Provides: caml_get_minor_free -function caml_get_minor_free(_unit) { +export function caml_get_minor_free(_unit) { return 0; } //Provides: caml_get_major_bucket //Version: < 5.0 -function caml_get_major_bucket(_n) { +export function caml_get_major_bucket(_n) { return 0; } //Provides: caml_get_major_credit //Version: < 5.0 -function caml_get_major_credit(_n) { +export function caml_get_major_credit(_n) { return 0; } diff --git a/runtime/js/graphics.js b/runtime/js/graphics.js index d31322dab9..9037af650c 100644 --- a/runtime/js/graphics.js +++ b/runtime/js/graphics.js @@ -16,14 +16,16 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +import { caml_failwith } from './fail.js'; +import { caml_maybe_attach_backtrace } from './jslib.js'; +import { caml_jsstring_of_string, caml_string_of_jsbytes } from './mlBytes.js'; +import { caml_named_value } from './stdlib.js'; + //Provides: caml_gr_state var caml_gr_state; //Provides: caml_gr_state_get -//Requires: caml_gr_state -//Requires: caml_named_value, caml_string_of_jsbytes -//Requires: caml_maybe_attach_backtrace -function caml_gr_state_get() { +export function caml_gr_state_get() { if (caml_gr_state) { return caml_gr_state; } @@ -34,19 +36,14 @@ function caml_gr_state_get() { ]); } //Provides: caml_gr_state_set -//Requires: caml_gr_state,caml_gr_state_init -function caml_gr_state_set(ctx) { +export function caml_gr_state_set(ctx) { caml_gr_state = ctx; caml_gr_state_init(); return 0; } //Provides: caml_gr_open_graph -//Requires: caml_gr_state_create -//Requires: caml_gr_state_set -//Requires: caml_failwith -//Requires: caml_jsstring_of_string -function caml_gr_open_graph(info) { +export function caml_gr_open_graph(info) { var info = caml_jsstring_of_string(info); function get(name) { var res = info.match("(^|,) *" + name + " *= *([a-zA-Z0-9_]+) *(,|$)"); @@ -87,11 +84,7 @@ function caml_gr_open_graph(info) { } //Provides: caml_gr_state_init -//Requires: caml_gr_state -//Requires: caml_gr_set_color,caml_gr_moveto,caml_gr_resize_window -//Requires: caml_gr_set_line_width,caml_gr_set_text_size,caml_gr_set_font -//Requires: caml_gr_set_window_title -function caml_gr_state_init() { +export function caml_gr_state_init() { caml_gr_moveto(caml_gr_state.x, caml_gr_state.y); caml_gr_resize_window(caml_gr_state.width, caml_gr_state.height); caml_gr_set_line_width(caml_gr_state.line_width); @@ -104,8 +97,7 @@ function caml_gr_state_init() { } //Provides: caml_gr_state_create -//Requires: caml_string_of_jsbytes -function caml_gr_state_create(canvas, w, h) { +export function caml_gr_state_create(canvas, w, h) { var context = canvas.getContext("2d"); return { context: context, @@ -123,13 +115,12 @@ function caml_gr_state_create(canvas, w, h) { } //Provides: caml_gr_doc_of_state -function caml_gr_doc_of_state(state) { +export function caml_gr_doc_of_state(state) { if (state.canvas.ownerDocument) return state.canvas.ownerDocument; } //Provides: caml_gr_close_graph -//Requires: caml_gr_state_get -function caml_gr_close_graph() { +export function caml_gr_close_graph() { var s = caml_gr_state_get(); s.canvas.width = 0; s.canvas.height = 0; @@ -137,9 +128,7 @@ function caml_gr_close_graph() { } //Provides: caml_gr_set_window_title -//Requires: caml_gr_state_get -//Requires: caml_jsstring_of_string -function caml_gr_set_window_title(name) { +export function caml_gr_set_window_title(name) { var s = caml_gr_state_get(); s.title = name; var jsname = caml_jsstring_of_string(name); @@ -148,8 +137,7 @@ function caml_gr_set_window_title(name) { } //Provides: caml_gr_resize_window -//Requires: caml_gr_state_get -function caml_gr_resize_window(w, h) { +export function caml_gr_resize_window(w, h) { var s = caml_gr_state_get(); s.width = w; s.height = h; @@ -159,8 +147,7 @@ function caml_gr_resize_window(w, h) { } //Provides: caml_gr_clear_graph -//Requires: caml_gr_state_get -function caml_gr_clear_graph() { +export function caml_gr_clear_graph() { var s = caml_gr_state_get(); s.canvas.width = s.width; s.canvas.height = s.height; @@ -169,21 +156,18 @@ function caml_gr_clear_graph() { } //Provides: caml_gr_size_x -//Requires: caml_gr_state_get -function caml_gr_size_x() { +export function caml_gr_size_x() { var s = caml_gr_state_get(); return s.width; } //Provides: caml_gr_size_y -//Requires: caml_gr_state_get -function caml_gr_size_y() { +export function caml_gr_size_y() { var s = caml_gr_state_get(); return s.height; } //Provides: caml_gr_set_color -//Requires: caml_gr_state_get -function caml_gr_set_color(color) { +export function caml_gr_set_color(color) { var s = caml_gr_state_get(); function convert(number) { var str = "" + number.toString(16); @@ -200,8 +184,7 @@ function caml_gr_set_color(color) { return 0; } //Provides: caml_gr_plot -//Requires: caml_gr_state_get -function caml_gr_plot(x, y) { +export function caml_gr_plot(x, y) { var s = caml_gr_state_get(); var im = s.context.createImageData(1, 1); var d = im.data; @@ -219,16 +202,14 @@ function caml_gr_plot(x, y) { } //Provides: caml_gr_point_color -//Requires: caml_gr_state_get -function caml_gr_point_color(x, y) { +export function caml_gr_point_color(x, y) { var s = caml_gr_state_get(); var im = s.context.getImageData(x, s.height - y, 1, 1); var d = im.data; return (d[0] << 16) + (d[1] << 8) + d[2]; } //Provides: caml_gr_moveto -//Requires: caml_gr_state_get -function caml_gr_moveto(x, y) { +export function caml_gr_moveto(x, y) { var s = caml_gr_state_get(); s.x = x; s.y = y; @@ -236,20 +217,17 @@ function caml_gr_moveto(x, y) { } //Provides: caml_gr_current_x -//Requires: caml_gr_state_get -function caml_gr_current_x() { +export function caml_gr_current_x() { var s = caml_gr_state_get(); return s.x; } //Provides: caml_gr_current_y -//Requires: caml_gr_state_get -function caml_gr_current_y() { +export function caml_gr_current_y() { var s = caml_gr_state_get(); return s.y; } //Provides: caml_gr_lineto -//Requires: caml_gr_state_get -function caml_gr_lineto(x, y) { +export function caml_gr_lineto(x, y) { var s = caml_gr_state_get(); s.context.beginPath(); s.context.moveTo(s.x, s.height - s.y); @@ -260,15 +238,14 @@ function caml_gr_lineto(x, y) { return 0; } //Provides: caml_gr_draw_rect -//Requires: caml_gr_state_get -function caml_gr_draw_rect(x, y, w, h) { +export function caml_gr_draw_rect(x, y, w, h) { var s = caml_gr_state_get(); s.context.strokeRect(x, s.height - y, w, -h); return 0; } //Provides: caml_gr_arc_aux -function caml_gr_arc_aux(ctx, cx, cy, ry, rx, a1, a2) { +export function caml_gr_arc_aux(ctx, cx, cy, ry, rx, a1, a2) { while (a1 > a2) a2 += 360; a1 /= 180; a2 /= 180; @@ -305,8 +282,7 @@ function caml_gr_arc_aux(ctx, cx, cy, ry, rx, a1, a2) { } //Provides: caml_gr_draw_arc -//Requires: caml_gr_state_get, caml_gr_arc_aux -function caml_gr_draw_arc(x, y, rx, ry, a1, a2) { +export function caml_gr_draw_arc(x, y, rx, ry, a1, a2) { var s = caml_gr_state_get(); s.context.beginPath(); caml_gr_arc_aux(s.context, x, s.height - y, rx, ry, a1, a2); @@ -315,8 +291,7 @@ function caml_gr_draw_arc(x, y, rx, ry, a1, a2) { } //Provides: caml_gr_set_line_width -//Requires: caml_gr_state_get -function caml_gr_set_line_width(w) { +export function caml_gr_set_line_width(w) { var s = caml_gr_state_get(); s.line_width = w; s.context.lineWidth = w; @@ -324,15 +299,13 @@ function caml_gr_set_line_width(w) { } //Provides: caml_gr_fill_rect -//Requires: caml_gr_state_get -function caml_gr_fill_rect(x, y, w, h) { +export function caml_gr_fill_rect(x, y, w, h) { var s = caml_gr_state_get(); s.context.fillRect(x, s.height - y, w, -h); return 0; } //Provides: caml_gr_fill_poly -//Requires: caml_gr_state_get -function caml_gr_fill_poly(ar) { +export function caml_gr_fill_poly(ar) { var s = caml_gr_state_get(); s.context.beginPath(); s.context.moveTo(ar[1][1], s.height - ar[1][2]); @@ -344,8 +317,7 @@ function caml_gr_fill_poly(ar) { } //Provides: caml_gr_fill_arc -//Requires: caml_gr_state_get, caml_gr_arc_aux -function caml_gr_fill_arc(x, y, rx, ry, a1, a2) { +export function caml_gr_fill_arc(x, y, rx, ry, a1, a2) { var s = caml_gr_state_get(); s.context.beginPath(); caml_gr_arc_aux(s.context, x, s.height - y, rx, ry, a1, a2); @@ -354,8 +326,7 @@ function caml_gr_fill_arc(x, y, rx, ry, a1, a2) { } //Provides: caml_gr_draw_str -//Requires: caml_gr_state_get -function caml_gr_draw_str(str) { +export function caml_gr_draw_str(str) { var s = caml_gr_state_get(); var m = s.context.measureText(str); var dx = m.width; @@ -365,24 +336,19 @@ function caml_gr_draw_str(str) { } //Provides: caml_gr_draw_char -//Requires: caml_gr_draw_str -function caml_gr_draw_char(c) { +export function caml_gr_draw_char(c) { caml_gr_draw_str(String.fromCharCode(c)); return 0; } //Provides: caml_gr_draw_string -//Requires: caml_gr_draw_str -//Requires: caml_jsstring_of_string -function caml_gr_draw_string(str) { +export function caml_gr_draw_string(str) { caml_gr_draw_str(caml_jsstring_of_string(str)); return 0; } //Provides: caml_gr_set_font -//Requires: caml_gr_state_get -//Requires: caml_jsstring_of_string -function caml_gr_set_font(f) { +export function caml_gr_set_font(f) { var s = caml_gr_state_get(); s.font = f; s.context.font = s.text_size + "px " + caml_jsstring_of_string(s.font); @@ -390,9 +356,7 @@ function caml_gr_set_font(f) { } //Provides: caml_gr_set_text_size -//Requires: caml_gr_state_get -//Requires: caml_jsstring_of_string -function caml_gr_set_text_size(size) { +export function caml_gr_set_text_size(size) { var s = caml_gr_state_get(); s.text_size = size; s.context.font = s.text_size + "px " + caml_jsstring_of_string(s.font); @@ -400,17 +364,14 @@ function caml_gr_set_text_size(size) { } //Provides: caml_gr_text_size -//Requires: caml_gr_state_get -//Requires: caml_jsstring_of_string -function caml_gr_text_size(txt) { +export function caml_gr_text_size(txt) { var s = caml_gr_state_get(); var w = s.context.measureText(caml_jsstring_of_string(txt)).width; return [0, w, s.text_size]; } //Provides: caml_gr_make_image -//Requires: caml_gr_state_get -function caml_gr_make_image(arr) { +export function caml_gr_make_image(arr) { var s = caml_gr_state_get(); var h = arr.length - 1; var w = arr[1].length - 1; @@ -435,8 +396,7 @@ function caml_gr_make_image(arr) { return im; } //Provides: caml_gr_dump_image -//Requires: caml_gr_state_get -function caml_gr_dump_image(im) { +export function caml_gr_dump_image(im) { var data = [0]; for (var i = 0; i < im.height; i++) { data[i + 1] = [0]; @@ -451,8 +411,7 @@ function caml_gr_dump_image(im) { return data; } //Provides: caml_gr_draw_image -//Requires: caml_gr_state_get -function caml_gr_draw_image(im, x, y) { +export function caml_gr_draw_image(im, x, y) { var s = caml_gr_state_get(); if (!im.image) { var canvas = document.createElement("canvas"); @@ -471,14 +430,12 @@ function caml_gr_draw_image(im, x, y) { return 0; } //Provides: caml_gr_create_image -//Requires: caml_gr_state_get -function caml_gr_create_image(x, y) { +export function caml_gr_create_image(x, y) { var s = caml_gr_state_get(); return s.context.createImageData(x, y); } //Provides: caml_gr_blit_image -//Requires: caml_gr_state_get -function caml_gr_blit_image(im, x, y) { +export function caml_gr_blit_image(im, x, y) { var s = caml_gr_state_get(); var im2 = s.context.getImageData( x, @@ -495,49 +452,42 @@ function caml_gr_blit_image(im, x, y) { return 0; } //Provides: caml_gr_sigio_handler -function caml_gr_sigio_handler() { +export function caml_gr_sigio_handler() { return 0; } //Provides: caml_gr_sigio_signal -function caml_gr_sigio_signal() { +export function caml_gr_sigio_signal() { return 0; } //Provides: caml_gr_wait_event -//Requires: caml_failwith -function caml_gr_wait_event(_evl) { +export function caml_gr_wait_event(_evl) { caml_failwith("caml_gr_wait_event not Implemented: use Graphics_js instead"); } //Provides: caml_gr_synchronize -//Requires: caml_failwith -function caml_gr_synchronize() { +export function caml_gr_synchronize() { caml_failwith("caml_gr_synchronize not Implemented"); } //Provides: caml_gr_remember_mode -//Requires: caml_failwith -function caml_gr_remember_mode() { +export function caml_gr_remember_mode() { caml_failwith("caml_gr_remember_mode not Implemented"); } //Provides: caml_gr_display_mode -//Requires: caml_failwith -function caml_gr_display_mode() { +export function caml_gr_display_mode() { caml_failwith("caml_gr_display_mode not Implemented"); } //Provides: caml_gr_window_id -//Requires: caml_failwith -function caml_gr_window_id(_a) { +export function caml_gr_window_id(_a) { caml_failwith("caml_gr_window_id not Implemented"); } //Provides: caml_gr_open_subwindow -//Requires: caml_failwith -function caml_gr_open_subwindow(_a, _b, _c, _d) { +export function caml_gr_open_subwindow(_a, _b, _c, _d) { caml_failwith("caml_gr_open_subwindow not Implemented"); } //Provides: caml_gr_close_subwindow -//Requires: caml_failwith -function caml_gr_close_subwindow(_a) { +export function caml_gr_close_subwindow(_a) { caml_failwith("caml_gr_close_subwindow not Implemented"); } diff --git a/runtime/js/hash.js b/runtime/js/hash.js index ae19898dae..0b26c59d35 100644 --- a/runtime/js/hash.js +++ b/runtime/js/hash.js @@ -18,9 +18,15 @@ ///////////// Hashtbl //function ROTL32(x,n) { return ((x << n) | (x >>> (32-n))); } +import { caml_int64_bits_of_float } from './ieee_754.js'; +import { caml_int64_hi32, caml_int64_lo32 } from './int64.js'; +import { caml_mul } from './ints.js'; +import { caml_custom_ops } from './marshal.js'; +import { caml_is_ml_bytes, caml_is_ml_string, caml_jsbytes_of_string, caml_ml_bytes_content } from './mlBytes.js'; +import { caml_is_continuation_tag } from './obj.js'; + //Provides: caml_hash_mix_int -//Requires: caml_mul -function caml_hash_mix_int(h, d) { +export function caml_hash_mix_int(h, d) { d = caml_mul(d, 0xcc9e2d51 | 0); d = (d << 15) | (d >>> (32 - 15)); // ROTL32(d, 15); d = caml_mul(d, 0x1b873593); @@ -30,8 +36,7 @@ function caml_hash_mix_int(h, d) { } //Provides: caml_hash_mix_final -//Requires: caml_mul -function caml_hash_mix_final(h) { +export function caml_hash_mix_final(h) { h ^= h >>> 16; h = caml_mul(h, 0x85ebca6b | 0); h ^= h >>> 13; @@ -41,10 +46,7 @@ function caml_hash_mix_final(h) { } //Provides: caml_hash_mix_float -//Requires: caml_int64_bits_of_float -//Requires: caml_hash_mix_int -//Requires: caml_int64_lo32, caml_int64_hi32 -function caml_hash_mix_float(hash, v0) { +export function caml_hash_mix_float(hash, v0) { var i64 = caml_int64_bits_of_float(v0); var l = caml_int64_lo32(i64); var h = caml_int64_hi32(i64); @@ -63,17 +65,14 @@ function caml_hash_mix_float(hash, v0) { return hash; } //Provides: caml_hash_mix_int64 -//Requires: caml_hash_mix_int -//Requires: caml_int64_lo32, caml_int64_hi32 -function caml_hash_mix_int64(h, v) { +export function caml_hash_mix_int64(h, v) { h = caml_hash_mix_int(h, caml_int64_lo32(v)); h = caml_hash_mix_int(h, caml_int64_hi32(v)); return h; } //Provides: caml_hash_mix_jsbytes -//Requires: caml_hash_mix_int -function caml_hash_mix_jsbytes(h, s) { +export function caml_hash_mix_jsbytes(h, s) { var len = s.length, i, w; @@ -104,8 +103,7 @@ function caml_hash_mix_jsbytes(h, s) { } //Provides: caml_hash_mix_bytes_arr -//Requires: caml_hash_mix_int -function caml_hash_mix_bytes_arr(h, s) { +export function caml_hash_mix_bytes_arr(h, s) { var len = s.length, i, w; @@ -132,28 +130,19 @@ function caml_hash_mix_bytes_arr(h, s) { } //Provides: caml_hash_mix_bytes -//Requires: caml_ml_bytes_content -//Requires: caml_hash_mix_jsbytes -//Requires: caml_hash_mix_bytes_arr -function caml_hash_mix_bytes(h, v) { +export function caml_hash_mix_bytes(h, v) { var content = caml_ml_bytes_content(v); if (typeof content === "string") return caml_hash_mix_jsbytes(h, content); /* ARRAY */ else return caml_hash_mix_bytes_arr(h, content); } //Provides: caml_hash_mix_string -//Requires: caml_hash_mix_jsbytes, caml_jsbytes_of_string -function caml_hash_mix_string(h, v) { +export function caml_hash_mix_string(h, v) { return caml_hash_mix_jsbytes(h, caml_jsbytes_of_string(v)); } //Provides: caml_hash mutable -//Requires: caml_is_ml_string, caml_is_ml_bytes -//Requires: caml_hash_mix_int, caml_hash_mix_final -//Requires: caml_hash_mix_float, caml_hash_mix_string, caml_hash_mix_bytes, caml_custom_ops -//Requires: caml_hash_mix_jsbytes -//Requires: caml_is_continuation_tag -function caml_hash(count, limit, seed, obj) { +export function caml_hash(count, limit, seed, obj) { var queue, rd, wr, sz, num, h, v, i, len; sz = limit; if (sz < 0 || sz > 256) sz = 256; @@ -222,9 +211,8 @@ function caml_hash(count, limit, seed, obj) { } //Provides: caml_string_hash -//Requires: caml_hash_mix_final, caml_hash_mix_string //Version: >= 5.0 -function caml_string_hash(h, v) { +export function caml_string_hash(h, v) { var h = caml_hash_mix_string(h, v); var h = caml_hash_mix_final(h); return h & 0x3fffffff; diff --git a/runtime/js/ieee_754.js b/runtime/js/ieee_754.js index c4e2137f4e..7939af5c9f 100644 --- a/runtime/js/ieee_754.js +++ b/runtime/js/ieee_754.js @@ -17,13 +17,16 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +import { caml_failwith } from './fail.js'; +import { caml_finish_formatting, caml_parse_format } from './format.js'; +import { caml_int64_add, caml_int64_create_lo_mi_hi, caml_int64_of_int32, caml_int64_sub } from './int64.js'; +import { caml_jsbytes_of_string, caml_str_repeat, caml_string_of_jsstring } from './mlBytes.js'; + //Provides: jsoo_dataview -var jsoo_dataview = new DataView(new ArrayBuffer(8)); +export let jsoo_dataview = new DataView(new ArrayBuffer(8)); //Provides: caml_int64_bits_of_float const -//Requires: caml_int64_create_lo_mi_hi -//Requires: jsoo_dataview -function caml_int64_bits_of_float(x) { +export function caml_int64_bits_of_float(x) { jsoo_dataview.setFloat64(0, x, true); var lo32 = jsoo_dataview.getUint32(0, true); var hi32 = jsoo_dataview.getUint32(4, true); @@ -38,8 +41,7 @@ function caml_int64_bits_of_float(x) { } //Provides: caml_int32_bits_of_float const -//Requires: jsoo_dataview -function caml_int32_bits_of_float(x) { +export function caml_int32_bits_of_float(x) { jsoo_dataview.setFloat32(0, x, true); return jsoo_dataview.getUint32(0, true) | 0; } @@ -48,8 +50,7 @@ function caml_int32_bits_of_float(x) { //notation 0xp from ISO C99. //https://github.com/dankogai/js-hexfloat/blob/master/hexfloat.js //Provides: caml_hexstring_of_float const -//Requires: caml_string_of_jsstring, caml_str_repeat -function caml_hexstring_of_float(x, prec, style) { +export function caml_hexstring_of_float(x, prec, style) { if (!Number.isFinite(x)) { if (Number.isNaN(x)) return caml_string_of_jsstring("nan"); return caml_string_of_jsstring(x > 0 ? "infinity" : "-infinity"); @@ -107,8 +108,7 @@ function caml_hexstring_of_float(x, prec, style) { } //Provides: caml_int64_float_of_bits const -//Requires: jsoo_dataview -function caml_int64_float_of_bits(x) { +export function caml_int64_float_of_bits(x) { var lo = x.lo; var mi = x.mi; var hi = x.hi; @@ -122,8 +122,7 @@ function caml_int64_float_of_bits(x) { } //Provides: caml_nextafter_float const -//Requires: caml_int64_float_of_bits, caml_int64_bits_of_float, caml_int64_add, caml_int64_sub,caml_int64_of_int32 -function caml_nextafter_float(x, y) { +export function caml_nextafter_float(x, y) { if (Number.isNaN(x) || Number.isNaN(y)) return Number.NaN; if (x === y) return y; if (x === 0) { @@ -138,19 +137,18 @@ function caml_nextafter_float(x, y) { } //Provides: caml_trunc_float const -function caml_trunc_float(x) { +export function caml_trunc_float(x) { return Math.trunc(x); } //Provides: caml_int32_float_of_bits const -//Requires: jsoo_dataview -function caml_int32_float_of_bits(x) { +export function caml_int32_float_of_bits(x) { jsoo_dataview.setUint32(0, x, true); return jsoo_dataview.getFloat32(0, true); } //Provides: caml_classify_float const -function caml_classify_float(x) { +export function caml_classify_float(x) { if (Number.isFinite(x)) { if (Math.abs(x) >= 2.2250738585072014e-308) return 0; if (x !== 0) return 1; @@ -159,7 +157,7 @@ function caml_classify_float(x) { return Number.isNaN(x) ? 4 : 3; } //Provides: caml_modf_float const -function caml_modf_float(x) { +export function caml_modf_float(x) { if (Number.isFinite(x)) { var neg = 1 / x < 0; x = Math.abs(x); @@ -175,7 +173,7 @@ function caml_modf_float(x) { return [0, 1 / x, x]; } //Provides: caml_ldexp_float const -function caml_ldexp_float(x, exp) { +export function caml_ldexp_float(x, exp) { exp |= 0; if (exp > 1023) { exp -= 1023; @@ -194,7 +192,7 @@ function caml_ldexp_float(x, exp) { return x; } //Provides: caml_frexp_float const -function caml_frexp_float(x) { +export function caml_frexp_float(x) { if (x === 0 || !Number.isFinite(x)) return [0, x, 0]; var neg = x < 0; if (neg) x = -x; @@ -213,7 +211,7 @@ function caml_frexp_float(x) { } //Provides: caml_float_compare const -function caml_float_compare(x, y) { +export function caml_float_compare(x, y) { if (x === y) return 0; if (x < y) return -1; if (x > y) return 1; @@ -223,7 +221,7 @@ function caml_float_compare(x, y) { } //Provides: caml_copysign_float const -function caml_copysign_float(x, y) { +export function caml_copysign_float(x, y) { if (y === 0) y = 1 / y; x = Math.abs(x); return y < 0 ? -x : x; @@ -231,61 +229,61 @@ function caml_copysign_float(x, y) { //Provides: caml_signbit_float const //Alias: caml_signbit -function caml_signbit_float(x) { +export function caml_signbit_float(x) { if (x === 0) x = 1 / x; return x < 0 ? 1 : 0; } //Provides: caml_expm1_float const -function caml_expm1_float(x) { +export function caml_expm1_float(x) { return Math.expm1(x); } //Provides: caml_exp2_float const -function caml_exp2_float(x) { +export function caml_exp2_float(x) { return Math.pow(2, x); } //Provides: caml_log1p_float const -function caml_log1p_float(x) { +export function caml_log1p_float(x) { return Math.log1p(x); } //Provides: caml_log2_float const -function caml_log2_float(x) { +export function caml_log2_float(x) { return Math.log2(x); } //Provides: caml_hypot_float const -function caml_hypot_float(x, y) { +export function caml_hypot_float(x, y) { return Math.hypot(x, y); } //Provides: caml_log10_float const -function caml_log10_float(x) { +export function caml_log10_float(x) { return Math.log10(x); } //Provides: caml_cosh_float const -function caml_cosh_float(x) { +export function caml_cosh_float(x) { return Math.cosh(x); } //Provides: caml_acosh_float const -function caml_acosh_float(x) { +export function caml_acosh_float(x) { return Math.acosh(x); } //Provides: caml_sinh_float const -function caml_sinh_float(x) { +export function caml_sinh_float(x) { return Math.sinh(x); } //Provides: caml_asinh_float const -function caml_asinh_float(x) { +export function caml_asinh_float(x) { return Math.asinh(x); } //Provides: caml_tanh_float const -function caml_tanh_float(x) { +export function caml_tanh_float(x) { return Math.tanh(x); } //Provides: caml_atanh_float const -function caml_atanh_float(x) { +export function caml_atanh_float(x) { return Math.atanh(x); } //Provides: caml_round_float const -function caml_round_float(x) { +export function caml_round_float(x) { if (x >= 0) { var y = Math.floor(x); return x - y >= 0.5 ? y + 1 : y; @@ -295,12 +293,12 @@ function caml_round_float(x) { } } //Provides: caml_cbrt_float const -function caml_cbrt_float(x) { +export function caml_cbrt_float(x) { return Math.cbrt(x); } //Provides: caml_erf_float const -function caml_erf_float(x) { +export function caml_erf_float(x) { var a1 = 0.254829592; var a2 = -0.284496736; var a3 = 1.421413741; @@ -321,13 +319,12 @@ function caml_erf_float(x) { } //Provides: caml_erfc_float const -//Requires: caml_erf_float -function caml_erfc_float(x) { +export function caml_erfc_float(x) { return 1 - caml_erf_float(x); } //Provides: caml_fma_float const -function caml_fma_float(x, y, z) { +export function caml_fma_float(x, y, z) { var SPLIT = Math.pow(2, 27) + 1; var MIN_VALUE = Math.pow(2, -1022); var EPSILON = Math.pow(2, -52); @@ -431,8 +428,7 @@ function caml_fma_float(x, y, z) { } //Provides: caml_format_float const -//Requires: caml_str_repeat, caml_parse_format, caml_finish_formatting -function caml_format_float(fmt, x) { +export function caml_format_float(fmt, x) { function toFixed(x, dp) { if (Math.abs(x) < 1.0) { return x.toFixed(dp); @@ -509,8 +505,7 @@ function caml_format_float(fmt, x) { } //Provides: caml_float_of_string (const) -//Requires: caml_failwith, caml_jsbytes_of_string -function caml_float_of_string(s) { +export function caml_float_of_string(s) { var res; var r_float = /^ *[-+]?(?:\d*\.?\d+|\d+\.?\d*)(?:[eE][-+]?\d+)?$/; s = caml_jsbytes_of_string(s); diff --git a/runtime/js/index.js b/runtime/js/index.js new file mode 100644 index 0000000000..0a9c152ff1 --- /dev/null +++ b/runtime/js/index.js @@ -0,0 +1,41 @@ +// Auto-generated index file for js_of_ocaml runtime +export * from './array.js'; +export * from './backtrace.js'; +export * from './bigarray.js'; +export * from './bigstring.js'; +export * from './blake2.js'; +export * from './compare.js'; +export * from './domain.js'; +export * from './dynlink.js'; +export * from './effect.js'; +export * from './fail.js'; +export * from './format.js'; +export * from './fs.js'; +export * from './fs_fake.js'; +export * from './fs_node.js'; +export * from './gc.js'; +export * from './graphics.js'; +export * from './hash.js'; +export * from './ieee_754.js'; +export * from './int64.js'; +export * from './ints.js'; +export * from './io.js'; +export * from './jslib.js'; +export * from './jslib_js_of_ocaml.js'; +export * from './lexing.js'; +export * from './marshal.js'; +export * from './md5.js'; +export * from './mlBytes.js'; +export * from './nat.js'; +export * from './obj.js'; +export * from './parsing.js'; +export * from './prng.js'; +export * from './runtime_events.js'; +export * from './stdlib.js'; +export * from './str.js'; +export * from './sync.js'; +export * from './sys.js'; +export * from './toplevel.js'; +export * from './unix.js'; +export * from './weak.js'; +export * from './zstd.js'; diff --git a/runtime/js/int64.js b/runtime/js/int64.js index 710198cddc..54d7a03488 100644 --- a/runtime/js/int64.js +++ b/runtime/js/int64.js @@ -17,12 +17,16 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +import { caml_failwith, caml_raise_zero_divide } from './fail.js'; +import { caml_finish_formatting, caml_parse_format } from './format.js'; +import { caml_parse_digit, caml_parse_sign_and_base } from './ints.js'; +import { caml_ml_string_length, caml_str_repeat, caml_string_unsafe_get } from './mlBytes.js'; + //Provides: caml_int64_offset -var caml_int64_offset = Math.pow(2, -24); +export let caml_int64_offset = Math.pow(2, -24); //Provides: MlInt64 -//Requires: caml_int64_offset, caml_raise_zero_divide -class MlInt64 { +export class MlInt64 { constructor(lo, mi, hi) { this.lo = lo & 0xffffff; this.mi = mi & 0xffffff; @@ -257,109 +261,106 @@ class MlInt64 { } //Provides: caml_int64_ult const -function caml_int64_ult(x, y) { +export function caml_int64_ult(x, y) { return x.ucompare(y) < 0; } //Provides: caml_int64_compare const -function caml_int64_compare(x, y, _total) { +export function caml_int64_compare(x, y, _total) { return x.compare(y); } //Provides: caml_int64_neg const -function caml_int64_neg(x) { +export function caml_int64_neg(x) { return x.neg(); } //Provides: caml_int64_add const -function caml_int64_add(x, y) { +export function caml_int64_add(x, y) { return x.add(y); } //Provides: caml_int64_sub const -function caml_int64_sub(x, y) { +export function caml_int64_sub(x, y) { return x.sub(y); } //Provides: caml_int64_mul const -//Requires: caml_int64_offset -function caml_int64_mul(x, y) { +export function caml_int64_mul(x, y) { return x.mul(y); } //Provides: caml_int64_is_zero const -function caml_int64_is_zero(x) { +export function caml_int64_is_zero(x) { return +x.isZero(); } //Provides: caml_int64_is_negative const -function caml_int64_is_negative(x) { +export function caml_int64_is_negative(x) { return +x.isNeg(); } //Provides: caml_int64_and const -function caml_int64_and(x, y) { +export function caml_int64_and(x, y) { return x.and(y); } //Provides: caml_int64_or const -function caml_int64_or(x, y) { +export function caml_int64_or(x, y) { return x.or(y); } //Provides: caml_int64_xor const -function caml_int64_xor(x, y) { +export function caml_int64_xor(x, y) { return x.xor(y); } //Provides: caml_int64_shift_left const -function caml_int64_shift_left(x, s) { +export function caml_int64_shift_left(x, s) { return x.shift_left(s); } //Provides: caml_int64_shift_right_unsigned const -function caml_int64_shift_right_unsigned(x, s) { +export function caml_int64_shift_right_unsigned(x, s) { return x.shift_right_unsigned(s); } //Provides: caml_int64_shift_right const -function caml_int64_shift_right(x, s) { +export function caml_int64_shift_right(x, s) { return x.shift_right(s); } //Provides: caml_int64_div -function caml_int64_div(x, y) { +export function caml_int64_div(x, y) { return x.div(y); } //Provides: caml_int64_mod -function caml_int64_mod(x, y) { +export function caml_int64_mod(x, y) { return x.mod(y); } //Provides: caml_int64_of_int32 const -//Requires: MlInt64 //Alias: caml_int64_of_int //Alias: caml_int64_of_nativeint -function caml_int64_of_int32(x) { +export function caml_int64_of_int32(x) { return new MlInt64(x & 0xffffff, (x >> 24) & 0xffffff, (x >> 31) & 0xffff); } //Provides: caml_int64_to_int32 const //Alias: caml_int64_to_int //Alias: caml_int64_to_nativeint -function caml_int64_to_int32(x) { +export function caml_int64_to_int32(x) { return x.toInt(); } //Provides: caml_int64_to_float const -function caml_int64_to_float(x) { +export function caml_int64_to_float(x) { return x.toFloat(); } //Provides: caml_int64_of_float const -//Requires: caml_int64_offset, MlInt64 -function caml_int64_of_float(x) { +export function caml_int64_of_float(x) { if (x < 0) x = Math.ceil(x); return new MlInt64( x & 0xffffff, @@ -369,11 +370,7 @@ function caml_int64_of_float(x) { } //Provides: caml_int64_format const -//Requires: caml_parse_format, caml_finish_formatting -//Requires: caml_int64_is_negative, caml_int64_neg -//Requires: caml_int64_of_int32, caml_int64_to_int32 -//Requires: caml_int64_is_zero, caml_str_repeat -function caml_int64_format(fmt, x) { +export function caml_int64_format(fmt, x) { var f = caml_parse_format(fmt); if (f.signedconv && caml_int64_is_negative(x)) { f.sign = -1; @@ -396,11 +393,7 @@ function caml_int64_format(fmt, x) { } //Provides: caml_int64_of_string -//Requires: caml_parse_sign_and_base, caml_failwith, caml_parse_digit -//Requires: caml_int64_of_int32, caml_int64_ult -//Requires: caml_int64_add, caml_int64_mul, caml_int64_neg -//Requires: caml_ml_string_length,caml_string_unsafe_get, MlInt64 -function caml_int64_of_string(s) { +export function caml_int64_of_string(s) { var r = caml_parse_sign_and_base(s); var i = r[0], sign = r[1], @@ -436,13 +429,11 @@ function caml_int64_of_string(s) { } //Provides: caml_int64_create_lo_mi_hi const -//Requires: MlInt64 -function caml_int64_create_lo_mi_hi(lo, mi, hi) { +export function caml_int64_create_lo_mi_hi(lo, mi, hi) { return new MlInt64(lo, mi, hi); } //Provides: caml_int64_create_lo_hi const -//Requires: MlInt64 -function caml_int64_create_lo_hi(lo, hi) { +export function caml_int64_create_lo_hi(lo, hi) { return new MlInt64( lo & 0xffffff, ((lo >>> 24) & 0xff) | ((hi & 0xffff) << 8), @@ -450,18 +441,17 @@ function caml_int64_create_lo_hi(lo, hi) { ); } //Provides: caml_int64_lo32 const -function caml_int64_lo32(v) { +export function caml_int64_lo32(v) { return v.lo32(); } //Provides: caml_int64_hi32 const -function caml_int64_hi32(v) { +export function caml_int64_hi32(v) { return v.hi32(); } //Provides: caml_int64_of_bytes const -//Requires: MlInt64 -function caml_int64_of_bytes(a) { +export function caml_int64_of_bytes(a) { return new MlInt64( (a[7] << 0) | (a[6] << 8) | (a[5] << 16), (a[4] << 0) | (a[3] << 8) | (a[2] << 16), @@ -469,11 +459,11 @@ function caml_int64_of_bytes(a) { ); } //Provides: caml_int64_to_bytes const -function caml_int64_to_bytes(x) { +export function caml_int64_to_bytes(x) { return x.toArray(); } //Provides: caml_int64_hash const -function caml_int64_hash(v) { +export function caml_int64_hash(v) { return v.lo32() ^ v.hi32(); } diff --git a/runtime/js/ints.js b/runtime/js/ints.js index 961058f2f3..a5ec766844 100644 --- a/runtime/js/ints.js +++ b/runtime/js/ints.js @@ -15,12 +15,15 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +import { caml_failwith, caml_raise_zero_divide } from './fail.js'; +import { caml_finish_formatting, caml_parse_format } from './format.js'; +import { caml_int64_of_bytes, caml_int64_to_bytes } from './int64.js'; +import { caml_jsbytes_of_string, caml_ml_string_length, caml_str_repeat, caml_string_of_jsbytes, caml_string_unsafe_get } from './mlBytes.js'; + //Provides: caml_format_int const (const, const) -//Requires: caml_parse_format, caml_finish_formatting, caml_str_repeat -//Requires: caml_string_of_jsbytes, caml_jsbytes_of_string //Alias: caml_int32_format //Alias: caml_nativeint_format -function caml_format_int(fmt, i) { +export function caml_format_int(fmt, i) { if (caml_jsbytes_of_string(fmt) === "%d") return caml_string_of_jsbytes("" + i); var f = caml_parse_format(fmt); @@ -40,8 +43,7 @@ function caml_format_int(fmt, i) { } //Provides: caml_parse_sign_and_base -//Requires: caml_string_unsafe_get, caml_ml_string_length -function caml_parse_sign_and_base(s) { +export function caml_parse_sign_and_base(s) { var i = 0, len = caml_ml_string_length(s), base = 10, @@ -89,7 +91,7 @@ function caml_parse_sign_and_base(s) { } //Provides: caml_parse_digit -function caml_parse_digit(c) { +export function caml_parse_digit(c) { if (c >= 48 && c <= 57) return c - 48; if (c >= 65 && c <= 90) return c - 55; if (c >= 97 && c <= 122) return c - 87; @@ -97,11 +99,9 @@ function caml_parse_digit(c) { } //Provides: caml_int_of_string (const) -//Requires: caml_ml_string_length, caml_string_unsafe_get -//Requires: caml_parse_sign_and_base, caml_parse_digit, caml_failwith //Alias: caml_int32_of_string //Alias: caml_nativeint_of_string -function caml_int_of_string(s) { +export function caml_int_of_string(s) { var r = caml_parse_sign_and_base(s); var i = r[0], sign = r[1], @@ -136,38 +136,36 @@ function caml_int_of_string(s) { //Alias: caml_int32_mul //Alias: caml_nativeint_mul //Alias: %int_mul -function caml_mul(a, b) { +export function caml_mul(a, b) { return Math.imul(a, b); } //Provides: caml_div -//Requires: caml_raise_zero_divide //Alias: caml_int32_div //Alias: caml_nativeint_div //Alias: %int_div -function caml_div(x, y) { +export function caml_div(x, y) { if (y === 0) caml_raise_zero_divide(); return (x / y) | 0; } //Provides: caml_mod -//Requires: caml_raise_zero_divide //Alias: caml_int32_mod //Alias: caml_nativeint_mod //Alias: %int_mod -function caml_mod(x, y) { +export function caml_mod(x, y) { if (y === 0) caml_raise_zero_divide(); return x % y; } //Provides: caml_bswap16 const -function caml_bswap16(x) { +export function caml_bswap16(x) { return ((x & 0x00ff) << 8) | ((x & 0xff00) >> 8); } //Provides: caml_int32_bswap const //Alias: caml_nativeint_bswap -function caml_int32_bswap(x) { +export function caml_int32_bswap(x) { return ( ((x & 0x000000ff) << 24) | ((x & 0x0000ff00) << 8) | @@ -176,8 +174,7 @@ function caml_int32_bswap(x) { ); } //Provides: caml_int64_bswap const -//Requires: caml_int64_to_bytes, caml_int64_of_bytes -function caml_int64_bswap(x) { +export function caml_int64_bswap(x) { var y = caml_int64_to_bytes(x); return caml_int64_of_bytes([y[7], y[6], y[5], y[4], y[3], y[2], y[1], y[0]]); } diff --git a/runtime/js/io.js b/runtime/js/io.js index f3a22683f0..e0062a56fd 100644 --- a/runtime/js/io.js +++ b/runtime/js/io.js @@ -19,12 +19,21 @@ ///////////// Io +import { caml_ba_to_typed_array } from './bigarray.js'; +import { caml_array_bound_error, caml_failwith, caml_raise_end_of_file } from './fail.js'; +import { MlFakeFd_out } from './fs_fake.js'; +import { caml_sys_open_for_node, fs_node_supported } from './fs_node.js'; +import { resolve_fs_device } from './fs.js'; +import { caml_int64_of_float, caml_int64_to_float } from './int64.js'; +import { caml_input_value_from_bytes, caml_marshal_data_size, caml_marshal_header_size, caml_output_value_to_string } from './marshal.js'; +import { caml_bytes_of_string, caml_bytes_of_uint8_array, caml_create_bytes, caml_ml_bytes_length, caml_ml_string_length, caml_string_of_jsbytes, caml_sub_uint8_array_to_jsbytes, caml_uint8_array_of_bytes, caml_uint8_array_of_string } from './mlBytes.js'; +import { caml_io_buffer_size, caml_raise_sys_error } from './sys.js'; + //Provides: caml_sys_fds -var caml_sys_fds = new Array(3); +export let caml_sys_fds = new Array(3); //Provides: caml_sys_close -//Requires: caml_sys_fds -function caml_sys_close(fd) { +export function caml_sys_close(fd) { var x = caml_sys_fds[fd]; if (x) { x.file.close(false); @@ -34,18 +43,11 @@ function caml_sys_close(fd) { } //Provides: MlChanid -function MlChanid(id) { +export function MlChanid(id) { this.id = id; } //Provides: caml_sys_open -//Requires: caml_raise_sys_error -//Requires: MlFakeFd_out -//Requires: resolve_fs_device -//Requires: fs_node_supported -//Requires: caml_sys_fds -//Requires: caml_sys_open_for_node -//Requires: MlChanid function caml_sys_open_internal(file, idx) { var chanid; if (idx === undefined) { @@ -57,7 +59,7 @@ function caml_sys_open_internal(file, idx) { caml_sys_fds[idx] = { file: file, chanid: chanid }; return idx | 0; } -function caml_sys_open(name, flags, perms) { +export function caml_sys_open(name, flags, perms) { var f = {}; while (flags) { switch (flags[1]) { @@ -120,15 +122,13 @@ function caml_sys_open(name, flags, perms) { // ocaml Channels //Provides: caml_ml_set_channel_name -//Requires: caml_ml_channel_get -function caml_ml_set_channel_name(chanid, name) { +export function caml_ml_set_channel_name(chanid, name) { var chan = caml_ml_channel_get(chanid); chan.name = name; return 0; } //Provides: caml_ml_channels -//Requires: MlChanid class caml_ml_channels_state { constructor() { this.map = new globalThis.WeakMap(); @@ -153,17 +153,15 @@ class caml_ml_channels_state { } } -var caml_ml_channels = new caml_ml_channels_state(); +export let caml_ml_channels = new caml_ml_channels_state(); //Provides: caml_ml_channel_get -//Requires: caml_ml_channels -function caml_ml_channel_get(id) { +export function caml_ml_channel_get(id) { return caml_ml_channels.get(id); } //Provides: caml_ml_channel_redirect -//Requires: caml_ml_channel_get, caml_ml_channels -function caml_ml_channel_redirect(captured, into) { +export function caml_ml_channel_redirect(captured, into) { var to_restore = caml_ml_channel_get(captured); var new_ = caml_ml_channel_get(into); caml_ml_channels.set(captured, new_); @@ -171,16 +169,13 @@ function caml_ml_channel_redirect(captured, into) { } //Provides: caml_ml_channel_restore -//Requires: caml_ml_channels -function caml_ml_channel_restore(captured, to_restore) { +export function caml_ml_channel_restore(captured, to_restore) { caml_ml_channels.set(captured, to_restore); return 0; } //Provides: caml_ml_out_channels_list -//Requires: caml_ml_channels -//Requires: caml_ml_channel_get -function caml_ml_out_channels_list() { +export function caml_ml_out_channels_list() { var l = 0; var keys = caml_ml_channels.all(); for (var k of keys) { @@ -191,11 +186,7 @@ function caml_ml_out_channels_list() { } //Provides: caml_ml_open_descriptor_out -//Requires: caml_ml_channels, caml_sys_fds -//Requires: caml_raise_sys_error -//Requires: caml_sys_open -//Requires: caml_io_buffer_size -function caml_ml_open_descriptor_out(fd) { +export function caml_ml_open_descriptor_out(fd) { var fd_desc = caml_sys_fds[fd]; if (fd_desc === undefined) caml_raise_sys_error("fd " + fd + " doesn't exist"); @@ -217,11 +208,7 @@ function caml_ml_open_descriptor_out(fd) { } //Provides: caml_ml_open_descriptor_in -//Requires: caml_ml_channels, caml_sys_fds -//Requires: caml_raise_sys_error -//Requires: caml_sys_open -//Requires: caml_io_buffer_size -function caml_ml_open_descriptor_in(fd) { +export function caml_ml_open_descriptor_in(fd) { var fd_desc = caml_sys_fds[fd]; if (fd_desc === undefined) caml_raise_sys_error("fd " + fd + " doesn't exist"); @@ -244,30 +231,26 @@ function caml_ml_open_descriptor_in(fd) { } //Provides: caml_ml_open_descriptor_in_with_flags -//Requires: caml_ml_open_descriptor_in //Version: >= 5.1 -function caml_ml_open_descriptor_in_with_flags(fd, _flags) { +export function caml_ml_open_descriptor_in_with_flags(fd, _flags) { return caml_ml_open_descriptor_in(fd); } //Provides: caml_ml_open_descriptor_out_with_flags -//Requires: caml_ml_open_descriptor_out //Version: >= 5.1 -function caml_ml_open_descriptor_out_with_flags(fd, _flags) { +export function caml_ml_open_descriptor_out_with_flags(fd, _flags) { return caml_ml_open_descriptor_out(fd); } //Provides: caml_channel_descriptor -//Requires: caml_ml_channel_get //Alias: win_filedescr_of_channel -function caml_channel_descriptor(chanid) { +export function caml_channel_descriptor(chanid) { var chan = caml_ml_channel_get(chanid); return chan.fd; } //Provides: caml_ml_set_binary_mode -//Requires: caml_ml_channel_get -function caml_ml_set_binary_mode(chanid, mode) { +export function caml_ml_set_binary_mode(chanid, mode) { var chan = caml_ml_channel_get(chanid); chan.file.flags.text = !mode; chan.file.flags.binary = mode; @@ -275,9 +258,8 @@ function caml_ml_set_binary_mode(chanid, mode) { } //Provides: caml_ml_is_binary_mode -//Requires: caml_ml_channel_get //Version: >= 5.2 -function caml_ml_is_binary_mode(chanid) { +export function caml_ml_is_binary_mode(chanid) { var chan = caml_ml_channel_get(chanid); return chan.file.flags.binary; } @@ -285,10 +267,7 @@ function caml_ml_is_binary_mode(chanid) { //Input from in_channel //Provides: caml_ml_close_channel -//Requires: caml_ml_flush, caml_ml_channel_get -//Requires: caml_sys_close -//Requires: caml_ml_channels -function caml_ml_close_channel(chanid) { +export function caml_ml_close_channel(chanid) { var chan = caml_ml_channel_get(chanid); if (chan.opened) { chan.opened = false; @@ -303,22 +282,19 @@ function caml_ml_close_channel(chanid) { } //Provides: caml_ml_channel_size -//Requires: caml_ml_channel_get -function caml_ml_channel_size(chanid) { +export function caml_ml_channel_size(chanid) { var chan = caml_ml_channel_get(chanid); return chan.file.length() | 0; } //Provides: caml_ml_channel_size_64 -//Requires: caml_int64_of_float,caml_ml_channel_get -function caml_ml_channel_size_64(chanid) { +export function caml_ml_channel_size_64(chanid) { var chan = caml_ml_channel_get(chanid); return caml_int64_of_float(chan.file.length()); } //Provides: caml_ml_set_channel_output -//Requires: caml_ml_channel_get -function caml_ml_set_channel_output(chanid, f) { +export function caml_ml_set_channel_output(chanid, f) { var chan = caml_ml_channel_get(chanid); chan.output = function (s) { f(s); @@ -327,16 +303,13 @@ function caml_ml_set_channel_output(chanid, f) { } //Provides: caml_ml_set_channel_refill -//Requires: caml_ml_channel_get -function caml_ml_set_channel_refill(chanid, f) { +export function caml_ml_set_channel_refill(chanid, f) { caml_ml_channel_get(chanid).refill = f; return 0; } //Provides: caml_refill -//Requires: caml_ml_string_length, caml_uint8_array_of_string -//Requires: caml_raise_sys_error -function caml_refill(chan) { +export function caml_refill(chan) { if (chan.refill != null) { var str = chan.refill(); var str_a = caml_uint8_array_of_string(str); @@ -368,25 +341,20 @@ function caml_refill(chan) { } //Provides: caml_ml_input -//Requires: caml_ml_input_block -//Requires: caml_uint8_array_of_bytes -function caml_ml_input(chanid, b, i, l) { +export function caml_ml_input(chanid, b, i, l) { var ba = caml_uint8_array_of_bytes(b); return caml_ml_input_block(chanid, ba, i, l); } //Provides: caml_ml_input_bigarray -//Requires: caml_ml_input_block -//Requires: caml_ba_to_typed_array //Version: >= 5.2 -function caml_ml_input_bigarray(chanid, b, i, l) { +export function caml_ml_input_bigarray(chanid, b, i, l) { var ba = caml_ba_to_typed_array(b); return caml_ml_input_block(chanid, ba, i, l); } //Provides: caml_ml_input_block -//Requires: caml_refill, caml_ml_channel_get -function caml_ml_input_block(chanid, ba, i, l) { +export function caml_ml_input_block(chanid, ba, i, l) { var chan = caml_ml_channel_get(chanid); var n = l; var avail = chan.buffer_max - chan.buffer_curr; @@ -410,10 +378,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_uint8_array -//Requires: caml_refill, caml_failwith, caml_raise_end_of_file -//Requires: caml_marshal_header_size -function caml_input_value(chanid) { +export function caml_input_value(chanid) { var chan = caml_ml_channel_get(chanid); var header = new Uint8Array(caml_marshal_header_size); function block(buffer, offset, n) { @@ -445,16 +410,13 @@ function caml_input_value(chanid) { } //Provides: caml_input_value_to_outside_heap -//Requires: caml_input_value //Version: >= 5 -function caml_input_value_to_outside_heap(c) { +export function caml_input_value_to_outside_heap(c) { return caml_input_value(c); } //Provides: caml_ml_input_char -//Requires: caml_raise_end_of_file, caml_array_bound_error -//Requires: caml_ml_channel_get, caml_refill -function caml_ml_input_char(chanid) { +export function caml_ml_input_char(chanid) { var chan = caml_ml_channel_get(chanid); if (chan.buffer_curr >= chan.buffer_max) { chan.buffer_curr = 0; @@ -468,8 +430,7 @@ function caml_ml_input_char(chanid) { } //Provides: caml_ml_input_int -//Requires: caml_ml_input_char -function caml_ml_input_int(chanid) { +export function caml_ml_input_int(chanid) { var res = 0; for (var i = 0; i < 4; i++) { res = ((res << 8) + caml_ml_input_char(chanid)) | 0; @@ -478,8 +439,7 @@ function caml_ml_input_int(chanid) { } //Provides: caml_seek_in -//Requires: caml_raise_sys_error, caml_ml_channel_get -function caml_seek_in(chanid, pos) { +export function caml_seek_in(chanid, pos) { var chan = caml_ml_channel_get(chanid); if (chan.refill != null) caml_raise_sys_error("Illegal seek"); if ( @@ -498,41 +458,34 @@ function caml_seek_in(chanid, pos) { } //Provides: caml_ml_seek_in -//Requires: caml_seek_in -function caml_ml_seek_in(chanid, pos) { +export function caml_ml_seek_in(chanid, pos) { return caml_seek_in(chanid, pos); } //Provides: caml_ml_seek_in_64 -//Requires: caml_int64_to_float, caml_seek_in -function caml_ml_seek_in_64(chanid, pos) { +export function caml_ml_seek_in_64(chanid, pos) { var pos = caml_int64_to_float(pos); return caml_seek_in(chanid, pos); } //Provides: caml_pos_in -//Requires: caml_ml_channel_get -function caml_pos_in(chanid) { +export function caml_pos_in(chanid) { var chan = caml_ml_channel_get(chanid); return chan.offset - (chan.buffer_max - chan.buffer_curr); } //Provides: caml_ml_pos_in -//Requires: caml_pos_in -function caml_ml_pos_in(chanid) { +export function caml_ml_pos_in(chanid) { return caml_pos_in(chanid) | 0; } //Provides: caml_ml_pos_in_64 -//Requires: caml_int64_of_float, caml_pos_in -function caml_ml_pos_in_64(chanid) { +export function caml_ml_pos_in_64(chanid) { return caml_int64_of_float(caml_pos_in(chanid)); } //Provides: caml_ml_input_scan_line -//Requires: caml_array_bound_error -//Requires: caml_ml_channel_get, caml_refill -function caml_ml_input_scan_line(chanid) { +export function caml_ml_input_scan_line(chanid) { var chan = caml_ml_channel_get(chanid); var p = chan.buffer_curr; do { @@ -557,9 +510,7 @@ function caml_ml_input_scan_line(chanid) { } //Provides: caml_ml_flush -//Requires: caml_raise_sys_error, caml_ml_channel_get -//Requires: caml_sub_uint8_array_to_jsbytes -function caml_ml_flush(chanid) { +export function caml_ml_flush(chanid) { var chan = caml_ml_channel_get(chanid); if (!chan.opened) caml_raise_sys_error("Cannot flush a closed channel"); if (!chan.buffer || chan.buffer_curr === 0) return 0; @@ -580,9 +531,7 @@ function caml_ml_flush(chanid) { //output to out_channel //Provides: caml_ml_output_ta -//Requires: caml_ml_flush,caml_ml_bytes_length -//Requires: caml_raise_sys_error, caml_ml_channel_get -function caml_ml_output_ta(chanid, buffer, offset, len) { +export function caml_ml_output_ta(chanid, buffer, offset, len) { var chan = caml_ml_channel_get(chanid); if (!chan.opened) caml_raise_sys_error("Cannot output to a closed channel"); buffer = buffer.subarray(offset, offset + len); @@ -621,23 +570,20 @@ function caml_ml_output_ta(chanid, buffer, offset, len) { } //Provides: caml_ml_output_bytes -//Requires: caml_uint8_array_of_bytes, caml_ml_output_ta -function caml_ml_output_bytes(chanid, buffer, offset, len) { +export function caml_ml_output_bytes(chanid, buffer, offset, len) { var buffer = caml_uint8_array_of_bytes(buffer); return caml_ml_output_ta(chanid, buffer, offset, len); } //Provides: caml_ml_output_bigarray -//Requires: caml_ba_to_typed_array, caml_ml_output_ta //Version: >= 5.2 -function caml_ml_output_bigarray(chanid, buffer, offset, len) { +export function caml_ml_output_bigarray(chanid, buffer, offset, len) { var buffer = caml_ba_to_typed_array(buffer); return caml_ml_output_ta(chanid, buffer, offset, len); } //Provides: caml_ml_output -//Requires: caml_ml_output_bytes, caml_bytes_of_string -function caml_ml_output(chanid, buffer, offset, len) { +export function caml_ml_output(chanid, buffer, offset, len) { return caml_ml_output_bytes( chanid, caml_bytes_of_string(buffer), @@ -647,25 +593,21 @@ function caml_ml_output(chanid, buffer, offset, len) { } //Provides: caml_ml_output_char -//Requires: caml_ml_output -//Requires: caml_string_of_jsbytes -function caml_ml_output_char(chanid, c) { +export function caml_ml_output_char(chanid, c) { var s = caml_string_of_jsbytes(String.fromCharCode(c)); caml_ml_output(chanid, s, 0, 1); return 0; } //Provides: caml_output_value -//Requires: caml_output_value_to_string, caml_ml_output,caml_ml_string_length -function caml_output_value(chanid, v, flags) { +export function caml_output_value(chanid, v, flags) { var s = caml_output_value_to_string(v, flags); caml_ml_output(chanid, s, 0, caml_ml_string_length(s)); return 0; } //Provides: caml_seek_out -//Requires: caml_ml_channel_get, caml_ml_flush -function caml_seek_out(chanid, pos) { +export function caml_seek_out(chanid, pos) { caml_ml_flush(chanid); var chan = caml_ml_channel_get(chanid); chan.file.seek(pos, 0); @@ -674,53 +616,45 @@ function caml_seek_out(chanid, pos) { } //Provides: caml_ml_seek_out -//Requires: caml_seek_out -function caml_ml_seek_out(chanid, pos) { +export function caml_ml_seek_out(chanid, pos) { return caml_seek_out(chanid, pos); } //Provides: caml_ml_seek_out_64 -//Requires: caml_int64_to_float, caml_seek_out -function caml_ml_seek_out_64(chanid, pos) { +export function caml_ml_seek_out_64(chanid, pos) { var pos = caml_int64_to_float(pos); return caml_seek_out(chanid, pos); } //Provides: caml_pos_out -//Requires: caml_ml_channel_get, caml_ml_flush -function caml_pos_out(chanid) { +export function caml_pos_out(chanid) { var chan = caml_ml_channel_get(chanid); return chan.offset + chan.buffer_curr; } //Provides: caml_ml_pos_out -//Requires: caml_pos_out -function caml_ml_pos_out(chanid) { +export function caml_ml_pos_out(chanid) { return caml_pos_out(chanid) | 0; } //Provides: caml_ml_pos_out_64 -//Requires: caml_int64_of_float, caml_pos_out -function caml_ml_pos_out_64(chanid) { +export function caml_ml_pos_out_64(chanid) { return caml_int64_of_float(caml_pos_out(chanid)); } //Provides: caml_ml_output_int -//Requires: caml_ml_output_ta -function caml_ml_output_int(chanid, i) { +export function caml_ml_output_int(chanid, i) { var arr = [(i >> 24) & 0xff, (i >> 16) & 0xff, (i >> 8) & 0xff, i & 0xff]; caml_ml_output_ta(chanid, new Uint8Array(arr), 0, 4); return 0; } //Provides: caml_ml_is_buffered -//Requires: caml_ml_channel_get -function caml_ml_is_buffered(chanid) { +export function caml_ml_is_buffered(chanid) { return caml_ml_channel_get(chanid).buffered ? 1 : 0; } //Provides: caml_ml_set_buffered -//Requires: caml_ml_channel_get, caml_ml_flush -function caml_ml_set_buffered(chanid, v) { +export function caml_ml_set_buffered(chanid, v) { caml_ml_channel_get(chanid).buffered = v; if (!v) caml_ml_flush(chanid); return 0; diff --git a/runtime/js/jslib.js b/runtime/js/jslib.js index 0513340f41..73bc16f3d9 100644 --- a/runtime/js/jslib.js +++ b/runtime/js/jslib.js @@ -19,39 +19,43 @@ ///////////// Jslib +import { caml_record_backtrace_env_flag, caml_record_backtrace_runtime_flag } from './backtrace.js'; +import { caml_current_stack } from './effect.js'; +import { caml_jsstring_of_string, caml_string_of_jsstring } from './mlBytes.js'; +import { caml_call_gen, caml_global_data, caml_named_value } from './stdlib.js'; + //Provides: caml_js_pure_expr const -//Requires: caml_callback -function caml_js_pure_expr(f) { +export function caml_js_pure_expr(f) { return caml_callback(f, [0]); } //Provides: caml_js_set (mutable, const, mutable) -function caml_js_set(o, f, v) { +export function caml_js_set(o, f, v) { o[f] = v; return 0; } //Provides: caml_js_get (mutable, const) -function caml_js_get(o, f) { +export function caml_js_get(o, f) { return o[f]; } //Provides: caml_js_delete (mutable, const) -function caml_js_delete(o, f) { +export function caml_js_delete(o, f) { delete o[f]; return 0; } //Provides: caml_js_instanceof (const, const) -function caml_js_instanceof(o, c) { +export function caml_js_instanceof(o, c) { return o instanceof c ? 1 : 0; } //Provides: caml_js_typeof (const) -function caml_js_typeof(o) { +export function caml_js_typeof(o) { return typeof o; } //Provides:caml_trampoline -function caml_trampoline(res) { +export function caml_trampoline(res) { var c = 1; while (res?.joo_tramp) { res = res.joo_tramp.apply(null, res.joo_args); @@ -61,33 +65,29 @@ function caml_trampoline(res) { } //Provides:caml_trampoline_return -function caml_trampoline_return(f, args, direct) { +export function caml_trampoline_return(f, args, direct) { return { joo_tramp: f, joo_args: args, joo_direct: direct }; } //Provides:caml_stack_depth //If: effects -var caml_stack_depth = 0; +export let caml_stack_depth = 0; //Provides:caml_stack_check_depth //If: effects -//Requires:caml_stack_depth -function caml_stack_check_depth() { +export function caml_stack_check_depth() { return --caml_stack_depth > 0; } //Provides: caml_callback //If: !effects -//Requires:caml_call_gen -var caml_callback = caml_call_gen; +export let caml_callback = caml_call_gen; //Provides: caml_callback //If: effects //If: !doubletranslate -//Requires: caml_stack_depth, caml_call_gen, caml_wrap_exception -//Requires: caml_current_stack //Alias: caml_cps_trampoline -function caml_callback(f, args) { +export function caml_callback(f, args) { var saved_stack_depth = caml_stack_depth; var saved_current_stack = caml_current_stack; try { @@ -120,28 +120,25 @@ function caml_callback(f, args) { //Provides: caml_callback //If: effects //If: doubletranslate -//Requires: caml_call_gen -var caml_callback = caml_call_gen; +export let caml_callback = caml_call_gen; //Provides: caml_is_js -function caml_is_js() { +export function caml_is_js() { return 1; } //Provides: caml_jsoo_flags_use_js_string -function caml_jsoo_flags_use_js_string(_unit) { +export function caml_jsoo_flags_use_js_string(_unit) { return FLAG("use-js-string"); } //Provides: caml_jsoo_flags_effects -//Requires: caml_string_of_jsstring -function caml_jsoo_flags_effects(_unit) { +export function caml_jsoo_flags_effects(_unit) { return caml_string_of_jsstring(CONFIG("effects")); } //Provides: caml_wrap_exception const (mutable) -//Requires: caml_global_data,caml_string_of_jsstring,caml_named_value -function caml_wrap_exception(e) { +export function caml_wrap_exception(e) { if (FLAG("excwrap")) { if (Array.isArray(e)) return e; var exn; @@ -174,10 +171,7 @@ function caml_wrap_exception(e) { } //Provides: caml_maybe_attach_backtrace -//Requires: caml_exn_with_js_backtrace -//Requires: caml_record_backtrace_env_flag -//Requires: caml_record_backtrace_runtime_flag -function caml_maybe_attach_backtrace(exn, force) { +export function caml_maybe_attach_backtrace(exn, force) { // Backtraces are very expensive, we only enable them when explicitly requested // at compile-time (--enable with-js-error) or at startup with OCAMLRUNPARAM=b=1. // Libraries such as Base unconditionally enable backtraces (programmatically) but @@ -189,8 +183,7 @@ function caml_maybe_attach_backtrace(exn, force) { // Experimental //Provides: caml_exn_with_js_backtrace -//Requires: caml_global_data -function caml_exn_with_js_backtrace(exn, force) { +export function caml_exn_with_js_backtrace(exn, force) { //never reraise for constant exn if (!exn.js_error || force || exn[0] === 248) exn.js_error = new globalThis.Error("Js exception containing backtrace"); @@ -198,7 +191,7 @@ function caml_exn_with_js_backtrace(exn, force) { } //Provides: caml_js_error_option_of_exception -function caml_js_error_option_of_exception(exn) { +export function caml_js_error_option_of_exception(exn) { if (exn.js_error) { return [0, exn.js_error]; } @@ -206,40 +199,40 @@ function caml_js_error_option_of_exception(exn) { } //Provides: caml_throw_js_exception -function caml_throw_js_exception(exn) { +export function caml_throw_js_exception(exn) { throw exn; } //Provides: caml_js_from_bool const (const) -function caml_js_from_bool(x) { +export function caml_js_from_bool(x) { return !!x; } //Provides: caml_js_to_bool const (const) -function caml_js_to_bool(x) { +export function caml_js_to_bool(x) { return +x; } //Provides: caml_js_from_float const (const) //Alias: caml_js_from_int32 //Alias: caml_js_from_nativeint -function caml_js_from_float(x) { +export function caml_js_from_float(x) { return x; } //Provides: caml_js_to_float const (const) -function caml_js_to_float(x) { +export function caml_js_to_float(x) { return x; } //Provides: caml_js_to_int32 const (const) //Alias: caml_js_to_nativeint -function caml_js_to_int32(x) { +export function caml_js_to_int32(x) { return x | 0; } //Provides: caml_js_from_array mutable (shallow) -function caml_js_from_array(a) { +export function caml_js_from_array(a) { return a.slice(1); } //Provides: caml_js_to_array mutable (shallow) -function caml_js_to_array(a) { +export function caml_js_to_array(a) { var len = a.length; var b = new Array(len + 1); b[0] = 0; @@ -248,7 +241,7 @@ function caml_js_to_array(a) { } //Provides: caml_list_of_js_array const (mutable) -function caml_list_of_js_array(a) { +export function caml_list_of_js_array(a) { var l = 0; for (var i = a.length - 1; i >= 0; i--) { var e = a[i]; @@ -258,7 +251,7 @@ function caml_list_of_js_array(a) { } //Provides: caml_list_to_js_array const (mutable) -function caml_list_to_js_array(l) { +export function caml_list_to_js_array(l) { var a = []; for (; l !== 0; l = l[2]) { a.push(l[1]); @@ -267,8 +260,7 @@ function caml_list_to_js_array(l) { } //Provides: caml_js_var mutable -//Requires: caml_jsstring_of_string -function caml_js_var(x) { +export function caml_js_var(x) { var x = caml_jsstring_of_string(x); //Checks that x has the form ident[.ident]* if (!x.match(/^[a-zA-Z_$][a-zA-Z_$0-9]*(\.[a-zA-Z_$][a-zA-Z_$0-9]*)*$/)) { @@ -283,13 +275,11 @@ function caml_js_var(x) { return eval(x); } //Provides: caml_js_call (const, mutable, shallow) -//Requires: caml_js_from_array -function caml_js_call(f, o, args) { +export function caml_js_call(f, o, args) { return f.apply(o, caml_js_from_array(args)); } //Provides: caml_js_fun_call (const, shallow) -//Requires: caml_js_from_array -function caml_js_fun_call(f, a) { +export function caml_js_fun_call(f, a) { switch (a.length) { case 1: return f(); @@ -311,14 +301,11 @@ function caml_js_fun_call(f, a) { return f.apply(null, caml_js_from_array(a)); } //Provides: caml_js_meth_call (mutable, const, shallow) -//Requires: caml_jsstring_of_string -//Requires: caml_js_from_array -function caml_js_meth_call(o, f, args) { +export function caml_js_meth_call(o, f, args) { return o[caml_jsstring_of_string(f)].apply(o, caml_js_from_array(args)); } //Provides: caml_js_new (const, shallow) -//Requires: caml_js_from_array -function caml_js_new(c, a) { +export function caml_js_new(c, a) { switch (a.length) { case 1: return new c(); @@ -344,8 +331,7 @@ function caml_js_new(c, a) { return new F(); } //Provides: caml_ojs_new_arr (const, shallow) -//Requires: caml_js_from_array -function caml_ojs_new_arr(c, a) { +export function caml_ojs_new_arr(c, a) { switch (a.length) { case 0: return new c(); @@ -371,8 +357,7 @@ function caml_ojs_new_arr(c, a) { return new F(); } //Provides: caml_js_wrap_callback const (const) -//Requires: caml_callback -function caml_js_wrap_callback(f) { +export function caml_js_wrap_callback(f) { return function (...args) { if (args.length === 0) { args = [undefined]; @@ -383,23 +368,20 @@ function caml_js_wrap_callback(f) { } //Provides: caml_js_wrap_callback_arguments -//Requires: caml_callback -function caml_js_wrap_callback_arguments(f) { +export function caml_js_wrap_callback_arguments(f) { return function (...args) { return caml_callback(f, [args]); }; } //Provides: caml_js_wrap_callback_strict const -//Requires: caml_callback -function caml_js_wrap_callback_strict(arity, f) { +export function caml_js_wrap_callback_strict(arity, f) { return function (...args) { args.length = arity; return caml_callback(f, args); }; } //Provides: caml_js_wrap_callback_unsafe const (const) -//Requires: caml_callback, caml_js_function_arity -function caml_js_wrap_callback_unsafe(f) { +export function caml_js_wrap_callback_unsafe(f) { return function (...args) { var len = caml_js_function_arity(f); args.length = len; @@ -407,8 +389,7 @@ function caml_js_wrap_callback_unsafe(f) { }; } //Provides: caml_js_wrap_meth_callback const (const) -//Requires: caml_callback, caml_js_wrap_callback -function caml_js_wrap_meth_callback(f) { +export function caml_js_wrap_meth_callback(f) { return function (...args) { args.unshift(this); var res = caml_callback(f, args); @@ -416,15 +397,13 @@ function caml_js_wrap_meth_callback(f) { }; } //Provides: caml_js_wrap_meth_callback_arguments const (const) -//Requires: caml_callback -function caml_js_wrap_meth_callback_arguments(f) { +export function caml_js_wrap_meth_callback_arguments(f) { return function (...args) { return caml_callback(f, [this, args]); }; } //Provides: caml_js_wrap_meth_callback_strict const -//Requires: caml_callback -function caml_js_wrap_meth_callback_strict(arity, f) { +export function caml_js_wrap_meth_callback_strict(arity, f) { return function (...args) { args.length = arity; args.unshift(this); @@ -432,8 +411,7 @@ function caml_js_wrap_meth_callback_strict(arity, f) { }; } //Provides: caml_js_wrap_meth_callback_unsafe const (const) -//Requires: caml_callback, caml_js_function_arity -function caml_js_wrap_meth_callback_unsafe(f) { +export function caml_js_wrap_meth_callback_unsafe(f) { return function (...args) { var len = caml_js_function_arity(f); args.unshift(this); @@ -444,63 +422,59 @@ function caml_js_wrap_meth_callback_unsafe(f) { //Provides: caml_js_function_arity //If: !effects -function caml_js_function_arity(f) { +export function caml_js_function_arity(f) { return f.l >= 0 ? f.l : (f.l = f.length); } //Provides: caml_js_function_arity //If: effects //If: doubletranslate -function caml_js_function_arity(f) { +export function caml_js_function_arity(f) { return f.l >= 0 ? f.l : (f.l = f.length); } //Provides: caml_js_function_arity //If: effects //If: !doubletranslate -function caml_js_function_arity(f) { +export function caml_js_function_arity(f) { // Functions have an additional continuation parameter. This should // not be visible when calling them from JavaScript return (f.l >= 0 ? f.l : (f.l = f.length)) - 1; } //Provides: caml_js_equals mutable (const, const) -function caml_js_equals(x, y) { +export function caml_js_equals(x, y) { // biome-ignore lint/suspicious/noDoubleEquals: return +(x == y); } //Provides: caml_js_strict_equals mutable (const, const) -function caml_js_strict_equals(x, y) { +export function caml_js_strict_equals(x, y) { return +(x === y); } //Provides: caml_js_eval_string (const) -//Requires: caml_jsstring_of_string -function caml_js_eval_string(s) { +export function caml_js_eval_string(s) { // biome-ignore lint/security/noGlobalEval: return eval(caml_jsstring_of_string(s)); } //Provides: caml_js_expr (const) -//Requires: caml_jsstring_of_string -function caml_js_expr(s) { +export function caml_js_expr(s) { console.error("caml_js_expr: fallback to runtime evaluation\n"); // biome-ignore lint/security/noGlobalEval: return eval(caml_jsstring_of_string(s)); } //Provides: caml_pure_js_expr const (const) -//Requires: caml_jsstring_of_string -function caml_pure_js_expr(s) { +export function caml_pure_js_expr(s) { console.error("caml_pure_js_expr: fallback to runtime evaluation\n"); // biome-ignore lint/security/noGlobalEval: return eval(caml_jsstring_of_string(s)); } //Provides: caml_js_object (object_literal) -//Requires: caml_jsstring_of_string -function caml_js_object(a) { +export function caml_js_object(a) { var o = {}; for (var i = 1; i < a.length; i++) { var p = a[i]; diff --git a/runtime/js/jslib_js_of_ocaml.js b/runtime/js/jslib_js_of_ocaml.js index 0a88c6a4d9..5e1b21a942 100644 --- a/runtime/js/jslib_js_of_ocaml.js +++ b/runtime/js/jslib_js_of_ocaml.js @@ -19,9 +19,11 @@ ///////////// Jslib: code specific to Js_of_ocaml +import { caml_failwith } from './fail.js'; + //Provides: caml_js_html_escape const (const) var caml_js_regexps = { amp: /&/g, lt: /= 5.1.0 -var caml_marshal_header_size = 16; +export let caml_marshal_header_size = 16; //Provides: caml_marshal_data_size mutable -//Requires: caml_failwith, caml_bytes_unsafe_get -//Requires: caml_uint8_array_of_bytes -//Requires: UInt8ArrayReader -//Requires: caml_marshal_header_size -function caml_marshal_data_size(s, ofs) { +export function caml_marshal_data_size(s, ofs) { var r = new UInt8ArrayReader(caml_uint8_array_of_bytes(s), ofs); function readvlq(overflow) { var c = r.read8u(); @@ -581,7 +569,7 @@ function caml_marshal_data_size(s, ofs) { } //Provides: MlObjectTable -class MlObjectTable { +export class MlObjectTable { constructor() { this.objs = []; this.lookup = new globalThis.Map(); @@ -601,13 +589,7 @@ class MlObjectTable { } //Provides: caml_output_val -//Requires: caml_int64_to_bytes, caml_failwith -//Requires: caml_int64_bits_of_float -//Requires: caml_is_ml_bytes, caml_ml_bytes_length, caml_bytes_unsafe_get -//Requires: caml_is_ml_string, caml_ml_string_length, caml_string_unsafe_get -//Requires: MlObjectTable, caml_list_to_js_array, caml_custom_ops -//Requires: caml_invalid_argument,caml_string_of_jsbytes, caml_is_continuation_tag -var caml_output_val = (function () { +export let caml_output_val = (function () { class Writer { constructor() { this.chunk = []; @@ -810,20 +792,17 @@ var caml_output_val = (function () { })(); //Provides: caml_output_value_to_string mutable -//Requires: caml_output_val, caml_string_of_uint8_array -function caml_output_value_to_string(v, flags) { +export function caml_output_value_to_string(v, flags) { return caml_string_of_uint8_array(caml_output_val(v, flags)); } //Provides: caml_output_value_to_bytes mutable -//Requires: caml_output_val, caml_bytes_of_uint8_array -function caml_output_value_to_bytes(v, flags) { +export function caml_output_value_to_bytes(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_uint8_array -function caml_output_value_to_buffer(s, ofs, len, v, flags) { +export 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_uint8_array(t), 0, s, ofs, t.length); diff --git a/runtime/js/md5.js b/runtime/js/md5.js index 950cf3a406..5652db8509 100644 --- a/runtime/js/md5.js +++ b/runtime/js/md5.js @@ -17,11 +17,12 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +import { caml_raise_end_of_file } from './fail.js'; +import { caml_ml_input_block } from './io.js'; +import { caml_bytes_of_string, caml_string_of_uint8_array, caml_uint8_array_of_bytes } from './mlBytes.js'; + //Provides: caml_md5_chan -//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) { +export function caml_md5_chan(chanid, toread) { var ctx = caml_MD5Init(); var buffer = new Uint8Array(4096); if (toread < 0) { @@ -47,13 +48,12 @@ function caml_md5_chan(chanid, toread) { } //Provides: caml_md5_string -//Requires: caml_bytes_of_string, caml_md5_bytes -function caml_md5_string(s, ofs, len) { +export function caml_md5_string(s, ofs, len) { return caml_md5_bytes(caml_bytes_of_string(s), ofs, len); } //Provides: caml_MD5Transform -var caml_MD5Transform = (function () { +export let caml_MD5Transform = (function () { function add(x, y) { return (x + y) | 0; } @@ -156,7 +156,7 @@ var caml_MD5Transform = (function () { })(); //Provides: caml_MD5Init -function caml_MD5Init() { +export function caml_MD5Init() { var buffer = new ArrayBuffer(64); var b32 = new Uint32Array(buffer); var b8 = new Uint8Array(buffer); @@ -169,8 +169,7 @@ function caml_MD5Init() { } //Provides: caml_MD5Update -//Requires: caml_MD5Transform -function caml_MD5Update(ctx, input, input_len) { +export function caml_MD5Update(ctx, input, input_len) { var in_buf = ctx.len & 0x3f; var input_pos = 0; ctx.len += input_len; @@ -196,8 +195,7 @@ function caml_MD5Update(ctx, input, input_len) { } //Provides: caml_MD5Final -//Requires: caml_MD5Transform -function caml_MD5Final(ctx) { +export function caml_MD5Final(ctx) { var in_buf = ctx.len & 0x3f; ctx.b8[in_buf] = 0x80; in_buf++; @@ -224,9 +222,7 @@ function caml_MD5Final(ctx) { } //Provides: caml_md5_bytes -//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) { +export 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); diff --git a/runtime/js/mlBytes.js b/runtime/js/mlBytes.js index 9fa029deb6..7453c1d4d1 100644 --- a/runtime/js/mlBytes.js +++ b/runtime/js/mlBytes.js @@ -46,8 +46,11 @@ // is_ascii(x) = x&1 // kind(x) = x&6 +import { caml_failwith, caml_invalid_argument } from './fail.js'; +import { caml_int64_of_bytes, caml_int64_to_bytes } from './int64.js'; + //Provides: caml_str_repeat -function caml_str_repeat(n, s) { +export function caml_str_repeat(n, s) { return s.repeat(n); } @@ -56,7 +59,7 @@ function caml_str_repeat(n, s) { // Pre ECMAScript 5, [apply] would not support array-like object. // In such setup, Typed_array would be implemented as polyfill, and [f.apply] would // fail here. Mark the primitive as Weakdef, so that people can override it easily. -function caml_subarray_to_jsbytes(a, i, len) { +export function caml_subarray_to_jsbytes(a, i, len) { var f = String.fromCharCode; if (i === 0 && len <= 4096 && len === a.length) return f.apply(null, a); var s = ""; @@ -70,7 +73,7 @@ function caml_subarray_to_jsbytes(a, i, len) { // Pre ECMAScript 5, [apply] would not support array-like object. // In such setup, Typed_array would be implemented as polyfill, and [f.apply] would // fail here. Mark the primitive as Weakdef, so that people can override it easily. -function caml_sub_uint8_array_to_jsbytes(a, i, len) { +export function caml_sub_uint8_array_to_jsbytes(a, i, len) { var f = String.fromCharCode; if (i === 0 && len <= 4096 && len === a.length) return f.apply(null, a); var s = ""; @@ -80,7 +83,7 @@ function caml_sub_uint8_array_to_jsbytes(a, i, len) { } //Provides: jsoo_is_ascii -function jsoo_is_ascii(s) { +export function jsoo_is_ascii(s) { // The regular expression gets better at around this point for all browsers if (s.length < 24) { // Spidermonkey gets much slower when s.length >= 24 (on 64 bit archs) @@ -92,7 +95,7 @@ function jsoo_is_ascii(s) { } //Provides: caml_bytes_unsafe_get mutable -function caml_bytes_unsafe_get(s, i) { +export function caml_bytes_unsafe_get(s, i) { switch (s.t & 6) { case 0 /* BYTES */: return s.c.charCodeAt(i); @@ -105,8 +108,7 @@ function caml_bytes_unsafe_get(s, i) { } //Provides: caml_bytes_unsafe_set -//Requires: caml_convert_bytes_to_array -function caml_bytes_unsafe_set(s, i, c) { +export function caml_bytes_unsafe_set(s, i, c) { // The OCaml compiler uses Char.unsafe_chr on integers larger than 255! c &= 0xff; if (s.t !== 4 /* ARRAY */) { @@ -122,29 +124,23 @@ function caml_bytes_unsafe_set(s, i, c) { } //Provides: caml_string_bound_error -//Requires: caml_invalid_argument -function caml_string_bound_error() { +export function caml_string_bound_error() { caml_invalid_argument("index out of bounds"); } //Provides: caml_bytes_bound_error -//Requires: caml_invalid_argument -function caml_bytes_bound_error() { +export function caml_bytes_bound_error() { caml_invalid_argument("index out of bounds"); } //Provides: caml_string_get -//Requires: caml_string_bound_error, caml_string_unsafe_get -//Requires: caml_ml_string_length -function caml_string_get(s, i) { +export function caml_string_get(s, i) { if (i >>> 0 >= caml_ml_string_length(s)) caml_string_bound_error(); return caml_string_unsafe_get(s, i); } //Provides: caml_string_get16 -//Requires: caml_string_unsafe_get, caml_string_bound_error -//Requires: caml_ml_string_length -function caml_string_get16(s, i) { +export function caml_string_get16(s, i) { if (i >>> 0 >= caml_ml_string_length(s) - 1) caml_string_bound_error(); var b1 = caml_string_unsafe_get(s, i), b2 = caml_string_unsafe_get(s, i + 1); @@ -152,8 +148,7 @@ function caml_string_get16(s, i) { } //Provides: caml_bytes_get16 -//Requires: caml_bytes_unsafe_get, caml_bytes_bound_error -function caml_bytes_get16(s, i) { +export function caml_bytes_get16(s, i) { if (i >>> 0 >= s.l - 1) caml_bytes_bound_error(); var b1 = caml_bytes_unsafe_get(s, i), b2 = caml_bytes_unsafe_get(s, i + 1); @@ -161,9 +156,7 @@ function caml_bytes_get16(s, i) { } //Provides: caml_string_get32 -//Requires: caml_string_unsafe_get, caml_string_bound_error -//Requires: caml_ml_string_length -function caml_string_get32(s, i) { +export function caml_string_get32(s, i) { if (i >>> 0 >= caml_ml_string_length(s) - 3) caml_string_bound_error(); var b1 = caml_string_unsafe_get(s, i), b2 = caml_string_unsafe_get(s, i + 1), @@ -173,8 +166,7 @@ function caml_string_get32(s, i) { } //Provides: caml_bytes_get32 -//Requires: caml_bytes_unsafe_get, caml_bytes_bound_error -function caml_bytes_get32(s, i) { +export function caml_bytes_get32(s, i) { if (i >>> 0 >= s.l - 3) caml_bytes_bound_error(); var b1 = caml_bytes_unsafe_get(s, i), b2 = caml_bytes_unsafe_get(s, i + 1), @@ -184,10 +176,7 @@ function caml_bytes_get32(s, i) { } //Provides: caml_string_get64 -//Requires: caml_string_unsafe_get, caml_string_bound_error -//Requires: caml_int64_of_bytes -//Requires: caml_ml_string_length -function caml_string_get64(s, i) { +export function caml_string_get64(s, i) { if (i >>> 0 >= caml_ml_string_length(s) - 7) caml_string_bound_error(); var a = new Array(8); for (var j = 0; j < 8; j++) { @@ -197,9 +186,7 @@ function caml_string_get64(s, i) { } //Provides: caml_bytes_get64 -//Requires: caml_bytes_unsafe_get, caml_bytes_bound_error -//Requires: caml_int64_of_bytes -function caml_bytes_get64(s, i) { +export function caml_bytes_get64(s, i) { if (i >>> 0 >= s.l - 7) caml_bytes_bound_error(); var a = new Array(8); for (var j = 0; j < 8; j++) { @@ -209,30 +196,26 @@ function caml_bytes_get64(s, i) { } //Provides: caml_bytes_get -//Requires: caml_bytes_bound_error, caml_bytes_unsafe_get -function caml_bytes_get(s, i) { +export function caml_bytes_get(s, i) { if (i >>> 0 >= s.l) caml_bytes_bound_error(); return caml_bytes_unsafe_get(s, i); } //Provides: caml_string_set -//Requires: caml_failwith //If: js-string -function caml_string_set(_s, _i, _c) { +export function caml_string_set(_s, _i, _c) { caml_failwith("caml_string_set"); } //Provides: caml_string_set -//Requires: caml_string_unsafe_set, caml_string_bound_error //If: !js-string -function caml_string_set(s, i, c) { +export function caml_string_set(s, i, c) { if (i >>> 0 >= s.l) caml_string_bound_error(); return caml_string_unsafe_set(s, i, c); } //Provides: caml_bytes_set16 -//Requires: caml_bytes_bound_error, caml_bytes_unsafe_set -function caml_bytes_set16(s, i, i16) { +export function caml_bytes_set16(s, i, i16) { if (i >>> 0 >= s.l - 1) caml_bytes_bound_error(); var b2 = 0xff & (i16 >> 8), b1 = 0xff & i16; @@ -242,8 +225,7 @@ function caml_bytes_set16(s, i, i16) { } //Provides: caml_bytes_set32 -//Requires: caml_bytes_bound_error, caml_bytes_unsafe_set -function caml_bytes_set32(s, i, i32) { +export function caml_bytes_set32(s, i, i32) { if (i >>> 0 >= s.l - 3) caml_bytes_bound_error(); var b4 = 0xff & (i32 >> 24), b3 = 0xff & (i32 >> 16), @@ -257,9 +239,7 @@ function caml_bytes_set32(s, i, i32) { } //Provides: caml_bytes_set64 -//Requires: caml_bytes_bound_error, caml_bytes_unsafe_set -//Requires: caml_int64_to_bytes -function caml_bytes_set64(s, i, i64) { +export function caml_bytes_set64(s, i, i64) { if (i >>> 0 >= s.l - 7) caml_bytes_bound_error(); var a = caml_int64_to_bytes(i64); for (var j = 0; j < 8; j++) { @@ -269,22 +249,19 @@ function caml_bytes_set64(s, i, i64) { } //Provides: caml_bytes_set -//Requires: caml_bytes_bound_error, caml_bytes_unsafe_set -function caml_bytes_set(s, i, c) { +export function caml_bytes_set(s, i, c) { if (i >>> 0 >= s.l) caml_bytes_bound_error(); return caml_bytes_unsafe_set(s, i, c); } //Provides: jsoo_text_encoder -var jsoo_text_encoder = new TextEncoder(); +export let jsoo_text_encoder = new TextEncoder(); //Provides: jsoo_text_decoder -var jsoo_text_decoder = new TextDecoder(); +export let jsoo_text_decoder = new TextDecoder(); //Provides: caml_bytes_of_utf16_jsstring -//Requires: MlBytes, jsoo_text_encoder -//Requires: jsoo_is_ascii -function caml_bytes_of_utf16_jsstring(s) { +export function caml_bytes_of_utf16_jsstring(s) { if (jsoo_is_ascii(s)) { return new MlBytes(9, s, s.length); } else { @@ -294,10 +271,7 @@ function caml_bytes_of_utf16_jsstring(s) { } //Provides: MlBytes -//Requires: caml_convert_string_to_bytes, jsoo_is_ascii -//Requires: caml_uint8_array_of_bytes -//Requires: jsoo_text_decoder -class MlBytes { +export class MlBytes { constructor(tag, contents, length) { this.t = tag; this.c = contents; @@ -334,8 +308,7 @@ class MlBytes { } //Provides: caml_convert_string_to_bytes -//Requires: caml_str_repeat, caml_sub_uint8_array_to_jsbytes -function caml_convert_string_to_bytes(s) { +export function caml_convert_string_to_bytes(s) { /* Assumes not BYTES */ if (s.t === 2 /* PARTIAL */) s.c += caml_str_repeat(s.l - s.c.length, "\0"); else s.c = caml_sub_uint8_array_to_jsbytes(s.c, 0, s.c.length); @@ -343,7 +316,7 @@ function caml_convert_string_to_bytes(s) { } //Provides: caml_convert_bytes_to_array -function caml_convert_bytes_to_array(s) { +export function caml_convert_bytes_to_array(s) { /* Assumes not ARRAY */ var a = new Uint8Array(s.l); var b = s.c, @@ -357,15 +330,13 @@ function caml_convert_bytes_to_array(s) { } //Provides: caml_uint8_array_of_bytes mutable -//Requires: caml_convert_bytes_to_array -function caml_uint8_array_of_bytes(s) { +export function caml_uint8_array_of_bytes(s) { if (s.t !== 4 /* ARRAY */) caml_convert_bytes_to_array(s); return s.c; } //Provides: caml_uint8_array_of_string mutable -//Requires: caml_ml_string_length, caml_string_unsafe_get -function caml_uint8_array_of_string(s) { +export function caml_uint8_array_of_string(s) { var l = caml_ml_string_length(s); var a = new Uint8Array(l); var i = 0; @@ -374,50 +345,43 @@ function caml_uint8_array_of_string(s) { } //Provides: caml_create_string const -//Requires: MlBytes, caml_invalid_argument //If: !js-string -function caml_create_string(len) { +export function caml_create_string(len) { if (len < 0) caml_invalid_argument("String.create"); return new MlBytes(len ? 2 : 9, "", len); } //Provides: caml_create_string const -//Requires: caml_invalid_argument //If: js-string -function caml_create_string(_len) { +export function caml_create_string(_len) { caml_invalid_argument("String.create"); } //Provides: caml_create_bytes const -//Requires: MlBytes,caml_invalid_argument -function caml_create_bytes(len) { +export function caml_create_bytes(len) { if (len < 0) caml_invalid_argument("Bytes.create"); return new MlBytes(len ? 2 : 9, "", len); } //Provides: caml_string_of_array -//Requires: caml_subarray_to_jsbytes, caml_string_of_jsbytes -function caml_string_of_array(a) { +export 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 //If: js-string -function caml_string_of_uint8_array(a) { +export function caml_string_of_uint8_array(a) { return caml_sub_uint8_array_to_jsbytes(a, 0, a.length); } //Provides: caml_string_of_uint8_array -//Requires: caml_bytes_of_uint8_array //If: !js-string -function caml_string_of_uint8_array(a) { +export function caml_string_of_uint8_array(a) { return caml_bytes_of_uint8_array(a.slice()); } //Provides: caml_bytes_of_array -//Requires: MlBytes -function caml_bytes_of_array(a) { +export function caml_bytes_of_array(a) { if (!(a instanceof Uint8Array)) { a = new Uint8Array(a); } @@ -425,22 +389,19 @@ function caml_bytes_of_array(a) { } //Provides: caml_bytes_of_uint8_array -//Requires: MlBytes -function caml_bytes_of_uint8_array(a) { +export 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) { +export function caml_bytes_compare(s1, s2) { s1.t & 6 && caml_convert_string_to_bytes(s1); s2.t & 6 && caml_convert_string_to_bytes(s2); return s1.c < s2.c ? -1 : s1.c > s2.c ? 1 : 0; } //Provides: caml_bytes_equal mutable (const, const) -//Requires: caml_convert_string_to_bytes -function caml_bytes_equal(s1, s2) { +export function caml_bytes_equal(s1, s2) { if (s1 === s2) return 1; s1.t & 6 && caml_convert_string_to_bytes(s1); s2.t & 6 && caml_convert_string_to_bytes(s2); @@ -448,60 +409,51 @@ function caml_bytes_equal(s1, s2) { } //Provides: caml_string_notequal mutable (const, const) -//Requires: caml_string_equal -function caml_string_notequal(s1, s2) { +export function caml_string_notequal(s1, s2) { return 1 - caml_string_equal(s1, s2); } //Provides: caml_bytes_notequal mutable (const, const) -//Requires: caml_bytes_equal -function caml_bytes_notequal(s1, s2) { +export function caml_bytes_notequal(s1, s2) { return 1 - caml_bytes_equal(s1, s2); } //Provides: caml_bytes_lessequal mutable -//Requires: caml_convert_string_to_bytes -function caml_bytes_lessequal(s1, s2) { +export function caml_bytes_lessequal(s1, s2) { s1.t & 6 && caml_convert_string_to_bytes(s1); s2.t & 6 && caml_convert_string_to_bytes(s2); return s1.c <= s2.c ? 1 : 0; } //Provides: caml_bytes_lessthan mutable -//Requires: caml_convert_string_to_bytes -function caml_bytes_lessthan(s1, s2) { +export function caml_bytes_lessthan(s1, s2) { s1.t & 6 && caml_convert_string_to_bytes(s1); s2.t & 6 && caml_convert_string_to_bytes(s2); return s1.c < s2.c ? 1 : 0; } //Provides: caml_string_greaterequal -//Requires: caml_string_lessequal -function caml_string_greaterequal(s1, s2) { +export function caml_string_greaterequal(s1, s2) { return caml_string_lessequal(s2, s1); } //Provides: caml_bytes_greaterequal -//Requires: caml_bytes_lessequal -function caml_bytes_greaterequal(s1, s2) { +export function caml_bytes_greaterequal(s1, s2) { return caml_bytes_lessequal(s2, s1); } //Provides: caml_string_greaterthan -//Requires: caml_string_lessthan -function caml_string_greaterthan(s1, s2) { +export function caml_string_greaterthan(s1, s2) { return caml_string_lessthan(s2, s1); } //Provides: caml_bytes_greaterthan -//Requires: caml_bytes_lessthan -function caml_bytes_greaterthan(s1, s2) { +export function caml_bytes_greaterthan(s1, s2) { return caml_bytes_lessthan(s2, s1); } //Provides: caml_fill_bytes -//Requires: caml_str_repeat, caml_convert_bytes_to_array //Alias: caml_fill_string -function caml_fill_bytes(s, i, l, c) { +export function caml_fill_bytes(s, i, l, c) { if (l > 0) { if (i === 0 && (l >= s.l || (s.t === 2 /* PARTIAL */ && l >= s.c.length))) { if (c === 0) { @@ -520,8 +472,7 @@ function caml_fill_bytes(s, i, l, c) { } //Provides: caml_blit_bytes -//Requires: caml_sub_uint8_array_to_jsbytes, caml_convert_bytes_to_array -function caml_blit_bytes(s1, i1, s2, i2, len) { +export function caml_blit_bytes(s1, i1, s2, i2, len) { if (len === 0) return 0; if ( i2 === 0 && @@ -562,27 +513,25 @@ function caml_blit_bytes(s1, i1, s2, i2, len) { } //Provides: caml_blit_string -//Requires: caml_blit_bytes, caml_bytes_of_string -function caml_blit_string(a, b, c, d, e) { +export function caml_blit_string(a, b, c, d, e) { caml_blit_bytes(caml_bytes_of_string(a), b, c, d, e); return 0; } //Provides: caml_ml_bytes_length const -function caml_ml_bytes_length(s) { +export function caml_ml_bytes_length(s) { return s.l; } //Provides: caml_string_concat const //If: js-string -function caml_string_concat(a, b) { +export function caml_string_concat(a, b) { return a + b; } //Provides: caml_string_concat const -//Requires: caml_convert_string_to_bytes, MlBytes //If: !js-string -function caml_string_concat(s1, s2) { +export function caml_string_concat(s1, s2) { s1.t & 6 && caml_convert_string_to_bytes(s1); s2.t & 6 && caml_convert_string_to_bytes(s2); return new MlBytes(0, s1.c + s2.c, s1.l + s2.l); @@ -590,77 +539,72 @@ function caml_string_concat(s1, s2) { //Provides: caml_string_unsafe_get const //If: js-string -function caml_string_unsafe_get(s, i) { +export function caml_string_unsafe_get(s, i) { return s.charCodeAt(i); } //Provides: caml_ml_string_length const //If: js-string -function caml_ml_string_length(s) { +export function caml_ml_string_length(s) { return s.length; } //Provides: caml_string_compare const //If: js-string -function caml_string_compare(s1, s2) { +export function caml_string_compare(s1, s2) { return s1 < s2 ? -1 : s1 > s2 ? 1 : 0; } //Provides: caml_string_equal const //If: js-string -function caml_string_equal(s1, s2) { +export function caml_string_equal(s1, s2) { if (s1 === s2) return 1; return 0; } //Provides: caml_string_lessequal const //If: js-string -function caml_string_lessequal(s1, s2) { +export function caml_string_lessequal(s1, s2) { return s1 <= s2 ? 1 : 0; } //Provides: caml_string_lessthan const //If: js-string -function caml_string_lessthan(s1, s2) { +export function caml_string_lessthan(s1, s2) { return s1 < s2 ? 1 : 0; } //Provides: caml_string_of_bytes -//Requires: caml_convert_string_to_bytes, caml_string_of_jsbytes //If: js-string -function caml_string_of_bytes(s) { +export function caml_string_of_bytes(s) { s.t & 6 && caml_convert_string_to_bytes(s); return caml_string_of_jsbytes(s.c); } //Provides: caml_bytes_of_string const -//Requires: caml_bytes_of_jsbytes, caml_jsbytes_of_string //If: js-string -function caml_bytes_of_string(s) { +export function caml_bytes_of_string(s) { return caml_bytes_of_jsbytes(caml_jsbytes_of_string(s)); } //Provides: caml_string_of_jsbytes const //If: js-string -function caml_string_of_jsbytes(x) { +export function caml_string_of_jsbytes(x) { return x; } //Provides: caml_jsbytes_of_string const //If: js-string -function caml_jsbytes_of_string(x) { +export function caml_jsbytes_of_string(x) { return x; } //Provides: jsoo_text_decoder_buff -var jsoo_text_decoder_buff = new ArrayBuffer(1024); +export let jsoo_text_decoder_buff = new ArrayBuffer(1024); //Provides: caml_jsstring_of_string const -//Requires: jsoo_is_ascii -//Requires: jsoo_text_decoder -//Requires: jsoo_text_decoder_buff //If: js-string -function caml_jsstring_of_string(s) { +export function caml_jsstring_of_string(s) { if (jsoo_is_ascii(s)) return s; var a = s.length <= jsoo_text_decoder_buff.length @@ -673,123 +617,107 @@ function caml_jsstring_of_string(s) { } //Provides: caml_string_of_jsstring const -//Requires: caml_string_of_array -//Requires: jsoo_text_encoder -//Requires: jsoo_is_ascii, caml_string_of_jsbytes //If: js-string -function caml_string_of_jsstring(s) { +export function caml_string_of_jsstring(s) { if (jsoo_is_ascii(s)) return caml_string_of_jsbytes(s); var a = jsoo_text_encoder.encode(s); return caml_string_of_array(a); } //Provides: caml_bytes_of_jsbytes const -//Requires: MlBytes -function caml_bytes_of_jsbytes(s) { +export function caml_bytes_of_jsbytes(s) { return new MlBytes(0, s, s.length); } // The section below should be used when use-js-string=false //Provides: caml_string_unsafe_get const -//Requires: caml_bytes_unsafe_get //If: !js-string -function caml_string_unsafe_get(s, i) { +export function caml_string_unsafe_get(s, i) { return caml_bytes_unsafe_get(s, i); } //Provides: caml_string_unsafe_set -//Requires: caml_bytes_unsafe_set //If: !js-string -function caml_string_unsafe_set(s, i, c) { +export function caml_string_unsafe_set(s, i, c) { return caml_bytes_unsafe_set(s, i, c); } //Provides: caml_ml_string_length const -//Requires: caml_ml_bytes_length //If: !js-string -function caml_ml_string_length(s) { +export function caml_ml_string_length(s) { return caml_ml_bytes_length(s); } //Provides: caml_string_compare -//Requires: caml_bytes_compare //If: !js-string -function caml_string_compare(s1, s2) { +export function caml_string_compare(s1, s2) { return caml_bytes_compare(s1, s2); } //Provides: caml_string_equal -//Requires: caml_bytes_equal //If: !js-string -function caml_string_equal(s1, s2) { +export function caml_string_equal(s1, s2) { return caml_bytes_equal(s1, s2); } //Provides: caml_string_lessequal -//Requires: caml_bytes_lessequal //If: !js-string -function caml_string_lessequal(s1, s2) { +export function caml_string_lessequal(s1, s2) { return caml_bytes_lessequal(s1, s2); } //Provides: caml_string_lessthan -//Requires: caml_bytes_lessthan //If: !js-string -function caml_string_lessthan(s1, s2) { +export function caml_string_lessthan(s1, s2) { return caml_bytes_lessthan(s1, s2); } //Provides: caml_string_of_bytes //If: !js-string -function caml_string_of_bytes(s) { +export function caml_string_of_bytes(s) { return s; } //Provides: caml_bytes_of_string const //If: !js-string -function caml_bytes_of_string(s) { +export function caml_bytes_of_string(s) { return s; } //Provides: caml_string_of_jsbytes const -//Requires: caml_bytes_of_jsbytes //If: !js-string -function caml_string_of_jsbytes(s) { +export function caml_string_of_jsbytes(s) { return caml_bytes_of_jsbytes(s); } //Provides: caml_jsbytes_of_string const -//Requires: caml_convert_string_to_bytes //If: !js-string -function caml_jsbytes_of_string(s) { +export function caml_jsbytes_of_string(s) { s.t & 6 && caml_convert_string_to_bytes(s); return s.c; } //Provides: caml_jsstring_of_string mutable (const) //If: !js-string -function caml_jsstring_of_string(s) { +export function caml_jsstring_of_string(s) { return s.toUtf16(); } //Provides: caml_string_of_jsstring -//Requires: caml_bytes_of_utf16_jsstring //If: !js-string -function caml_string_of_jsstring(s) { +export function caml_string_of_jsstring(s) { return caml_bytes_of_utf16_jsstring(s); } //Provides: caml_is_ml_bytes -//Requires: MlBytes -function caml_is_ml_bytes(s) { +export function caml_is_ml_bytes(s) { return s instanceof MlBytes; } //Provides: caml_ml_bytes_content -//Requires: MlBytes, caml_convert_string_to_bytes //Returns a (full) string of bytes or an array -function caml_ml_bytes_content(s) { +export function caml_ml_bytes_content(s) { switch (s.t & 6) { case 2 /* PARTIAL */: caml_convert_string_to_bytes(s); @@ -801,65 +729,57 @@ function caml_ml_bytes_content(s) { //Provides: caml_is_ml_string //If: js-string -function caml_is_ml_string(s) { +export function caml_is_ml_string(s) { // biome-ignore lint/suspicious/noControlCharactersInRegex: expected return typeof s === "string" && !/[^\x00-\xff]/.test(s); } //Provides: caml_is_ml_string -//Requires: caml_is_ml_bytes //If: !js-string -function caml_is_ml_string(s) { +export function caml_is_ml_string(s) { return caml_is_ml_bytes(s); } // The functions below are deprecated //Provides: caml_js_to_byte_string const -//Requires: caml_string_of_jsbytes //Deprecated: Use [caml_string_of_jsbytes] instead -function caml_js_to_byte_string(s) { +export function caml_js_to_byte_string(s) { return caml_string_of_jsbytes(s); } //Provides: caml_js_from_string mutable (const) -//Requires: caml_jsstring_of_string //Deprecated: Use [caml_jsstring_of_string] instead -function caml_js_from_string(s) { +export function caml_js_from_string(s) { return caml_jsstring_of_string(s); } //Provides: caml_to_js_string mutable (const) -//Requires: caml_jsstring_of_string //Deprecated: Use [caml_jsstring_of_string] instead -function caml_to_js_string(s) { +export function caml_to_js_string(s) { return caml_jsstring_of_string(s); } //Provides: caml_js_to_string const -//Requires: caml_string_of_jsstring //Deprecated: Use [caml_string_of_jsstring] instead -function caml_js_to_string(s) { +export function caml_js_to_string(s) { return caml_string_of_jsstring(s); } //Provides: caml_array_of_string -//Requires: caml_uint8_array_of_string //Deprecated: Use [caml_uint8_array_of_string] instead -function caml_array_of_string(x) { +export function caml_array_of_string(x) { return caml_uint8_array_of_string(x); } //Provides: caml_array_of_bytes -//Requires: caml_uint8_array_of_bytes //Deprecated: Use [caml_uint8_array_of_bytes] instead -function caml_array_of_bytes(x) { +export function caml_array_of_bytes(x) { return caml_uint8_array_of_bytes(x); } //Provides: caml_new_string -//Requires: caml_string_of_jsbytes //Deprecated: Use [caml_string_of_jsbytes] instead -function caml_new_string(s) { +export function caml_new_string(s) { return caml_string_of_jsbytes(s); } diff --git a/runtime/js/nat.js b/runtime/js/nat.js index 12716d9101..a287d68507 100644 --- a/runtime/js/nat.js +++ b/runtime/js/nat.js @@ -1,7 +1,8 @@ +import { caml_hash_mix_int } from './hash.js'; +import { caml_custom_ops } from './marshal.js'; + //Provides: initialize_nat -//Requires: caml_custom_ops -//Requires: serialize_nat, deserialize_nat, caml_hash_nat -function initialize_nat() { +export function initialize_nat() { caml_custom_ops._nat = { deserialize: deserialize_nat, serialize: serialize_nat, @@ -10,7 +11,7 @@ function initialize_nat() { } //Provides: MlNat -class MlNat { +export class MlNat { constructor(x) { this.data = new Int32Array(x); // For num < 1.5 @@ -25,8 +26,7 @@ class MlNat { } //Provides: caml_hash_nat -//Requires: caml_hash_mix_int, num_digits_nat -function caml_hash_nat(x) { +export function caml_hash_nat(x) { var len = num_digits_nat(x, 0, x.data.length); var h = 0; for (var i = 0; i < len; i++) { @@ -36,19 +36,17 @@ function caml_hash_nat(x) { } //Provides: length_nat -function length_nat(x) { +export function length_nat(x) { return x.data.length; } //Provides: nat_of_array -//Requires: MlNat -function nat_of_array(l) { +export function nat_of_array(l) { return new MlNat(l); } //Provides: create_nat -//Requires: MlNat -function create_nat(size) { +export function create_nat(size) { var arr = new MlNat(size); for (var i = 0; i < size; i++) { arr.data[i] = -1; @@ -57,7 +55,7 @@ function create_nat(size) { } //Provides: set_to_zero_nat -function set_to_zero_nat(nat, ofs, len) { +export function set_to_zero_nat(nat, ofs, len) { for (var i = 0; i < len; i++) { nat.data[ofs + i] = 0; } @@ -65,7 +63,7 @@ function set_to_zero_nat(nat, ofs, len) { } //Provides: blit_nat -function blit_nat(nat1, ofs1, nat2, ofs2, len) { +export function blit_nat(nat1, ofs1, nat2, ofs2, len) { for (var i = 0; i < len; i++) { nat1.data[ofs1 + i] = nat2.data[ofs2 + i]; } @@ -73,29 +71,29 @@ function blit_nat(nat1, ofs1, nat2, ofs2, len) { } //Provides: set_digit_nat -function set_digit_nat(nat, ofs, digit) { +export function set_digit_nat(nat, ofs, digit) { nat.data[ofs] = digit; return 0; } //Provides: nth_digit_nat -function nth_digit_nat(nat, ofs) { +export function nth_digit_nat(nat, ofs) { return nat.data[ofs]; } //Provides: set_digit_nat_native -function set_digit_nat_native(nat, ofs, digit) { +export function set_digit_nat_native(nat, ofs, digit) { nat.data[ofs] = digit; return 0; } //Provides: nth_digit_nat_native -function nth_digit_nat_native(nat, ofs) { +export function nth_digit_nat_native(nat, ofs) { return nat.data[ofs]; } //Provides: num_digits_nat -function num_digits_nat(nat, ofs, len) { +export function num_digits_nat(nat, ofs, len) { for (var i = len - 1; i >= 0; i--) { if (nat.data[ofs + i] !== 0) return i + 1; } @@ -103,7 +101,7 @@ function num_digits_nat(nat, ofs, len) { } //Provides: num_leading_zero_bits_in_digit -function num_leading_zero_bits_in_digit(nat, ofs) { +export function num_leading_zero_bits_in_digit(nat, ofs) { var a = nat.data[ofs]; var b = 0; if (a & 0xffff0000) { @@ -133,30 +131,30 @@ function num_leading_zero_bits_in_digit(nat, ofs) { } //Provides: is_digit_int -function is_digit_int(nat, ofs) { +export function is_digit_int(nat, ofs) { if (nat.data[ofs] >= 0) return 1; return 0; } //Provides: is_digit_zero -function is_digit_zero(nat, ofs) { +export function is_digit_zero(nat, ofs) { if (nat.data[ofs] === 0) return 1; return 0; } //Provides: is_digit_normalized -function is_digit_normalized(_nat, _ofs) { +export function is_digit_normalized(_nat, _ofs) { return 1; } //Provides: is_digit_odd -function is_digit_odd(nat, ofs) { +export function is_digit_odd(nat, ofs) { if (nat.data[ofs] & 1) return 1; return 0; } //Provides: incr_nat -function incr_nat(nat, ofs, len, carry_in) { +export function incr_nat(nat, ofs, len, carry_in) { var carry = carry_in; for (var i = 0; i < len; i++) { var x = (nat.data[ofs + i] >>> 0) + carry; @@ -173,8 +171,7 @@ function incr_nat(nat, ofs, len, carry_in) { // len1 >= len2 //Provides: add_nat -//Requires: incr_nat -function add_nat(nat1, ofs1, len1, nat2, ofs2, len2, carry_in) { +export function add_nat(nat1, ofs1, len1, nat2, ofs2, len2, carry_in) { var carry = carry_in; for (var i = 0; i < len2; i++) { var x = (nat1.data[ofs1 + i] >>> 0) + (nat2.data[ofs2 + i] >>> 0) + carry; @@ -189,7 +186,7 @@ function add_nat(nat1, ofs1, len1, nat2, ofs2, len2, carry_in) { } //Provides: complement_nat -function complement_nat(nat, ofs, len) { +export function complement_nat(nat, ofs, len) { for (var i = 0; i < len; i++) { nat.data[ofs + i] = (-1 >>> 0) - (nat.data[ofs + i] >>> 0); } @@ -197,7 +194,7 @@ function complement_nat(nat, ofs, len) { // ocaml flips carry_in //Provides: decr_nat -function decr_nat(nat, ofs, len, carry_in) { +export function decr_nat(nat, ofs, len, carry_in) { var borrow = carry_in === 1 ? 0 : 1; for (var i = 0; i < len; i++) { var x = (nat.data[ofs + i] >>> 0) - borrow; @@ -215,8 +212,7 @@ function decr_nat(nat, ofs, len, carry_in) { // ocaml flips carry_in // len1 >= len2 //Provides: sub_nat -//Requires: decr_nat -function sub_nat(nat1, ofs1, len1, nat2, ofs2, len2, carry_in) { +export function sub_nat(nat1, ofs1, len1, nat2, ofs2, len2, carry_in) { var borrow = carry_in === 1 ? 0 : 1; for (var i = 0; i < len2; i++) { var x = (nat1.data[ofs1 + i] >>> 0) - (nat2.data[ofs2 + i] >>> 0) - borrow; @@ -233,8 +229,7 @@ function sub_nat(nat1, ofs1, len1, nat2, ofs2, len2, carry_in) { // nat1 += nat2 * nat3[ofs3] // len1 >= len2 //Provides: mult_digit_nat -//Requires: add_nat, nat_of_array -function mult_digit_nat(nat1, ofs1, len1, nat2, ofs2, len2, nat3, ofs3) { +export function mult_digit_nat(nat1, ofs1, len1, nat2, ofs2, len2, nat3, ofs3) { var carry = 0; var a = nat3.data[ofs3] >>> 0; for (var i = 0; i < len2; i++) { @@ -267,8 +262,7 @@ function mult_digit_nat(nat1, ofs1, len1, nat2, ofs2, len2, nat3, ofs3) { // nat1 += nat2 * nat3 // len1 >= len2 + len3. //Provides: mult_nat -//Requires: mult_digit_nat -function mult_nat(nat1, ofs1, len1, nat2, ofs2, len2, nat3, ofs3, len3) { +export function mult_nat(nat1, ofs1, len1, nat2, ofs2, len2, nat3, ofs3, len3) { var carry = 0; for (var i = 0; i < len3; i++) { carry += mult_digit_nat( @@ -288,8 +282,7 @@ function mult_nat(nat1, ofs1, len1, nat2, ofs2, len2, nat3, ofs3, len3) { // nat1 = 2 * nat1 + nat2 * nat2 // len1 >= 2 * len2 //Provides: square_nat -//Requires: mult_nat, add_nat -function square_nat(nat1, ofs1, len1, nat2, ofs2, len2) { +export function square_nat(nat1, ofs1, len1, nat2, ofs2, len2) { var carry = 0; carry += add_nat(nat1, ofs1, len1, nat1, ofs1, len1, 0); carry += mult_nat(nat1, ofs1, len1, nat2, ofs2, len2, nat2, ofs2, len2); @@ -298,7 +291,7 @@ function square_nat(nat1, ofs1, len1, nat2, ofs2, len2) { // 0 <= shift < 32 //Provides: shift_left_nat -function shift_left_nat(nat1, ofs1, len1, nat2, ofs2, nbits) { +export function shift_left_nat(nat1, ofs1, len1, nat2, ofs2, nbits) { if (nbits === 0) { nat2.data[ofs2] = 0; return 0; @@ -315,7 +308,7 @@ function shift_left_nat(nat1, ofs1, len1, nat2, ofs2, nbits) { // Assuming c > a, returns [quotient, remainder] of (a<<32 + b)/c //Provides: div_helper -function div_helper(a, b, c) { +export function div_helper(a, b, c) { var x = a * 65536 + (b >>> 16); var y = Math.floor(x / c) * 65536; var z = (x % c) * 65536; @@ -325,8 +318,7 @@ function div_helper(a, b, c) { // nat1[ofs1+len] < nat2[ofs2] //Provides: div_digit_nat -//Requires: div_helper -function div_digit_nat(natq, ofsq, natr, ofsr, nat1, ofs1, len, nat2, ofs2) { +export function div_digit_nat(natq, ofsq, natr, ofsr, nat1, ofs1, len, nat2, ofs2) { var rem = nat1.data[ofs1 + len - 1] >>> 0; // natq[ofsq+len-1] is guaranteed to be zero (due to the MSD requirement), // and should not be written to. @@ -343,8 +335,7 @@ function div_digit_nat(natq, ofsq, natr, ofsr, nat1, ofs1, len, nat2, ofs2) { // nat1[:nat2] := nat1 % nat2 // len1 > len2, nat2[ofs2+len2-1] > nat1[ofs1+len1-1] //Provides: div_nat -//Requires: div_digit_nat, div_helper, num_leading_zero_bits_in_digit, shift_left_nat, shift_right_nat, create_nat, set_to_zero_nat, mult_digit_nat, sub_nat, compare_nat, nat_of_array -function div_nat(nat1, ofs1, len1, nat2, ofs2, len2) { +export function div_nat(nat1, ofs1, len1, nat2, ofs2, len2) { if (len2 === 1) { div_digit_nat(nat1, ofs1 + 1, nat1, ofs1, nat1, ofs1, len1, nat2, ofs2); return 0; @@ -388,7 +379,7 @@ function div_nat(nat1, ofs1, len1, nat2, ofs2, len2) { // 0 <= shift < 32 //Provides: shift_right_nat -function shift_right_nat(nat1, ofs1, len1, nat2, ofs2, nbits) { +export function shift_right_nat(nat1, ofs1, len1, nat2, ofs2, nbits) { if (nbits === 0) { nat2.data[ofs2] = 0; return 0; @@ -404,15 +395,14 @@ function shift_right_nat(nat1, ofs1, len1, nat2, ofs2, nbits) { } //Provides: compare_digits_nat -function compare_digits_nat(nat1, ofs1, nat2, ofs2) { +export function compare_digits_nat(nat1, ofs1, nat2, ofs2) { if (nat1.data[ofs1] > nat2.data[ofs2]) return 1; if (nat1.data[ofs1] < nat2.data[ofs2]) return -1; return 0; } //Provides: compare_nat -//Requires: num_digits_nat -function compare_nat(nat1, ofs1, len1, nat2, ofs2, len2) { +export function compare_nat(nat1, ofs1, len1, nat2, ofs2, len2) { var a = num_digits_nat(nat1, ofs1, len1); var b = num_digits_nat(nat2, ofs2, len2); if (a > b) return 1; @@ -425,25 +415,25 @@ function compare_nat(nat1, ofs1, len1, nat2, ofs2, len2) { } //Provides: land_digit_nat -function land_digit_nat(nat1, ofs1, nat2, ofs2) { +export function land_digit_nat(nat1, ofs1, nat2, ofs2) { nat1.data[ofs1] &= nat2.data[ofs2]; return 0; } //Provides: lor_digit_nat -function lor_digit_nat(nat1, ofs1, nat2, ofs2) { +export function lor_digit_nat(nat1, ofs1, nat2, ofs2) { nat1.data[ofs1] |= nat2.data[ofs2]; return 0; } //Provides: lxor_digit_nat -function lxor_digit_nat(nat1, ofs1, nat2, ofs2) { +export function lxor_digit_nat(nat1, ofs1, nat2, ofs2) { nat1.data[ofs1] ^= nat2.data[ofs2]; return 0; } //Provides: serialize_nat -function serialize_nat(writer, nat, sz) { +export function serialize_nat(writer, nat, sz) { var len = nat.data.length; writer.write(32, len); for (var i = 0; i < len; i++) { @@ -454,8 +444,7 @@ function serialize_nat(writer, nat, sz) { } //Provides: deserialize_nat -//Requires: MlNat -function deserialize_nat(reader, sz) { +export function deserialize_nat(reader, sz) { var len = reader.read32s(); var nat = new MlNat(len); for (var i = 0; i < len; i++) { diff --git a/runtime/js/obj.js b/runtime/js/obj.js index c8c96f928c..3c2389118c 100644 --- a/runtime/js/obj.js +++ b/runtime/js/obj.js @@ -15,8 +15,13 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +import { caml_failwith, caml_invalid_argument } from './fail.js'; +import { caml_callback } from './jslib.js'; +import { caml_is_ml_bytes, caml_is_ml_string, caml_string_of_jsstring } from './mlBytes.js'; +import { caml_call_gen } from './stdlib.js'; + //Provides: caml_update_dummy -function caml_update_dummy(x, y) { +export function caml_update_dummy(x, y) { if (y.fun) { x.fun = y.fun; return 0; @@ -31,9 +36,8 @@ function caml_update_dummy(x, y) { } //Provides: caml_alloc_dummy_infix -//Requires: caml_call_gen //Version: < 5.4 -function caml_alloc_dummy_infix() { +export function caml_alloc_dummy_infix() { return function f(x) { return caml_call_gen(f.fun, [x]); }; @@ -41,15 +45,13 @@ function caml_alloc_dummy_infix() { //Provides: caml_alloc_dummy_lazy //Version: >= 5.4 -function caml_alloc_dummy_lazy(_unit) { +export function caml_alloc_dummy_lazy(_unit) { return [0, 0]; } //Provides: caml_update_dummy_lazy -//Requires: caml_obj_tag -//Requires: caml_update_dummy //Version: >= 5.4 -function caml_update_dummy_lazy(dummy, newval) { +export function caml_update_dummy_lazy(dummy, newval) { switch (caml_obj_tag(newval)) { case 246: // Lazy case 244: // Forcing @@ -65,8 +67,7 @@ function caml_update_dummy_lazy(dummy, newval) { } //Provides: caml_obj_tag -//Requires: caml_is_ml_bytes, caml_is_ml_string -function caml_obj_tag(x) { +export function caml_obj_tag(x) { if (Array.isArray(x) && x[0] === x[0] >>> 0) return x[0]; else if (caml_is_ml_bytes(x)) return 252; else if (caml_is_ml_string(x)) return 252; @@ -77,12 +78,12 @@ function caml_obj_tag(x) { //Provides: caml_obj_set_tag (mutable, const) //Version: < 5.0 -function caml_obj_set_tag(x, tag) { +export function caml_obj_set_tag(x, tag) { x[0] = tag; return 0; } //Provides: caml_obj_block const (const,const) -function caml_obj_block(tag, size) { +export function caml_obj_block(tag, size) { // TODO: fail for value that are not represented as an array var o = new Array(size + 1); o[0] = tag; @@ -91,7 +92,7 @@ function caml_obj_block(tag, size) { } //Provides: caml_obj_with_tag -function caml_obj_with_tag(tag, x) { +export function caml_obj_with_tag(tag, x) { var l = x.length; var a = new Array(l); a[0] = tag; @@ -100,14 +101,13 @@ function caml_obj_with_tag(tag, x) { } //Provides: caml_obj_dup mutable (mutable) -function caml_obj_dup(x) { +export function caml_obj_dup(x) { return typeof x === "number" ? x : x.slice(); } //Provides: caml_obj_truncate (mutable, const) -//Requires: caml_invalid_argument //Version: < 5.0 -function caml_obj_truncate(x, s) { +export function caml_obj_truncate(x, s) { if (s <= 0 || s + 1 > x.length) caml_invalid_argument("Obj.truncate"); if (x.length !== s + 1) x.length = s + 1; return 0; @@ -115,7 +115,7 @@ function caml_obj_truncate(x, s) { //Provides: caml_obj_make_forward //Version: < 5.0 -function caml_obj_make_forward(b, v) { +export function caml_obj_make_forward(b, v) { b[0] = 250; b[1] = v; return 0; @@ -123,7 +123,7 @@ function caml_obj_make_forward(b, v) { //Provides: caml_obj_compare_and_swap //Version: >= 5.0 -function caml_obj_compare_and_swap(x, i, old, n) { +export function caml_obj_compare_and_swap(x, i, old, n) { if (x[i + 1] === old) { x[i + 1] = n; return 1; @@ -133,21 +133,20 @@ function caml_obj_compare_and_swap(x, i, old, n) { //Provides: caml_obj_is_shared //Version: >= 5.0 -function caml_obj_is_shared(_x) { +export function caml_obj_is_shared(_x) { return 1; } //Provides: caml_lazy_make_forward const (mutable) -function caml_lazy_make_forward(v) { +export function caml_lazy_make_forward(v) { return [250, v]; } //Provides: caml_method_cache -var caml_method_cache = []; +export let caml_method_cache = []; //Provides: caml_oo_cache_id const -//Requires: caml_method_cache -function caml_oo_cache_id() { +export function caml_oo_cache_id() { var cacheid = caml_method_cache.length; caml_method_cache[cacheid] = 0; cacheid; @@ -155,8 +154,7 @@ function caml_oo_cache_id() { ///////////// CamlinternalOO //Provides: caml_get_cached_method const -//Requires: caml_method_cache -function caml_get_cached_method(obj, tag, cacheid) { +export function caml_get_cached_method(obj, tag, cacheid) { var meths = obj[1]; var ofs = caml_method_cache[cacheid]; if (meths[ofs + 4] === tag) { @@ -175,7 +173,7 @@ function caml_get_cached_method(obj, tag, cacheid) { } //Provides: caml_get_public_method const -function caml_get_public_method(obj, tag) { +export function caml_get_public_method(obj, tag) { var meths = obj[1]; var li = 3, hi = meths[1] * 2 + 1, @@ -190,45 +188,42 @@ function caml_get_public_method(obj, tag) { } //Provides: caml_oo_last_id -var caml_oo_last_id = 0; +export let caml_oo_last_id = 0; //Provides: caml_set_oo_id -//Requires: caml_oo_last_id -function caml_set_oo_id(b) { +export function caml_set_oo_id(b) { b[2] = caml_oo_last_id++; return b; } //Provides: caml_fresh_oo_id const -//Requires: caml_oo_last_id -function caml_fresh_oo_id() { +export function caml_fresh_oo_id() { return caml_oo_last_id++; } //Provides: caml_obj_raw_field -function caml_obj_raw_field(o, i) { +export function caml_obj_raw_field(o, i) { return o[i + 1]; } //Provides: caml_obj_set_raw_field -function caml_obj_set_raw_field(o, i, v) { +export function caml_obj_set_raw_field(o, i, v) { return (o[i + 1] = v); } //Provides: caml_obj_reachable_words -function caml_obj_reachable_words(_o) { +export function caml_obj_reachable_words(_o) { return 0; } //Provides: caml_obj_add_offset -//Requires: caml_failwith -function caml_obj_add_offset(_v, _offset) { +export function caml_obj_add_offset(_v, _offset) { caml_failwith("Obj.add_offset is not supported"); } //Provides: caml_obj_update_tag //Version: >= 5.0 -function caml_obj_update_tag(b, o, n) { +export function caml_obj_update_tag(b, o, n) { if (b[0] === o) { b[0] = n; return 1; @@ -237,9 +232,8 @@ function caml_obj_update_tag(b, o, n) { } //Provides: caml_lazy_update_to_forcing -//Requires: caml_obj_update_tag //Version: >= 5.0 -function caml_lazy_update_to_forcing(o) { +export function caml_lazy_update_to_forcing(o) { if ( Array.isArray(o) && o[0] === o[0] >>> 0 && @@ -252,50 +246,45 @@ function caml_lazy_update_to_forcing(o) { } //Provides: caml_lazy_update_to_forward -//Requires: caml_obj_update_tag //Version: >= 5.0 -function caml_lazy_update_to_forward(o) { +export function caml_lazy_update_to_forward(o) { caml_obj_update_tag(o, 244, 250); return 0; // unit } //Provides: caml_lazy_reset_to_lazy -//Requires: caml_obj_update_tag //Version: >= 5.0 -function caml_lazy_reset_to_lazy(o) { +export function caml_lazy_reset_to_lazy(o) { caml_obj_update_tag(o, 244, 246); return 0; } //Provides: caml_lazy_read_result -//Requires: caml_obj_tag //Version: >= 5.0 -function caml_lazy_read_result(o) { +export function caml_lazy_read_result(o) { return caml_obj_tag(o) === 250 ? o[1] : o; } //Provides: caml_is_continuation_tag //Version: < 5 -function caml_is_continuation_tag(_t) { +export function caml_is_continuation_tag(_t) { return 0; } //Provides: caml_is_continuation_tag //Version: >= 5 -function caml_is_continuation_tag(t) { +export function caml_is_continuation_tag(t) { return t === 245 ? 1 : 0; } //Provides: caml_custom_identifier -//Requires: caml_string_of_jsstring -function caml_custom_identifier(o) { +export function caml_custom_identifier(o) { return caml_string_of_jsstring(o.caml_custom); } //Provides: caml_ml_gc_ramp_up -//Requires: caml_callback //Version: >= 5.4 -function caml_ml_gc_ramp_up(f) { +export function caml_ml_gc_ramp_up(f) { var a = caml_callback(f, [0]); var suspended = 0; return [0, a, suspended]; @@ -303,6 +292,6 @@ function caml_ml_gc_ramp_up(f) { //Provides: caml_ml_gc_ramp_down //Version: >= 5.4 -function caml_ml_gc_ramp_down(_suspended_collection_work) { +export function caml_ml_gc_ramp_down(_suspended_collection_work) { return 0; } diff --git a/runtime/js/parsing.js b/runtime/js/parsing.js index 8ce9de8a8a..9478ac368f 100644 --- a/runtime/js/parsing.js +++ b/runtime/js/parsing.js @@ -17,15 +17,15 @@ /* The pushdown automata */ +import { caml_ml_output, caml_sys_fds } from './io.js'; +import { caml_lex_array } from './lexing.js'; +import { MlBytes, caml_jsbytes_of_string, caml_jsstring_of_string, caml_ml_string_length, caml_string_of_jsbytes } from './mlBytes.js'; + //Provides: caml_parser_trace -var caml_parser_trace = 0; +export let caml_parser_trace = 0; //Provides: caml_parse_engine -//Requires: caml_lex_array, caml_parser_trace,caml_jsstring_of_string -//Requires: caml_ml_output, caml_ml_string_length, caml_string_of_jsbytes -//Requires: caml_jsbytes_of_string, MlBytes -//Requires: caml_sys_fds -function caml_parse_engine(tables, env, cmd, arg) { +export function caml_parse_engine(tables, env, cmd, arg) { var ERRCODE = 256; //var START = 0; @@ -309,8 +309,7 @@ function caml_parse_engine(tables, env, cmd, arg) { } //Provides: caml_set_parser_trace -//Requires: caml_parser_trace -function caml_set_parser_trace(bool) { +export function caml_set_parser_trace(bool) { var oldflag = caml_parser_trace; caml_parser_trace = bool; return oldflag; diff --git a/runtime/js/prng.js b/runtime/js/prng.js index 0478bada3a..7018d988a6 100644 --- a/runtime/js/prng.js +++ b/runtime/js/prng.js @@ -1,32 +1,22 @@ +import { caml_ba_get_1, caml_ba_set_1 } from './bigarray.js'; +import { caml_int64_add, caml_int64_mul, caml_int64_of_string, caml_int64_or, caml_int64_shift_left, caml_int64_shift_right_unsigned, caml_int64_xor } from './int64.js'; +import { caml_string_of_jsstring } from './mlBytes.js'; + //Provides: caml_lxm_M -//Requires: caml_int64_of_string -//Requires: caml_string_of_jsstring //Version: >= 5 -var caml_lxm_M = caml_int64_of_string( +export let caml_lxm_M = caml_int64_of_string( caml_string_of_jsstring("0xd1342543de82ef95"), ); //Provides: caml_lxm_daba -//Requires: caml_int64_of_string -//Requires: caml_string_of_jsstring //Version: >= 5 -var caml_lxm_daba = caml_int64_of_string( +export let caml_lxm_daba = caml_int64_of_string( caml_string_of_jsstring("0xdaba0b6eb09322e3"), ); //Provides: caml_lxm_next mutable -//Requires: caml_int64_shift_left -//Requires: caml_int64_shift_right_unsigned -//Requires: caml_int64_or -//Requires: caml_int64_xor -//Requires: caml_int64_add -//Requires: caml_int64_mul -//Requires: caml_ba_get_1 -//Requires: caml_ba_set_1 -//Requires: caml_lxm_M -//Requires: caml_lxm_daba //Version: >= 5 -function caml_lxm_next(v) { +export function caml_lxm_next(v) { function shift_l(x, k) { return caml_int64_shift_left(x, k); } diff --git a/runtime/js/runtime_events.js b/runtime/js/runtime_events.js index 69f36b68ed..c2fa49f8d7 100644 --- a/runtime/js/runtime_events.js +++ b/runtime/js/runtime_events.js @@ -1,89 +1,88 @@ //Provides: caml_custom_event_index //Version: >= 5.1 -var caml_custom_event_index = 0; +export let caml_custom_event_index = 0; //Provides: caml_runtime_events_user_register -//Requires: caml_custom_event_index //Version: >= 5.1 -function caml_runtime_events_user_register(event_name, event_tag, event_type) { +export function caml_runtime_events_user_register(event_name, event_tag, event_type) { caml_custom_event_index += 1; return [0, caml_custom_event_index, event_name, event_type, event_tag]; } //Provides: caml_runtime_events_user_write //Version: >= 5.1 -function caml_runtime_events_user_write(_event, _event_content) { +export function caml_runtime_events_user_write(_event, _event_content) { return 0; } //Provides: caml_runtime_events_user_resolve //Version: >= 5.0 -function caml_runtime_events_user_resolve() { +export function caml_runtime_events_user_resolve() { return 0; } //Provides: caml_ml_runtime_events_start //Version: >= 5.2 -function caml_ml_runtime_events_start() { +export function caml_ml_runtime_events_start() { return 0; } //Provides: caml_runtime_events_start //Version: >= 5.0, < 5.2 -function caml_runtime_events_start() { +export function caml_runtime_events_start() { return 0; } //Provides: caml_ml_runtime_events_pause //Version: >= 5.2 -function caml_ml_runtime_events_pause() { +export function caml_ml_runtime_events_pause() { return 0; } //Provides: caml_runtime_events_pause //Version: >= 5.0, < 5.2 -function caml_runtime_events_pause() { +export function caml_runtime_events_pause() { return 0; } //Provides: caml_ml_runtime_events_are_active //Version: >= 5.2 -function caml_ml_runtime_events_are_active() { +export function caml_ml_runtime_events_are_active() { return 0; } //Provides: caml_runtime_events_resume //Version: >=5.0, < 5.2 -function caml_runtime_events_resume() { +export function caml_runtime_events_resume() { return 0; } //Provides: caml_ml_runtime_events_resume //Version: >= 5.2 -function caml_ml_runtime_events_resume() { +export function caml_ml_runtime_events_resume() { return 0; } //Provides: caml_runtime_events_create_cursor //Version: >= 5.0 -function caml_runtime_events_create_cursor(_target) { +export function caml_runtime_events_create_cursor(_target) { return {}; } //Provides: caml_runtime_events_free_cursor //Version: >= 5.0 -function caml_runtime_events_free_cursor(_cursor) { +export function caml_runtime_events_free_cursor(_cursor) { return 0; } //Provides: caml_runtime_events_read_poll //Version: >= 5.0 -function caml_runtime_events_read_poll(_cursor, _callbacks, _num) { +export function caml_runtime_events_read_poll(_cursor, _callbacks, _num) { return 0; } //Provides: caml_ml_runtime_events_path const //Version: >= 5.3 -function caml_ml_runtime_events_path(_unit) { +export function caml_ml_runtime_events_path(_unit) { return 0; } diff --git a/runtime/js/stdlib.js b/runtime/js/stdlib.js index 5bfb024119..73ed7e989c 100644 --- a/runtime/js/stdlib.js +++ b/runtime/js/stdlib.js @@ -17,10 +17,15 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +import { caml_cps_closure } from './effect.js'; +import { caml_failwith } from './fail.js'; +import { caml_callback } from './jslib.js'; +import { caml_jsbytes_of_string, caml_jsstring_of_string } from './mlBytes.js'; + //Provides: caml_call_gen (const, shallow) //If: !effects //Weakdef -function caml_call_gen(f, args) { +export function caml_call_gen(f, args) { var n = f.l >= 0 ? f.l : (f.l = f.length); var argsLen = args.length; var d = n - argsLen; @@ -66,7 +71,7 @@ function caml_call_gen(f, args) { //If: effects //If: !doubletranslate //Weakdef -function caml_call_gen(f, args) { +export function caml_call_gen(f, args) { var n = f.l >= 0 ? f.l : (f.l = f.length); var argsLen = args.length; var d = n - argsLen; @@ -122,18 +127,16 @@ function caml_call_gen(f, args) { } //Provides: caml_call_gen_cps -//Requires: caml_call_gen //If: effects //If: !doubletranslate //Weakdef -var caml_call_gen_cps = caml_call_gen; +export let caml_call_gen_cps = caml_call_gen; //Provides: caml_call_gen_tuple (const, shallow) -//Requires: caml_cps_closure //If: effects //If: doubletranslate //Weakdef -var caml_call_gen_tuple = (function () { +export let caml_call_gen_tuple = (function () { function caml_call_gen_direct(f, args) { var n = f.l >= 0 ? f.l : (f.l = f.length); var argsLen = args.length; @@ -207,41 +210,36 @@ var caml_call_gen_tuple = (function () { })(); //Provides: caml_call_gen -//Requires: caml_call_gen_tuple //If: effects //If: doubletranslate //Weakdef -var caml_call_gen = caml_call_gen_tuple[0]; +export let caml_call_gen = caml_call_gen_tuple[0]; //Provides: caml_call_gen_cps -//Requires: caml_call_gen_tuple //If: effects //If: doubletranslate //Weakdef -var caml_call_gen_cps = caml_call_gen_tuple[1]; +export let caml_call_gen_cps = caml_call_gen_tuple[1]; //Provides: caml_named_values -var caml_named_values = {}; +export let caml_named_values = {}; //Provides: caml_register_named_value (const,mutable) -//Requires: caml_named_values, caml_jsbytes_of_string -function caml_register_named_value(nm, v) { +export function caml_register_named_value(nm, v) { caml_named_values[caml_jsbytes_of_string(nm)] = v; return 0; } //Provides: caml_named_value -//Requires: caml_named_values -function caml_named_value(nm) { +export function caml_named_value(nm) { return caml_named_values[nm]; } //Provides: caml_global_data -var caml_global_data = [0]; +export let caml_global_data = [0]; //Provides: caml_build_symbols -//Requires: caml_jsstring_of_string -function caml_build_symbols(symb) { +export function caml_build_symbols(symb) { var r = {}; var max = -1; if (symb) { @@ -256,13 +254,10 @@ function caml_build_symbols(symb) { } //Provides: jsoo_toplevel_reloc -var jsoo_toplevel_reloc = undefined; +export let jsoo_toplevel_reloc = undefined; //Provides: caml_register_global (const, shallow, const) -//Requires: caml_global_data, caml_callback, caml_build_symbols -//Requires: caml_failwith -//Requires: jsoo_toplevel_reloc -function caml_register_global(n, v, name_opt) { +export function caml_register_global(n, v, name_opt) { if (name_opt) { var name = name_opt; if (jsoo_toplevel_reloc) { @@ -286,17 +281,16 @@ function caml_register_global(n, v, name_opt) { } //Provides: caml_get_global_data mutable -//Requires: caml_global_data -function caml_get_global_data(_unit) { +export function caml_get_global_data(_unit) { return caml_global_data; } //Provides: caml_is_printable const (const) -function caml_is_printable(c) { +export function caml_is_printable(c) { return +(c > 31 && c < 127); } //Provides: caml_maybe_print_stats -function caml_maybe_print_stats(_unit) { +export function caml_maybe_print_stats(_unit) { return 0; } diff --git a/runtime/js/str.js b/runtime/js/str.js index 70dc4ec5a2..ebb60285ca 100644 --- a/runtime/js/str.js +++ b/runtime/js/str.js @@ -20,9 +20,12 @@ // Based on https://github.com/ocaml/ocaml/blob/4.07/otherlibs/str/strstubs.c // Copied from https://github.com/jscoq/jscoq/blob/v8.11/coq-js/js_stub/str.js +import { caml_array_get } from './array.js'; +import { caml_failwith, caml_invalid_argument } from './fail.js'; +import { caml_js_from_array } from './jslib.js'; +import { caml_jsbytes_of_string, caml_ml_string_length, caml_string_get, caml_string_of_jsbytes, caml_uint8_array_of_string } from './mlBytes.js'; + //Provides: re_match -//Requires: caml_jsbytes_of_string, caml_js_from_array, caml_uint8_array_of_string -//Requires: caml_string_get var re_match = (function () { var re_word_letters = [ @@ -291,9 +294,7 @@ var re_match = (function () { })(); //Provides: re_search_forward -//Requires: re_match, caml_ml_string_length, caml_invalid_argument -//Requires: caml_string_get -function re_search_forward(re, s, pos) { +export function re_search_forward(re, s, pos) { if (pos < 0 || pos > caml_ml_string_length(s)) caml_invalid_argument("Str.search_forward"); var startchars = re[6] | 0; @@ -321,9 +322,7 @@ function re_search_forward(re, s, pos) { } //Provides: re_search_backward -//Requires: re_match, caml_ml_string_length, caml_invalid_argument -//Requires: caml_string_get -function re_search_backward(re, s, pos) { +export function re_search_backward(re, s, pos) { if (pos < 0 || pos > caml_ml_string_length(s)) caml_invalid_argument("Str.search_backward"); var startchars = re[6] | 0; @@ -352,8 +351,7 @@ function re_search_backward(re, s, pos) { } //Provides: re_string_match -//Requires: re_match, caml_ml_string_length, caml_invalid_argument -function re_string_match(re, s, pos) { +export function re_string_match(re, s, pos) { if (pos < 0 || pos > caml_ml_string_length(s)) caml_invalid_argument("Str.string_match"); var res = re_match(re, s, pos, 0); @@ -362,8 +360,7 @@ function re_string_match(re, s, pos) { } //Provides: re_partial_match -//Requires: re_match, caml_ml_string_length, caml_invalid_argument -function re_partial_match(re, s, pos) { +export function re_partial_match(re, s, pos) { if (pos < 0 || pos > caml_ml_string_length(s)) caml_invalid_argument("Str.partial_match"); var res = re_match(re, s, pos, 1); @@ -372,11 +369,8 @@ function re_partial_match(re, s, pos) { } //Provides: re_replacement_text -//Requires: caml_jsbytes_of_string, caml_string_of_jsbytes -//Requires: caml_array_get -//Requires: caml_failwith // external re_replacement_text: string -> int array -> string -> string -function re_replacement_text(repl, groups, orig) { +export function re_replacement_text(repl, groups, orig) { var repl = caml_jsbytes_of_string(repl); var len = repl.length; var orig = caml_jsbytes_of_string(orig); diff --git a/runtime/js/sync.js b/runtime/js/sync.js index accb6150e8..89ba18270f 100644 --- a/runtime/js/sync.js +++ b/runtime/js/sync.js @@ -1,26 +1,26 @@ +import { caml_failwith } from './fail.js'; + //Provides: MlMutex -class MlMutex { +export class MlMutex { constructor() { this.locked = false; } } //Provides: caml_ml_mutex_new -//Requires: MlMutex -function caml_ml_mutex_new(_unit) { +export function caml_ml_mutex_new(_unit) { return new MlMutex(); } //Provides: caml_ml_mutex_lock -//Requires: caml_failwith -function caml_ml_mutex_lock(t) { +export function caml_ml_mutex_lock(t) { if (t.locked) caml_failwith("Mutex.lock: mutex already locked. Cannot wait."); else t.locked = true; return 0; } //Provides: caml_ml_mutex_try_lock -function caml_ml_mutex_try_lock(t) { +export function caml_ml_mutex_try_lock(t) { if (!t.locked) { t.locked = true; return 1; @@ -29,7 +29,7 @@ function caml_ml_mutex_try_lock(t) { } //Provides: caml_ml_mutex_unlock -function caml_ml_mutex_unlock(t) { +export function caml_ml_mutex_unlock(t) { t.locked = false; return 0; } diff --git a/runtime/js/sys.js b/runtime/js/sys.js index 35679638af..8d628d166e 100644 --- a/runtime/js/sys.js +++ b/runtime/js/sys.js @@ -17,17 +17,22 @@ ///////////// Sys +import { caml_invalid_argument, caml_raise_not_found, caml_raise_with_arg } from './fail.js'; +import { jsoo_is_win32 } from './fs_node.js'; +import { resolve_fs_device } from './fs.js'; +import { caml_callback } from './jslib.js'; +import { MlBytes, caml_jsstring_of_string, caml_string_of_jsbytes, caml_string_of_jsstring } from './mlBytes.js'; +import { caml_global_data, caml_named_value } from './stdlib.js'; + //Provides: caml_raise_sys_error (const) -//Requires: caml_raise_with_arg, caml_global_data, caml_string_of_jsstring -function caml_raise_sys_error(msg) { +export function caml_raise_sys_error(msg) { caml_raise_with_arg(caml_global_data.Sys_error, caml_string_of_jsstring(msg)); } //Provides: caml_sys_exit -//Requires: caml_invalid_argument //Alias: caml_unix_exit //Alias: unix_exit -function caml_sys_exit(code) { +export function caml_sys_exit(code) { if (globalThis.quit) globalThis.quit(code); //nodejs if (globalThis.process?.exit) globalThis.process.exit(code); @@ -35,7 +40,7 @@ function caml_sys_exit(code) { } //Provides: caml_is_special_exception -function caml_is_special_exception(exn) { +export function caml_is_special_exception(exn) { switch (exn[2]) { case -8: // Match_failure case -11: // Assert_failure @@ -47,8 +52,7 @@ function caml_is_special_exception(exn) { } //Provides: caml_format_exception -//Requires: MlBytes, caml_is_special_exception -function caml_format_exception(exn) { +export function caml_format_exception(exn) { var r = ""; if (exn[0] === 0) { r += exn[1][1]; @@ -82,8 +86,7 @@ function caml_format_exception(exn) { } //Provides: caml_fatal_uncaught_exception -//Requires: caml_named_value, caml_format_exception, caml_callback -function caml_fatal_uncaught_exception(err) { +export function caml_fatal_uncaught_exception(err) { if (Array.isArray(err) && (err[0] === 0 || err[0] === 248)) { var handler = caml_named_value("Printexc.handle_uncaught_exception"); if (handler) caml_callback(handler, [err, false]); @@ -100,18 +103,16 @@ function caml_fatal_uncaught_exception(err) { } //Provides: jsoo_static_env -var jsoo_static_env = {}; +export let jsoo_static_env = {}; //Provides: caml_set_static_env -//Requires: jsoo_static_env -function caml_set_static_env(k, v) { +export function caml_set_static_env(k, v) { jsoo_static_env[k] = v; return 0; } //Provides: jsoo_sys_getenv (const) -//Requires: jsoo_static_env -function jsoo_sys_getenv(n) { +export function jsoo_sys_getenv(n) { if (jsoo_static_env[n]) return jsoo_static_env[n]; var process = globalThis.process; //nodejs env @@ -123,36 +124,27 @@ function jsoo_sys_getenv(n) { } //Provides: caml_sys_getenv (const) -//Requires: caml_raise_not_found -//Requires: caml_string_of_jsstring -//Requires: caml_jsstring_of_string -//Requires: jsoo_sys_getenv -function caml_sys_getenv(name) { +export function caml_sys_getenv(name) { var r = jsoo_sys_getenv(caml_jsstring_of_string(name)); if (r === undefined) caml_raise_not_found(); return caml_string_of_jsstring(r); } //Provides: caml_sys_getenv_opt (const) -//Requires: caml_string_of_jsstring -//Requires: caml_jsstring_of_string -//Requires: jsoo_sys_getenv //Version: >= 5.4 -function caml_sys_getenv_opt(name) { +export function caml_sys_getenv_opt(name) { var r = jsoo_sys_getenv(caml_jsstring_of_string(name)); if (r === undefined) return 0; return [0, caml_string_of_jsstring(r)]; } //Provides: caml_sys_unsafe_getenv -//Requires: caml_sys_getenv -function caml_sys_unsafe_getenv(name) { +export function caml_sys_unsafe_getenv(name) { return caml_sys_getenv(name); } //Provides: caml_argv -//Requires: caml_string_of_jsstring -var caml_argv = (function () { +export let caml_argv = (function () { var process = globalThis.process; var main = "a.out"; var args = []; @@ -172,37 +164,31 @@ var caml_argv = (function () { })(); //Provides: caml_executable_name -//Requires: caml_argv -var caml_executable_name = caml_argv[1]; +export let caml_executable_name = caml_argv[1]; //Provides: caml_sys_get_argv -//Requires: caml_argv -function caml_sys_get_argv(_unit) { +export function caml_sys_get_argv(_unit) { return [0, caml_argv[1], caml_argv]; } //Provides: caml_sys_argv -//Requires: caml_argv -function caml_sys_argv(_unit) { +export function caml_sys_argv(_unit) { return caml_argv; } //Provides: caml_sys_modify_argv -//Requires: caml_argv -function caml_sys_modify_argv(arg) { +export function caml_sys_modify_argv(arg) { caml_argv = arg; return 0; } //Provides: caml_sys_executable_name const -//Requires: caml_executable_name -function caml_sys_executable_name(_unit) { +export function caml_sys_executable_name(_unit) { return caml_executable_name; } //Provides: caml_sys_system_command -//Requires: caml_jsstring_of_string -function caml_sys_system_command(cmd) { +export function caml_sys_system_command(cmd) { var cmd = caml_jsstring_of_string(cmd); if (typeof require !== "undefined") { var child_process = require("node:child_process"); @@ -217,28 +203,26 @@ function caml_sys_system_command(cmd) { } //Provides: caml_sys_system_command -//Requires: caml_jsstring_of_string //If: browser -function caml_sys_system_command(_cmd) { +export function caml_sys_system_command(_cmd) { return 127; } //Provides: caml_sys_time mutable var caml_initial_time = new Date().getTime() * 0.001; -function caml_sys_time() { +export function caml_sys_time() { var now = new Date().getTime(); return now * 0.001 - caml_initial_time; } //Provides: caml_sys_time_include_children -//Requires: caml_sys_time -function caml_sys_time_include_children(_b) { +export function caml_sys_time_include_children(_b) { return caml_sys_time(); } //Provides: caml_sys_random_seed mutable //The function needs to return an array since OCaml 4.0... -function caml_sys_random_seed() { +export function caml_sys_random_seed() { if (globalThis.crypto) { if (globalThis.crypto.getRandomValues) { var a = globalThis.crypto.getRandomValues(new Int32Array(4)); @@ -254,130 +238,116 @@ function caml_sys_random_seed() { } //Provides: caml_sys_const_big_endian const -function caml_sys_const_big_endian() { +export function caml_sys_const_big_endian() { return 0; } //Provides: caml_sys_const_word_size const -function caml_sys_const_word_size() { +export function caml_sys_const_word_size() { return 32; } //Provides: caml_sys_const_int_size const -function caml_sys_const_int_size() { +export function caml_sys_const_int_size() { return 32; } //Provides: caml_sys_const_max_wosize const // max_int / 4 so that the following does not overflow //let max_string_length = word_size / 8 * max_array_length - 1;; -function caml_sys_const_max_wosize() { +export function caml_sys_const_max_wosize() { return (0x7fffffff / 4) | 0; } //Provides: caml_sys_const_ostype_unix const -//Requires: os_type -function caml_sys_const_ostype_unix() { +export function caml_sys_const_ostype_unix() { return os_type === "Unix" ? 1 : 0; } //Provides: caml_sys_const_ostype_win32 const -//Requires: os_type -function caml_sys_const_ostype_win32() { +export function caml_sys_const_ostype_win32() { return os_type === "Win32" ? 1 : 0; } //Provides: caml_sys_const_ostype_cygwin const -//Requires: os_type -function caml_sys_const_ostype_cygwin() { +export function caml_sys_const_ostype_cygwin() { return os_type === "Cygwin" ? 1 : 0; } //Provides: caml_sys_const_backend_type const -//Requires: caml_string_of_jsbytes -function caml_sys_const_backend_type() { +export function caml_sys_const_backend_type() { return [0, caml_string_of_jsbytes("js_of_ocaml")]; } //Provides: os_type -//Requires: jsoo_is_win32 -var os_type = jsoo_is_win32 ? "Win32" : "Unix"; +export let os_type = jsoo_is_win32 ? "Win32" : "Unix"; //Provides: caml_sys_get_config const -//Requires: caml_string_of_jsbytes, os_type -function caml_sys_get_config() { +export function caml_sys_get_config() { return [0, caml_string_of_jsbytes(os_type), 32, 0]; } //Provides: caml_sys_isatty -function caml_sys_isatty(_chan) { +export function caml_sys_isatty(_chan) { return 0; } //Provides: caml_runtime_variant -//Requires: caml_string_of_jsbytes -function caml_runtime_variant(_unit) { +export function caml_runtime_variant(_unit) { return caml_string_of_jsbytes(""); } //Provides: caml_runtime_parameters -//Requires: caml_string_of_jsbytes -function caml_runtime_parameters(_unit) { +export function caml_runtime_parameters(_unit) { return caml_string_of_jsbytes(""); } //Provides: caml_install_signal_handler const -function caml_install_signal_handler() { +export function caml_install_signal_handler() { return 0; } //Provides: caml_runtime_warnings -var caml_runtime_warnings = 0; +export let caml_runtime_warnings = 0; //Provides: caml_ml_enable_runtime_warnings -//Requires: caml_runtime_warnings -function caml_ml_enable_runtime_warnings(bool) { +export function caml_ml_enable_runtime_warnings(bool) { caml_runtime_warnings = bool; return 0; } //Provides: caml_ml_runtime_warnings_enabled -//Requires: caml_runtime_warnings -function caml_ml_runtime_warnings_enabled(_unit) { +export function caml_ml_runtime_warnings_enabled(_unit) { return caml_runtime_warnings; } //Provides: caml_sys_const_naked_pointers_checked const (const) -function caml_sys_const_naked_pointers_checked(_unit) { +export function caml_sys_const_naked_pointers_checked(_unit) { return 0; } //Provides: caml_xdg_defaults //Version: >= 5.2 -function caml_xdg_defaults(_unit) { +export function caml_xdg_defaults(_unit) { return 0; // empty list } //Provides: caml_sys_is_regular_file -//Requires: resolve_fs_device //Version: >= 5.1 -function caml_sys_is_regular_file(name) { +export function caml_sys_is_regular_file(name) { var root = resolve_fs_device(name); return root.device.isFile(root.rest); } //Provides: caml_io_buffer_size -var caml_io_buffer_size = 65536; +export let caml_io_buffer_size = 65536; //Provides: caml_sys_io_buffer_size -//Requires: caml_io_buffer_size //Version: >= 5.4 -function caml_sys_io_buffer_size(_unit) { +export function caml_sys_io_buffer_size(_unit) { return caml_io_buffer_size; } //Provides: caml_sys_temp_dir_name -//Requires: os_type -//Requires: caml_string_of_jsstring //Version: >= 5.4 -function caml_sys_temp_dir_name(_unit) { +export function caml_sys_temp_dir_name(_unit) { if (os_type === "Win32") { return caml_string_of_jsstring(require("node:os").tmpdir()); } else { @@ -386,27 +356,25 @@ function caml_sys_temp_dir_name(_unit) { } //Provides: caml_sys_temp_dir_name -//Requires: caml_string_of_jsstring //Version: >= 5.4 //If: browser -function caml_sys_temp_dir_name(_unit) { +export function caml_sys_temp_dir_name(_unit) { return caml_string_of_jsstring(""); } //Provides: caml_sys_convert_signal_number //Version: >= 5.4 -function caml_sys_convert_signal_number(signo) { +export function caml_sys_convert_signal_number(signo) { return signo; } //Provides: caml_sys_rev_convert_signal_number //Version: >= 5.4 -function caml_sys_rev_convert_signal_number(signo) { +export function caml_sys_rev_convert_signal_number(signo) { return signo; } //Always -//Requires: caml_fatal_uncaught_exception //If: !wasm function caml_setup_uncaught_exception_handler() { var process = globalThis.process; diff --git a/runtime/js/toplevel.js b/runtime/js/toplevel.js index 3b65660823..a977d9fdc5 100644 --- a/runtime/js/toplevel.js +++ b/runtime/js/toplevel.js @@ -17,28 +17,29 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +import { caml_ba_to_typed_array } from './bigarray.js'; +import { caml_failwith, caml_invalid_argument } from './fail.js'; +import { caml_callback, caml_list_of_js_array } from './jslib.js'; +import { caml_jsbytes_of_string, caml_string_of_jsbytes, caml_string_of_uint8_array, caml_uint8_array_of_bytes } from './mlBytes.js'; +import { caml_global_data, jsoo_toplevel_reloc } from './stdlib.js'; + //Provides: caml_terminfo_rows -function caml_terminfo_rows() { +export function caml_terminfo_rows() { return 0; } //Provides: caml_invoke_traced_function -//Requires: caml_invalid_argument -function caml_invoke_traced_function() { +export function caml_invoke_traced_function() { caml_invalid_argument("Meta.invoke_traced_function"); } //Provides: caml_get_current_environment -//Requires: caml_failwith -function caml_get_current_environment() { +export function caml_get_current_environment() { caml_failwith("caml_get_current_environment not Implemented"); } ////////////////////////////////////////////////////////////////////// //Provides: caml_get_section_table -//Requires: caml_global_data, caml_failwith -//Requires: caml_string_of_jsbytes, caml_jsbytes_of_string -//Requires: caml_list_of_js_array //Version: < 5.3 -function caml_get_section_table() { +export function caml_get_section_table() { if (!caml_global_data.sections) { caml_failwith("Program not compiled with --toplevel"); } @@ -65,9 +66,8 @@ function caml_get_section_table() { } //Provides: caml_dynlink_get_bytecode_sections -//Requires: caml_global_data, caml_failwith //Alias: jsoo_get_bytecode_sections -function caml_dynlink_get_bytecode_sections() { +export function caml_dynlink_get_bytecode_sections() { if (!caml_global_data.sections) { caml_failwith("Program not compiled with --toplevel"); } @@ -75,8 +75,7 @@ function caml_dynlink_get_bytecode_sections() { } //Provides: jsoo_get_runtime_aliases -//Requires: caml_global_data, caml_failwith -function jsoo_get_runtime_aliases() { +export function jsoo_get_runtime_aliases() { if (caml_global_data.aliases === undefined) { caml_failwith("Program not compiled with --toplevel"); } @@ -84,27 +83,21 @@ function jsoo_get_runtime_aliases() { } //Provides: jsoo_toplevel_compile -//Requires: caml_failwith -var jsoo_toplevel_compile = undefined; +export let jsoo_toplevel_compile = undefined; //Provides: jsoo_toplevel_init_compile -//Requires: jsoo_toplevel_compile -function jsoo_toplevel_init_compile(f) { +export function jsoo_toplevel_init_compile(f) { jsoo_toplevel_compile = f; } //Provides: jsoo_toplevel_init_reloc -//Requires: jsoo_toplevel_reloc -function jsoo_toplevel_init_reloc(f) { +export function jsoo_toplevel_init_reloc(f) { jsoo_toplevel_reloc = f; } //Provides: caml_reify_bytecode -//Requires: caml_callback -//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) { +export function caml_reify_bytecode(code, debug, _digest) { if (!jsoo_toplevel_compile) { caml_failwith("Toplevel not initialized (jsoo_toplevel_compile)"); } @@ -113,11 +106,8 @@ function caml_reify_bytecode(code, debug, _digest) { } //Provides: caml_reify_bytecode -//Requires: caml_callback -//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) { +export function caml_reify_bytecode(code, debug, _digest) { if (!jsoo_toplevel_compile) { caml_failwith("Toplevel not initialized (jsoo_toplevel_compile)"); } @@ -138,13 +128,12 @@ function caml_reify_bytecode(code, debug, _digest) { } //Provides: caml_static_release_bytecode -function caml_static_release_bytecode() { +export function caml_static_release_bytecode() { return 0; } //Provides: caml_realloc_global -//Requires: caml_global_data -function caml_realloc_global(len) { +export function caml_realloc_global(len) { if (len + 1 > caml_global_data.length) caml_global_data.length = len + 1; return 0; } diff --git a/runtime/js/transform-to-esm.mjs b/runtime/js/transform-to-esm.mjs new file mode 100644 index 0000000000..df89e36944 --- /dev/null +++ b/runtime/js/transform-to-esm.mjs @@ -0,0 +1,383 @@ +#!/usr/bin/env node +// Transform runtime/js files to ES modules +import { readFileSync, writeFileSync, readdirSync } from 'fs'; +import { join, basename } from 'path'; + +const runtimeDir = '/home/hugo/js_of_ocaml/runtime/js'; + +// Phase 1: Build provider map +// Maps: providedName -> { file, hasConditions, variants: [{conditions, lineNum}] } +const providerMap = new Map(); + +// Get all JS files +const jsFiles = readdirSync(runtimeDir) + .filter(f => f.endsWith('.js') && f !== 'index.js' && f !== 'transform-to-esm.mjs') + .sort(); + +console.log(`Found ${jsFiles.length} JS files to process`); + +// Parse a provides block to extract name and conditions +function parseProvides(lines, startIdx) { + const providesLine = lines[startIdx]; + const match = providesLine.match(/^\/\/Provides:\s*(\S+)/); + if (!match) return null; + + const name = match[1]; + const conditions = []; + + // Look for conditions (//If:, //Version:) following the provides + let i = startIdx + 1; + while (i < lines.length) { + const line = lines[i]; + if (line.startsWith('//If:')) { + const cond = line.replace('//If:', '').trim(); + conditions.push({ type: 'if', value: cond }); + } else if (line.startsWith('//Version:')) { + const cond = line.replace('//Version:', '').trim(); + conditions.push({ type: 'version', value: cond }); + } else if (line.startsWith('//Requires:') || line.startsWith('//Alias:') || line.startsWith('//Weakdef')) { + // These are metadata, continue + } else { + break; + } + i++; + } + + return { name, conditions, endMetaLine: i }; +} + +// Generate export name - keep original name even for conditional variants +// The //If: and //Version: annotations are preserved as comments for the compiler +function generateExportName(baseName, conditions) { + // Always return the base name - no suffix for conditional variants + return baseName; +} + +// Extract declaration name from a line +function extractDeclName(line) { + // Check for function declaration + const funcMatch = line.match(/^function\s+(\w+)\s*\(/); + if (funcMatch) return { type: 'function', name: funcMatch[1] }; + + // Check for class declaration + const classMatch = line.match(/^class\s+(\w+)/); + if (classMatch) return { type: 'class', name: classMatch[1] }; + + // Check for var declaration with = + const varMatch = line.match(/^var\s+(\w+)\s*=/); + if (varMatch) return { type: 'var', name: varMatch[1] }; + + // Check for var declaration without = on same line + const varDeclMatch = line.match(/^var\s+(\w+)\s*$/); + if (varDeclMatch) return { type: 'var', name: varDeclMatch[1] }; + + return null; +} + +// Check if a line is a non-metadata comment (regular comment) +function isRegularComment(line) { + if (!line.startsWith('//')) return false; + // Check if it's a metadata comment + if (line.startsWith('//Provides:') || + line.startsWith('//Requires:') || + line.startsWith('//If:') || + line.startsWith('//Version:') || + line.startsWith('//Alias:') || + line.startsWith('//Weakdef')) { + return false; + } + return true; +} + +// First pass: scan all files to build provider map +for (const file of jsFiles) { + const filePath = join(runtimeDir, file); + const content = readFileSync(filePath, 'utf-8'); + const lines = content.split('\n'); + + for (let i = 0; i < lines.length; i++) { + if (lines[i].startsWith('//Provides:')) { + const parsed = parseProvides(lines, i); + if (parsed) { + const { name, conditions } = parsed; + + if (!providerMap.has(name)) { + providerMap.set(name, { + file, + hasConditions: conditions.length > 0, + variants: [] + }); + } + + const entry = providerMap.get(name); + entry.variants.push({ conditions, lineNum: i }); + + // If this file differs from recorded, or new conditions found, mark as having conditions + if (entry.file !== file || conditions.length > 0) { + entry.hasConditions = true; + } + } + } + } +} + +console.log(`Built provider map with ${providerMap.size} entries`); + +// Check for providers with multiple variants +let multiVariantCount = 0; +for (const [name, info] of providerMap) { + if (info.variants.length > 1) { + multiVariantCount++; + console.log(` Multi-variant: ${name} (${info.variants.length} variants in ${info.file})`); + } +} +console.log(`Found ${multiVariantCount} multi-variant providers`); + +// Phase 2: Transform each file +for (const file of jsFiles) { + const filePath = join(runtimeDir, file); + const content = readFileSync(filePath, 'utf-8'); + const lines = content.split('\n'); + + // Collect all requires for this file + const requires = new Set(); + // Collect provides in this file (to skip self-requires) + const providesInFile = new Set(); + + // First pass: identify all provides and requires + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; + + if (line.startsWith('//Provides:')) { + const parsed = parseProvides(lines, i); + if (parsed) { + providesInFile.add(parsed.name); + } + } + + if (line.startsWith('//Requires:')) { + const reqMatch = line.match(/^\/\/Requires:\s*(.+)$/); + if (reqMatch) { + const deps = reqMatch[1].split(',').map(d => d.trim()); + for (const dep of deps) { + requires.add(dep); + } + } + } + } + + // Remove self-requires + for (const name of providesInFile) { + requires.delete(name); + } + + // Build import statements grouped by source file + const importsByFile = new Map(); + for (const req of requires) { + const provider = providerMap.get(req); + if (!provider) { + console.warn(`Warning: ${file} requires unknown '${req}'`); + continue; + } + const sourceFile = provider.file; + if (sourceFile === file) continue; // Same file, skip + + if (!importsByFile.has(sourceFile)) { + importsByFile.set(sourceFile, []); + } + importsByFile.get(sourceFile).push(req); + } + + // Generate import statements + const imports = []; + for (const [sourceFile, deps] of Array.from(importsByFile).sort((a, b) => a[0].localeCompare(b[0]))) { + const sortedDeps = deps.sort(); + imports.push(`import { ${sortedDeps.join(', ')} } from './${sourceFile}';`); + } + + // Find insert position for imports: + // - After license header (block of // comments at the start) + // - Before the first //Provides: block + let insertPos = 0; + + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; + + // We're done with header when we see first //Provides: + if (line.startsWith('//Provides:')) { + insertPos = i; + break; + } + + // Continue past license header (// comments and blank lines) + if (line.startsWith('//') || line.trim() === '') { + insertPos = i + 1; + } + } + + // Second pass: transform the file + const newLines = []; + + // Add lines before insert position + for (let i = 0; i < insertPos; i++) { + newLines.push(lines[i]); + } + + // Add imports + if (imports.length > 0) { + newLines.push(...imports); + newLines.push(''); + } + + // Track which lines we've already processed (for finding function declarations) + const processedLines = new Set(); + + // Process remaining lines + let i = insertPos; + while (i < lines.length) { + const line = lines[i]; + + // Skip if already processed + if (processedLines.has(i)) { + i++; + continue; + } + + // Handle //Provides: block + if (line.startsWith('//Provides:')) { + const parsed = parseProvides(lines, i); + if (parsed) { + const { name: providedName, conditions, endMetaLine } = parsed; + const exportName = generateExportName(providedName, conditions); + const needsRename = exportName !== providedName; + + // Copy the //Provides: line as-is (keeping metadata) + newLines.push(line); + i++; + + // Copy all metadata lines (//If:, //Version:, //Alias:, //Weakdef) + // But delete //Requires: lines + while (i < lines.length) { + const metaLine = lines[i]; + if (metaLine.startsWith('//If:') || + metaLine.startsWith('//Version:') || + metaLine.startsWith('//Alias:') || + metaLine.startsWith('//Weakdef')) { + newLines.push(metaLine); + i++; + } else if (metaLine.startsWith('//Requires:')) { + // Skip/delete requires line + i++; + } else { + break; + } + } + + // Skip any regular comment lines (non-metadata comments between metadata and declaration) + while (i < lines.length && isRegularComment(lines[i])) { + newLines.push(lines[i]); + i++; + } + + // Now find the actual declaration that matches the provided name + // It might be on the immediate next line or we might need to look ahead + if (i < lines.length) { + const declLine = lines[i]; + const declInfo = extractDeclName(declLine); + + // Check if this declaration matches the provided name + if (declInfo && declInfo.name === providedName) { + // Direct match - apply export + if (needsRename) { + if (declInfo.type === 'function') { + newLines.push(declLine.replace(`function ${providedName}`, `export function ${exportName}`)); + } else if (declInfo.type === 'class') { + newLines.push(declLine.replace(`class ${providedName}`, `export class ${exportName}`)); + } else { + newLines.push(declLine.replace(`var ${providedName}`, `export let ${exportName}`)); + } + } else { + // Replace var with let for exports + const exportLine = declInfo.type === 'var' + ? declLine.replace(/^var\s+/, 'let ') + : declLine; + newLines.push(`export ${exportLine}`); + } + processedLines.add(i); + i++; + } else if (declInfo && declInfo.name !== providedName) { + // The declaration doesn't match - it might be a helper variable + // Output as-is and look for the matching function + newLines.push(declLine); + processedLines.add(i); + i++; + + // Search forward for the matching function + while (i < lines.length && !lines[i].startsWith('//Provides:')) { + const searchLine = lines[i]; + const searchDeclInfo = extractDeclName(searchLine); + + if (searchDeclInfo && searchDeclInfo.name === providedName) { + // Found it! + if (needsRename) { + if (searchDeclInfo.type === 'function') { + newLines.push(searchLine.replace(`function ${providedName}`, `export function ${exportName}`)); + } else if (searchDeclInfo.type === 'class') { + newLines.push(searchLine.replace(`class ${providedName}`, `export class ${exportName}`)); + } else { + newLines.push(searchLine.replace(`var ${providedName}`, `export let ${exportName}`)); + } + } else { + // Replace var with let for exports + const exportLine = searchDeclInfo.type === 'var' + ? searchLine.replace(/^var\s+/, 'let ') + : searchLine; + newLines.push(`export ${exportLine}`); + } + processedLines.add(i); + i++; + break; + } else { + // Not the matching declaration, copy as-is + newLines.push(searchLine); + processedLines.add(i); + i++; + } + } + } else { + // No declaration info - just copy the line + newLines.push(declLine); + i++; + } + } + continue; + } + } + + // Skip standalone //Requires: lines (should not exist outside of //Provides blocks, but just in case) + if (line.startsWith('//Requires:')) { + i++; + continue; + } + + newLines.push(line); + i++; + } + + // Write transformed file + const newContent = newLines.join('\n'); + writeFileSync(filePath, newContent); + console.log(`Transformed: ${file}`); +} + +// Phase 3: Create index.js +const indexLines = ['// Auto-generated index file for js_of_ocaml runtime']; +for (const file of jsFiles) { + indexLines.push(`export * from './${file}';`); +} +indexLines.push(''); + +writeFileSync(join(runtimeDir, 'index.js'), indexLines.join('\n')); +console.log('Created index.js'); + +console.log('Done!'); diff --git a/runtime/js/unix.js b/runtime/js/unix.js index 0a0581abdb..915482a719 100644 --- a/runtime/js/unix.js +++ b/runtime/js/unix.js @@ -1,20 +1,28 @@ +import { caml_ba_to_typed_array } from './bigarray.js'; +import { caml_failwith, caml_raise_end_of_file, caml_raise_not_found, caml_raise_with_args } from './fail.js'; +import { fs_node_supported } from './fs_node.js'; +import { caml_sys_chdir, resolve_fs_device } from './fs.js'; +import { caml_int64_to_float } from './int64.js'; +import { MlChanid, caml_ml_open_descriptor_in, caml_ml_open_descriptor_out, caml_sys_fds } from './io.js'; +import { caml_jsstring_of_string, caml_string_of_jsstring, caml_uint8_array_of_bytes } from './mlBytes.js'; +import { caml_named_value } from './stdlib.js'; +import { caml_raise_sys_error } from './sys.js'; + //Provides: caml_unix_gettimeofday //Alias: unix_gettimeofday -function caml_unix_gettimeofday() { +export function caml_unix_gettimeofday() { return new Date().getTime() / 1000; } //Provides: caml_unix_time -//Requires: caml_unix_gettimeofday //Alias: unix_time -function caml_unix_time() { +export function caml_unix_time() { return Math.floor(caml_unix_gettimeofday()); } //Provides: caml_unix_times -//Requires: caml_failwith //Alias: unix_times -function caml_unix_times() { +export function caml_unix_times() { if (globalThis.process?.cpuUsage) { var t = globalThis.process.cpuUsage(); return BLOCK(0, t.user / 1e6, t.system / 1e6, 0, 0); @@ -27,7 +35,7 @@ function caml_unix_times() { //Provides: caml_unix_gmtime //Alias: unix_gmtime -function caml_unix_gmtime(t) { +export function caml_unix_gmtime(t) { var d = new Date(t * 1000); var d_num = d.getTime(); var januaryfirst = new Date(Date.UTC(d.getUTCFullYear(), 0, 1)).getTime(); @@ -48,7 +56,7 @@ function caml_unix_gmtime(t) { //Provides: caml_unix_localtime //Alias: unix_localtime -function caml_unix_localtime(t) { +export function caml_unix_localtime(t) { var d = new Date(t * 1000); var d_num = d.getTime(); var januaryfirst = new Date(d.getFullYear(), 0, 1).getTime(); @@ -75,9 +83,8 @@ function caml_unix_localtime(t) { } //Provides: caml_unix_mktime -//Requires: caml_unix_localtime //Alias: unix_mktime -function caml_unix_mktime(tm) { +export function caml_unix_mktime(tm) { var d = new Date(tm[6] + 1900, tm[5], tm[4], tm[3], tm[2], tm[1]).getTime(); var t = Math.floor(d / 1000); var tm2 = caml_unix_localtime(t); @@ -85,22 +92,21 @@ function caml_unix_mktime(tm) { } //Provides: caml_unix_startup const //Alias: win_startup -function caml_unix_startup() {} +export function caml_unix_startup() {} //Provides: caml_unix_cleanup const //Alias: win_cleanup -function caml_unix_cleanup() {} +export function caml_unix_cleanup() {} //Provides: caml_unix_filedescr_of_fd const //Alias: win_handle_fd -function caml_unix_filedescr_of_fd(x) { +export function caml_unix_filedescr_of_fd(x) { return x; } //Provides: caml_unix_isatty -//Requires: caml_unix_lookup_file //Alias: unix_isatty -function caml_unix_isatty(fd) { +export function caml_unix_isatty(fd) { var file = caml_unix_lookup_file(fd); if (!file.isatty) return 0; return file.isatty(); @@ -109,12 +115,12 @@ function caml_unix_isatty(fd) { //Provides: caml_unix_isatty //Alias: unix_isatty //If: browser -function caml_unix_isatty(_fileDescriptor) { +export function caml_unix_isatty(_fileDescriptor) { return 0; } //Provides: unix_error -var unix_error = [ +export let unix_error = [ /* ===Unix.error=== * * This array is in order of the variant in OCaml @@ -190,8 +196,7 @@ var unix_error = [ ]; //Provides: make_unix_err_args -//Requires: unix_error, caml_string_of_jsstring -function make_unix_err_args(code, syscall, path, errno) { +export function make_unix_err_args(code, syscall, path, errno) { var variant = unix_error.indexOf(code); if (variant < 0) { // Default if undefined @@ -211,8 +216,7 @@ function make_unix_err_args(code, syscall, path, errno) { } //Provides: caml_strerror -//Requires: unix_error -function caml_strerror(errno) { +export function caml_strerror(errno) { const util = require("node:util"); if (errno >= 0) { const code = unix_error[errno]; @@ -226,32 +230,28 @@ function caml_strerror(errno) { } //Provides: caml_strerror -//Requires: unix_error //If: browser -function caml_strerror(errno) { +export function caml_strerror(errno) { const code = unix_error[errno]; return code || "Unknown error " + errno; } //Provides: unix_error_message //Alias: caml_unix_error_message -//Requires: caml_strerror, caml_string_of_jsstring -function unix_error_message(err) { +export function unix_error_message(err) { const errno = typeof err === "number" ? err : -err[1]; return caml_string_of_jsstring(caml_strerror(errno)); } //Provides: caml_unix_chdir -//Requires: caml_sys_chdir //Alias: unix_chdir -function caml_unix_chdir(dir) { +export function caml_unix_chdir(dir) { return caml_sys_chdir(dir, /* raise Unix_error */ true); } //Provides: caml_unix_stat -//Requires: resolve_fs_device, caml_failwith //Alias: unix_stat -function caml_unix_stat(name) { +export function caml_unix_stat(name) { var root = resolve_fs_device(name); if (!root.device.stat) { caml_failwith("caml_unix_stat: not implemented"); @@ -264,9 +264,8 @@ function caml_unix_stat(name) { } //Provides: caml_unix_stat_64 -//Requires: resolve_fs_device, caml_failwith //Alias: unix_stat_64 -function caml_unix_stat_64(name) { +export function caml_unix_stat_64(name) { var root = resolve_fs_device(name); if (!root.device.stat) { caml_failwith("caml_unix_stat_64: not implemented"); @@ -279,9 +278,8 @@ function caml_unix_stat_64(name) { } //Provides: caml_unix_lstat -//Requires: resolve_fs_device, caml_failwith //Alias: unix_lstat -function caml_unix_lstat(name) { +export function caml_unix_lstat(name) { var root = resolve_fs_device(name); if (!root.device.lstat) { caml_failwith("caml_unix_lstat: not implemented"); @@ -294,9 +292,8 @@ function caml_unix_lstat(name) { } //Provides: caml_unix_lstat_64 -//Requires: resolve_fs_device, caml_failwith //Alias: unix_lstat_64 -function caml_unix_lstat_64(name) { +export function caml_unix_lstat_64(name) { var root = resolve_fs_device(name); if (!root.device.lstat) { caml_failwith("caml_unix_lstat_64: not implemented"); @@ -309,9 +306,8 @@ function caml_unix_lstat_64(name) { } //Provides: caml_unix_chmod -//Requires: resolve_fs_device, caml_failwith //Alias: unix_chmod -function caml_unix_chmod(name, perms) { +export function caml_unix_chmod(name, perms) { var root = resolve_fs_device(name); if (!root.device.chmod) { caml_failwith("caml_unix_chmod: not implemented"); @@ -320,10 +316,8 @@ function caml_unix_chmod(name, perms) { } //Provides: caml_unix_rename -//Requires: caml_failwith, resolve_fs_device -//Requires: caml_raise_system_error //Alias: unix_rename -function caml_unix_rename(o, n) { +export function caml_unix_rename(o, n) { var o_root = resolve_fs_device(o); var n_root = resolve_fs_device(n); if (o_root.device !== n_root.device) @@ -333,9 +327,8 @@ function caml_unix_rename(o, n) { } //Provides: caml_unix_mkdir -//Requires: resolve_fs_device, caml_failwith //Alias: unix_mkdir -function caml_unix_mkdir(name, perm) { +export function caml_unix_mkdir(name, perm) { var root = resolve_fs_device(name); if (!root.device.mkdir) { caml_failwith("caml_unix_mkdir: not implemented"); @@ -344,9 +337,8 @@ function caml_unix_mkdir(name, perm) { } //Provides: caml_unix_rmdir -//Requires: resolve_fs_device, caml_failwith //Alias: unix_rmdir -function caml_unix_rmdir(name) { +export function caml_unix_rmdir(name) { var root = resolve_fs_device(name); if (!root.device.rmdir) { caml_failwith("caml_unix_rmdir: not implemented"); @@ -355,9 +347,8 @@ function caml_unix_rmdir(name) { } //Provides: caml_unix_link -//Requires: resolve_fs_device, caml_failwith, caml_raise_system_error //Alias: unix_link -function caml_unix_link(follow, src, dst) { +export function caml_unix_link(follow, src, dst) { var src_root = resolve_fs_device(src); var dst_root = resolve_fs_device(dst); if (!src_root.device.link) { @@ -377,9 +368,8 @@ function caml_unix_link(follow, src, dst) { } //Provides: caml_unix_symlink -//Requires: resolve_fs_device, caml_failwith, caml_jsstring_of_string //Alias: unix_symlink -function caml_unix_symlink(to_dir, src, dst) { +export function caml_unix_symlink(to_dir, src, dst) { var dst_root = resolve_fs_device(dst); if (!dst_root.device.symlink) { caml_failwith("caml_unix_symlink: not implemented"); @@ -393,9 +383,8 @@ function caml_unix_symlink(to_dir, src, dst) { } //Provides: caml_unix_readlink -//Requires: resolve_fs_device, caml_failwith //Alias: unix_readlink -function caml_unix_readlink(name) { +export function caml_unix_readlink(name) { var root = resolve_fs_device(name); if (!root.device.readlink) { caml_failwith("caml_unix_readlink: not implemented"); @@ -404,9 +393,8 @@ function caml_unix_readlink(name) { } //Provides: caml_unix_unlink -//Requires: resolve_fs_device, caml_failwith //Alias: unix_unlink -function caml_unix_unlink(name) { +export function caml_unix_unlink(name) { var root = resolve_fs_device(name); if (!root.device.unlink) { caml_failwith("caml_unix_unlink: not implemented"); @@ -416,9 +404,8 @@ function caml_unix_unlink(name) { } //Provides: caml_unix_utimes -//Requires: resolve_fs_device, caml_failwith //Alias: unix_utimes -function caml_unix_utimes(name, atime, mtime) { +export function caml_unix_utimes(name, atime, mtime) { var root = resolve_fs_device(name); if (!root.device.utimes) { caml_failwith("caml_unix_utimes: not implemented"); @@ -428,9 +415,8 @@ function caml_unix_utimes(name, atime, mtime) { } //Provides: caml_unix_truncate -//Requires: resolve_fs_device, caml_failwith //Alias: unix_truncate -function caml_unix_truncate(name, len) { +export function caml_unix_truncate(name, len) { var root = resolve_fs_device(name); if (!root.device.truncate) { caml_failwith("caml_unix_truncate: not implemented"); @@ -440,9 +426,8 @@ function caml_unix_truncate(name, len) { } //Provides: caml_unix_truncate_64 -//Requires: resolve_fs_device, caml_failwith, caml_int64_to_float //Alias: unix_truncate_64 -function caml_unix_truncate_64(name, len) { +export function caml_unix_truncate_64(name, len) { var root = resolve_fs_device(name); if (!root.device.truncate) { caml_failwith("caml_unix_truncate_64: not implemented"); @@ -456,9 +441,8 @@ function caml_unix_truncate_64(name, len) { } //Provides: caml_unix_access -//Requires: resolve_fs_device, caml_failwith //Alias: unix_access -function caml_unix_access(name, flags) { +export function caml_unix_access(name, flags) { var f = {}; while (flags) { switch (flags[1]) { @@ -486,9 +470,8 @@ function caml_unix_access(name, flags) { } //Provides: caml_unix_open -//Requires: resolve_fs_device, caml_sys_fds, MlChanid //Alias: unix_open -function caml_unix_open(name, flags, perms) { +export function caml_unix_open(name, flags, perms) { var f = {}; while (flags) { switch (flags[1]) { @@ -537,8 +520,7 @@ function caml_unix_open(name, flags, perms) { } //Provides: caml_unix_lookup_file -//Requires: caml_sys_fds, caml_raise_system_error -function caml_unix_lookup_file(fd, cmd) { +export function caml_unix_lookup_file(fd, cmd) { var fd_desc = caml_sys_fds[fd]; if (fd_desc === undefined) caml_raise_system_error(/* raise Unix_error */ 1, "EBADF", cmd); @@ -547,8 +529,7 @@ function caml_unix_lookup_file(fd, cmd) { //Provides: caml_unix_fstat //Alias: unix_fstat -//Requires: caml_unix_lookup_file, caml_failwith -function caml_unix_fstat(fd) { +export function caml_unix_fstat(fd) { var file = caml_unix_lookup_file(fd, "fstat"); if (!file.stat) { caml_failwith("caml_unix_fstat: not implemented"); @@ -558,8 +539,7 @@ function caml_unix_fstat(fd) { //Provides: caml_unix_fstat_64 //Alias: unix_fstat_64 -//Requires: caml_unix_lookup_file, caml_failwith -function caml_unix_fstat_64(fd) { +export function caml_unix_fstat_64(fd) { var file = caml_unix_lookup_file(fd, "fstat"); if (!file.stat) { caml_failwith("caml_unix_fstat64: not implemented"); @@ -569,8 +549,7 @@ function caml_unix_fstat_64(fd) { //Provides: caml_unix_fchmod //Alias: unix_fchmod -//Requires: caml_unix_lookup_file, caml_failwith -function caml_unix_fchmod(fd, perms) { +export function caml_unix_fchmod(fd, perms) { var file = caml_unix_lookup_file(fd, "fchmod"); if (!file.chmod) { caml_failwith("caml_unix_fchmod: not implemented"); @@ -580,8 +559,7 @@ function caml_unix_fchmod(fd, perms) { //Provides: caml_unix_fsync //Alias: unix_fsync -//Requires: caml_unix_lookup_file, caml_failwith -function caml_unix_fsync(fd) { +export function caml_unix_fsync(fd) { var file = caml_unix_lookup_file(fd, "fsync"); if (!file.sync) { caml_failwith("caml_unix_fsync: not implemented"); @@ -591,8 +569,7 @@ function caml_unix_fsync(fd) { //Provides: caml_unix_write //Alias: unix_write -//Requires: caml_unix_lookup_file, caml_uint8_array_of_bytes -function caml_unix_write(fd, buf, pos, len) { +export function caml_unix_write(fd, buf, pos, len) { var file = caml_unix_lookup_file(fd, "write"); var a = caml_uint8_array_of_bytes(buf); var written = 0; @@ -607,8 +584,7 @@ function caml_unix_write(fd, buf, pos, len) { //Provides: caml_unix_single_write //Alias: unix_single_write -//Requires: caml_unix_lookup_file, caml_uint8_array_of_bytes -function caml_unix_single_write(fd, buf, pos, len) { +export function caml_unix_single_write(fd, buf, pos, len) { var file = caml_unix_lookup_file(fd, "write"); if (len === 0) return 0; return file.write( @@ -621,9 +597,8 @@ function caml_unix_single_write(fd, buf, pos, len) { //Provides: caml_unix_write_bigarray //Alias: caml_unix_lookup_file -//Requires: caml_ba_to_typed_array, caml_unix_lookup_file //Version: >= 5.2 -function caml_unix_write_bigarray(fd, buf, pos, len) { +export function caml_unix_write_bigarray(fd, buf, pos, len) { var a = caml_ba_to_typed_array(buf); var file = caml_unix_lookup_file(fd, "write"); var written = 0; @@ -638,8 +613,7 @@ function caml_unix_write_bigarray(fd, buf, pos, len) { //Provides: caml_unix_read //Alias: unix_read -//Requires: caml_unix_lookup_file, caml_uint8_array_of_bytes -function caml_unix_read(fd, buf, pos, len) { +export function caml_unix_read(fd, buf, pos, len) { var file = caml_unix_lookup_file(fd, "read"); return file.read( caml_uint8_array_of_bytes(buf), @@ -651,9 +625,8 @@ function caml_unix_read(fd, buf, pos, len) { //Provides: caml_unix_read_bigarray //Alias: unix_read_bigarray -//Requires: caml_ba_to_typed_array, caml_unix_lookup_file //Version: >= 5.2 -function caml_unix_read_bigarray(fd, buf, pos, len) { +export function caml_unix_read_bigarray(fd, buf, pos, len) { var a = caml_ba_to_typed_array(buf); var file = caml_unix_lookup_file(fd, "read"); return file.read(a, pos, len, /* raise unix_error */ 1); @@ -661,24 +634,21 @@ function caml_unix_read_bigarray(fd, buf, pos, len) { //Provides: caml_unix_lseek //Alias: unix_lseek -//Requires: caml_unix_lookup_file -function caml_unix_lseek(fd, len, whence) { +export function caml_unix_lseek(fd, len, whence) { var file = caml_unix_lookup_file(fd, "lseek"); return file.seek(len, whence, /* raise unix_error */ 1); } //Provides: caml_unix_lseek_64 //Alias: unix_lseek_64 -//Requires: caml_unix_lookup_file, caml_int64_to_float -function caml_unix_lseek_64(fd, len, whence) { +export function caml_unix_lseek_64(fd, len, whence) { var file = caml_unix_lookup_file(fd, "lseek"); return file.seek(caml_int64_to_float(len), whence, /* raise unix_error */ 1); } //Provides: caml_unix_ftruncate //Alias: unix_ftruncate -//Requires: caml_unix_lookup_file, caml_failwith -function caml_unix_ftruncate(fd, len) { +export function caml_unix_ftruncate(fd, len) { var file = caml_unix_lookup_file(fd, "ftruncate"); if (!file.truncate) { caml_failwith("caml_unix_ftruncate: not implemented"); @@ -689,8 +659,7 @@ function caml_unix_ftruncate(fd, len) { //Provides: caml_unix_ftruncate_64 //Alias: unix_ftruncate_64 -//Requires: caml_unix_lookup_file, caml_failwith, caml_int64_to_float -function caml_unix_ftruncate_64(fd, len) { +export function caml_unix_ftruncate_64(fd, len) { var file = caml_unix_lookup_file(fd, "ftruncate"); if (!file.truncate) { caml_failwith("caml_unix_ftruncate_64: not implemented"); @@ -701,8 +670,7 @@ function caml_unix_ftruncate_64(fd, len) { //Provides: caml_unix_close //Alias: unix_close -//Requires: caml_unix_lookup_file -function caml_unix_close(fd) { +export function caml_unix_close(fd) { var file = caml_unix_lookup_file(fd, "close"); file.close(/* raise unix_error */ 1); return 0; @@ -711,8 +679,7 @@ function caml_unix_close(fd) { //Provides: caml_unix_inchannel_of_filedescr //Alias: unix_inchannel_of_filedescr //Alias: win_inchannel_of_filedescr -//Requires: caml_unix_lookup_file, caml_ml_open_descriptor_in -function caml_unix_inchannel_of_filedescr(fd) { +export function caml_unix_inchannel_of_filedescr(fd) { var file = caml_unix_lookup_file(fd, "in_channel_of_descr"); file.check_stream_semantics("in_channel_of_descr"); return caml_ml_open_descriptor_in(fd); @@ -721,8 +688,7 @@ function caml_unix_inchannel_of_filedescr(fd) { //Provides: caml_unix_outchannel_of_filedescr //Alias: unix_outchannel_of_filedescr //Alias: win_outchannel_of_filedescr -//Requires: caml_unix_lookup_file, caml_ml_open_descriptor_out -function caml_unix_outchannel_of_filedescr(fd) { +export function caml_unix_outchannel_of_filedescr(fd) { var file = caml_unix_lookup_file(fd, "out_channel_of_descr"); file.check_stream_semantics("out_channel_of_descr"); return caml_ml_open_descriptor_out(fd); @@ -730,7 +696,7 @@ function caml_unix_outchannel_of_filedescr(fd) { //Provides: caml_unix_getuid //Alias: unix_getuid -function caml_unix_getuid(_unit) { +export function caml_unix_getuid(_unit) { if (globalThis.process?.getuid) { return globalThis.process.getuid(); } @@ -739,7 +705,7 @@ function caml_unix_getuid(_unit) { //Provides: caml_unix_geteuid //Alias: unix_geteuid -function caml_unix_geteuid(_unit) { +export function caml_unix_geteuid(_unit) { if (globalThis.process?.geteuid) { return globalThis.process.geteuid(); } @@ -748,7 +714,7 @@ function caml_unix_geteuid(_unit) { //Provides: caml_unix_getgid //Alias: unix_getgid -function caml_unix_getgid(_unit) { +export function caml_unix_getgid(_unit) { if (globalThis.process?.getgid) { return globalThis.process.getgid(); } @@ -757,7 +723,7 @@ function caml_unix_getgid(_unit) { //Provides: caml_unix_getegid //Alias: unix_getegid -function caml_unix_getegid(_unit) { +export function caml_unix_getegid(_unit) { if (globalThis.process?.getegid) { return globalThis.process.getegid(); } @@ -765,7 +731,6 @@ function caml_unix_getegid(_unit) { } //Provides: caml_unix_getpwnam -//Requires: caml_raise_not_found //Alias: unix_getpwnam //Alias: caml_unix_getpwuid //Alias: unix_getpwuid @@ -773,21 +738,19 @@ function caml_unix_getegid(_unit) { //Alias: unix_getgrnam //Alias: caml_unix_getgrgid //Alias: unix_getgrgid -function caml_unix_getpwnam(_unit) { +export function caml_unix_getpwnam(_unit) { caml_raise_not_found(); } //Provides: caml_unix_has_symlink -//Requires: fs_node_supported //Alias: unix_has_symlink -function caml_unix_has_symlink(_unit) { +export function caml_unix_has_symlink(_unit) { return fs_node_supported() ? 1 : 0; } //Provides: caml_unix_opendir -//Requires: resolve_fs_device, caml_failwith //Alias: unix_opendir -function caml_unix_opendir(path) { +export function caml_unix_opendir(path) { var root = resolve_fs_device(path); if (!root.device.opendir) { caml_failwith("caml_unix_opendir: not implemented"); @@ -797,11 +760,8 @@ function caml_unix_opendir(path) { } //Provides: caml_unix_readdir -//Requires: caml_raise_end_of_file -//Requires: caml_string_of_jsstring -//Requires: caml_raise_system_error //Alias: unix_readdir -function caml_unix_readdir(dir_handle) { +export function caml_unix_readdir(dir_handle) { var entry; try { entry = dir_handle.pointer.readSync(); @@ -816,9 +776,8 @@ function caml_unix_readdir(dir_handle) { } //Provides: caml_unix_closedir -//Requires: caml_raise_system_error //Alias: unix_closedir -function caml_unix_closedir(dir_handle) { +export function caml_unix_closedir(dir_handle) { try { dir_handle.pointer.closeSync(); } catch (e) { @@ -827,9 +786,8 @@ function caml_unix_closedir(dir_handle) { } //Provides: caml_unix_rewinddir -//Requires: caml_unix_closedir, caml_unix_opendir //Alias: unix_rewinddir -function caml_unix_rewinddir(dir_handle) { +export function caml_unix_rewinddir(dir_handle) { caml_unix_closedir(dir_handle); var new_dir_handle = caml_unix_opendir(dir_handle.path); dir_handle.pointer = new_dir_handle.pointer; @@ -837,10 +795,8 @@ function caml_unix_rewinddir(dir_handle) { } //Provides: caml_unix_findfirst -//Requires: caml_jsstring_of_string, caml_string_of_jsstring -//Requires: caml_unix_opendir, caml_unix_readdir //Alias: win_findfirst -function caml_unix_findfirst(path) { +export function caml_unix_findfirst(path) { // The Windows code adds this glob to the path, so we need to remove it var path_js = caml_jsstring_of_string(path); path_js = path_js.replace(/(^|[\\/])\*\.\*$/, ""); @@ -853,29 +809,25 @@ function caml_unix_findfirst(path) { } //Provides: caml_unix_findnext -//Requires: caml_unix_readdir //Alias: win_findnext -function caml_unix_findnext(dir_handle) { +export function caml_unix_findnext(dir_handle) { return caml_unix_readdir(dir_handle); } //Provides: caml_unix_findclose -//Requires: caml_unix_closedir //Alias: win_findclose -function caml_unix_findclose(dir_handle) { +export function caml_unix_findclose(dir_handle) { return caml_unix_closedir(dir_handle); } //Provides: caml_unix_inet_addr_of_string const //Alias: unix_inet_addr_of_string -function caml_unix_inet_addr_of_string() { +export function caml_unix_inet_addr_of_string() { return 0; } //Provides: caml_raise_system_error -//Requires: caml_raise_with_args, make_unix_err_args, caml_named_value -//Requires: caml_raise_sys_error -function caml_raise_system_error(raise_unix, code, cmd, msg, path) { +export function caml_raise_system_error(raise_unix, code, cmd, msg, path) { var unix_error = caml_named_value("Unix.Unix_error"); if (raise_unix && unix_error) caml_raise_with_args(unix_error, make_unix_err_args(code, cmd, path)); diff --git a/runtime/js/weak.js b/runtime/js/weak.js index 364d002b81..652af140bd 100644 --- a/runtime/js/weak.js +++ b/runtime/js/weak.js @@ -19,20 +19,20 @@ // Weak API +import { caml_array_blit } from './array.js'; +import { caml_obj_dup } from './obj.js'; + //Provides: caml_ephe_key_offset -var caml_ephe_key_offset = 3; +export let caml_ephe_key_offset = 3; //Provides: caml_ephe_data_offset -var caml_ephe_data_offset = 2; +export let caml_ephe_data_offset = 2; //Provides: caml_ephe_none -var caml_ephe_none = { caml_ephe_none: 0 }; +export let caml_ephe_none = { caml_ephe_none: 0 }; //Provides: caml_ephe_set_key -//Requires: caml_ephe_key_offset -//Requires: caml_ephe_get_data -//Requires: caml_ephe_set_data_opt -function caml_ephe_set_key(x, i, v) { +export function caml_ephe_set_key(x, i, v) { var old = caml_ephe_get_data(x); if (globalThis.WeakRef && v instanceof Object) v = new globalThis.WeakRef(v); x[caml_ephe_key_offset + i] = v; @@ -41,11 +41,7 @@ function caml_ephe_set_key(x, i, v) { } //Provides: caml_ephe_unset_key -//Requires: caml_ephe_key_offset -//Requires: caml_ephe_get_data -//Requires: caml_ephe_set_data_opt -//Requires: caml_ephe_none -function caml_ephe_unset_key(x, i) { +export function caml_ephe_unset_key(x, i) { var old = caml_ephe_get_data(x); x[caml_ephe_key_offset + i] = caml_ephe_none; caml_ephe_set_data_opt(x, old); @@ -53,15 +49,12 @@ function caml_ephe_unset_key(x, i) { } //Provides: caml_ephe_create -//Requires: caml_weak_create -function caml_ephe_create(n) { +export function caml_ephe_create(n) { return caml_weak_create(n); } //Provides: caml_weak_create -//Requires: caml_ephe_key_offset -//Requires: caml_ephe_none -function caml_weak_create(n) { +export function caml_weak_create(n) { var alen = caml_ephe_key_offset + n; var x = new Array(alen); x[0] = 251; @@ -73,15 +66,12 @@ function caml_weak_create(n) { } //Provides: caml_weak_set -//Requires: caml_ephe_set_key, caml_ephe_unset_key -function caml_weak_set(x, i, v) { +export function caml_weak_set(x, i, v) { if (v === 0) caml_ephe_unset_key(x, i); else caml_ephe_set_key(x, i, v[1]); return 0; } //Provides: caml_ephe_get_key -//Requires: caml_ephe_key_offset, caml_ephe_data_offset -//Requires: caml_ephe_none //Alias: caml_weak_get function caml_ephe_get_key(x, i) { @@ -98,10 +88,8 @@ function caml_ephe_get_key(x, i) { return [0, weak]; } //Provides: caml_ephe_get_key_copy -//Requires: caml_ephe_get_key,caml_ephe_key_offset -//Requires: caml_obj_dup //Alias: caml_weak_get_copy -function caml_ephe_get_key_copy(x, i) { +export function caml_ephe_get_key_copy(x, i) { var y = caml_ephe_get_key(x, i); if (y === 0) return y; var z = y[1]; @@ -110,10 +98,8 @@ function caml_ephe_get_key_copy(x, i) { } //Provides: caml_ephe_check_key mutable -//Requires: caml_ephe_key_offset, caml_ephe_data_offset -//Requires: caml_ephe_none //Alias: caml_weak_check -function caml_ephe_check_key(x, i) { +export function caml_ephe_check_key(x, i) { var weak = x[caml_ephe_key_offset + i]; if (weak === caml_ephe_none) return 0; if (globalThis.WeakRef && weak instanceof globalThis.WeakRef) { @@ -128,12 +114,8 @@ function caml_ephe_check_key(x, i) { } //Provides: caml_ephe_blit_key -//Requires: caml_array_blit -//Requires: caml_ephe_key_offset -//Requires: caml_ephe_get_data -//Requires: caml_ephe_set_data_opt //Alias: caml_weak_blit -function caml_ephe_blit_key(a1, i1, a2, i2, len) { +export function caml_ephe_blit_key(a1, i1, a2, i2, len) { var old = caml_ephe_get_data(a1); // minus one because caml_array_blit works on ocaml array caml_array_blit( @@ -148,17 +130,14 @@ function caml_ephe_blit_key(a1, i1, a2, i2, len) { } //Provides: caml_ephe_blit_data -//Requires: caml_ephe_get_data, caml_ephe_set_data_opt -function caml_ephe_blit_data(src, dst) { +export function caml_ephe_blit_data(src, dst) { var old = caml_ephe_get_data(src); caml_ephe_set_data_opt(dst, old); return 0; } //Provides: caml_ephe_get_data -//Requires: caml_ephe_data_offset, caml_ephe_key_offset -//Requires: caml_ephe_none -function caml_ephe_get_data(x) { +export function caml_ephe_get_data(x) { var data = x[caml_ephe_data_offset]; if (data === caml_ephe_none) return 0; for (var i = caml_ephe_key_offset; i < x.length; i++) { @@ -183,9 +162,7 @@ function caml_ephe_get_data(x) { } //Provides: caml_ephe_get_data_copy -//Requires: caml_ephe_get_data -//Requires: caml_obj_dup -function caml_ephe_get_data_copy(x) { +export function caml_ephe_get_data_copy(x) { var r = caml_ephe_get_data(x); if (r === 0) return 0; var z = r[1]; @@ -194,9 +171,7 @@ function caml_ephe_get_data_copy(x) { } //Provides: caml_ephe_set_data -//Requires: caml_ephe_data_offset, caml_ephe_key_offset -//Requires: caml_ephe_none -function caml_ephe_set_data(x, data) { +export function caml_ephe_set_data(x, data) { for (var i = x.length - 1; i >= caml_ephe_key_offset; i--) { var k = x[i]; if (globalThis.WeakRef && k instanceof globalThis.WeakRef) { @@ -215,25 +190,20 @@ function caml_ephe_set_data(x, data) { } //Provides: caml_ephe_set_data_opt -//Requires: caml_ephe_set_data -//Requires: caml_ephe_unset_data -function caml_ephe_set_data_opt(x, data_opt) { +export function caml_ephe_set_data_opt(x, data_opt) { if (data_opt === 0) caml_ephe_unset_data(x); else caml_ephe_set_data(x, data_opt[1]); return 0; } //Provides: caml_ephe_unset_data -//Requires: caml_ephe_data_offset -//Requires: caml_ephe_none -function caml_ephe_unset_data(x) { +export function caml_ephe_unset_data(x) { x[caml_ephe_data_offset] = caml_ephe_none; return 0; } //Provides: caml_ephe_check_data -//Requires: caml_ephe_get_data -function caml_ephe_check_data(x) { +export function caml_ephe_check_data(x) { var data = caml_ephe_get_data(x); if (data === 0) return 0; else return 1; diff --git a/runtime/js/zstd.js b/runtime/js/zstd.js index ff9956957c..14ebe4fdc6 100644 --- a/runtime/js/zstd.js +++ b/runtime/js/zstd.js @@ -1,6 +1,6 @@ //Provides: zstd_decompress //Version: >= 5.1 -var zstd_decompress = (function () { +export let zstd_decompress = (function () { // aliases for shorter compressed code (most minifers don't do this) var ab = ArrayBuffer, u8 = Uint8Array, @@ -700,29 +700,25 @@ var zstd_decompress = (function () { //Provides: caml_decompress_input //Version: < 5.1.0 -var caml_decompress_input = null; +export let caml_decompress_input = null; //Provides: caml_decompress_input //Version: >= 5.1.0 //Version: < 5.1.1 -//Requires: zstd_decompress -var caml_decompress_input = zstd_decompress; +export let caml_decompress_input = zstd_decompress; //Provides: caml_decompress_input //Version: >= 5.1.1 //Version: < 5.2.0 -var caml_decompress_input = null; +export let caml_decompress_input = null; //Provides: caml_decompress_input //Version: >= 5.2 -//Requires: zstd_decompress -var caml_decompress_input = zstd_decompress; +export let caml_decompress_input = zstd_decompress; //Provides: caml_zstd_initialize -//Requires: caml_decompress_input -//Requires: zstd_decompress //Version: >= 5.1.1 -function caml_zstd_initialize(_unit) { +export function caml_zstd_initialize(_unit) { caml_decompress_input = zstd_decompress; return 1; }