forked from ocsigen/js_of_ocaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjslib.js
528 lines (496 loc) · 14.8 KB
/
jslib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// Js_of_ocaml library
// http://www.ocsigen.org/js_of_ocaml/
// Copyright (C) 2010 Jérôme Vouillon
// Laboratoire PPS - CNRS Université Paris Diderot
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, with linking exception;
// either version 2.1 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
///////////// Jslib
//Provides: caml_js_pure_expr const
//Requires: caml_callback
function caml_js_pure_expr(f) {
return caml_callback(f, [0]);
}
//Provides: caml_js_set (mutable, const, mutable)
function caml_js_set(o, f, v) {
o[f] = v;
return 0;
}
//Provides: caml_js_get (mutable, const)
function caml_js_get(o, f) {
return o[f];
}
//Provides: caml_js_delete (mutable, const)
function caml_js_delete(o, f) {
delete o[f];
return 0;
}
//Provides: caml_js_instanceof (const, const)
function caml_js_instanceof(o, c) {
return o instanceof c ? 1 : 0;
}
//Provides: caml_js_typeof (const)
function caml_js_typeof(o) {
return typeof o;
}
//Provides:caml_trampoline
function caml_trampoline(res) {
var c = 1;
while (res && res.joo_tramp) {
res = res.joo_tramp.apply(null, res.joo_args);
c++;
}
return res;
}
//Provides:caml_trampoline_return
function caml_trampoline_return(f, args) {
return { joo_tramp: f, joo_args: args };
}
//Provides:caml_stack_depth
//If: effects
var caml_stack_depth = 0;
//Provides:caml_stack_check_depth
//If: effects
//Requires:caml_stack_depth
function caml_stack_check_depth() {
return --caml_stack_depth > 0;
}
//Provides: caml_callback
//If: !effects
//Requires:caml_call_gen
var caml_callback = caml_call_gen;
//Provides: caml_callback
//If: effects
//Requires:caml_stack_depth, caml_call_gen, caml_exn_stack, caml_fiber_stack, caml_wrap_exception, caml_resume_stack, caml_fresh_oo_id, caml_named_value, caml_raise_with_arg, caml_string_of_jsbytes
//Requires: caml_raise_constant
function caml_callback(f, args) {
function uncaught_effect_handler(eff, k, ms) {
// Resumes the continuation k by raising exception Unhandled.
caml_resume_stack(k[1], ms);
var exn = caml_named_value("Effect.Unhandled");
if (exn) caml_raise_with_arg(exn, eff);
else {
exn = [
248,
caml_string_of_jsbytes("Effect.Unhandled"),
caml_fresh_oo_id(0),
];
caml_raise_constant(exn);
}
}
var saved_stack_depth = caml_stack_depth;
var saved_exn_stack = caml_exn_stack;
var saved_fiber_stack = caml_fiber_stack;
try {
caml_exn_stack = 0;
caml_fiber_stack = {
h: [0, 0, 0, uncaught_effect_handler],
r: { k: 0, x: 0, e: 0 },
};
var res = {
joo_tramp: f,
joo_args: args.concat(function (x) {
return x;
}),
};
do {
caml_stack_depth = 40;
try {
res = caml_call_gen(res.joo_tramp, res.joo_args);
} catch (e) {
/* Handle exception coming from JavaScript or from the runtime. */
if (!caml_exn_stack) throw e;
var handler = caml_exn_stack[1];
caml_exn_stack = caml_exn_stack[2];
res = { joo_tramp: handler, joo_args: [caml_wrap_exception(e)] };
}
} while (res && res.joo_args);
} finally {
caml_stack_depth = saved_stack_depth;
caml_exn_stack = saved_exn_stack;
caml_fiber_stack = saved_fiber_stack;
}
return res;
}
//Provides: caml_is_js
function caml_is_js() {
return 1;
}
//Provides: caml_jsoo_flags_use_js_string
function caml_jsoo_flags_use_js_string(unit) {
return FLAG("use-js-string");
}
//Provides: caml_jsoo_flags_effects
function caml_jsoo_flags_effects(unit) {
return FLAG("effects");
}
//Provides: caml_wrap_exception const (mutable)
//Requires: caml_global_data,caml_string_of_jsstring,caml_named_value
function caml_wrap_exception(e) {
if (FLAG("excwrap")) {
if (Array.isArray(e)) return e;
var exn;
//Stack_overflow: chrome, safari
if (
globalThis.RangeError &&
e instanceof globalThis.RangeError &&
e.message &&
e.message.match(/maximum call stack/i)
)
exn = caml_global_data.Stack_overflow;
//Stack_overflow: firefox
else if (
globalThis.InternalError &&
e instanceof globalThis.InternalError &&
e.message &&
e.message.match(/too much recursion/i)
)
exn = caml_global_data.Stack_overflow;
//Wrap Error in Js.Error exception
else if (e instanceof globalThis.Error && caml_named_value("jsError"))
exn = [0, caml_named_value("jsError"), e];
//fallback: wrapped in Failure
else
exn = [0, caml_global_data.Failure, caml_string_of_jsstring(String(e))];
// We already have an error at hand, let's use it.
if (e instanceof globalThis.Error) exn.js_error = e;
return exn;
} else return e;
}
//Provides: caml_maybe_attach_backtrace
//Requires: caml_exn_with_js_backtrace
//Requires: caml_record_backtrace_env_flag
//Requires: caml_record_backtrace_runtime_flag
function caml_maybe_attach_backtrace(exn, force) {
// Backtraces are very expensive, we only enable them when explicitly requested
// at compile-time (--enable with-js-error) or at startup with OCAMLRUNPARAM=b=1.
// Libraries such as Base unconditionally enable backtraces (programmatically) but
// it's way to slow. Here, we force the end-user to opt-in to backtraces.
if (caml_record_backtrace_env_flag && caml_record_backtrace_runtime_flag)
return caml_exn_with_js_backtrace(exn, force);
else return exn;
}
// Experimental
//Provides: caml_exn_with_js_backtrace
//Requires: caml_global_data
function caml_exn_with_js_backtrace(exn, force) {
//never reraise for constant exn
if (!exn.js_error || force || exn[0] === 248)
exn.js_error = new globalThis.Error("Js exception containing backtrace");
return exn;
}
//Provides: caml_js_error_option_of_exception
function caml_js_error_option_of_exception(exn) {
if (exn.js_error) {
return [0, exn.js_error];
}
return 0;
}
//Provides: caml_js_from_bool const (const)
function caml_js_from_bool(x) {
return !!x;
}
//Provides: caml_js_to_bool const (const)
function caml_js_to_bool(x) {
return +x;
}
//Provides: caml_js_from_float const (const)
//Alias: caml_js_from_int32
//Alias: caml_js_from_nativeint
function caml_js_from_float(x) {
return x;
}
//Provides: caml_js_to_float const (const)
function caml_js_to_float(x) {
return x;
}
//Provides: caml_js_to_int32 const (const)
//Alias: caml_js_to_nativeint
function caml_js_to_int32(x) {
return x | 0;
}
//Provides: caml_js_from_array mutable (shallow)
function caml_js_from_array(a) {
return a.slice(1);
}
//Provides: caml_js_to_array mutable (shallow)
function caml_js_to_array(a) {
var len = a.length;
var b = new Array(len + 1);
b[0] = 0;
for (var i = 0; i < len; i++) b[i + 1] = a[i];
return b;
}
//Provides: caml_list_of_js_array const (mutable)
function caml_list_of_js_array(a) {
var l = 0;
for (var i = a.length - 1; i >= 0; i--) {
var e = a[i];
l = [0, e, l];
}
return l;
}
//Provides: caml_list_to_js_array const (mutable)
function caml_list_to_js_array(l) {
var a = [];
for (; l !== 0; l = l[2]) {
a.push(l[1]);
}
return a;
}
//Provides: caml_js_var mutable
//Requires: caml_jsstring_of_string
function caml_js_var(x) {
var x = caml_jsstring_of_string(x);
//Checks that x has the form ident[.ident]*
if (!x.match(/^[a-zA-Z_$][a-zA-Z_$0-9]*(\.[a-zA-Z_$][a-zA-Z_$0-9]*)*$/)) {
console.error(
'caml_js_var: "' +
x +
'" is not a valid JavaScript variable. continuing ..',
);
//console.error("Js.Unsafe.eval_string")
}
// biome-ignore lint/security/noGlobalEval:
return eval(x);
}
//Provides: caml_js_call (const, mutable, shallow)
//Requires: caml_js_from_array
function caml_js_call(f, o, args) {
return f.apply(o, caml_js_from_array(args));
}
//Provides: caml_js_fun_call (const, shallow)
//Requires: caml_js_from_array
function caml_js_fun_call(f, a) {
switch (a.length) {
case 1:
return f();
case 2:
return f(a[1]);
case 3:
return f(a[1], a[2]);
case 4:
return f(a[1], a[2], a[3]);
case 5:
return f(a[1], a[2], a[3], a[4]);
case 6:
return f(a[1], a[2], a[3], a[4], a[5]);
case 7:
return f(a[1], a[2], a[3], a[4], a[5], a[6]);
case 8:
return f(a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
}
return f.apply(null, caml_js_from_array(a));
}
//Provides: caml_js_meth_call (mutable, const, shallow)
//Requires: caml_jsstring_of_string
//Requires: caml_js_from_array
function caml_js_meth_call(o, f, args) {
return o[caml_jsstring_of_string(f)].apply(o, caml_js_from_array(args));
}
//Provides: caml_js_new (const, shallow)
//Requires: caml_js_from_array
function caml_js_new(c, a) {
switch (a.length) {
case 1:
return new c();
case 2:
return new c(a[1]);
case 3:
return new c(a[1], a[2]);
case 4:
return new c(a[1], a[2], a[3]);
case 5:
return new c(a[1], a[2], a[3], a[4]);
case 6:
return new c(a[1], a[2], a[3], a[4], a[5]);
case 7:
return new c(a[1], a[2], a[3], a[4], a[5], a[6]);
case 8:
return new c(a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
}
function F() {
return c.apply(this, caml_js_from_array(a));
}
F.prototype = c.prototype;
return new F();
}
//Provides: caml_ojs_new_arr (const, shallow)
//Requires: caml_js_from_array
function caml_ojs_new_arr(c, a) {
switch (a.length) {
case 0:
return new c();
case 1:
return new c(a[0]);
case 2:
return new c(a[0], a[1]);
case 3:
return new c(a[0], a[1], a[2]);
case 4:
return new c(a[0], a[1], a[2], a[3]);
case 5:
return new c(a[0], a[1], a[2], a[3], a[4]);
case 6:
return new c(a[0], a[1], a[2], a[3], a[4], a[5]);
case 7:
return new c(a[0], a[1], a[2], a[3], a[4], a[5], a[6]);
}
function F() {
return c.apply(this, a);
}
F.prototype = c.prototype;
return new F();
}
//Provides: caml_js_wrap_callback const (const)
//Requires: caml_callback
function caml_js_wrap_callback(f) {
return function () {
var len = arguments.length;
if (len > 0) {
var args = new Array(len);
for (var i = 0; i < len; i++) args[i] = arguments[i];
} else {
args = [undefined];
}
var res = caml_callback(f, args);
return res instanceof Function ? caml_js_wrap_callback(res) : res;
};
}
//Provides: caml_js_wrap_callback_arguments
//Requires: caml_callback
function caml_js_wrap_callback_arguments(f) {
return function () {
var len = arguments.length;
var args = new Array(len);
for (var i = 0; i < len; i++) args[i] = arguments[i];
return caml_callback(f, [args]);
};
}
//Provides: caml_js_wrap_callback_strict const
//Requires: caml_callback
function caml_js_wrap_callback_strict(arity, f) {
return function () {
var n = arguments.length;
var args = new Array(arity);
var len = Math.min(arguments.length, arity);
for (var i = 0; i < len; i++) args[i] = arguments[i];
return caml_callback(f, args);
};
}
//Provides: caml_js_wrap_callback_unsafe const (const)
//Requires: caml_callback, caml_js_function_arity
function caml_js_wrap_callback_unsafe(f) {
return function () {
var len = caml_js_function_arity(f);
var args = new Array(len);
for (var i = 0; i < len; i++) args[i] = arguments[i];
return caml_callback(f, args);
};
}
//Provides: caml_js_wrap_meth_callback const (const)
//Requires: caml_callback, caml_js_wrap_callback
function caml_js_wrap_meth_callback(f) {
return function () {
var len = arguments.length;
var args = new Array(len + 1);
args[0] = this;
for (var i = 0; i < len; i++) args[i + 1] = arguments[i];
var res = caml_callback(f, args);
return res instanceof Function ? caml_js_wrap_callback(res) : res;
};
}
//Provides: caml_js_wrap_meth_callback_arguments const (const)
//Requires: caml_callback
function caml_js_wrap_meth_callback_arguments(f) {
return function () {
var len = arguments.length;
var args = new Array(len);
for (var i = 0; i < len; i++) args[i] = arguments[i];
return caml_callback(f, [this, args]);
};
}
//Provides: caml_js_wrap_meth_callback_strict const
//Requires: caml_callback
function caml_js_wrap_meth_callback_strict(arity, f) {
return function () {
var args = new Array(arity + 1);
var len = Math.min(arguments.length, arity);
args[0] = this;
for (var i = 0; i < len; i++) args[i + 1] = arguments[i];
return caml_callback(f, args);
};
}
//Provides: caml_js_wrap_meth_callback_unsafe const (const)
//Requires: caml_callback, caml_js_function_arity
function caml_js_wrap_meth_callback_unsafe(f) {
return function () {
var len = caml_js_function_arity(f) - 1;
var args = new Array(len + 1);
args[0] = this;
for (var i = 0; i < len; i++) args[i + 1] = arguments[i];
return caml_callback(f, args);
};
}
//Provides: caml_js_function_arity
//If: !effects
function caml_js_function_arity(f) {
return f.l >= 0 ? f.l : (f.l = f.length);
}
//Provides: caml_js_function_arity
//If: effects
function caml_js_function_arity(f) {
// Functions have an additional continuation parameter. This should
// not be visible when calling them from JavaScript
return (f.l >= 0 ? f.l : (f.l = f.length)) - 1;
}
//Provides: caml_js_equals mutable (const, const)
function caml_js_equals(x, y) {
// biome-ignore lint/suspicious/noDoubleEquals:
return +(x == y);
}
//Provides: caml_js_strict_equals mutable (const, const)
function caml_js_strict_equals(x, y) {
return +(x === y);
}
//Provides: caml_js_eval_string (const)
//Requires: caml_jsstring_of_string
function caml_js_eval_string(s) {
// biome-ignore lint/security/noGlobalEval:
return eval(caml_jsstring_of_string(s));
}
//Provides: caml_js_expr (const)
//Requires: caml_jsstring_of_string
function caml_js_expr(s) {
console.error("caml_js_expr: fallback to runtime evaluation\n");
// biome-ignore lint/security/noGlobalEval:
return eval(caml_jsstring_of_string(s));
}
//Provides: caml_pure_js_expr const (const)
//Requires: caml_jsstring_of_string
function caml_pure_js_expr(s) {
console.error("caml_pure_js_expr: fallback to runtime evaluation\n");
// biome-ignore lint/security/noGlobalEval:
return eval(caml_jsstring_of_string(s));
}
//Provides: caml_js_object (object_literal)
//Requires: caml_jsstring_of_string
function caml_js_object(a) {
var o = {};
for (var i = 1; i < a.length; i++) {
var p = a[i];
o[caml_jsstring_of_string(p[1])] = p[2];
}
return o;
}