@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,621 @@
1
+ // Js_of_ocaml runtime support
2
+ // http://www.ocsigen.org/js_of_ocaml/
3
+ // Copyright (C) 2010 Jérôme Vouillon
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
+ //Provides: jsoo_dataview
21
+ var jsoo_dataview = new DataView(new ArrayBuffer(8));
22
+
23
+ //Provides: caml_int64_bits_of_float const
24
+ //Requires: caml_int64_create_lo_mi_hi
25
+ //Requires: jsoo_dataview
26
+ function caml_int64_bits_of_float(x) {
27
+ jsoo_dataview.setFloat64(0, x, true);
28
+ var lo32 = jsoo_dataview.getUint32(0, true);
29
+ var hi32 = jsoo_dataview.getUint32(4, true);
30
+ var lo = lo32 & 0xffffff;
31
+ var mi = (lo32 >>> 24) | ((hi32 << 8) & 0xffffff);
32
+ var hi = (hi32 >>> 16) & 0xffff;
33
+ // V8 uses signaling NaNs as sentinel. So, NaNs are made quiet when
34
+ // they are stored in an array. Make them quiet here so that we get
35
+ // consistent results.
36
+ if ((hi & 0x7ff8) === 0x7ff0 && (mi | lo | (hi & 0xf)) !== 0) hi |= 8;
37
+ return caml_int64_create_lo_mi_hi(lo, mi, hi);
38
+ }
39
+
40
+ //Provides: caml_int32_bits_of_float const
41
+ //Requires: jsoo_dataview
42
+ function caml_int32_bits_of_float(x) {
43
+ jsoo_dataview.setFloat32(0, x, true);
44
+ return jsoo_dataview.getUint32(0, true) | 0;
45
+ }
46
+
47
+ //FP literals can be written using the hexadecimal
48
+ //notation 0x<mantissa in hex>p<exponent> from ISO C99.
49
+ //https://github.com/dankogai/js-hexfloat/blob/master/hexfloat.js
50
+ //Provides: caml_hexstring_of_float const
51
+ //Requires: caml_string_of_jsstring, caml_str_repeat
52
+ function caml_hexstring_of_float(x, prec, style) {
53
+ if (!Number.isFinite(x)) {
54
+ if (Number.isNaN(x)) return caml_string_of_jsstring("nan");
55
+ return caml_string_of_jsstring(x > 0 ? "infinity" : "-infinity");
56
+ }
57
+ var sign = x === 0 && 1 / x === Number.NEGATIVE_INFINITY ? 1 : x >= 0 ? 0 : 1;
58
+ if (sign) x = -x;
59
+ var exp = 0;
60
+ if (x === 0) {
61
+ } else if (x < 1) {
62
+ while (x < 1 && exp > -1022) {
63
+ x *= 2;
64
+ exp--;
65
+ }
66
+ } else {
67
+ while (x >= 2) {
68
+ x /= 2;
69
+ exp++;
70
+ }
71
+ }
72
+ var exp_sign = exp < 0 ? "" : "+";
73
+ var sign_str = "";
74
+ if (sign) sign_str = "-";
75
+ else {
76
+ switch (style) {
77
+ case 43 /* '+' */:
78
+ sign_str = "+";
79
+ break;
80
+ case 32 /* ' ' */:
81
+ sign_str = " ";
82
+ break;
83
+ default:
84
+ break;
85
+ }
86
+ }
87
+ if (prec >= 0 && prec < 13) {
88
+ /* If a precision is given, and is small, round mantissa accordingly */
89
+ var cst = Math.pow(2, prec * 4);
90
+ x = Math.round(x * cst) / cst;
91
+ }
92
+ var x_str = x.toString(16);
93
+ if (prec >= 0) {
94
+ var idx = x_str.indexOf(".");
95
+ if (idx < 0) {
96
+ x_str += "." + caml_str_repeat(prec, "0");
97
+ } else {
98
+ var size = idx + 1 + prec;
99
+ if (x_str.length < size)
100
+ x_str += caml_str_repeat(size - x_str.length, "0");
101
+ else x_str = x_str.slice(0, size);
102
+ }
103
+ }
104
+ return caml_string_of_jsstring(
105
+ sign_str + "0x" + x_str + "p" + exp_sign + exp.toString(10),
106
+ );
107
+ }
108
+
109
+ //Provides: caml_int64_float_of_bits const
110
+ //Requires: jsoo_dataview
111
+ function caml_int64_float_of_bits(x) {
112
+ var lo = x.lo;
113
+ var mi = x.mi;
114
+ var hi = x.hi;
115
+ // V8 uses signaling NaNs as sentinel. So, NaNs are made quiet when
116
+ // they are stored in an array. Make them quiet here so that we get
117
+ // consistent results.
118
+ if ((hi & 0x7ff8) === 0x7ff0 && (mi | lo | (hi & 0xf)) !== 0) hi |= 8;
119
+ jsoo_dataview.setUint32(0, lo | (mi << 24), true);
120
+ jsoo_dataview.setUint32(4, (mi >>> 8) | (hi << 16), true);
121
+ return jsoo_dataview.getFloat64(0, true);
122
+ }
123
+
124
+ //Provides: caml_nextafter_float const
125
+ //Requires: caml_int64_float_of_bits, caml_int64_bits_of_float, caml_int64_add, caml_int64_sub,caml_int64_of_int32
126
+ function caml_nextafter_float(x, y) {
127
+ if (Number.isNaN(x) || Number.isNaN(y)) return Number.NaN;
128
+ if (x === y) return y;
129
+ if (x === 0) {
130
+ if (y < 0) return -Math.pow(2, -1074);
131
+ else return Math.pow(2, -1074);
132
+ }
133
+ var bits = caml_int64_bits_of_float(x);
134
+ var one = caml_int64_of_int32(1);
135
+ if (x < y === x > 0) bits = caml_int64_add(bits, one);
136
+ else bits = caml_int64_sub(bits, one);
137
+ return caml_int64_float_of_bits(bits);
138
+ }
139
+
140
+ //Provides: caml_trunc_float const
141
+ function caml_trunc_float(x) {
142
+ return Math.trunc(x);
143
+ }
144
+
145
+ //Provides: caml_int32_float_of_bits const
146
+ //Requires: jsoo_dataview
147
+ function caml_int32_float_of_bits(x) {
148
+ jsoo_dataview.setUint32(0, x, true);
149
+ return jsoo_dataview.getFloat32(0, true);
150
+ }
151
+
152
+ //Provides: caml_classify_float const
153
+ function caml_classify_float(x) {
154
+ if (Number.isFinite(x)) {
155
+ if (Math.abs(x) >= 2.2250738585072014e-308) return 0;
156
+ if (x !== 0) return 1;
157
+ return 2;
158
+ }
159
+ return Number.isNaN(x) ? 4 : 3;
160
+ }
161
+ //Provides: caml_modf_float const
162
+ function caml_modf_float(x) {
163
+ if (Number.isFinite(x)) {
164
+ var neg = 1 / x < 0;
165
+ x = Math.abs(x);
166
+ var i = Math.floor(x);
167
+ var f = x - i;
168
+ if (neg) {
169
+ i = -i;
170
+ f = -f;
171
+ }
172
+ return [0, f, i];
173
+ }
174
+ if (Number.isNaN(x)) return [0, Number.NaN, Number.NaN];
175
+ return [0, 1 / x, x];
176
+ }
177
+ //Provides: caml_ldexp_float const
178
+ function caml_ldexp_float(x, exp) {
179
+ // musl's scalbn: scaling down goes through 2^-1022 * 2^53 so that
180
+ // the intermediate values stay normal; the only rounding happens in
181
+ // the final multiplication
182
+ exp |= 0;
183
+ if (exp > 1023) {
184
+ exp -= 1023;
185
+ x *= Math.pow(2, 1023);
186
+ if (exp > 1023) {
187
+ // in case x is subnormal
188
+ exp -= 1023;
189
+ x *= Math.pow(2, 1023);
190
+ if (exp > 1023) exp = 1023;
191
+ }
192
+ } else if (exp < -1022) {
193
+ exp += 1022 - 53;
194
+ x *= Math.pow(2, -1022) * Math.pow(2, 53);
195
+ if (exp < -1022) {
196
+ exp += 1022 - 53;
197
+ x *= Math.pow(2, -1022) * Math.pow(2, 53);
198
+ if (exp < -1022) exp = -1022;
199
+ }
200
+ }
201
+ x *= Math.pow(2, exp);
202
+ return x;
203
+ }
204
+ //Provides: caml_frexp_float const
205
+ function caml_frexp_float(x) {
206
+ if (x === 0 || !Number.isFinite(x)) return [0, x, 0];
207
+ var neg = x < 0;
208
+ if (neg) x = -x;
209
+ var exp = Math.max(-1023, Math.floor(Math.log2(x)) + 1);
210
+ x *= Math.pow(2, -exp);
211
+ while (x < 0.5) {
212
+ x *= 2;
213
+ exp--;
214
+ }
215
+ while (x >= 1) {
216
+ x *= 0.5;
217
+ exp++;
218
+ }
219
+ if (neg) x = -x;
220
+ return [0, x, exp];
221
+ }
222
+
223
+ //Provides: caml_float_compare const
224
+ function caml_float_compare(x, y) {
225
+ if (x === y) return 0;
226
+ if (x < y) return -1;
227
+ if (x > y) return 1;
228
+ if (!Number.isNaN(x)) return 1;
229
+ if (!Number.isNaN(y)) return -1;
230
+ return 0;
231
+ }
232
+
233
+ //Provides: caml_copysign_float const
234
+ function caml_copysign_float(x, y) {
235
+ if (y === 0) y = 1 / y;
236
+ x = Math.abs(x);
237
+ return y < 0 ? -x : x;
238
+ }
239
+
240
+ //Provides: caml_signbit_float const
241
+ //Alias: caml_signbit
242
+ function caml_signbit_float(x) {
243
+ if (x === 0) x = 1 / x;
244
+ return x < 0 ? 1 : 0;
245
+ }
246
+
247
+ //Provides: caml_expm1_float const
248
+ function caml_expm1_float(x) {
249
+ return Math.expm1(x);
250
+ }
251
+ //Provides: caml_exp2_float const
252
+ function caml_exp2_float(x) {
253
+ return Math.pow(2, x);
254
+ }
255
+ //Provides: caml_log1p_float const
256
+ function caml_log1p_float(x) {
257
+ return Math.log1p(x);
258
+ }
259
+ //Provides: caml_log2_float const
260
+ function caml_log2_float(x) {
261
+ return Math.log2(x);
262
+ }
263
+ //Provides: caml_hypot_float const
264
+ function caml_hypot_float(x, y) {
265
+ return Math.hypot(x, y);
266
+ }
267
+ //Provides: caml_log10_float const
268
+ function caml_log10_float(x) {
269
+ return Math.log10(x);
270
+ }
271
+ //Provides: caml_cosh_float const
272
+ function caml_cosh_float(x) {
273
+ return Math.cosh(x);
274
+ }
275
+ //Provides: caml_acosh_float const
276
+ function caml_acosh_float(x) {
277
+ return Math.acosh(x);
278
+ }
279
+ //Provides: caml_sinh_float const
280
+ function caml_sinh_float(x) {
281
+ return Math.sinh(x);
282
+ }
283
+ //Provides: caml_asinh_float const
284
+ function caml_asinh_float(x) {
285
+ return Math.asinh(x);
286
+ }
287
+ //Provides: caml_tanh_float const
288
+ function caml_tanh_float(x) {
289
+ return Math.tanh(x);
290
+ }
291
+ //Provides: caml_atanh_float const
292
+ function caml_atanh_float(x) {
293
+ return Math.atanh(x);
294
+ }
295
+ //Provides: caml_round_float const
296
+ function caml_round_float(x) {
297
+ if (x >= 0) {
298
+ var y = Math.floor(x);
299
+ return x - y >= 0.5 ? y + 1 : y;
300
+ } else {
301
+ var y = Math.ceil(x);
302
+ return y - x >= 0.5 ? y - 1 : y;
303
+ }
304
+ }
305
+ //Provides: caml_cbrt_float const
306
+ function caml_cbrt_float(x) {
307
+ return Math.cbrt(x);
308
+ }
309
+
310
+ //Provides: caml_erf_float const
311
+ function caml_erf_float(x) {
312
+ var a1 = 0.254829592;
313
+ var a2 = -0.284496736;
314
+ var a3 = 1.421413741;
315
+ var a4 = -1.453152027;
316
+ var a5 = 1.061405429;
317
+ var p = 0.3275911;
318
+
319
+ var sign = 1;
320
+ if (x < 0) {
321
+ sign = -1;
322
+ }
323
+ x = Math.abs(x);
324
+ var t = 1.0 / (1.0 + p * x);
325
+ var y =
326
+ 1.0 - ((((a5 * t + a4) * t + a3) * t + a2) * t + a1) * t * Math.exp(-x * x);
327
+
328
+ return sign * y;
329
+ }
330
+
331
+ //Provides: caml_erfc_float const
332
+ //Requires: caml_erf_float
333
+ function caml_erfc_float(x) {
334
+ return 1 - caml_erf_float(x);
335
+ }
336
+
337
+ //Provides: caml_fma_float const
338
+ function caml_fma_float(x, y, z) {
339
+ var SPLIT = Math.pow(2, 27) + 1;
340
+ var MIN_VALUE = Math.pow(2, -1022);
341
+ var EPSILON = Math.pow(2, -52);
342
+ var C = 416;
343
+ var A = Math.pow(2, +C);
344
+ var B = Math.pow(2, -C);
345
+
346
+ function multiply(a, b) {
347
+ var at = SPLIT * a;
348
+ var ahi = at - (at - a);
349
+ var alo = a - ahi;
350
+ var bt = SPLIT * b;
351
+ var bhi = bt - (bt - b);
352
+ var blo = b - bhi;
353
+ var p = a * b;
354
+ var e = ahi * bhi - p + ahi * blo + alo * bhi + alo * blo;
355
+ return {
356
+ p: p,
357
+ e: e,
358
+ };
359
+ }
360
+
361
+ function add(a, b) {
362
+ var s = a + b;
363
+ var v = s - a;
364
+ var e = a - (s - v) + (b - v);
365
+ return {
366
+ s: s,
367
+ e: e,
368
+ };
369
+ }
370
+
371
+ function adjust(x, y) {
372
+ return x !== 0 && y !== 0 && SPLIT * x - (SPLIT * x - x) === x
373
+ ? x * (1 + (x < 0 ? -1 : +1) * (y < 0 ? -1 : +1) * EPSILON)
374
+ : x;
375
+ }
376
+
377
+ if (x === 0 || y === 0 || !Number.isFinite(x) || !Number.isFinite(y)) {
378
+ return x * y + z;
379
+ }
380
+ if (z === 0) {
381
+ return x * y;
382
+ }
383
+ if (!Number.isFinite(z)) {
384
+ return z;
385
+ }
386
+
387
+ var scale = 1;
388
+ while (Math.abs(x) > A) {
389
+ scale *= A;
390
+ x *= B;
391
+ }
392
+ while (Math.abs(y) > A) {
393
+ scale *= A;
394
+ y *= B;
395
+ }
396
+ if (scale === 1 / 0) {
397
+ return x * y * scale;
398
+ }
399
+ while (Math.abs(x) < B) {
400
+ scale *= B;
401
+ x *= A;
402
+ }
403
+ while (Math.abs(y) < B) {
404
+ scale *= B;
405
+ y *= A;
406
+ }
407
+ if (scale === 0) {
408
+ return z;
409
+ }
410
+
411
+ var xs = x;
412
+ var ys = y;
413
+ var zs = z / scale;
414
+
415
+ if (Math.abs(zs) > (Math.abs(xs * ys) * 4) / EPSILON) {
416
+ return z;
417
+ }
418
+ if (Math.abs(zs) < (((Math.abs(xs * ys) * EPSILON) / 4) * EPSILON) / 4) {
419
+ zs = (z < 0 ? -1 : +1) * MIN_VALUE;
420
+ }
421
+
422
+ var xy = multiply(xs, ys);
423
+ var s = add(xy.p, zs);
424
+ var u = add(xy.e, s.e);
425
+ var i = add(s.s, u.s);
426
+
427
+ var f = i.s + adjust(i.e, u.e);
428
+ if (f === 0) {
429
+ return f;
430
+ }
431
+
432
+ var fs = f * scale;
433
+ if (Math.abs(fs) > MIN_VALUE) {
434
+ return fs;
435
+ }
436
+
437
+ // It is possible that there was extra rounding for a denormalized value.
438
+ return fs + adjust(f - fs / scale, i.e) * scale;
439
+ }
440
+
441
+ //Provides: caml_format_float const
442
+ //Requires: caml_str_repeat, caml_parse_format, caml_finish_formatting
443
+ function caml_format_float(fmt, x) {
444
+ // Exact decimal expansion through BigInt, for precisions beyond
445
+ // toFixed/toExponential's limit of 100 and for %f of values >= 1e21
446
+ function decompose(x) {
447
+ // x (finite, >= 0) is m * 2^e
448
+ var dv = new DataView(new ArrayBuffer(8));
449
+ dv.setFloat64(0, x);
450
+ var hi = dv.getUint32(0),
451
+ lo = dv.getUint32(4);
452
+ var eb = (hi >>> 20) & 0x7ff;
453
+ var m = (BigInt(hi & 0xfffff) << 32n) | BigInt(lo);
454
+ if (eb === 0) return [m, -1074];
455
+ return [m | (1n << 52n), eb - 1075];
456
+ }
457
+ function exact_scaled(x, k) {
458
+ // round_half_even(x * 10^k) as a BigInt
459
+ var d = decompose(x);
460
+ var num = d[0],
461
+ den = 1n;
462
+ if (k >= 0) num *= 10n ** BigInt(k);
463
+ else den = 10n ** BigInt(-k);
464
+ if (d[1] >= 0) num <<= BigInt(d[1]);
465
+ else den <<= BigInt(-d[1]);
466
+ var q = num / den,
467
+ r2 = (num % den) * 2n;
468
+ if (r2 > den || (r2 === den && q & 1n)) q += 1n;
469
+ return q;
470
+ }
471
+ function exact_fixed(x, prec) {
472
+ // toFixed, exactly
473
+ var q = exact_scaled(x, prec).toString();
474
+ if (prec === 0) return q;
475
+ if (q.length <= prec) q = caml_str_repeat(prec + 1 - q.length, "0") + q;
476
+ return q.slice(0, q.length - prec) + "." + q.slice(q.length - prec);
477
+ }
478
+ function exact_exponential(x, prec) {
479
+ // toExponential, exactly
480
+ if (x === 0)
481
+ return (prec > 0 ? "0." + caml_str_repeat(prec, "0") : "0") + "e+0";
482
+ var e10 = Math.floor(Math.log10(x));
483
+ for (;;) {
484
+ // we want round(x * 10^(prec - e10)) to have exactly prec + 1
485
+ // digits; the estimate of e10 is off by at most one (and
486
+ // rounding can add a digit), so adjust and retry
487
+ var s = exact_scaled(x, prec - e10).toString();
488
+ if (s.length === prec + 1) {
489
+ var m = prec > 0 ? s.charAt(0) + "." + s.slice(1) : s;
490
+ return m + "e" + (e10 < 0 ? "-" : "+") + Math.abs(e10);
491
+ }
492
+ e10 += s.length - (prec + 1);
493
+ }
494
+ }
495
+ function toExponential(x, prec) {
496
+ return prec > 100 ? exact_exponential(x, prec) : x.toExponential(prec);
497
+ }
498
+ function toFixed(x, dp) {
499
+ if (dp > 100 || x >= 1e21) return exact_fixed(x, dp);
500
+ return x.toFixed(dp);
501
+ }
502
+ var s,
503
+ f = caml_parse_format(fmt);
504
+ var prec = f.prec < 0 ? 6 : f.prec;
505
+ if (x < 0 || (x === 0 && 1 / x === Number.NEGATIVE_INFINITY)) {
506
+ f.sign = -1;
507
+ x = -x;
508
+ }
509
+ if (Number.isNaN(x)) {
510
+ s = "nan";
511
+ f.filler = " ";
512
+ } else if (!Number.isFinite(x)) {
513
+ s = "inf";
514
+ f.filler = " ";
515
+ } else
516
+ switch (f.conv) {
517
+ case "e":
518
+ var s = toExponential(x, prec);
519
+ // exponent should be at least two digits
520
+ var i = s.length;
521
+ if (s.charAt(i - 3) === "e")
522
+ s = s.slice(0, i - 1) + "0" + s.slice(i - 1);
523
+ break;
524
+ case "f":
525
+ s = toFixed(x, prec);
526
+ break;
527
+ case "g":
528
+ prec = prec ? prec : 1;
529
+ s = toExponential(x, prec - 1);
530
+ var j = s.indexOf("e");
531
+ var exp = +s.slice(j + 1);
532
+ if (exp < -4 || x >= 1e21 || x.toFixed(0).length > prec) {
533
+ // remove trailing zeroes
534
+ var i = j - 1;
535
+ while (s.charAt(i) === "0") i--;
536
+ if (s.charAt(i) === ".") i--;
537
+ s = s.slice(0, i + 1) + s.slice(j);
538
+ i = s.length;
539
+ if (s.charAt(i - 3) === "e")
540
+ s = s.slice(0, i - 1) + "0" + s.slice(i - 1);
541
+ break;
542
+ } else {
543
+ var p = prec;
544
+ if (exp < 0) {
545
+ p -= exp + 1;
546
+ s = toFixed(x, p);
547
+ } else while (((s = toFixed(x, p)), s.length > prec + 1)) p--;
548
+ if (p) {
549
+ // remove trailing zeroes
550
+ var i = s.length - 1;
551
+ while (s.charAt(i) === "0") i--;
552
+ if (s.charAt(i) === ".") i--;
553
+ s = s.slice(0, i + 1);
554
+ }
555
+ }
556
+ break;
557
+ }
558
+ return caml_finish_formatting(f, s);
559
+ }
560
+
561
+ //Provides: caml_parse_float
562
+ //Requires: caml_failwith, caml_jsbytes_of_string
563
+ function caml_parse_float(s, err_msg) {
564
+ var res;
565
+ var r_float = /^[ \t\n\v\f\r]*[-+]?(?:\d*\.?\d+|\d+\.?\d*)(?:[eE][-+]?\d+)?$/;
566
+ s = caml_jsbytes_of_string(s);
567
+ res = +s;
568
+ //Fast path
569
+ if (!Number.isNaN(res) && r_float.test(s)) return res;
570
+ s = s.replace(/_/g, "");
571
+ res = +s;
572
+ if (
573
+ (!Number.isNaN(res) && r_float.test(s)) ||
574
+ /^[ \t\n\v\f\r]*[+-]?nan$/i.test(s)
575
+ )
576
+ return res;
577
+ var m =
578
+ /^[ \t\n\v\f\r]*([+-]?)0x([0-9a-f]*)\.?([0-9a-f]*)(?:p([+-]?[0-9]+))?$/i.exec(
579
+ s,
580
+ );
581
+ // 1 2 3 4
582
+ if (m && m[2].length + m[3].length > 0) {
583
+ var sign = m[1] === "-" ? -1 : 1;
584
+ var mant = BigInt("0x0" + m[2] + m[3]);
585
+ var e = (+m[4] || 0) - 4 * m[3].length;
586
+ if (mant === 0n) return sign * 0;
587
+ // Round [mant * 2^e] to the nearest representable double
588
+ // (round-to-nearest, ties-to-even), exactly
589
+ for (;;) {
590
+ var bl = mant.toString(2).length; // bit length
591
+ var be = bl - 1 + e; // exponent of the leading bit
592
+ if (be > 1023) return sign * Number.POSITIVE_INFINITY;
593
+ if (be < -1075) return sign * 0;
594
+ // number of mantissa bits a double can hold at this magnitude
595
+ var p = be >= -1022 ? 53 : 53 + 1022 + be;
596
+ var shift = bl - p;
597
+ if (shift <= 0)
598
+ // [mant * 2^e] is representable: both operations are exact
599
+ return sign * Number(mant) * Math.pow(2, e);
600
+ var keep = mant >> BigInt(shift);
601
+ var rem = mant & ((1n << BigInt(shift)) - 1n);
602
+ var half = 1n << BigInt(shift - 1);
603
+ if (rem > half || (rem === half && keep & 1n)) keep += 1n;
604
+ if (keep < (p > 0 ? 1n << BigInt(p) : 1n))
605
+ return sign * Number(keep) * Math.pow(2, e + shift);
606
+ // the carry needs one more bit: renormalize and round again
607
+ mant = keep;
608
+ e += shift;
609
+ }
610
+ }
611
+ if (/^[ \t\n\v\f\r]*\+?inf(inity)?$/i.test(s))
612
+ return Number.POSITIVE_INFINITY;
613
+ if (/^[ \t\n\v\f\r]*-inf(inity)?$/i.test(s)) return Number.NEGATIVE_INFINITY;
614
+ caml_failwith(err_msg);
615
+ }
616
+
617
+ //Provides: caml_float_of_string (const)
618
+ //Requires: caml_parse_float
619
+ function caml_float_of_string(s) {
620
+ return caml_parse_float(s, "float_of_string");
621
+ }