@live-codes/ocaml 0.0.0-stage → 0.1.0

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 +170 -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,739 @@
1
+ // Js_of_ocaml runtime support
2
+ // http://www.ocsigen.org/js_of_ocaml/
3
+ // Copyright (C) 2014 Jérôme Vouillon, Hugo Heuzard
4
+ // Laboratoire PPS - CNRS Université Paris Diderot
5
+ //
6
+ // This program is free software; you can redistribute it and/or modify
7
+ // it under the terms of the GNU Lesser General Public License as published by
8
+ // the Free Software Foundation, with linking exception;
9
+ // either version 2.1 of the License, or (at your option) any later version.
10
+ //
11
+ // This program is distributed in the hope that it will be useful,
12
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ // GNU Lesser General Public License for more details.
15
+ //
16
+ // You should have received a copy of the GNU Lesser General Public License
17
+ // along with this program; if not, write to the Free Software
18
+ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
+
20
+ ///////////// Io
21
+
22
+ //Provides: caml_sys_fds
23
+ var caml_sys_fds = new Array(3);
24
+
25
+ //Provides: caml_sys_close
26
+ //Requires: caml_sys_fds
27
+ function caml_sys_close(fd) {
28
+ var x = caml_sys_fds[fd];
29
+ if (x) {
30
+ x.file.close(false);
31
+ delete caml_sys_fds[fd];
32
+ }
33
+ return 0;
34
+ }
35
+
36
+ //Provides: MlChanid
37
+ function MlChanid(id) {
38
+ this.id = id;
39
+ }
40
+
41
+ //Provides: caml_sys_open
42
+ //Requires: caml_raise_sys_error
43
+ //Requires: MlFakeFd_out
44
+ //Requires: resolve_fs_device
45
+ //Requires: fs_node_supported
46
+ //Requires: fs_quickjs_supported
47
+ //Requires: caml_sys_fds
48
+ //Requires: caml_sys_open_for_node
49
+ //Requires: caml_sys_open_for_quickjs
50
+ function caml_sys_open_internal(file, idx) {
51
+ if (idx === undefined) idx = caml_sys_fds.length;
52
+ caml_sys_fds[idx] = { file: file };
53
+ return idx | 0;
54
+ }
55
+ function caml_sys_open(name, flags, perms) {
56
+ var f = {};
57
+ while (flags) {
58
+ switch (flags[1]) {
59
+ case 0:
60
+ f.rdonly = 1;
61
+ break;
62
+ case 1:
63
+ f.wronly = 1;
64
+ break;
65
+ case 2:
66
+ // Open_append implies write access (O_WRONLY | O_APPEND in the
67
+ // C runtime)
68
+ f.append = 1;
69
+ f.wronly = 1;
70
+ break;
71
+ case 3:
72
+ f.create = 1;
73
+ break;
74
+ case 4:
75
+ f.truncate = 1;
76
+ break;
77
+ case 5:
78
+ f.excl = 1;
79
+ break;
80
+ case 6:
81
+ f.binary = 1;
82
+ break;
83
+ case 7:
84
+ f.text = 1;
85
+ break;
86
+ case 8:
87
+ f.nonblock = 1;
88
+ break;
89
+ }
90
+ flags = flags[2];
91
+ }
92
+ var root = resolve_fs_device(name);
93
+ var file = root.device.open(root.rest, f, perms);
94
+ return caml_sys_open_internal(file, undefined);
95
+ }
96
+ (function () {
97
+ var is_node = fs_node_supported();
98
+ var is_qjs = !is_node && fs_quickjs_supported();
99
+ var buffered = is_node || is_qjs ? 1 : 2;
100
+ function file(fd, flags) {
101
+ if (is_node) return caml_sys_open_for_node(fd, flags);
102
+ if (is_qjs) return caml_sys_open_for_quickjs(fd, flags);
103
+ return new MlFakeFd_out(fd, flags);
104
+ }
105
+ caml_sys_open_internal(
106
+ file(0, { rdonly: 1, altname: "/dev/stdin", isCharacterDevice: true }),
107
+ 0,
108
+ );
109
+ caml_sys_open_internal(
110
+ file(1, { buffered: buffered, wronly: 1, isCharacterDevice: true }),
111
+ 1,
112
+ );
113
+ caml_sys_open_internal(
114
+ file(2, { buffered: buffered, wronly: 1, isCharacterDevice: true }),
115
+ 2,
116
+ );
117
+ })();
118
+
119
+ // ocaml Channels
120
+
121
+ //Provides: caml_ml_set_channel_name
122
+ //Requires: caml_ml_channel_get
123
+ function caml_ml_set_channel_name(chanid, name) {
124
+ var chan = caml_ml_channel_get(chanid);
125
+ chan.name = name;
126
+ return 0;
127
+ }
128
+
129
+ //Provides: caml_ml_std_channel_id
130
+ // The channels created by the stdlib at startup for fds 0, 1 and 2
131
+ // (the runtime itself writes through them, e.g. the parser trace)
132
+ var caml_ml_std_channel_id = [];
133
+
134
+ //Provides: caml_ml_channels
135
+ //Requires: MlChanid
136
+ class caml_ml_channels_state {
137
+ constructor() {
138
+ this.map = new globalThis.WeakMap();
139
+ this.opened = new globalThis.Set();
140
+ }
141
+
142
+ close(chanid) {
143
+ this.opened.delete(chanid);
144
+ }
145
+
146
+ get(chanid) {
147
+ return this.map.get(chanid);
148
+ }
149
+
150
+ set(chanid, val) {
151
+ if (val.opened) this.opened.add(chanid);
152
+ return this.map.set(chanid, val);
153
+ }
154
+
155
+ all() {
156
+ return this.opened.values();
157
+ }
158
+ }
159
+
160
+ var caml_ml_channels = new caml_ml_channels_state();
161
+
162
+ //Provides: caml_ml_channel_get
163
+ //Requires: caml_ml_channels
164
+ function caml_ml_channel_get(id) {
165
+ return caml_ml_channels.get(id);
166
+ }
167
+
168
+ //Provides: caml_ml_channel_redirect
169
+ //Requires: caml_ml_channel_get, caml_ml_channels
170
+ function caml_ml_channel_redirect(captured, into) {
171
+ var to_restore = caml_ml_channel_get(captured);
172
+ var new_ = caml_ml_channel_get(into);
173
+ caml_ml_channels.set(captured, new_);
174
+ return to_restore;
175
+ }
176
+
177
+ //Provides: caml_ml_channel_restore
178
+ //Requires: caml_ml_channels
179
+ function caml_ml_channel_restore(captured, to_restore) {
180
+ caml_ml_channels.set(captured, to_restore);
181
+ return 0;
182
+ }
183
+
184
+ //Provides: caml_ml_out_channels_list
185
+ //Requires: caml_ml_channels
186
+ //Requires: caml_ml_channel_get
187
+ function caml_ml_out_channels_list() {
188
+ var l = 0;
189
+ var keys = caml_ml_channels.all();
190
+ for (var k of keys) {
191
+ var chan = caml_ml_channel_get(k);
192
+ if (chan.opened && chan.out) l = [0, k, l];
193
+ }
194
+ return l;
195
+ }
196
+
197
+ //Provides: caml_ml_open_descriptor_out
198
+ //Requires: caml_ml_channels, caml_sys_fds
199
+ //Requires: caml_raise_sys_error
200
+ //Requires: caml_sys_open
201
+ //Requires: caml_io_buffer_size
202
+ //Requires: MlChanid, caml_ml_std_channel_id
203
+ function caml_ml_open_descriptor_out(fd) {
204
+ var fd_desc = caml_sys_fds[fd];
205
+ if (fd_desc === undefined)
206
+ caml_raise_sys_error("fd " + fd + " doesn't exist");
207
+ var file = fd_desc.file;
208
+ // each call allocates a fresh channel, like the C runtime
209
+ var chanid = new MlChanid(fd);
210
+ var buffered = file.flags.buffered !== undefined ? file.flags.buffered : 1;
211
+ var channel = {
212
+ file: file,
213
+ offset: file.pos(),
214
+ fd: fd,
215
+ opened: true,
216
+ out: true,
217
+ buffer_curr: 0,
218
+ buffer: new Uint8Array(caml_io_buffer_size),
219
+ buffered: buffered,
220
+ };
221
+ caml_ml_channels.set(chanid, channel);
222
+ if (fd <= 2 && !caml_ml_std_channel_id[fd])
223
+ caml_ml_std_channel_id[fd] = chanid;
224
+ return chanid;
225
+ }
226
+
227
+ //Provides: caml_ml_open_descriptor_in
228
+ //Requires: caml_ml_channels, caml_sys_fds
229
+ //Requires: caml_raise_sys_error
230
+ //Requires: caml_sys_open
231
+ //Requires: caml_io_buffer_size
232
+ //Requires: MlChanid, caml_ml_std_channel_id
233
+ function caml_ml_open_descriptor_in(fd) {
234
+ var fd_desc = caml_sys_fds[fd];
235
+ if (fd_desc === undefined)
236
+ caml_raise_sys_error("fd " + fd + " doesn't exist");
237
+ var file = fd_desc.file;
238
+ // each call allocates a fresh channel, like the C runtime
239
+ var chanid = new MlChanid(fd);
240
+ var refill = null;
241
+ var channel = {
242
+ file: file,
243
+ offset: file.pos(),
244
+ fd: fd,
245
+ opened: true,
246
+ out: false,
247
+ buffer_curr: 0,
248
+ buffer_max: 0,
249
+ buffer: new Uint8Array(caml_io_buffer_size),
250
+ refill: refill,
251
+ };
252
+ caml_ml_channels.set(chanid, channel);
253
+ if (fd <= 2 && !caml_ml_std_channel_id[fd])
254
+ caml_ml_std_channel_id[fd] = chanid;
255
+ return chanid;
256
+ }
257
+
258
+ //Provides: caml_ml_open_descriptor_in_with_flags
259
+ //Requires: caml_ml_open_descriptor_in
260
+ //Version: >= 5.1
261
+ function caml_ml_open_descriptor_in_with_flags(fd, _flags) {
262
+ return caml_ml_open_descriptor_in(fd);
263
+ }
264
+
265
+ //Provides: caml_ml_open_descriptor_out_with_flags
266
+ //Requires: caml_ml_open_descriptor_out
267
+ //Version: >= 5.1
268
+ function caml_ml_open_descriptor_out_with_flags(fd, _flags) {
269
+ return caml_ml_open_descriptor_out(fd);
270
+ }
271
+
272
+ //Provides: caml_channel_descriptor
273
+ //Requires: caml_ml_channel_get
274
+ //Alias: win_filedescr_of_channel
275
+ function caml_channel_descriptor(chanid) {
276
+ var chan = caml_ml_channel_get(chanid);
277
+ return chan.fd;
278
+ }
279
+
280
+ //Provides: caml_ml_set_binary_mode
281
+ //Requires: caml_ml_channel_get
282
+ function caml_ml_set_binary_mode(chanid, mode) {
283
+ var chan = caml_ml_channel_get(chanid);
284
+ chan.file.flags.text = !mode;
285
+ chan.file.flags.binary = mode;
286
+ return 0;
287
+ }
288
+
289
+ //Provides: caml_ml_is_binary_mode
290
+ //Version: >= 5.2
291
+ function caml_ml_is_binary_mode(_chanid) {
292
+ // Like the C runtime on Unix, which always reports binary mode
293
+ // (set_binary_mode is a no-op there).
294
+ return 1;
295
+ }
296
+
297
+ //Input from in_channel
298
+
299
+ //Provides: caml_ml_close_channel
300
+ //Requires: caml_ml_flush, caml_ml_channel_get
301
+ //Requires: caml_sys_close
302
+ //Requires: caml_ml_channels
303
+ function caml_ml_close_channel(chanid) {
304
+ var chan = caml_ml_channel_get(chanid);
305
+ if (chan.opened) {
306
+ chan.opened = false;
307
+ caml_ml_channels.close(chanid);
308
+ caml_sys_close(chan.fd);
309
+ chan.fd = -1;
310
+ chan.buffer = new Uint8Array(0);
311
+ chan.buffer_curr = 0;
312
+ chan.buffer_max = 0;
313
+ }
314
+ return 0;
315
+ }
316
+
317
+ //Provides: caml_ml_channel_size
318
+ //Requires: caml_ml_channel_get
319
+ function caml_ml_channel_size(chanid) {
320
+ var chan = caml_ml_channel_get(chanid);
321
+ return chan.file.length() | 0;
322
+ }
323
+
324
+ //Provides: caml_ml_channel_size_64
325
+ //Requires: caml_int64_of_float,caml_ml_channel_get
326
+ function caml_ml_channel_size_64(chanid) {
327
+ var chan = caml_ml_channel_get(chanid);
328
+ return caml_int64_of_float(chan.file.length());
329
+ }
330
+
331
+ //Provides: caml_ml_set_channel_output
332
+ //Requires: caml_ml_channel_get
333
+ function caml_ml_set_channel_output(chanid, f) {
334
+ var chan = caml_ml_channel_get(chanid);
335
+ chan.output = function (s) {
336
+ f(s);
337
+ };
338
+ return 0;
339
+ }
340
+
341
+ //Provides: caml_ml_set_channel_refill
342
+ //Requires: caml_ml_channel_get
343
+ function caml_ml_set_channel_refill(chanid, f) {
344
+ caml_ml_channel_get(chanid).refill = f;
345
+ return 0;
346
+ }
347
+
348
+ //Provides: caml_refill
349
+ //Requires: caml_ml_string_length, caml_uint8_array_of_string
350
+ //Requires: caml_raise_sys_error
351
+ function caml_refill(chan) {
352
+ if (chan.refill != null) {
353
+ var str = chan.refill();
354
+ var str_a = caml_uint8_array_of_string(str);
355
+ if (str_a.length === 0) {
356
+ chan.refill = null;
357
+ } else {
358
+ if (chan.buffer.length < chan.buffer_max + str_a.length) {
359
+ var b = new Uint8Array(chan.buffer_max + str_a.length);
360
+ b.set(chan.buffer);
361
+ chan.buffer = b;
362
+ }
363
+ chan.buffer.set(str_a, chan.buffer_max);
364
+ chan.offset += str_a.length;
365
+ chan.buffer_max += str_a.length;
366
+ }
367
+ } else {
368
+ if (chan.fd === -1) {
369
+ caml_raise_sys_error("Bad file descriptor");
370
+ }
371
+ var nread = chan.file.read(
372
+ chan.buffer,
373
+ chan.buffer_max,
374
+ chan.buffer.length - chan.buffer_max,
375
+ false,
376
+ );
377
+ chan.offset += nread;
378
+ chan.buffer_max += nread;
379
+ }
380
+ }
381
+
382
+ //Provides: caml_ml_input
383
+ //Requires: caml_ml_input_block
384
+ //Requires: caml_uint8_array_of_bytes
385
+ function caml_ml_input(chanid, b, i, l) {
386
+ var ba = caml_uint8_array_of_bytes(b);
387
+ return caml_ml_input_block(chanid, ba, i, l);
388
+ }
389
+
390
+ //Provides: caml_ml_input_bigarray
391
+ //Requires: caml_ml_input_block
392
+ //Requires: caml_ba_to_typed_array
393
+ //Version: >= 5.2
394
+ function caml_ml_input_bigarray(chanid, b, i, l) {
395
+ var ba = caml_ba_to_typed_array(b);
396
+ return caml_ml_input_block(chanid, ba, i, l);
397
+ }
398
+
399
+ //Provides: caml_ml_input_block
400
+ //Requires: caml_refill, caml_ml_channel_get
401
+ function caml_ml_input_block(chanid, ba, i, l) {
402
+ var chan = caml_ml_channel_get(chanid);
403
+ var n = l;
404
+ var avail = chan.buffer_max - chan.buffer_curr;
405
+ if (l <= avail) {
406
+ ba.set(chan.buffer.subarray(chan.buffer_curr, chan.buffer_curr + l), i);
407
+ chan.buffer_curr += l;
408
+ } else if (avail > 0) {
409
+ ba.set(chan.buffer.subarray(chan.buffer_curr, chan.buffer_curr + avail), i);
410
+ chan.buffer_curr += avail;
411
+ n = avail;
412
+ } else {
413
+ chan.buffer_curr = 0;
414
+ chan.buffer_max = 0;
415
+ caml_refill(chan);
416
+ var avail = chan.buffer_max - chan.buffer_curr;
417
+ if (n > avail) n = avail;
418
+ ba.set(chan.buffer.subarray(chan.buffer_curr, chan.buffer_curr + n), i);
419
+ chan.buffer_curr += n;
420
+ }
421
+ return n | 0;
422
+ }
423
+
424
+ //Provides: caml_input_value
425
+ //Requires: caml_marshal_data_size, caml_input_value_from_bytes, caml_create_bytes, caml_ml_channel_get, caml_bytes_of_uint8_array
426
+ //Requires: caml_refill, caml_failwith, caml_raise_end_of_file
427
+ //Requires: caml_marshal_header_size
428
+ function caml_input_value(chanid) {
429
+ var chan = caml_ml_channel_get(chanid);
430
+ var header = new Uint8Array(caml_marshal_header_size);
431
+ function block(buffer, offset, n) {
432
+ var r = 0;
433
+ while (r < n) {
434
+ if (chan.buffer_curr >= chan.buffer_max) {
435
+ chan.buffer_curr = 0;
436
+ chan.buffer_max = 0;
437
+ caml_refill(chan);
438
+ }
439
+ if (chan.buffer_curr >= chan.buffer_max) break;
440
+ buffer[offset + r] = chan.buffer[chan.buffer_curr];
441
+ chan.buffer_curr++;
442
+ r++;
443
+ }
444
+ return r;
445
+ }
446
+ var r = block(header, 0, caml_marshal_header_size);
447
+ if (r === 0) caml_raise_end_of_file();
448
+ else if (r < caml_marshal_header_size)
449
+ caml_failwith("input_value: truncated object");
450
+ var len = caml_marshal_data_size(caml_bytes_of_uint8_array(header), 0);
451
+ var buf = new Uint8Array(len + caml_marshal_header_size);
452
+ buf.set(header, 0);
453
+ var r = block(buf, caml_marshal_header_size, len);
454
+ if (r < len) caml_failwith("input_value: truncated object " + r + " " + len);
455
+ var res = caml_input_value_from_bytes(caml_bytes_of_uint8_array(buf), 0);
456
+ return res;
457
+ }
458
+
459
+ //Provides: caml_input_value_to_outside_heap
460
+ //Requires: caml_input_value
461
+ //Version: >= 5
462
+ function caml_input_value_to_outside_heap(c) {
463
+ return caml_input_value(c);
464
+ }
465
+
466
+ //Provides: caml_ml_input_char
467
+ //Requires: caml_raise_end_of_file, caml_array_bound_error
468
+ //Requires: caml_ml_channel_get, caml_refill
469
+ function caml_ml_input_char(chanid) {
470
+ var chan = caml_ml_channel_get(chanid);
471
+ if (chan.buffer_curr >= chan.buffer_max) {
472
+ chan.buffer_curr = 0;
473
+ chan.buffer_max = 0;
474
+ caml_refill(chan);
475
+ }
476
+ if (chan.buffer_curr >= chan.buffer_max) caml_raise_end_of_file();
477
+ var res = chan.buffer[chan.buffer_curr];
478
+ chan.buffer_curr++;
479
+ return res;
480
+ }
481
+
482
+ //Provides: caml_ml_input_int
483
+ //Requires: caml_ml_input_char
484
+ function caml_ml_input_int(chanid) {
485
+ var res = 0;
486
+ for (var i = 0; i < 4; i++) {
487
+ res = ((res << 8) + caml_ml_input_char(chanid)) | 0;
488
+ }
489
+ return res | 0;
490
+ }
491
+
492
+ //Provides: caml_seek_in
493
+ //Requires: caml_raise_sys_error, caml_ml_channel_get
494
+ function caml_seek_in(chanid, pos) {
495
+ var chan = caml_ml_channel_get(chanid);
496
+ if (chan.refill != null) caml_raise_sys_error("Illegal seek");
497
+ if (
498
+ pos >= chan.offset - chan.buffer_max &&
499
+ pos <= chan.offset &&
500
+ chan.file.flags.binary
501
+ ) {
502
+ chan.buffer_curr = chan.buffer_max - (chan.offset - pos);
503
+ } else {
504
+ chan.file.seek(pos, 0);
505
+ chan.offset = pos;
506
+ chan.buffer_curr = 0;
507
+ chan.buffer_max = 0;
508
+ }
509
+ return 0;
510
+ }
511
+
512
+ //Provides: caml_ml_seek_in
513
+ //Requires: caml_seek_in
514
+ function caml_ml_seek_in(chanid, pos) {
515
+ return caml_seek_in(chanid, pos);
516
+ }
517
+
518
+ //Provides: caml_ml_seek_in_64
519
+ //Requires: caml_int64_to_float, caml_seek_in
520
+ function caml_ml_seek_in_64(chanid, pos) {
521
+ var pos = caml_int64_to_float(pos);
522
+ return caml_seek_in(chanid, pos);
523
+ }
524
+
525
+ //Provides: caml_pos_in
526
+ //Requires: caml_ml_channel_get
527
+ function caml_pos_in(chanid) {
528
+ var chan = caml_ml_channel_get(chanid);
529
+ return chan.offset - (chan.buffer_max - chan.buffer_curr);
530
+ }
531
+
532
+ //Provides: caml_ml_pos_in
533
+ //Requires: caml_pos_in
534
+ function caml_ml_pos_in(chanid) {
535
+ return caml_pos_in(chanid) | 0;
536
+ }
537
+
538
+ //Provides: caml_ml_pos_in_64
539
+ //Requires: caml_int64_of_float, caml_pos_in
540
+ function caml_ml_pos_in_64(chanid) {
541
+ return caml_int64_of_float(caml_pos_in(chanid));
542
+ }
543
+
544
+ //Provides: caml_ml_input_scan_line
545
+ //Requires: caml_array_bound_error
546
+ //Requires: caml_ml_channel_get, caml_refill
547
+ function caml_ml_input_scan_line(chanid) {
548
+ var chan = caml_ml_channel_get(chanid);
549
+ var p = chan.buffer_curr;
550
+ do {
551
+ if (p >= chan.buffer_max) {
552
+ if (chan.buffer_curr > 0) {
553
+ chan.buffer.set(chan.buffer.subarray(chan.buffer_curr), 0);
554
+ p -= chan.buffer_curr;
555
+ chan.buffer_max -= chan.buffer_curr;
556
+ chan.buffer_curr = 0;
557
+ }
558
+ if (chan.buffer_max >= chan.buffer.length) {
559
+ return -chan.buffer_max | 0;
560
+ }
561
+ var prev_max = chan.buffer_max;
562
+ caml_refill(chan);
563
+ if (prev_max === chan.buffer_max) {
564
+ return -chan.buffer_max | 0;
565
+ }
566
+ }
567
+ } while (chan.buffer[p++] !== 10);
568
+ return (p - chan.buffer_curr) | 0;
569
+ }
570
+
571
+ //Provides: caml_ml_flush
572
+ //Requires: caml_ml_channel_get
573
+ //Requires: caml_sub_uint8_array_to_jsbytes
574
+ function caml_ml_flush(chanid) {
575
+ var chan = caml_ml_channel_get(chanid);
576
+ if (!chan.opened) return 0;
577
+ if (!chan.buffer || chan.buffer_curr === 0) return 0;
578
+ if (chan.output) {
579
+ chan.output(
580
+ caml_sub_uint8_array_to_jsbytes(chan.buffer, 0, chan.buffer_curr),
581
+ );
582
+ } else {
583
+ for (var pos = 0; pos < chan.buffer_curr; ) {
584
+ pos += chan.file.write(chan.buffer, pos, chan.buffer_curr - pos, false);
585
+ }
586
+ }
587
+ chan.offset += chan.buffer_curr;
588
+ chan.buffer_curr = 0;
589
+ return 0;
590
+ }
591
+
592
+ //output to out_channel
593
+
594
+ //Provides: caml_ml_output_ta
595
+ //Requires: caml_ml_flush,caml_ml_bytes_length
596
+ //Requires: caml_raise_sys_error, caml_ml_channel_get
597
+ function caml_ml_output_ta(chanid, buffer, offset, len) {
598
+ var chan = caml_ml_channel_get(chanid);
599
+ if (!chan.opened) caml_raise_sys_error("Cannot output to a closed channel");
600
+ buffer = buffer.subarray(offset, offset + len);
601
+ if (chan.buffer_curr + buffer.length > chan.buffer.length) {
602
+ var b = new Uint8Array(chan.buffer_curr + buffer.length);
603
+ b.set(chan.buffer);
604
+ chan.buffer = b;
605
+ }
606
+ switch (chan.buffered) {
607
+ case 0: // Unbuffered
608
+ chan.buffer.set(buffer, chan.buffer_curr);
609
+ chan.buffer_curr += buffer.length;
610
+ caml_ml_flush(chanid);
611
+ break;
612
+ case 1: // Buffered (the default)
613
+ chan.buffer.set(buffer, chan.buffer_curr);
614
+ chan.buffer_curr += buffer.length;
615
+ if (chan.buffer_curr >= chan.buffer.length) caml_ml_flush(chanid);
616
+ break;
617
+ case 2: // Buffered (only for stdout and stderr)
618
+ var id = buffer.lastIndexOf(10);
619
+ if (id < 0) {
620
+ chan.buffer.set(buffer, chan.buffer_curr);
621
+ chan.buffer_curr += buffer.length;
622
+ if (chan.buffer_curr >= chan.buffer.length) caml_ml_flush(chanid);
623
+ } else {
624
+ chan.buffer.set(buffer.subarray(0, id + 1), chan.buffer_curr);
625
+ chan.buffer_curr += id + 1;
626
+ caml_ml_flush(chanid);
627
+ chan.buffer.set(buffer.subarray(id + 1), chan.buffer_curr);
628
+ chan.buffer_curr += buffer.length - id - 1;
629
+ }
630
+ break;
631
+ }
632
+ return 0;
633
+ }
634
+
635
+ //Provides: caml_ml_output_bytes
636
+ //Requires: caml_uint8_array_of_bytes, caml_ml_output_ta
637
+ function caml_ml_output_bytes(chanid, buffer, offset, len) {
638
+ var buffer = caml_uint8_array_of_bytes(buffer);
639
+ return caml_ml_output_ta(chanid, buffer, offset, len);
640
+ }
641
+
642
+ //Provides: caml_ml_output_bigarray
643
+ //Requires: caml_ba_to_typed_array, caml_ml_output_ta
644
+ //Version: >= 5.2
645
+ function caml_ml_output_bigarray(chanid, buffer, offset, len) {
646
+ var buffer = caml_ba_to_typed_array(buffer);
647
+ return caml_ml_output_ta(chanid, buffer, offset, len);
648
+ }
649
+
650
+ //Provides: caml_ml_output
651
+ //Requires: caml_ml_output_bytes, caml_bytes_of_string
652
+ function caml_ml_output(chanid, buffer, offset, len) {
653
+ return caml_ml_output_bytes(
654
+ chanid,
655
+ caml_bytes_of_string(buffer),
656
+ offset,
657
+ len,
658
+ );
659
+ }
660
+
661
+ //Provides: caml_ml_output_char
662
+ //Requires: caml_ml_output
663
+ //Requires: caml_string_of_jsbytes
664
+ function caml_ml_output_char(chanid, c) {
665
+ var s = caml_string_of_jsbytes(String.fromCharCode(c));
666
+ caml_ml_output(chanid, s, 0, 1);
667
+ return 0;
668
+ }
669
+
670
+ //Provides: caml_output_value
671
+ //Requires: caml_output_value_to_string, caml_ml_output,caml_ml_string_length
672
+ function caml_output_value(chanid, v, flags) {
673
+ var s = caml_output_value_to_string(v, flags);
674
+ caml_ml_output(chanid, s, 0, caml_ml_string_length(s));
675
+ return 0;
676
+ }
677
+
678
+ //Provides: caml_seek_out
679
+ //Requires: caml_ml_channel_get, caml_ml_flush
680
+ function caml_seek_out(chanid, pos) {
681
+ caml_ml_flush(chanid);
682
+ var chan = caml_ml_channel_get(chanid);
683
+ chan.file.seek(pos, 0);
684
+ chan.offset = pos;
685
+ return 0;
686
+ }
687
+
688
+ //Provides: caml_ml_seek_out
689
+ //Requires: caml_seek_out
690
+ function caml_ml_seek_out(chanid, pos) {
691
+ return caml_seek_out(chanid, pos);
692
+ }
693
+ //Provides: caml_ml_seek_out_64
694
+ //Requires: caml_int64_to_float, caml_seek_out
695
+ function caml_ml_seek_out_64(chanid, pos) {
696
+ var pos = caml_int64_to_float(pos);
697
+ return caml_seek_out(chanid, pos);
698
+ }
699
+
700
+ //Provides: caml_pos_out
701
+ //Requires: caml_ml_channel_get, caml_ml_flush
702
+ function caml_pos_out(chanid) {
703
+ var chan = caml_ml_channel_get(chanid);
704
+ return chan.offset + chan.buffer_curr;
705
+ }
706
+
707
+ //Provides: caml_ml_pos_out
708
+ //Requires: caml_pos_out
709
+ function caml_ml_pos_out(chanid) {
710
+ return caml_pos_out(chanid) | 0;
711
+ }
712
+
713
+ //Provides: caml_ml_pos_out_64
714
+ //Requires: caml_int64_of_float, caml_pos_out
715
+ function caml_ml_pos_out_64(chanid) {
716
+ return caml_int64_of_float(caml_pos_out(chanid));
717
+ }
718
+
719
+ //Provides: caml_ml_output_int
720
+ //Requires: caml_ml_output_ta
721
+ function caml_ml_output_int(chanid, i) {
722
+ var arr = [(i >> 24) & 0xff, (i >> 16) & 0xff, (i >> 8) & 0xff, i & 0xff];
723
+ caml_ml_output_ta(chanid, new Uint8Array(arr), 0, 4);
724
+ return 0;
725
+ }
726
+
727
+ //Provides: caml_ml_is_buffered
728
+ //Requires: caml_ml_channel_get
729
+ function caml_ml_is_buffered(chanid) {
730
+ return caml_ml_channel_get(chanid).buffered ? 1 : 0;
731
+ }
732
+
733
+ //Provides: caml_ml_set_buffered
734
+ //Requires: caml_ml_channel_get, caml_ml_flush
735
+ function caml_ml_set_buffered(chanid, v) {
736
+ caml_ml_channel_get(chanid).buffered = v;
737
+ if (!v) caml_ml_flush(chanid);
738
+ return 0;
739
+ }