@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.
- package/LICENSE +220 -0
- package/README.md +165 -2
- package/THIRD-PARTY-NOTICES.md +52 -0
- package/assets/compiler/compile_stdout.browser.js +27856 -0
- package/assets/compiler/jsoo.js +12631 -0
- package/assets/compiler/package.json +3 -0
- package/assets/compiler/runtime/bigarray.js +1072 -0
- package/assets/compiler/runtime/bigstring.js +137 -0
- package/assets/compiler/runtime/domain.js +393 -0
- package/assets/compiler/runtime/dynlink.js +91 -0
- package/assets/compiler/runtime/float32.js +486 -0
- package/assets/compiler/runtime/fs.js +416 -0
- package/assets/compiler/runtime/fs_fake.js +600 -0
- package/assets/compiler/runtime/fs_node.js +609 -0
- package/assets/compiler/runtime/fs_quickjs.js +532 -0
- package/assets/compiler/runtime/gc.js +201 -0
- package/assets/compiler/runtime/graphics.js +1053 -0
- package/assets/compiler/runtime/ieee_754.js +621 -0
- package/assets/compiler/runtime/int64.js +516 -0
- package/assets/compiler/runtime/io.js +739 -0
- package/assets/compiler/runtime/jslib.js +553 -0
- package/assets/compiler/runtime/jslib_js_of_ocaml.js +81 -0
- package/assets/compiler/runtime/lexing.js +240 -0
- package/assets/compiler/runtime/manifest.txt +31 -0
- package/assets/compiler/runtime/marshal.js +863 -0
- package/assets/compiler/runtime/md5.js +236 -0
- package/assets/compiler/runtime/mlBytes.js +1007 -0
- package/assets/compiler/runtime/nat.js +469 -0
- package/assets/compiler/runtime/parsing.js +317 -0
- package/assets/compiler/runtime/prng.js +84 -0
- package/assets/compiler/runtime/promise.js +45 -0
- package/assets/compiler/runtime/runtime.js +16688 -0
- package/assets/compiler/runtime/runtime_events.js +90 -0
- package/assets/compiler/runtime/stdlib.js +363 -0
- package/assets/compiler/runtime/sync.js +35 -0
- package/assets/compiler/runtime/toplevel.js +150 -0
- package/assets/compiler/runtime/unix.js +914 -0
- package/assets/compiler/runtime/weak.js +247 -0
- package/assets/compiler/stdlib.js +254 -0
- package/assets/compiler/worker.js +46 -0
- package/assets/interpreter/libs/greet.cma +0 -0
- package/assets/interpreter/libs/greet.cmi +0 -0
- package/assets/interpreter/libs/manifest.txt +1 -0
- package/assets/interpreter/package.json +3 -0
- package/assets/interpreter/stdlib/camlinternalFormat.cmi +0 -0
- package/assets/interpreter/stdlib/camlinternalFormatBasics.cmi +0 -0
- package/assets/interpreter/stdlib/camlinternalLazy.cmi +0 -0
- package/assets/interpreter/stdlib/camlinternalMod.cmi +0 -0
- package/assets/interpreter/stdlib/camlinternalOO.cmi +0 -0
- package/assets/interpreter/stdlib/std_exit.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Arg.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Array.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__ArrayLabels.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Atomic.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Bigarray.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Bool.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Buffer.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Bytes.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__BytesLabels.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Callback.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Char.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Complex.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Condition.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Digest.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Domain.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Dynarray.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Effect.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Either.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Ephemeron.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Filename.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Float.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Format.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Fun.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Gc.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Hashtbl.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Iarray.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__In_channel.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Int.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Int32.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Int64.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Lazy.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Lexing.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__List.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__ListLabels.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Map.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Marshal.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__MoreLabels.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Mutex.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Nativeint.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Obj.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Oo.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Option.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Out_channel.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Pair.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Parsing.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Pqueue.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Printexc.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Printf.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Queue.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Random.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Repr.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Result.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Scanf.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Semaphore.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Seq.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Set.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Stack.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__StdLabels.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__String.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__StringLabels.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Sys.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Type.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Uchar.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Unit.cmi +0 -0
- package/assets/interpreter/stdlib/stdlib__Weak.cmi +0 -0
- package/assets/interpreter/stdlib/topdirs.cmi +0 -0
- package/assets/interpreter/stdlib/units.txt +75 -0
- package/assets/interpreter/stdlib/unix.cmi +0 -0
- package/assets/interpreter/stdlib/unixLabels.cmi +0 -0
- package/assets/interpreter/worker-entry.js +39 -0
- package/assets/interpreter/worker.js +42356 -0
- package/bin/copy-assets.mjs +75 -0
- package/package.json +41 -4
- package/src/compiler/compile.js +195 -0
- package/src/compiler/index.js +46 -0
- package/src/compiler/worker.js +46 -0
- package/src/index.js +12 -0
- package/src/interpreter/api.js +182 -0
- package/src/interpreter/index.js +5 -0
|
@@ -0,0 +1,553 @@
|
|
|
1
|
+
// Js_of_ocaml library
|
|
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
|
+
///////////// Jslib
|
|
21
|
+
|
|
22
|
+
//Provides: caml_js_pure_expr const
|
|
23
|
+
//Requires: caml_callback
|
|
24
|
+
function caml_js_pure_expr(f) {
|
|
25
|
+
return caml_callback(f, [0]);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
//Provides: caml_js_set (mutable, const, mutable)
|
|
29
|
+
function caml_js_set(o, f, v) {
|
|
30
|
+
o[f] = v;
|
|
31
|
+
return 0;
|
|
32
|
+
}
|
|
33
|
+
//Provides: caml_js_get (mutable, const)
|
|
34
|
+
function caml_js_get(o, f) {
|
|
35
|
+
return o[f];
|
|
36
|
+
}
|
|
37
|
+
//Provides: caml_js_delete (mutable, const)
|
|
38
|
+
function caml_js_delete(o, f) {
|
|
39
|
+
delete o[f];
|
|
40
|
+
return 0;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
//Provides: caml_js_instanceof (const, const)
|
|
44
|
+
function caml_js_instanceof(o, c) {
|
|
45
|
+
return o instanceof c ? 1 : 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
//Provides: caml_js_typeof (const)
|
|
49
|
+
function caml_js_typeof(o) {
|
|
50
|
+
return typeof o;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
//Provides:caml_trampoline
|
|
54
|
+
function caml_trampoline(res) {
|
|
55
|
+
var c = 1;
|
|
56
|
+
while (res?.joo_tramp) {
|
|
57
|
+
res = res.joo_tramp.apply(null, res.joo_args);
|
|
58
|
+
c++;
|
|
59
|
+
}
|
|
60
|
+
return res;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
//Provides:caml_trampoline_return
|
|
64
|
+
function caml_trampoline_return(f, args, direct) {
|
|
65
|
+
return { joo_tramp: f, joo_args: args, joo_direct: direct };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
//Provides:caml_stack_depth
|
|
69
|
+
//If: effects
|
|
70
|
+
var caml_stack_depth = 0;
|
|
71
|
+
|
|
72
|
+
//Provides:caml_stack_check_depth
|
|
73
|
+
//If: effects
|
|
74
|
+
//Requires:caml_stack_depth
|
|
75
|
+
function caml_stack_check_depth() {
|
|
76
|
+
return --caml_stack_depth > 0;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
//Provides: caml_direct_trampoline
|
|
80
|
+
//If: effects
|
|
81
|
+
//If: doubletranslate
|
|
82
|
+
//Requires: caml_stack_depth
|
|
83
|
+
// Entry trampoline for the direct version of a cps_needed mutually
|
|
84
|
+
// recursive function under --effects=double-translation. Sets up a stack
|
|
85
|
+
// budget, runs the inner direct body, then loops on caml_trampoline_return
|
|
86
|
+
// bounce objects until a plain value comes back. Mirrors the CPS-side
|
|
87
|
+
// trampoline (see caml_callback / caml_resume): each iteration starts with
|
|
88
|
+
// a fresh stack budget and dispatches the bounce target directly (the
|
|
89
|
+
// joo_tramp is always the inner direct closure of an SCC member, never a
|
|
90
|
+
// paired wrapper, so plain apply is correct).
|
|
91
|
+
function caml_direct_trampoline(f, args) {
|
|
92
|
+
var saved = caml_stack_depth;
|
|
93
|
+
try {
|
|
94
|
+
caml_stack_depth = 40;
|
|
95
|
+
var res = f.apply(null, args);
|
|
96
|
+
while (res?.joo_tramp) {
|
|
97
|
+
caml_stack_depth = 40;
|
|
98
|
+
res = res.joo_tramp.apply(null, res.joo_args);
|
|
99
|
+
}
|
|
100
|
+
return res;
|
|
101
|
+
} finally {
|
|
102
|
+
caml_stack_depth = saved;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
//Provides: caml_callback
|
|
107
|
+
//If: !effects
|
|
108
|
+
//Requires:caml_call_gen
|
|
109
|
+
var caml_callback = caml_call_gen;
|
|
110
|
+
|
|
111
|
+
//Provides: caml_callback
|
|
112
|
+
//If: effects
|
|
113
|
+
//If: !doubletranslate
|
|
114
|
+
//Requires: caml_stack_depth, caml_call_gen, caml_wrap_exception
|
|
115
|
+
//Requires: caml_current_stack
|
|
116
|
+
//Alias: caml_cps_trampoline
|
|
117
|
+
function caml_callback(f, args) {
|
|
118
|
+
var saved_stack_depth = caml_stack_depth;
|
|
119
|
+
var saved_current_stack = caml_current_stack;
|
|
120
|
+
try {
|
|
121
|
+
caml_current_stack = { k: 0, x: 0, h: 0, e: 0 };
|
|
122
|
+
var res = {
|
|
123
|
+
joo_tramp: f,
|
|
124
|
+
joo_args: args.concat(function (x) {
|
|
125
|
+
return x;
|
|
126
|
+
}),
|
|
127
|
+
};
|
|
128
|
+
do {
|
|
129
|
+
caml_stack_depth = 40;
|
|
130
|
+
try {
|
|
131
|
+
res = caml_call_gen(res.joo_tramp, res.joo_args);
|
|
132
|
+
} catch (e) {
|
|
133
|
+
/* Handle exception coming from JavaScript or from the runtime. */
|
|
134
|
+
if (!caml_current_stack.x) throw e;
|
|
135
|
+
var handler = caml_current_stack.x.h;
|
|
136
|
+
caml_current_stack.x = caml_current_stack.x.t;
|
|
137
|
+
res = { joo_tramp: handler, joo_args: [caml_wrap_exception(e)] };
|
|
138
|
+
}
|
|
139
|
+
} while (res?.joo_args);
|
|
140
|
+
} finally {
|
|
141
|
+
caml_stack_depth = saved_stack_depth;
|
|
142
|
+
caml_current_stack = saved_current_stack;
|
|
143
|
+
}
|
|
144
|
+
return res;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
//Provides: caml_callback
|
|
148
|
+
//If: effects
|
|
149
|
+
//If: doubletranslate
|
|
150
|
+
//Requires: caml_call_gen
|
|
151
|
+
var caml_callback = caml_call_gen;
|
|
152
|
+
|
|
153
|
+
//Provides: caml_is_js
|
|
154
|
+
function caml_is_js() {
|
|
155
|
+
return 1;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
//Provides: caml_jsoo_flags_use_js_string
|
|
159
|
+
function caml_jsoo_flags_use_js_string(_unit) {
|
|
160
|
+
return FLAG("use-js-string");
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
//Provides: caml_jsoo_flags_effects
|
|
164
|
+
//Requires: caml_string_of_jsstring
|
|
165
|
+
function caml_jsoo_flags_effects(_unit) {
|
|
166
|
+
return caml_string_of_jsstring(CONFIG("effects"));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
//Provides: caml_jsoo_build_config
|
|
170
|
+
//Requires: caml_string_of_jsstring
|
|
171
|
+
function caml_jsoo_build_config(_unit) {
|
|
172
|
+
return caml_string_of_jsstring(BUILD_CONFIG());
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
//Provides: caml_wrap_exception const (mutable)
|
|
176
|
+
//Requires: caml_global_data,caml_string_of_jsstring,caml_named_value
|
|
177
|
+
function caml_wrap_exception(e) {
|
|
178
|
+
if (FLAG("excwrap")) {
|
|
179
|
+
if (Array.isArray(e)) return e;
|
|
180
|
+
var exn;
|
|
181
|
+
//Stack_overflow: chrome, safari
|
|
182
|
+
if (
|
|
183
|
+
globalThis.RangeError &&
|
|
184
|
+
e instanceof globalThis.RangeError &&
|
|
185
|
+
e.message?.match(/maximum call stack/i)
|
|
186
|
+
)
|
|
187
|
+
exn = caml_global_data["predef:Stack_overflow"];
|
|
188
|
+
//Stack_overflow: firefox
|
|
189
|
+
else if (
|
|
190
|
+
globalThis.InternalError &&
|
|
191
|
+
e instanceof globalThis.InternalError &&
|
|
192
|
+
e.message?.match(/too much recursion/i)
|
|
193
|
+
)
|
|
194
|
+
exn = caml_global_data["predef:Stack_overflow"];
|
|
195
|
+
//Wrap Error in Js.Error exception
|
|
196
|
+
else if (e instanceof globalThis.Error && caml_named_value("jsError"))
|
|
197
|
+
exn = [0, caml_named_value("jsError"), e];
|
|
198
|
+
//fallback: wrapped in Failure
|
|
199
|
+
else
|
|
200
|
+
exn = [
|
|
201
|
+
0,
|
|
202
|
+
caml_global_data["predef:Failure"],
|
|
203
|
+
caml_string_of_jsstring(String(e)),
|
|
204
|
+
];
|
|
205
|
+
// We already have an error at hand, let's use it.
|
|
206
|
+
if (e instanceof globalThis.Error) exn.js_error = e;
|
|
207
|
+
return exn;
|
|
208
|
+
} else return e;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
//Provides: caml_maybe_attach_backtrace
|
|
212
|
+
//Requires: caml_exn_with_js_backtrace
|
|
213
|
+
//Requires: caml_record_backtrace_env_flag
|
|
214
|
+
//Requires: caml_record_backtrace_runtime_flag
|
|
215
|
+
function caml_maybe_attach_backtrace(exn, force) {
|
|
216
|
+
// Backtraces are very expensive, we only enable them when explicitly requested
|
|
217
|
+
// at compile-time (--enable with-js-error) or at startup with OCAMLRUNPARAM=b=1.
|
|
218
|
+
// Libraries such as Base unconditionally enable backtraces (programmatically) but
|
|
219
|
+
// it's way to slow. Here, we force the end-user to opt-in to backtraces.
|
|
220
|
+
if (caml_record_backtrace_env_flag && caml_record_backtrace_runtime_flag)
|
|
221
|
+
return caml_exn_with_js_backtrace(exn, force);
|
|
222
|
+
else return exn;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Experimental
|
|
226
|
+
//Provides: caml_exn_with_js_backtrace
|
|
227
|
+
//Requires: caml_global_data
|
|
228
|
+
function caml_exn_with_js_backtrace(exn, force) {
|
|
229
|
+
//never reraise for constant exn
|
|
230
|
+
if (!exn.js_error || force || exn[0] === 248)
|
|
231
|
+
exn.js_error = new globalThis.Error("Js exception containing backtrace");
|
|
232
|
+
return exn;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
//Provides: caml_js_error_option_of_exception
|
|
236
|
+
function caml_js_error_option_of_exception(exn) {
|
|
237
|
+
if (exn.js_error) {
|
|
238
|
+
return [0, exn.js_error];
|
|
239
|
+
}
|
|
240
|
+
return 0;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
//Provides: caml_throw_js_exception
|
|
244
|
+
function caml_throw_js_exception(exn) {
|
|
245
|
+
throw exn;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
//Provides: caml_js_from_bool const (const)
|
|
249
|
+
function caml_js_from_bool(x) {
|
|
250
|
+
return !!x;
|
|
251
|
+
}
|
|
252
|
+
//Provides: caml_js_to_bool const (const)
|
|
253
|
+
function caml_js_to_bool(x) {
|
|
254
|
+
return +x;
|
|
255
|
+
}
|
|
256
|
+
//Provides: caml_js_from_float const (const)
|
|
257
|
+
//Alias: caml_js_from_int32
|
|
258
|
+
//Alias: caml_js_from_nativeint
|
|
259
|
+
function caml_js_from_float(x) {
|
|
260
|
+
return x;
|
|
261
|
+
}
|
|
262
|
+
//Provides: caml_js_to_float const (const)
|
|
263
|
+
function caml_js_to_float(x) {
|
|
264
|
+
return x;
|
|
265
|
+
}
|
|
266
|
+
//Provides: caml_js_to_int32 const (const)
|
|
267
|
+
//Alias: caml_js_to_nativeint
|
|
268
|
+
function caml_js_to_int32(x) {
|
|
269
|
+
return x | 0;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
//Provides: caml_js_from_array mutable (shallow)
|
|
273
|
+
function caml_js_from_array(a) {
|
|
274
|
+
return a.slice(1);
|
|
275
|
+
}
|
|
276
|
+
//Provides: caml_js_to_array mutable (shallow)
|
|
277
|
+
function caml_js_to_array(a) {
|
|
278
|
+
var len = a.length;
|
|
279
|
+
var b = new Array(len + 1);
|
|
280
|
+
b[0] = 0;
|
|
281
|
+
for (var i = 0; i < len; i++) b[i + 1] = a[i];
|
|
282
|
+
return b;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
//Provides: caml_list_of_js_array const (mutable)
|
|
286
|
+
function caml_list_of_js_array(a) {
|
|
287
|
+
var l = 0;
|
|
288
|
+
for (var i = a.length - 1; i >= 0; i--) {
|
|
289
|
+
var e = a[i];
|
|
290
|
+
l = [0, e, l];
|
|
291
|
+
}
|
|
292
|
+
return l;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
//Provides: caml_list_to_js_array const (mutable)
|
|
296
|
+
function caml_list_to_js_array(l) {
|
|
297
|
+
var a = [];
|
|
298
|
+
for (; l !== 0; l = l[2]) {
|
|
299
|
+
a.push(l[1]);
|
|
300
|
+
}
|
|
301
|
+
return a;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
//Provides: caml_js_var mutable
|
|
305
|
+
//Requires: caml_jsstring_of_string
|
|
306
|
+
function caml_js_var(x) {
|
|
307
|
+
var x = caml_jsstring_of_string(x);
|
|
308
|
+
//Checks that x has the form ident[.ident]*
|
|
309
|
+
if (!x.match(/^[a-zA-Z_$][a-zA-Z_$0-9]*(\.[a-zA-Z_$][a-zA-Z_$0-9]*)*$/)) {
|
|
310
|
+
console.error(
|
|
311
|
+
'caml_js_var: "' +
|
|
312
|
+
x +
|
|
313
|
+
'" is not a valid JavaScript variable. continuing ..',
|
|
314
|
+
);
|
|
315
|
+
//console.error("Js.Unsafe.eval_string")
|
|
316
|
+
}
|
|
317
|
+
// biome-ignore lint/security/noGlobalEval: ..
|
|
318
|
+
return eval?.(x);
|
|
319
|
+
}
|
|
320
|
+
//Provides: caml_js_call (const, mutable, shallow)
|
|
321
|
+
//Requires: caml_js_from_array
|
|
322
|
+
function caml_js_call(f, o, args) {
|
|
323
|
+
return f.apply(o, caml_js_from_array(args));
|
|
324
|
+
}
|
|
325
|
+
//Provides: caml_js_fun_call (const, shallow)
|
|
326
|
+
//Requires: caml_js_from_array
|
|
327
|
+
function caml_js_fun_call(f, a) {
|
|
328
|
+
switch (a.length) {
|
|
329
|
+
case 1:
|
|
330
|
+
return f();
|
|
331
|
+
case 2:
|
|
332
|
+
return f(a[1]);
|
|
333
|
+
case 3:
|
|
334
|
+
return f(a[1], a[2]);
|
|
335
|
+
case 4:
|
|
336
|
+
return f(a[1], a[2], a[3]);
|
|
337
|
+
case 5:
|
|
338
|
+
return f(a[1], a[2], a[3], a[4]);
|
|
339
|
+
case 6:
|
|
340
|
+
return f(a[1], a[2], a[3], a[4], a[5]);
|
|
341
|
+
case 7:
|
|
342
|
+
return f(a[1], a[2], a[3], a[4], a[5], a[6]);
|
|
343
|
+
case 8:
|
|
344
|
+
return f(a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
|
|
345
|
+
}
|
|
346
|
+
return f.apply(null, caml_js_from_array(a));
|
|
347
|
+
}
|
|
348
|
+
//Provides: caml_js_meth_call (mutable, const, shallow)
|
|
349
|
+
//Requires: caml_jsstring_of_string
|
|
350
|
+
//Requires: caml_js_from_array
|
|
351
|
+
function caml_js_meth_call(o, f, args) {
|
|
352
|
+
return o[caml_jsstring_of_string(f)].apply(o, caml_js_from_array(args));
|
|
353
|
+
}
|
|
354
|
+
//Provides: caml_js_new (const, shallow)
|
|
355
|
+
//Requires: caml_js_from_array
|
|
356
|
+
function caml_js_new(c, a) {
|
|
357
|
+
switch (a.length) {
|
|
358
|
+
case 1:
|
|
359
|
+
return new c();
|
|
360
|
+
case 2:
|
|
361
|
+
return new c(a[1]);
|
|
362
|
+
case 3:
|
|
363
|
+
return new c(a[1], a[2]);
|
|
364
|
+
case 4:
|
|
365
|
+
return new c(a[1], a[2], a[3]);
|
|
366
|
+
case 5:
|
|
367
|
+
return new c(a[1], a[2], a[3], a[4]);
|
|
368
|
+
case 6:
|
|
369
|
+
return new c(a[1], a[2], a[3], a[4], a[5]);
|
|
370
|
+
case 7:
|
|
371
|
+
return new c(a[1], a[2], a[3], a[4], a[5], a[6]);
|
|
372
|
+
case 8:
|
|
373
|
+
return new c(a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
|
|
374
|
+
}
|
|
375
|
+
function F() {
|
|
376
|
+
return c.apply(this, caml_js_from_array(a));
|
|
377
|
+
}
|
|
378
|
+
F.prototype = c.prototype;
|
|
379
|
+
return new F();
|
|
380
|
+
}
|
|
381
|
+
//Provides: caml_ojs_new_arr (const, shallow)
|
|
382
|
+
//Requires: caml_js_from_array
|
|
383
|
+
function caml_ojs_new_arr(c, a) {
|
|
384
|
+
switch (a.length) {
|
|
385
|
+
case 0:
|
|
386
|
+
return new c();
|
|
387
|
+
case 1:
|
|
388
|
+
return new c(a[0]);
|
|
389
|
+
case 2:
|
|
390
|
+
return new c(a[0], a[1]);
|
|
391
|
+
case 3:
|
|
392
|
+
return new c(a[0], a[1], a[2]);
|
|
393
|
+
case 4:
|
|
394
|
+
return new c(a[0], a[1], a[2], a[3]);
|
|
395
|
+
case 5:
|
|
396
|
+
return new c(a[0], a[1], a[2], a[3], a[4]);
|
|
397
|
+
case 6:
|
|
398
|
+
return new c(a[0], a[1], a[2], a[3], a[4], a[5]);
|
|
399
|
+
case 7:
|
|
400
|
+
return new c(a[0], a[1], a[2], a[3], a[4], a[5], a[6]);
|
|
401
|
+
}
|
|
402
|
+
function F() {
|
|
403
|
+
return c.apply(this, a);
|
|
404
|
+
}
|
|
405
|
+
F.prototype = c.prototype;
|
|
406
|
+
return new F();
|
|
407
|
+
}
|
|
408
|
+
//Provides: caml_js_wrap_callback const (const)
|
|
409
|
+
//Requires: caml_callback
|
|
410
|
+
function caml_js_wrap_callback(f) {
|
|
411
|
+
return function (...args) {
|
|
412
|
+
if (args.length === 0) {
|
|
413
|
+
args = [undefined];
|
|
414
|
+
}
|
|
415
|
+
var res = caml_callback(f, args);
|
|
416
|
+
return res instanceof Function ? caml_js_wrap_callback(res) : res;
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
//Provides: caml_js_wrap_callback_arguments
|
|
421
|
+
//Requires: caml_callback
|
|
422
|
+
function caml_js_wrap_callback_arguments(f) {
|
|
423
|
+
return function (...args) {
|
|
424
|
+
return caml_callback(f, [args]);
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
//Provides: caml_js_wrap_callback_strict const
|
|
428
|
+
//Requires: caml_callback
|
|
429
|
+
function caml_js_wrap_callback_strict(arity, f) {
|
|
430
|
+
return function (...args) {
|
|
431
|
+
args.length = arity;
|
|
432
|
+
return caml_callback(f, args);
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
//Provides: caml_js_wrap_callback_unsafe const (const)
|
|
436
|
+
//Requires: caml_callback, caml_js_function_arity
|
|
437
|
+
function caml_js_wrap_callback_unsafe(f) {
|
|
438
|
+
return function (...args) {
|
|
439
|
+
var len = caml_js_function_arity(f);
|
|
440
|
+
args.length = len;
|
|
441
|
+
return caml_callback(f, args);
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
//Provides: caml_js_wrap_meth_callback const (const)
|
|
445
|
+
//Requires: caml_callback, caml_js_wrap_callback
|
|
446
|
+
function caml_js_wrap_meth_callback(f) {
|
|
447
|
+
return function (...args) {
|
|
448
|
+
args.unshift(this);
|
|
449
|
+
var res = caml_callback(f, args);
|
|
450
|
+
return res instanceof Function ? caml_js_wrap_callback(res) : res;
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
//Provides: caml_js_wrap_meth_callback_arguments const (const)
|
|
454
|
+
//Requires: caml_callback
|
|
455
|
+
function caml_js_wrap_meth_callback_arguments(f) {
|
|
456
|
+
return function (...args) {
|
|
457
|
+
return caml_callback(f, [this, args]);
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
//Provides: caml_js_wrap_meth_callback_strict const
|
|
461
|
+
//Requires: caml_callback
|
|
462
|
+
function caml_js_wrap_meth_callback_strict(arity, f) {
|
|
463
|
+
return function (...args) {
|
|
464
|
+
args.length = arity;
|
|
465
|
+
args.unshift(this);
|
|
466
|
+
return caml_callback(f, args);
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
//Provides: caml_js_wrap_meth_callback_unsafe const (const)
|
|
470
|
+
//Requires: caml_callback, caml_js_function_arity
|
|
471
|
+
function caml_js_wrap_meth_callback_unsafe(f) {
|
|
472
|
+
return function (...args) {
|
|
473
|
+
var len = caml_js_function_arity(f);
|
|
474
|
+
args.unshift(this);
|
|
475
|
+
args.length = len;
|
|
476
|
+
return caml_callback(f, args);
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
//Provides: caml_js_function_arity
|
|
481
|
+
//If: !effects
|
|
482
|
+
function caml_js_function_arity(f) {
|
|
483
|
+
return f.l >= 0 ? f.l : (f.l = f.length);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
//Provides: caml_js_function_arity
|
|
487
|
+
//If: effects
|
|
488
|
+
//If: doubletranslate
|
|
489
|
+
function caml_js_function_arity(f) {
|
|
490
|
+
return f.l >= 0 ? f.l : (f.l = f.length);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
//Provides: caml_js_function_arity
|
|
494
|
+
//If: effects
|
|
495
|
+
//If: !doubletranslate
|
|
496
|
+
function caml_js_function_arity(f) {
|
|
497
|
+
// Functions have an additional continuation parameter. This should
|
|
498
|
+
// not be visible when calling them from JavaScript
|
|
499
|
+
return (f.l >= 0 ? f.l : (f.l = f.length)) - 1;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
//Provides: caml_js_equals mutable (const, const)
|
|
503
|
+
function caml_js_equals(x, y) {
|
|
504
|
+
// biome-ignore lint/suspicious/noDoubleEquals: ..
|
|
505
|
+
return +(x == y);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
//Provides: caml_js_strict_equals mutable (const, const)
|
|
509
|
+
function caml_js_strict_equals(x, y) {
|
|
510
|
+
return +(x === y);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
//Provides: caml_js_eval_string (const)
|
|
514
|
+
//Requires: caml_jsstring_of_string
|
|
515
|
+
function caml_js_eval_string(s) {
|
|
516
|
+
// Uses an indirect eval through the optional chaining operator.
|
|
517
|
+
// (see https://mdn.dev/docs/Web/JavaScript/Reference/Global_Objects/eval)
|
|
518
|
+
// This is faster and avoid variable captures.
|
|
519
|
+
// Also prepends `"use strict"` directive since this is not inherited
|
|
520
|
+
// from the enclosing function with an indirect eval.
|
|
521
|
+
// biome-ignore lint/security/noGlobalEval: ..
|
|
522
|
+
return eval?.('"use strict";' + caml_jsstring_of_string(s));
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
//Provides: caml_js_expr (const)
|
|
526
|
+
//Requires: caml_jsstring_of_string
|
|
527
|
+
function caml_js_expr(s) {
|
|
528
|
+
console.error("caml_js_expr: fallback to runtime evaluation\n");
|
|
529
|
+
// We add parentheses to avoid the ambiguity between expressions
|
|
530
|
+
// and statements. This means that we accept invalid inputs like
|
|
531
|
+
// "a)(b", but this is unlikely to be an issue in practice.
|
|
532
|
+
// biome-ignore lint/security/noGlobalEval: ..
|
|
533
|
+
return eval?.('"use strict";(' + caml_jsstring_of_string(s) + ")");
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
//Provides: caml_pure_js_expr const (const)
|
|
537
|
+
//Requires: caml_jsstring_of_string
|
|
538
|
+
function caml_pure_js_expr(s) {
|
|
539
|
+
console.error("caml_pure_js_expr: fallback to runtime evaluation\n");
|
|
540
|
+
// biome-ignore lint/security/noGlobalEval: ..
|
|
541
|
+
return eval?.('"use strict";(' + caml_jsstring_of_string(s) + ")");
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
//Provides: caml_js_object (object_literal)
|
|
545
|
+
//Requires: caml_jsstring_of_string
|
|
546
|
+
function caml_js_object(a) {
|
|
547
|
+
var o = {};
|
|
548
|
+
for (var i = 1; i < a.length; i++) {
|
|
549
|
+
var p = a[i];
|
|
550
|
+
o[caml_jsstring_of_string(p[1])] = p[2];
|
|
551
|
+
}
|
|
552
|
+
return o;
|
|
553
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// Js_of_ocaml library
|
|
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
|
+
///////////// Jslib: code specific to Js_of_ocaml
|
|
21
|
+
|
|
22
|
+
//Provides: caml_js_html_entities
|
|
23
|
+
function caml_js_html_entities(s) {
|
|
24
|
+
var entity = /^&#?[0-9a-zA-Z]+;$/;
|
|
25
|
+
if (s.match(entity)) {
|
|
26
|
+
var str,
|
|
27
|
+
temp = document.createElement("p");
|
|
28
|
+
temp.innerHTML = s;
|
|
29
|
+
str = temp.textContent || temp.innerText;
|
|
30
|
+
temp = null;
|
|
31
|
+
return str;
|
|
32
|
+
} else {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
//Provides: caml_js_get_console const
|
|
38
|
+
function caml_js_get_console() {
|
|
39
|
+
var c = console;
|
|
40
|
+
var m = [
|
|
41
|
+
"log",
|
|
42
|
+
"debug",
|
|
43
|
+
"info",
|
|
44
|
+
"warn",
|
|
45
|
+
"error",
|
|
46
|
+
"assert",
|
|
47
|
+
"dir",
|
|
48
|
+
"dirxml",
|
|
49
|
+
"trace",
|
|
50
|
+
"group",
|
|
51
|
+
"groupCollapsed",
|
|
52
|
+
"groupEnd",
|
|
53
|
+
"time",
|
|
54
|
+
"timeEnd",
|
|
55
|
+
];
|
|
56
|
+
function f() {}
|
|
57
|
+
for (var i = 0; i < m.length; i++) if (!c[m[i]]) c[m[i]] = f;
|
|
58
|
+
return c;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
//Provides: caml_xmlhttprequest_create
|
|
62
|
+
//Requires: caml_failwith
|
|
63
|
+
//Weakdef
|
|
64
|
+
function caml_xmlhttprequest_create(_unit) {
|
|
65
|
+
if (typeof XMLHttpRequest === "undefined") {
|
|
66
|
+
caml_failwith("XMLHttpRequest is not available");
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
return new XMLHttpRequest();
|
|
70
|
+
} catch {
|
|
71
|
+
caml_failwith("Failed to create XMLHttpRequest");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
//Provides: caml_js_error_of_exception
|
|
76
|
+
function caml_js_error_of_exception(exn) {
|
|
77
|
+
if (exn.js_error) {
|
|
78
|
+
return exn.js_error;
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|