Skip to content

Commit 8b78a78

Browse files
committed
Runtime: add js primitives for OCaml 5.4
1 parent 75d8cdb commit 8b78a78

File tree

5 files changed

+147
-2
lines changed

5 files changed

+147
-2
lines changed

runtime/js/array.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,30 @@ function caml_array_concat(l) {
8080
return a;
8181
}
8282

83+
//Provides: caml_floatarray_concat mutable
84+
//Version: >= 5.4
85+
function caml_floatarray_concat(l) {
86+
var a = [0];
87+
while (l !== 0) {
88+
var b = l[1];
89+
for (var i = 1; i < b.length; i++) a.push(b[i]);
90+
l = l[2];
91+
}
92+
return a;
93+
}
94+
95+
//Provides: caml_uniform_array_concat mutable
96+
//Version: >= 5.4
97+
function caml_uniform_array_concat(l) {
98+
var a = [0];
99+
while (l !== 0) {
100+
var b = l[1];
101+
for (var i = 1; i < b.length; i++) a.push(b[i]);
102+
l = l[2];
103+
}
104+
return a;
105+
}
106+
83107
//Provides: caml_array_blit
84108
function caml_array_blit(a1, i1, a2, i2, len) {
85109
if (i2 <= i1) {

runtime/js/domain.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ function caml_atomic_load(ref) {
3131
return ref[1];
3232
}
3333

34+
//Provides: caml_atomic_load_field
35+
//Version: >= 5.4
36+
function caml_atomic_load_field(b, i) {
37+
return b[i + 1];
38+
}
39+
3440
//Provides: caml_atomic_cas
3541
//Version: >= 5
3642
function caml_atomic_cas(ref, o, n) {
@@ -41,6 +47,16 @@ function caml_atomic_cas(ref, o, n) {
4147
return 0;
4248
}
4349

50+
//Provides: caml_atomic_cas_field
51+
//Version: >= 5.4
52+
function caml_atomic_cas_field(b, i, o, n) {
53+
if (b[i + 1] === o) {
54+
b[i + 1] = n;
55+
return 1;
56+
}
57+
return 0;
58+
}
59+
4460
//Provides: caml_atomic_fetch_add
4561
//Version: >= 5
4662
function caml_atomic_fetch_add(ref, i) {
@@ -49,6 +65,14 @@ function caml_atomic_fetch_add(ref, i) {
4965
return old;
5066
}
5167

68+
//Provides: caml_atomic_fetch_add_field
69+
//Version: >= 5.4
70+
function caml_atomic_fetch_add_field(b, i, n) {
71+
var old = b[i + 1];
72+
b[i + 1] += n;
73+
return old;
74+
}
75+
5276
//Provides: caml_atomic_exchange
5377
//Version: >= 5
5478
function caml_atomic_exchange(ref, v) {
@@ -57,6 +81,14 @@ function caml_atomic_exchange(ref, v) {
5781
return r;
5882
}
5983

84+
//Provides: caml_atomic_exchange_field
85+
//Version: >= 5.4
86+
function caml_atomic_exchange_field(b, i, v) {
87+
var r = b[i + 1];
88+
b[i + 1] = v;
89+
return r;
90+
}
91+
6092
//Provides: caml_atomic_make_contended
6193
//Version: >= 5.2
6294
function caml_atomic_make_contended(a) {

runtime/js/io.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ function caml_ml_out_channels_list() {
194194
//Requires: caml_ml_channels, caml_sys_fds
195195
//Requires: caml_raise_sys_error
196196
//Requires: caml_sys_open
197+
//Requires: caml_io_buffer_size
197198
function caml_ml_open_descriptor_out(fd) {
198199
var fd_desc = caml_sys_fds[fd];
199200
if (fd_desc === undefined)
@@ -208,7 +209,7 @@ function caml_ml_open_descriptor_out(fd) {
208209
opened: true,
209210
out: true,
210211
buffer_curr: 0,
211-
buffer: new Uint8Array(65536),
212+
buffer: new Uint8Array(caml_io_buffer_size),
212213
buffered: buffered,
213214
};
214215
caml_ml_channels.set(chanid, channel);
@@ -219,6 +220,7 @@ function caml_ml_open_descriptor_out(fd) {
219220
//Requires: caml_ml_channels, caml_sys_fds
220221
//Requires: caml_raise_sys_error
221222
//Requires: caml_sys_open
223+
//Requires: caml_io_buffer_size
222224
function caml_ml_open_descriptor_in(fd) {
223225
var fd_desc = caml_sys_fds[fd];
224226
if (fd_desc === undefined)
@@ -234,7 +236,7 @@ function caml_ml_open_descriptor_in(fd) {
234236
out: false,
235237
buffer_curr: 0,
236238
buffer_max: 0,
237-
buffer: new Uint8Array(65536),
239+
buffer: new Uint8Array(caml_io_buffer_size),
238240
refill: refill,
239241
};
240242
caml_ml_channels.set(chanid, channel);

runtime/js/obj.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,38 @@ function caml_update_dummy(x, y) {
3232

3333
//Provides: caml_alloc_dummy_infix
3434
//Requires: caml_call_gen
35+
//Version: < 5.4
3536
function caml_alloc_dummy_infix() {
3637
return function f(x) {
3738
return caml_call_gen(f.fun, [x]);
3839
};
3940
}
4041

42+
//Provides: caml_alloc_dummy_lazy
43+
//Version: >= 5.4
44+
function caml_alloc_dummy_lazy(_unit) {
45+
return [0, 0];
46+
}
47+
48+
//Provides: caml_update_dummy_lazy
49+
//Requires: caml_obj_tag
50+
//Requires: caml_update_dummy
51+
//Version: >= 5.4
52+
function caml_update_dummy_lazy(dummy, newval) {
53+
switch (caml_obj_tag(newval)) {
54+
case 246: // Lazy
55+
case 244: // Forcing
56+
case 250: // Forward
57+
caml_update_dummy(dummy, newval);
58+
break;
59+
default:
60+
dummy[1] = newval;
61+
dummy[0] = 250;
62+
break;
63+
}
64+
return 0;
65+
}
66+
4167
//Provides: caml_obj_tag
4268
//Requires: caml_is_ml_bytes, caml_is_ml_string
4369
function caml_obj_tag(x) {
@@ -244,3 +270,18 @@ function caml_is_continuation_tag(t) {
244270
function caml_custom_identifier(o) {
245271
return caml_string_of_jsstring(o.caml_custom);
246272
}
273+
274+
//Provides: caml_ml_gc_ramp_up
275+
//Requires: caml_callback
276+
//Version: >= 5.4
277+
function caml_ml_gc_ramp_up(f) {
278+
var a = caml_callback(f, [0]);
279+
var suspended = 0;
280+
return [0, a, suspended];
281+
}
282+
283+
//Provides: caml_ml_gc_ramp_down
284+
//Version: >= 5.4
285+
function caml_ml_gc_ramp_down(_suspended_collection_work) {
286+
return 0;
287+
}

runtime/js/sys.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ function caml_sys_getenv(name) {
133133
return caml_string_of_jsstring(r);
134134
}
135135

136+
//Provides: caml_sys_getenv_opt (const)
137+
//Requires: caml_string_of_jsstring
138+
//Requires: caml_jsstring_of_string
139+
//Requires: jsoo_sys_getenv
140+
//Version: >= 5.4
141+
function caml_sys_getenv_opt(name) {
142+
var r = jsoo_sys_getenv(caml_jsstring_of_string(name));
143+
if (r === undefined) return 0;
144+
return [0, caml_string_of_jsstring(r)];
145+
}
146+
136147
//Provides: caml_sys_unsafe_getenv
137148
//Requires: caml_sys_getenv
138149
function caml_sys_unsafe_getenv(name) {
@@ -350,6 +361,41 @@ function caml_sys_is_regular_file(name) {
350361
var root = resolve_fs_device(name);
351362
return root.device.isFile(root.rest);
352363
}
364+
365+
//Provides: caml_io_buffer_size
366+
var caml_io_buffer_size = 65536;
367+
368+
//Provides: caml_sys_io_buffer_size
369+
//Requires: caml_io_buffer_size
370+
//Version: >= 5.4
371+
function caml_sys_io_buffer_size(_unit) {
372+
return caml_io_buffer_size;
373+
}
374+
375+
//Provides: caml_sys_temp_dir_name
376+
//Requires: os_type
377+
//Requires: caml_string_of_jsstring
378+
//Version: >= 5.4
379+
function caml_sys_temp_dir_name(_unit) {
380+
if (os_type === "Win32" && require("node:os").tmpdir) {
381+
return caml_string_of_jsstring(globalThis.os.tmpdir());
382+
} else {
383+
return caml_string_of_jsstring("");
384+
}
385+
}
386+
387+
//Provides: caml_sys_convert_signal_number
388+
//Version: >= 5.4
389+
function caml_sys_convert_signal_number(signo) {
390+
return signo;
391+
}
392+
393+
//Provides: caml_sys_rev_convert_signal_number
394+
//Version: >= 5.4
395+
function caml_sys_rev_convert_signal_number(signo) {
396+
return signo;
397+
}
398+
353399
//Always
354400
//Requires: caml_fatal_uncaught_exception
355401
//If: !wasm

0 commit comments

Comments
 (0)