@live-codes/ocaml 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. package/LICENSE +220 -0
  2. package/README.md +165 -2
  3. package/THIRD-PARTY-NOTICES.md +52 -0
  4. package/assets/compiler/compile_stdout.browser.js +27856 -0
  5. package/assets/compiler/jsoo.js +12631 -0
  6. package/assets/compiler/package.json +3 -0
  7. package/assets/compiler/runtime/bigarray.js +1072 -0
  8. package/assets/compiler/runtime/bigstring.js +137 -0
  9. package/assets/compiler/runtime/domain.js +393 -0
  10. package/assets/compiler/runtime/dynlink.js +91 -0
  11. package/assets/compiler/runtime/float32.js +486 -0
  12. package/assets/compiler/runtime/fs.js +416 -0
  13. package/assets/compiler/runtime/fs_fake.js +600 -0
  14. package/assets/compiler/runtime/fs_node.js +609 -0
  15. package/assets/compiler/runtime/fs_quickjs.js +532 -0
  16. package/assets/compiler/runtime/gc.js +201 -0
  17. package/assets/compiler/runtime/graphics.js +1053 -0
  18. package/assets/compiler/runtime/ieee_754.js +621 -0
  19. package/assets/compiler/runtime/int64.js +516 -0
  20. package/assets/compiler/runtime/io.js +739 -0
  21. package/assets/compiler/runtime/jslib.js +553 -0
  22. package/assets/compiler/runtime/jslib_js_of_ocaml.js +81 -0
  23. package/assets/compiler/runtime/lexing.js +240 -0
  24. package/assets/compiler/runtime/manifest.txt +31 -0
  25. package/assets/compiler/runtime/marshal.js +863 -0
  26. package/assets/compiler/runtime/md5.js +236 -0
  27. package/assets/compiler/runtime/mlBytes.js +1007 -0
  28. package/assets/compiler/runtime/nat.js +469 -0
  29. package/assets/compiler/runtime/parsing.js +317 -0
  30. package/assets/compiler/runtime/prng.js +84 -0
  31. package/assets/compiler/runtime/promise.js +45 -0
  32. package/assets/compiler/runtime/runtime.js +16688 -0
  33. package/assets/compiler/runtime/runtime_events.js +90 -0
  34. package/assets/compiler/runtime/stdlib.js +363 -0
  35. package/assets/compiler/runtime/sync.js +35 -0
  36. package/assets/compiler/runtime/toplevel.js +150 -0
  37. package/assets/compiler/runtime/unix.js +914 -0
  38. package/assets/compiler/runtime/weak.js +247 -0
  39. package/assets/compiler/stdlib.js +254 -0
  40. package/assets/compiler/worker.js +46 -0
  41. package/assets/interpreter/libs/greet.cma +0 -0
  42. package/assets/interpreter/libs/greet.cmi +0 -0
  43. package/assets/interpreter/libs/manifest.txt +1 -0
  44. package/assets/interpreter/package.json +3 -0
  45. package/assets/interpreter/stdlib/camlinternalFormat.cmi +0 -0
  46. package/assets/interpreter/stdlib/camlinternalFormatBasics.cmi +0 -0
  47. package/assets/interpreter/stdlib/camlinternalLazy.cmi +0 -0
  48. package/assets/interpreter/stdlib/camlinternalMod.cmi +0 -0
  49. package/assets/interpreter/stdlib/camlinternalOO.cmi +0 -0
  50. package/assets/interpreter/stdlib/std_exit.cmi +0 -0
  51. package/assets/interpreter/stdlib/stdlib.cmi +0 -0
  52. package/assets/interpreter/stdlib/stdlib__Arg.cmi +0 -0
  53. package/assets/interpreter/stdlib/stdlib__Array.cmi +0 -0
  54. package/assets/interpreter/stdlib/stdlib__ArrayLabels.cmi +0 -0
  55. package/assets/interpreter/stdlib/stdlib__Atomic.cmi +0 -0
  56. package/assets/interpreter/stdlib/stdlib__Bigarray.cmi +0 -0
  57. package/assets/interpreter/stdlib/stdlib__Bool.cmi +0 -0
  58. package/assets/interpreter/stdlib/stdlib__Buffer.cmi +0 -0
  59. package/assets/interpreter/stdlib/stdlib__Bytes.cmi +0 -0
  60. package/assets/interpreter/stdlib/stdlib__BytesLabels.cmi +0 -0
  61. package/assets/interpreter/stdlib/stdlib__Callback.cmi +0 -0
  62. package/assets/interpreter/stdlib/stdlib__Char.cmi +0 -0
  63. package/assets/interpreter/stdlib/stdlib__Complex.cmi +0 -0
  64. package/assets/interpreter/stdlib/stdlib__Condition.cmi +0 -0
  65. package/assets/interpreter/stdlib/stdlib__Digest.cmi +0 -0
  66. package/assets/interpreter/stdlib/stdlib__Domain.cmi +0 -0
  67. package/assets/interpreter/stdlib/stdlib__Dynarray.cmi +0 -0
  68. package/assets/interpreter/stdlib/stdlib__Effect.cmi +0 -0
  69. package/assets/interpreter/stdlib/stdlib__Either.cmi +0 -0
  70. package/assets/interpreter/stdlib/stdlib__Ephemeron.cmi +0 -0
  71. package/assets/interpreter/stdlib/stdlib__Filename.cmi +0 -0
  72. package/assets/interpreter/stdlib/stdlib__Float.cmi +0 -0
  73. package/assets/interpreter/stdlib/stdlib__Format.cmi +0 -0
  74. package/assets/interpreter/stdlib/stdlib__Fun.cmi +0 -0
  75. package/assets/interpreter/stdlib/stdlib__Gc.cmi +0 -0
  76. package/assets/interpreter/stdlib/stdlib__Hashtbl.cmi +0 -0
  77. package/assets/interpreter/stdlib/stdlib__Iarray.cmi +0 -0
  78. package/assets/interpreter/stdlib/stdlib__In_channel.cmi +0 -0
  79. package/assets/interpreter/stdlib/stdlib__Int.cmi +0 -0
  80. package/assets/interpreter/stdlib/stdlib__Int32.cmi +0 -0
  81. package/assets/interpreter/stdlib/stdlib__Int64.cmi +0 -0
  82. package/assets/interpreter/stdlib/stdlib__Lazy.cmi +0 -0
  83. package/assets/interpreter/stdlib/stdlib__Lexing.cmi +0 -0
  84. package/assets/interpreter/stdlib/stdlib__List.cmi +0 -0
  85. package/assets/interpreter/stdlib/stdlib__ListLabels.cmi +0 -0
  86. package/assets/interpreter/stdlib/stdlib__Map.cmi +0 -0
  87. package/assets/interpreter/stdlib/stdlib__Marshal.cmi +0 -0
  88. package/assets/interpreter/stdlib/stdlib__MoreLabels.cmi +0 -0
  89. package/assets/interpreter/stdlib/stdlib__Mutex.cmi +0 -0
  90. package/assets/interpreter/stdlib/stdlib__Nativeint.cmi +0 -0
  91. package/assets/interpreter/stdlib/stdlib__Obj.cmi +0 -0
  92. package/assets/interpreter/stdlib/stdlib__Oo.cmi +0 -0
  93. package/assets/interpreter/stdlib/stdlib__Option.cmi +0 -0
  94. package/assets/interpreter/stdlib/stdlib__Out_channel.cmi +0 -0
  95. package/assets/interpreter/stdlib/stdlib__Pair.cmi +0 -0
  96. package/assets/interpreter/stdlib/stdlib__Parsing.cmi +0 -0
  97. package/assets/interpreter/stdlib/stdlib__Pqueue.cmi +0 -0
  98. package/assets/interpreter/stdlib/stdlib__Printexc.cmi +0 -0
  99. package/assets/interpreter/stdlib/stdlib__Printf.cmi +0 -0
  100. package/assets/interpreter/stdlib/stdlib__Queue.cmi +0 -0
  101. package/assets/interpreter/stdlib/stdlib__Random.cmi +0 -0
  102. package/assets/interpreter/stdlib/stdlib__Repr.cmi +0 -0
  103. package/assets/interpreter/stdlib/stdlib__Result.cmi +0 -0
  104. package/assets/interpreter/stdlib/stdlib__Scanf.cmi +0 -0
  105. package/assets/interpreter/stdlib/stdlib__Semaphore.cmi +0 -0
  106. package/assets/interpreter/stdlib/stdlib__Seq.cmi +0 -0
  107. package/assets/interpreter/stdlib/stdlib__Set.cmi +0 -0
  108. package/assets/interpreter/stdlib/stdlib__Stack.cmi +0 -0
  109. package/assets/interpreter/stdlib/stdlib__StdLabels.cmi +0 -0
  110. package/assets/interpreter/stdlib/stdlib__String.cmi +0 -0
  111. package/assets/interpreter/stdlib/stdlib__StringLabels.cmi +0 -0
  112. package/assets/interpreter/stdlib/stdlib__Sys.cmi +0 -0
  113. package/assets/interpreter/stdlib/stdlib__Type.cmi +0 -0
  114. package/assets/interpreter/stdlib/stdlib__Uchar.cmi +0 -0
  115. package/assets/interpreter/stdlib/stdlib__Unit.cmi +0 -0
  116. package/assets/interpreter/stdlib/stdlib__Weak.cmi +0 -0
  117. package/assets/interpreter/stdlib/topdirs.cmi +0 -0
  118. package/assets/interpreter/stdlib/units.txt +75 -0
  119. package/assets/interpreter/stdlib/unix.cmi +0 -0
  120. package/assets/interpreter/stdlib/unixLabels.cmi +0 -0
  121. package/assets/interpreter/worker-entry.js +39 -0
  122. package/assets/interpreter/worker.js +42356 -0
  123. package/bin/copy-assets.mjs +75 -0
  124. package/package.json +41 -4
  125. package/src/compiler/compile.js +195 -0
  126. package/src/compiler/index.js +46 -0
  127. package/src/compiler/worker.js +46 -0
  128. package/src/index.js +12 -0
  129. package/src/interpreter/api.js +182 -0
  130. package/src/interpreter/index.js +5 -0
@@ -0,0 +1,914 @@
1
+ //Provides: caml_unix_gettimeofday
2
+ //Alias: unix_gettimeofday
3
+ function caml_unix_gettimeofday() {
4
+ return Date.now() / 1000;
5
+ }
6
+
7
+ //Provides: caml_unix_time
8
+ //Requires: caml_unix_gettimeofday
9
+ //Alias: unix_time
10
+ function caml_unix_time() {
11
+ return Math.floor(caml_unix_gettimeofday());
12
+ }
13
+
14
+ //Provides: caml_unix_times
15
+ //Requires: caml_failwith
16
+ //Alias: unix_times
17
+ function caml_unix_times() {
18
+ if (globalThis.process?.cpuUsage) {
19
+ var t = globalThis.process.cpuUsage();
20
+ return BLOCK(0, t.user / 1e6, t.system / 1e6, 0, 0);
21
+ } else if (globalThis.performance?.now) {
22
+ return BLOCK(0, globalThis.performance.now() / 1000, 0, 0, 0);
23
+ } else {
24
+ caml_failwith("caml_unix_times: not implemented");
25
+ }
26
+ }
27
+
28
+ //Provides: caml_unix_gmtime
29
+ //Alias: unix_gmtime
30
+ function caml_unix_gmtime(t) {
31
+ var d = new Date(t * 1000);
32
+ var d_num = d.getTime();
33
+ var januaryfirst = new Date(Date.UTC(d.getUTCFullYear(), 0, 1)).getTime();
34
+ var doy = Math.floor((d_num - januaryfirst) / 86400000);
35
+ return BLOCK(
36
+ 0,
37
+ d.getUTCSeconds(),
38
+ d.getUTCMinutes(),
39
+ d.getUTCHours(),
40
+ d.getUTCDate(),
41
+ d.getUTCMonth(),
42
+ d.getUTCFullYear() - 1900,
43
+ d.getUTCDay(),
44
+ doy,
45
+ false | 0 /* for UTC daylight savings time is false */,
46
+ );
47
+ }
48
+
49
+ //Provides: caml_unix_localtime
50
+ //Alias: unix_localtime
51
+ function caml_unix_localtime(t) {
52
+ var d = new Date(t * 1000);
53
+ // tm_yday is the distance in days to January 1; compute it in UTC so
54
+ // that the one-hour DST shift of wall-clock arithmetic does not apply
55
+ var doy = Math.floor(
56
+ (Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()) -
57
+ Date.UTC(d.getFullYear(), 0, 1)) /
58
+ 86400000,
59
+ );
60
+ var jan = new Date(d.getFullYear(), 0, 1);
61
+ var jul = new Date(d.getFullYear(), 6, 1);
62
+ var stdTimezoneOffset = Math.max(
63
+ jan.getTimezoneOffset(),
64
+ jul.getTimezoneOffset(),
65
+ );
66
+ var isdst = d.getTimezoneOffset() < stdTimezoneOffset;
67
+ // Europe/Dublin uses "negative DST": the IANA database marks winter
68
+ // (GMT) as the daylight deviation from its summer standard (IST), so the
69
+ // native runtime reports tm_isdst inverted from the offset heuristic.
70
+ // The offsets alone cannot distinguish it from Europe/London, so match
71
+ // it by name (guarded to the GMT/+1 signature to avoid the Intl lookup
72
+ // for every other zone).
73
+ if (
74
+ stdTimezoneOffset === 0 &&
75
+ jan.getTimezoneOffset() !== jul.getTimezoneOffset() &&
76
+ globalThis.Intl?.DateTimeFormat?.().resolvedOptions().timeZone ===
77
+ "Europe/Dublin"
78
+ )
79
+ isdst = !isdst;
80
+ return BLOCK(
81
+ 0,
82
+ d.getSeconds(),
83
+ d.getMinutes(),
84
+ d.getHours(),
85
+ d.getDate(),
86
+ d.getMonth(),
87
+ d.getFullYear() - 1900,
88
+ d.getDay(),
89
+ doy,
90
+ isdst | 0 /* daylight savings time field. */,
91
+ );
92
+ }
93
+
94
+ //Provides: caml_unix_mktime
95
+ //Requires: caml_unix_localtime
96
+ //Alias: unix_mktime
97
+ function caml_unix_mktime(tm) {
98
+ var d = new Date(tm[6] + 1900, tm[5], tm[4], tm[3], tm[2], tm[1]).getTime();
99
+ var t = Math.floor(d / 1000);
100
+ var tm2 = caml_unix_localtime(t);
101
+ return BLOCK(0, t, tm2);
102
+ }
103
+ //Provides: caml_unix_startup const
104
+ //Alias: win_startup
105
+ function caml_unix_startup() {}
106
+
107
+ //Provides: caml_unix_cleanup const
108
+ //Alias: win_cleanup
109
+ function caml_unix_cleanup() {}
110
+
111
+ //Provides: caml_unix_filedescr_of_fd const
112
+ //Alias: win_handle_fd
113
+ function caml_unix_filedescr_of_fd(x) {
114
+ return x;
115
+ }
116
+
117
+ //Provides: caml_unix_isatty
118
+ //Requires: caml_unix_lookup_file
119
+ //Alias: unix_isatty
120
+ function caml_unix_isatty(fd) {
121
+ var file = caml_unix_lookup_file(fd);
122
+ if (!file.isatty) return 0;
123
+ return file.isatty();
124
+ }
125
+
126
+ //Provides: caml_unix_isatty
127
+ //Alias: unix_isatty
128
+ //If: browser
129
+ function caml_unix_isatty(_fileDescriptor) {
130
+ return 0;
131
+ }
132
+
133
+ //Provides: unix_error
134
+ var unix_error = [
135
+ /* ===Unix.error===
136
+ *
137
+ * This array is in order of the variant in OCaml
138
+ */
139
+ "E2BIG",
140
+ "EACCES",
141
+ "EAGAIN",
142
+ "EBADF",
143
+ "EBUSY",
144
+ "ECHILD",
145
+ "EDEADLK",
146
+ "EDOM",
147
+ "EEXIST",
148
+ "EFAULT",
149
+ "EFBIG",
150
+ "EINTR",
151
+ "EINVAL",
152
+ "EIO",
153
+ "EISDIR",
154
+ "EMFILE",
155
+ "EMLINK",
156
+ "ENAMETOOLONG",
157
+ "ENFILE",
158
+ "ENODEV",
159
+ "ENOENT",
160
+ "ENOEXEC",
161
+ "ENOLCK",
162
+ "ENOMEM",
163
+ "ENOSPC",
164
+ "ENOSYS",
165
+ "ENOTDIR",
166
+ "ENOTEMPTY",
167
+ "ENOTTY",
168
+ "ENXIO",
169
+ "EPERM",
170
+ "EPIPE",
171
+ "ERANGE",
172
+ "EROFS",
173
+ "ESPIPE",
174
+ "ESRCH",
175
+ "EXDEV",
176
+ "EWOULDBLOCK",
177
+ "EINPROGRESS",
178
+ "EALREADY",
179
+ "ENOTSOCK",
180
+ "EDESTADDRREQ",
181
+ "EMSGSIZE",
182
+ "EPROTOTYPE",
183
+ "ENOPROTOOPT",
184
+ "EPROTONOSUPPORT",
185
+ "ESOCKTNOSUPPORT",
186
+ "EOPNOTSUPP",
187
+ "EPFNOSUPPORT",
188
+ "EAFNOSUPPORT",
189
+ "EADDRINUSE",
190
+ "EADDRNOTAVAIL",
191
+ "ENETDOWN",
192
+ "ENETUNREACH",
193
+ "ENETRESET",
194
+ "ECONNABORTED",
195
+ "ECONNRESET",
196
+ "ENOBUFS",
197
+ "EISCONN",
198
+ "ENOTCONN",
199
+ "ESHUTDOWN",
200
+ "ETOOMANYREFS",
201
+ "ETIMEDOUT",
202
+ "ECONNREFUSED",
203
+ "EHOSTDOWN",
204
+ "EHOSTUNREACH",
205
+ "ELOOP",
206
+ "EOVERFLOW",
207
+ ];
208
+
209
+ //Provides: make_unix_err_args
210
+ //Requires: unix_error, caml_string_of_jsstring
211
+ function make_unix_err_args(code, syscall, path, errno) {
212
+ var variant = unix_error.indexOf(code);
213
+ if (variant < 0) {
214
+ // Default if undefined
215
+ if (errno == null) {
216
+ errno = -9999;
217
+ }
218
+ // If none of the above variants, fallback to EUNKNOWNERR(int)
219
+ // errno is expected to be positive
220
+ variant = BLOCK(0, -errno);
221
+ }
222
+ var args = [
223
+ variant,
224
+ caml_string_of_jsstring(syscall || ""),
225
+ caml_string_of_jsstring(path || ""),
226
+ ];
227
+ return args;
228
+ }
229
+
230
+ //Provides: caml_strerror
231
+ //Requires: unix_error
232
+ function caml_strerror(errno) {
233
+ if (typeof require === "undefined") {
234
+ const code = unix_error[errno];
235
+ return code || "Unknown error " + errno;
236
+ }
237
+ const util = require("node:util");
238
+ if (errno >= 0) {
239
+ const code = unix_error[errno];
240
+ // no Iterator.prototype.find: it requires node >= 22
241
+ for (const e of util.getSystemErrorMap())
242
+ if (e[1][0] === code) return e[1][1];
243
+ // not every code is in libuv's error map
244
+ return code || "Unknown error " + errno;
245
+ } else {
246
+ return util.getSystemErrorMessage(errno);
247
+ }
248
+ }
249
+
250
+ //Provides: caml_strerror
251
+ //Requires: unix_error
252
+ //If: browser
253
+ function caml_strerror(errno) {
254
+ const code = unix_error[errno];
255
+ return code || "Unknown error " + errno;
256
+ }
257
+
258
+ //Provides: unix_error_message
259
+ //Alias: caml_unix_error_message
260
+ //Requires: caml_strerror, caml_string_of_jsstring
261
+ function unix_error_message(err) {
262
+ const errno = typeof err === "number" ? err : -err[1];
263
+ return caml_string_of_jsstring(caml_strerror(errno));
264
+ }
265
+
266
+ //Provides: caml_unix_chdir
267
+ //Requires: caml_sys_chdir
268
+ //Alias: unix_chdir
269
+ function caml_unix_chdir(dir) {
270
+ return caml_sys_chdir(dir, /* raise Unix_error */ true);
271
+ }
272
+
273
+ //Provides: caml_unix_stat
274
+ //Requires: resolve_fs_device, caml_failwith
275
+ //Alias: unix_stat
276
+ function caml_unix_stat(name) {
277
+ var root = resolve_fs_device(name, /* raise Unix_error */ 1);
278
+ if (!root.device.stat) {
279
+ caml_failwith("caml_unix_stat: not implemented");
280
+ }
281
+ return root.device.stat(
282
+ root.rest,
283
+ /* large */ false,
284
+ /* raise Unix_error */ true,
285
+ );
286
+ }
287
+
288
+ //Provides: caml_unix_stat_64
289
+ //Requires: resolve_fs_device, caml_failwith
290
+ //Alias: unix_stat_64
291
+ function caml_unix_stat_64(name) {
292
+ var root = resolve_fs_device(name, /* raise Unix_error */ 1);
293
+ if (!root.device.stat) {
294
+ caml_failwith("caml_unix_stat_64: not implemented");
295
+ }
296
+ return root.device.stat(
297
+ root.rest,
298
+ /* large */ true,
299
+ /* raise Unix_error */ true,
300
+ );
301
+ }
302
+
303
+ //Provides: caml_unix_lstat
304
+ //Requires: resolve_fs_device, caml_failwith
305
+ //Alias: unix_lstat
306
+ function caml_unix_lstat(name) {
307
+ var root = resolve_fs_device(name, /* raise Unix_error */ 1);
308
+ if (!root.device.lstat) {
309
+ caml_failwith("caml_unix_lstat: not implemented");
310
+ }
311
+ return root.device.lstat(
312
+ root.rest,
313
+ /* large */ false,
314
+ /* raise Unix_error */ true,
315
+ );
316
+ }
317
+
318
+ //Provides: caml_unix_lstat_64
319
+ //Requires: resolve_fs_device, caml_failwith
320
+ //Alias: unix_lstat_64
321
+ function caml_unix_lstat_64(name) {
322
+ var root = resolve_fs_device(name, /* raise Unix_error */ 1);
323
+ if (!root.device.lstat) {
324
+ caml_failwith("caml_unix_lstat_64: not implemented");
325
+ }
326
+ return root.device.lstat(
327
+ root.rest,
328
+ /* large */ true,
329
+ /* raise Unix_error */ true,
330
+ );
331
+ }
332
+
333
+ //Provides: caml_unix_chmod
334
+ //Requires: resolve_fs_device, caml_failwith
335
+ //Alias: unix_chmod
336
+ function caml_unix_chmod(name, perms) {
337
+ var root = resolve_fs_device(name, /* raise Unix_error */ 1);
338
+ if (!root.device.chmod) {
339
+ caml_failwith("caml_unix_chmod: not implemented");
340
+ }
341
+ return root.device.chmod(root.rest, perms, /* raise Unix_error */ 1);
342
+ }
343
+
344
+ //Provides: caml_unix_rename
345
+ //Requires: caml_failwith, resolve_fs_device
346
+ //Requires: caml_raise_system_error
347
+ //Alias: unix_rename
348
+ function caml_unix_rename(o, n) {
349
+ var o_root = resolve_fs_device(o, /* raise Unix_error */ 1);
350
+ var n_root = resolve_fs_device(n, /* raise Unix_error */ 1);
351
+ if (o_root.device !== n_root.device)
352
+ caml_raise_system_error(/* raise Unix_error */ 1, "EXDEV", "rename");
353
+ if (!o_root.device.rename) caml_failwith("caml_sys_rename: not implemented");
354
+ o_root.device.rename(o_root.rest, n_root.rest, /* raise Unix_error */ true);
355
+ }
356
+
357
+ //Provides: caml_unix_mkdir
358
+ //Requires: resolve_fs_device, caml_failwith
359
+ //Alias: unix_mkdir
360
+ function caml_unix_mkdir(name, perm) {
361
+ var root = resolve_fs_device(name, /* raise Unix_error */ 1);
362
+ if (!root.device.mkdir) {
363
+ caml_failwith("caml_unix_mkdir: not implemented");
364
+ }
365
+ return root.device.mkdir(root.rest, perm, /* raise Unix_error */ true);
366
+ }
367
+
368
+ //Provides: caml_unix_rmdir
369
+ //Requires: resolve_fs_device, caml_failwith
370
+ //Alias: unix_rmdir
371
+ function caml_unix_rmdir(name) {
372
+ var root = resolve_fs_device(name, /* raise Unix_error */ 1);
373
+ if (!root.device.rmdir) {
374
+ caml_failwith("caml_unix_rmdir: not implemented");
375
+ }
376
+ return root.device.rmdir(root.rest, /* raise Unix_error */ true);
377
+ }
378
+
379
+ //Provides: caml_unix_link
380
+ //Requires: resolve_fs_device, caml_failwith, caml_raise_system_error
381
+ //Alias: unix_link
382
+ function caml_unix_link(follow, src, dst) {
383
+ var src_root = resolve_fs_device(src, /* raise Unix_error */ 1);
384
+ var dst_root = resolve_fs_device(dst, /* raise Unix_error */ 1);
385
+ if (!src_root.device.link) {
386
+ caml_failwith("caml_unix_link: not implemented");
387
+ }
388
+ // We can't control whether a 'src' symlink is followed or not.
389
+ // So we fail when 'follow' is set, as documented in the Unix module.
390
+ if (typeof follow !== "number")
391
+ caml_raise_system_error(/* raise Unix_error */ 1, "ENOSYS", "link");
392
+ if (src_root.device !== dst_root.device)
393
+ caml_raise_system_error(/* raise Unix_error */ 1, "EXDEV", "link");
394
+ return src_root.device.link(
395
+ src_root.rest,
396
+ dst_root.rest,
397
+ /* raise Unix_error */ true,
398
+ );
399
+ }
400
+
401
+ //Provides: caml_unix_symlink
402
+ //Requires: resolve_fs_device, caml_failwith, caml_jsstring_of_string
403
+ //Alias: unix_symlink
404
+ function caml_unix_symlink(to_dir, src, dst) {
405
+ var dst_root = resolve_fs_device(dst, /* raise Unix_error */ 1);
406
+ if (!dst_root.device.symlink) {
407
+ caml_failwith("caml_unix_symlink: not implemented");
408
+ }
409
+ return dst_root.device.symlink(
410
+ to_dir,
411
+ caml_jsstring_of_string(src),
412
+ dst_root.rest,
413
+ /* raise Unix_error */ true,
414
+ );
415
+ }
416
+
417
+ //Provides: caml_unix_readlink
418
+ //Requires: resolve_fs_device, caml_failwith
419
+ //Alias: unix_readlink
420
+ function caml_unix_readlink(name) {
421
+ var root = resolve_fs_device(name, /* raise Unix_error */ 1);
422
+ if (!root.device.readlink) {
423
+ caml_failwith("caml_unix_readlink: not implemented");
424
+ }
425
+ return root.device.readlink(root.rest, /* raise Unix_error */ true);
426
+ }
427
+
428
+ //Provides: caml_unix_unlink
429
+ //Requires: resolve_fs_device, caml_failwith
430
+ //Alias: unix_unlink
431
+ function caml_unix_unlink(name) {
432
+ var root = resolve_fs_device(name, /* raise Unix_error */ 1);
433
+ if (!root.device.unlink) {
434
+ caml_failwith("caml_unix_unlink: not implemented");
435
+ }
436
+ root.device.unlink(root.rest, /* raise Unix_error */ true);
437
+ return 0;
438
+ }
439
+
440
+ //Provides: caml_unix_utimes
441
+ //Requires: resolve_fs_device, caml_failwith
442
+ //Alias: unix_utimes
443
+ function caml_unix_utimes(name, atime, mtime) {
444
+ var root = resolve_fs_device(name, /* raise Unix_error */ 1);
445
+ if (!root.device.utimes) {
446
+ caml_failwith("caml_unix_utimes: not implemented");
447
+ }
448
+ root.device.utimes(root.rest, atime, mtime, /* raise Unix_error */ true);
449
+ return 0;
450
+ }
451
+
452
+ //Provides: caml_unix_truncate
453
+ //Requires: resolve_fs_device, caml_failwith
454
+ //Alias: unix_truncate
455
+ function caml_unix_truncate(name, len) {
456
+ var root = resolve_fs_device(name, /* raise Unix_error */ 1);
457
+ if (!root.device.truncate) {
458
+ caml_failwith("caml_unix_truncate: not implemented");
459
+ }
460
+ root.device.truncate(root.rest, len, /* raise Unix_error */ true);
461
+ return 0;
462
+ }
463
+
464
+ //Provides: caml_unix_truncate_64
465
+ //Requires: resolve_fs_device, caml_failwith, caml_int64_to_float
466
+ //Alias: unix_truncate_64
467
+ function caml_unix_truncate_64(name, len) {
468
+ var root = resolve_fs_device(name, /* raise Unix_error */ 1);
469
+ if (!root.device.truncate) {
470
+ caml_failwith("caml_unix_truncate_64: not implemented");
471
+ }
472
+ root.device.truncate(
473
+ root.rest,
474
+ caml_int64_to_float(len),
475
+ /* raise Unix_error */ true,
476
+ );
477
+ return 0;
478
+ }
479
+
480
+ //Provides: caml_unix_access
481
+ //Requires: resolve_fs_device, caml_failwith
482
+ //Alias: unix_access
483
+ function caml_unix_access(name, flags) {
484
+ var f = {};
485
+ while (flags) {
486
+ switch (flags[1]) {
487
+ case 0:
488
+ f.r = 1;
489
+ break;
490
+ case 1:
491
+ f.w = 1;
492
+ break;
493
+ case 2:
494
+ f.x = 1;
495
+ break;
496
+ case 3:
497
+ f.f = 1;
498
+ break;
499
+ }
500
+ flags = flags[2];
501
+ }
502
+ var root = resolve_fs_device(name, /* raise Unix_error */ 1);
503
+ if (!root.device.access) {
504
+ caml_failwith("caml_unix_access: not implemented");
505
+ }
506
+ root.device.access(root.rest, f, /* raise Unix_error */ true);
507
+ return 0;
508
+ }
509
+
510
+ //Provides: caml_unix_open
511
+ //Requires: resolve_fs_device, caml_sys_fds
512
+ //Alias: unix_open
513
+ function caml_unix_open(name, flags, perms) {
514
+ var f = {};
515
+ while (flags) {
516
+ switch (flags[1]) {
517
+ case 0:
518
+ f.rdonly = 1;
519
+ break;
520
+ case 1:
521
+ f.wronly = 1;
522
+ break;
523
+ case 2:
524
+ f.rdwr = 1;
525
+ break;
526
+ case 3:
527
+ f.nonblock = 1;
528
+ break;
529
+ case 4:
530
+ f.append = 1;
531
+ break;
532
+ case 5:
533
+ f.create = 1;
534
+ break;
535
+ case 6:
536
+ f.truncate = 1;
537
+ break;
538
+ case 7:
539
+ f.excl = 1;
540
+ break;
541
+ case 8:
542
+ f.noctty = 1;
543
+ break;
544
+ case 9:
545
+ f.dsync = 1;
546
+ break;
547
+ case 10:
548
+ f.sync = 1;
549
+ break;
550
+ }
551
+ flags = flags[2];
552
+ }
553
+ var root = resolve_fs_device(name, /* raise Unix_error */ 1);
554
+ var file = root.device.open(root.rest, f, perms, /* raise Unix_error */ true);
555
+ var idx = caml_sys_fds.length;
556
+ caml_sys_fds[idx] = { file: file };
557
+ return idx | 0;
558
+ }
559
+
560
+ //Provides: caml_unix_lookup_file
561
+ //Requires: caml_sys_fds, caml_raise_system_error
562
+ function caml_unix_lookup_file(fd, cmd) {
563
+ var fd_desc = caml_sys_fds[fd];
564
+ if (fd_desc === undefined)
565
+ caml_raise_system_error(/* raise Unix_error */ 1, "EBADF", cmd);
566
+ return fd_desc.file;
567
+ }
568
+
569
+ //Provides: caml_unix_fstat
570
+ //Alias: unix_fstat
571
+ //Requires: caml_unix_lookup_file, caml_failwith
572
+ function caml_unix_fstat(fd) {
573
+ var file = caml_unix_lookup_file(fd, "fstat");
574
+ if (!file.stat) {
575
+ caml_failwith("caml_unix_fstat: not implemented");
576
+ }
577
+ return file.stat(/* large */ false);
578
+ }
579
+
580
+ //Provides: caml_unix_fstat_64
581
+ //Alias: unix_fstat_64
582
+ //Requires: caml_unix_lookup_file, caml_failwith
583
+ function caml_unix_fstat_64(fd) {
584
+ var file = caml_unix_lookup_file(fd, "fstat");
585
+ if (!file.stat) {
586
+ caml_failwith("caml_unix_fstat64: not implemented");
587
+ }
588
+ return file.stat(/* large */ true);
589
+ }
590
+
591
+ //Provides: caml_unix_fchmod
592
+ //Alias: unix_fchmod
593
+ //Requires: caml_unix_lookup_file, caml_failwith
594
+ function caml_unix_fchmod(fd, perms) {
595
+ var file = caml_unix_lookup_file(fd, "fchmod");
596
+ if (!file.chmod) {
597
+ caml_failwith("caml_unix_fchmod: not implemented");
598
+ }
599
+ return file.chmod(perms);
600
+ }
601
+
602
+ //Provides: caml_unix_fsync
603
+ //Alias: unix_fsync
604
+ //Requires: caml_unix_lookup_file, caml_failwith
605
+ function caml_unix_fsync(fd) {
606
+ var file = caml_unix_lookup_file(fd, "fsync");
607
+ if (!file.sync) {
608
+ caml_failwith("caml_unix_fsync: not implemented");
609
+ }
610
+ return file.sync();
611
+ }
612
+
613
+ //Provides: caml_unix_write
614
+ //Alias: unix_write
615
+ //Requires: caml_unix_lookup_file, caml_uint8_array_of_bytes
616
+ function caml_unix_write(fd, buf, pos, len) {
617
+ var file = caml_unix_lookup_file(fd, "write");
618
+ var a = caml_uint8_array_of_bytes(buf);
619
+ var written = 0;
620
+ while (len > 0) {
621
+ var n = file.write(a, pos, len, /* raise unix_error */ 1);
622
+ written += n;
623
+ pos += n;
624
+ len -= n;
625
+ }
626
+ return written;
627
+ }
628
+
629
+ //Provides: caml_unix_single_write
630
+ //Alias: unix_single_write
631
+ //Requires: caml_unix_lookup_file, caml_uint8_array_of_bytes
632
+ function caml_unix_single_write(fd, buf, pos, len) {
633
+ var file = caml_unix_lookup_file(fd, "write");
634
+ if (len === 0) return 0;
635
+ return file.write(
636
+ caml_uint8_array_of_bytes(buf),
637
+ pos,
638
+ len,
639
+ /* raise unix_error */ 1,
640
+ );
641
+ }
642
+
643
+ //Provides: caml_unix_write_bigarray
644
+ //Requires: caml_ba_to_typed_array, caml_unix_lookup_file
645
+ //Version: >= 5.2
646
+ function caml_unix_write_bigarray(fd, buf, pos, len) {
647
+ var a = caml_ba_to_typed_array(buf);
648
+ var file = caml_unix_lookup_file(fd, "write");
649
+ var written = 0;
650
+ while (len > 0) {
651
+ var n = file.write(a, pos, len, /* raise unix_error */ 1);
652
+ written += n;
653
+ pos += n;
654
+ len -= n;
655
+ }
656
+ return written;
657
+ }
658
+
659
+ //Provides: caml_unix_read
660
+ //Alias: unix_read
661
+ //Requires: caml_unix_lookup_file, caml_uint8_array_of_bytes
662
+ function caml_unix_read(fd, buf, pos, len) {
663
+ var file = caml_unix_lookup_file(fd, "read");
664
+ return file.read(
665
+ caml_uint8_array_of_bytes(buf),
666
+ pos,
667
+ len,
668
+ /* raise unix_error */ 1,
669
+ );
670
+ }
671
+
672
+ //Provides: caml_unix_read_bigarray
673
+ //Alias: unix_read_bigarray
674
+ //Requires: caml_ba_to_typed_array, caml_unix_lookup_file
675
+ //Version: >= 5.2
676
+ function caml_unix_read_bigarray(fd, buf, pos, len) {
677
+ var a = caml_ba_to_typed_array(buf);
678
+ var file = caml_unix_lookup_file(fd, "read");
679
+ return file.read(a, pos, len, /* raise unix_error */ 1);
680
+ }
681
+
682
+ //Provides: caml_unix_lseek
683
+ //Alias: unix_lseek
684
+ //Requires: caml_unix_lookup_file
685
+ function caml_unix_lseek(fd, len, whence) {
686
+ var file = caml_unix_lookup_file(fd, "lseek");
687
+ return file.seek(len, whence, /* raise unix_error */ 1);
688
+ }
689
+
690
+ //Provides: caml_unix_lseek_64
691
+ //Alias: unix_lseek_64
692
+ //Requires: caml_unix_lookup_file, caml_int64_to_float
693
+ function caml_unix_lseek_64(fd, len, whence) {
694
+ var file = caml_unix_lookup_file(fd, "lseek");
695
+ return file.seek(caml_int64_to_float(len), whence, /* raise unix_error */ 1);
696
+ }
697
+
698
+ //Provides: caml_unix_ftruncate
699
+ //Alias: unix_ftruncate
700
+ //Requires: caml_unix_lookup_file, caml_failwith
701
+ function caml_unix_ftruncate(fd, len) {
702
+ var file = caml_unix_lookup_file(fd, "ftruncate");
703
+ if (!file.truncate) {
704
+ caml_failwith("caml_unix_ftruncate: not implemented");
705
+ }
706
+ file.truncate(len, /* raise unix_error */ 1);
707
+ return 0;
708
+ }
709
+
710
+ //Provides: caml_unix_ftruncate_64
711
+ //Alias: unix_ftruncate_64
712
+ //Requires: caml_unix_lookup_file, caml_failwith, caml_int64_to_float
713
+ function caml_unix_ftruncate_64(fd, len) {
714
+ var file = caml_unix_lookup_file(fd, "ftruncate");
715
+ if (!file.truncate) {
716
+ caml_failwith("caml_unix_ftruncate_64: not implemented");
717
+ }
718
+ file.truncate(caml_int64_to_float(len), /* raise unix_error */ 1);
719
+ return 0;
720
+ }
721
+
722
+ //Provides: caml_unix_close
723
+ //Alias: unix_close
724
+ //Requires: caml_unix_lookup_file, caml_sys_fds
725
+ function caml_unix_close(fd) {
726
+ var file = caml_unix_lookup_file(fd, "close");
727
+ file.close(/* raise unix_error */ 1);
728
+ delete caml_sys_fds[fd];
729
+ return 0;
730
+ }
731
+
732
+ //Provides: caml_unix_inchannel_of_filedescr
733
+ //Alias: unix_inchannel_of_filedescr
734
+ //Alias: win_inchannel_of_filedescr
735
+ //Requires: caml_unix_lookup_file, caml_ml_open_descriptor_in
736
+ function caml_unix_inchannel_of_filedescr(fd) {
737
+ var file = caml_unix_lookup_file(fd, "in_channel_of_descr");
738
+ file.check_stream_semantics("in_channel_of_descr");
739
+ return caml_ml_open_descriptor_in(fd);
740
+ }
741
+
742
+ //Provides: caml_unix_outchannel_of_filedescr
743
+ //Alias: unix_outchannel_of_filedescr
744
+ //Alias: win_outchannel_of_filedescr
745
+ //Requires: caml_unix_lookup_file, caml_ml_open_descriptor_out
746
+ function caml_unix_outchannel_of_filedescr(fd) {
747
+ var file = caml_unix_lookup_file(fd, "out_channel_of_descr");
748
+ file.check_stream_semantics("out_channel_of_descr");
749
+ return caml_ml_open_descriptor_out(fd);
750
+ }
751
+
752
+ //Provides: caml_unix_getuid
753
+ //Alias: unix_getuid
754
+ function caml_unix_getuid(_unit) {
755
+ if (globalThis.process?.getuid) {
756
+ return globalThis.process.getuid();
757
+ }
758
+ return 1;
759
+ }
760
+
761
+ //Provides: caml_unix_geteuid
762
+ //Alias: unix_geteuid
763
+ function caml_unix_geteuid(_unit) {
764
+ if (globalThis.process?.geteuid) {
765
+ return globalThis.process.geteuid();
766
+ }
767
+ return 1;
768
+ }
769
+
770
+ //Provides: caml_unix_getgid
771
+ //Alias: unix_getgid
772
+ function caml_unix_getgid(_unit) {
773
+ if (globalThis.process?.getgid) {
774
+ return globalThis.process.getgid();
775
+ }
776
+ return 1;
777
+ }
778
+
779
+ //Provides: caml_unix_getegid
780
+ //Alias: unix_getegid
781
+ function caml_unix_getegid(_unit) {
782
+ if (globalThis.process?.getegid) {
783
+ return globalThis.process.getegid();
784
+ }
785
+ return 1;
786
+ }
787
+
788
+ //Provides: caml_unix_getpwnam
789
+ //Requires: caml_raise_not_found
790
+ //Alias: unix_getpwnam
791
+ //Alias: caml_unix_getpwuid
792
+ //Alias: unix_getpwuid
793
+ //Alias: caml_unix_getgrnam
794
+ //Alias: unix_getgrnam
795
+ //Alias: caml_unix_getgrgid
796
+ //Alias: unix_getgrgid
797
+ function caml_unix_getpwnam(_unit) {
798
+ caml_raise_not_found();
799
+ }
800
+
801
+ //Provides: caml_unix_has_symlink
802
+ //Requires: fs_node_supported
803
+ //Alias: unix_has_symlink
804
+ function caml_unix_has_symlink(_unit) {
805
+ return fs_node_supported() ? 1 : 0;
806
+ }
807
+
808
+ //Provides: caml_unix_opendir
809
+ //Requires: resolve_fs_device, caml_failwith
810
+ //Alias: unix_opendir
811
+ function caml_unix_opendir(path) {
812
+ var root = resolve_fs_device(path, /* raise Unix_error */ 1);
813
+ if (!root.device.opendir) {
814
+ caml_failwith("caml_unix_opendir: not implemented");
815
+ }
816
+ var dir_handle = root.device.opendir(root.rest, /* raise Unix_error */ true);
817
+ // node's Dir.readSync does not report "." and ".."; native readdir does
818
+ return { pointer: dir_handle, path: path, dots: [".", ".."] };
819
+ }
820
+
821
+ //Provides: caml_unix_readdir
822
+ //Requires: caml_raise_end_of_file
823
+ //Requires: caml_string_of_jsstring
824
+ //Requires: caml_raise_system_error
825
+ //Alias: unix_readdir
826
+ function caml_unix_readdir(dir_handle) {
827
+ var entry;
828
+ if (dir_handle.dots && dir_handle.dots.length > 0)
829
+ return caml_string_of_jsstring(dir_handle.dots.shift());
830
+ try {
831
+ entry = dir_handle.pointer.readSync();
832
+ } catch (e) {
833
+ caml_raise_system_error(/* raise Unix_error */ 1, "EBADF", "readdir");
834
+ }
835
+ if (entry === null) {
836
+ caml_raise_end_of_file();
837
+ } else {
838
+ return caml_string_of_jsstring(entry.name);
839
+ }
840
+ }
841
+
842
+ //Provides: caml_unix_closedir
843
+ //Requires: caml_raise_system_error
844
+ //Alias: unix_closedir
845
+ function caml_unix_closedir(dir_handle) {
846
+ // drop the synthesized "." / ".." so readdir on a closed handle fails
847
+ dir_handle.dots = null;
848
+ try {
849
+ dir_handle.pointer.closeSync();
850
+ } catch (e) {
851
+ caml_raise_system_error(/* raise Unix_error */ 1, "EBADF", "closedir");
852
+ }
853
+ }
854
+
855
+ //Provides: caml_unix_rewinddir
856
+ //Requires: caml_unix_closedir, caml_unix_opendir
857
+ //Alias: unix_rewinddir
858
+ function caml_unix_rewinddir(dir_handle) {
859
+ caml_unix_closedir(dir_handle);
860
+ var new_dir_handle = caml_unix_opendir(dir_handle.path);
861
+ dir_handle.pointer = new_dir_handle.pointer;
862
+ dir_handle.dots = new_dir_handle.dots;
863
+ return 0;
864
+ }
865
+
866
+ //Provides: caml_unix_findfirst
867
+ //Requires: caml_jsstring_of_string, caml_string_of_jsstring
868
+ //Requires: caml_unix_opendir, caml_unix_readdir
869
+ //Alias: win_findfirst
870
+ function caml_unix_findfirst(path) {
871
+ // The Windows code adds this glob to the path, so we need to remove it
872
+ var path_js = caml_jsstring_of_string(path);
873
+ path_js = path_js.replace(/(^|[\\/])\*\.\*$/, "");
874
+ path = caml_string_of_jsstring(path_js);
875
+ // *.* is now stripped
876
+ var dir_handle = caml_unix_opendir(path);
877
+ var first_entry = caml_unix_readdir(dir_handle);
878
+ // The Windows bindings type dir_handle as an `int` but it's not in JS
879
+ return [0, first_entry, dir_handle];
880
+ }
881
+
882
+ //Provides: caml_unix_findnext
883
+ //Requires: caml_unix_readdir
884
+ //Alias: win_findnext
885
+ function caml_unix_findnext(dir_handle) {
886
+ return caml_unix_readdir(dir_handle);
887
+ }
888
+
889
+ //Provides: caml_unix_findclose
890
+ //Requires: caml_unix_closedir
891
+ //Alias: win_findclose
892
+ function caml_unix_findclose(dir_handle) {
893
+ return caml_unix_closedir(dir_handle);
894
+ }
895
+
896
+ //Provides: caml_unix_inet_addr_of_string const
897
+ //Alias: unix_inet_addr_of_string
898
+ function caml_unix_inet_addr_of_string() {
899
+ return 0;
900
+ }
901
+
902
+ //Provides: caml_raise_system_error
903
+ //Requires: caml_raise_with_args, make_unix_err_args, caml_named_value
904
+ //Requires: caml_raise_sys_error
905
+ function caml_raise_system_error(raise_unix, code, cmd, msg, path) {
906
+ var unix_error = caml_named_value("Unix.Unix_error");
907
+ if (raise_unix && unix_error)
908
+ caml_raise_with_args(unix_error, make_unix_err_args(code, cmd, path));
909
+ else {
910
+ var msg = code + ": " + msg + ", " + cmd;
911
+ if (path !== undefined) msg += " '" + path + "'";
912
+ caml_raise_sys_error(msg);
913
+ }
914
+ }