forked from ocsigen/js_of_ocaml
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsys.js
354 lines (308 loc) · 9.67 KB
/
sys.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
// Js_of_ocaml runtime support
// http://www.ocsigen.org/js_of_ocaml/
//
// 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.
///////////// Sys
//Provides: caml_raise_sys_error (const)
//Requires: caml_raise_with_string, caml_global_data
function caml_raise_sys_error (msg) {
caml_raise_with_string(caml_global_data.Sys_error, msg);
}
//Provides: caml_sys_exit
//Requires: caml_invalid_argument
function caml_sys_exit (code) {
if(globalThis.quit) globalThis.quit(code);
//nodejs
if(globalThis.process && globalThis.process.exit)
globalThis.process.exit(code);
caml_invalid_argument("Function 'exit' not implemented");
}
//Provides: caml_is_special_exception
function caml_is_special_exception(exn){
switch(exn[2]) {
case -8: // Match_failure
case -11: // Assert_failure
case -12: // Undefined_recursive_module
return 1;
default:
return 0;
}
}
//Provides: caml_format_exception
//Requires: MlBytes, caml_is_special_exception
function caml_format_exception(exn){
var r = "";
if(exn[0] == 0) {
r += exn[1][1];
if(exn.length == 3 && exn[2][0] == 0 && caml_is_special_exception(exn[1])) {
var bucket = exn[2];
var start = 1;
} else {
var start = 2
var bucket = exn;
}
r += "(";
for(var i = start; i < bucket.length; i ++){
if(i > start) r+=", ";
var v = bucket[i]
if(typeof v == "number")
r+= v.toString();
else if(v instanceof MlBytes){
r+= '"' + v.toString() + '"';
}
else if(typeof v == "string"){
r+= '"' + v.toString() + '"';
}
else r += "_";
}
r += ")"
} else if (exn[0] == 248){
r += exn[1]
}
return r
}
//Provides: caml_fatal_uncaught_exception
//Requires: caml_named_value, caml_format_exception
function caml_fatal_uncaught_exception(err){
if(err instanceof Array && (err[0] == 0 || err[0] == 248)) {
var handler = caml_named_value("Printexc.handle_uncaught_exception");
if(handler) handler(err,false);
else {
var msg = caml_format_exception(err);
var at_exit = caml_named_value("Pervasives.do_at_exit");
if(at_exit) { at_exit(0) }
console.error("Fatal error: exception " + msg + "\n");
}
}
else {
throw err
}
}
//Provides: caml_set_static_env
function caml_set_static_env(k,v){
if(!globalThis.jsoo_static_env)
globalThis.jsoo_static_env = {}
globalThis.jsoo_static_env[k] = v;
return 0;
}
//Provides: caml_sys_getenv (const)
//Requires: caml_raise_not_found
//Requires: caml_string_of_jsstring
//Requires: caml_jsstring_of_string
function caml_sys_getenv (name) {
var process = globalThis.process;
var n = caml_jsstring_of_string(name);
//nodejs env
if(process
&& process.env
&& process.env[n] != undefined)
return caml_string_of_jsstring(process.env[n]);
if(globalThis.jsoo_static_env
&& globalThis.jsoo_static_env[n])
return caml_string_of_jsstring(globalThis.jsoo_static_env[n])
caml_raise_not_found ();
}
//Provides: caml_sys_unsafe_getenv
//Requires: caml_sys_getenv
function caml_sys_unsafe_getenv(name){
return caml_sys_getenv (name);
}
//Provides: caml_argv
//Requires: caml_string_of_jsstring
var caml_argv = ((function () {
var process = globalThis.process;
var main = "a.out";
var args = []
if(process
&& process.argv
&& process.argv.length > 1) {
var argv = process.argv
//nodejs
main = argv[1];
args = argv.slice(2);
}
var p = caml_string_of_jsstring(main);
var args2 = [0, p];
for(var i = 0; i < args.length; i++)
args2.push(caml_string_of_jsstring(args[i]));
return args2;
})())
//Provides: caml_executable_name
//Requires: caml_argv
var caml_executable_name = caml_argv[1]
//Provides: caml_sys_get_argv
//Requires: caml_argv
function caml_sys_get_argv (a) {
return [0, caml_argv[1], caml_argv];
}
//Provides: caml_sys_argv
//Requires: caml_argv
function caml_sys_argv (a) {
return caml_argv;
}
//Provides: caml_sys_modify_argv
//Requires: caml_argv
function caml_sys_modify_argv(arg){
caml_argv = arg;
return 0;
}
//Provides: caml_sys_executable_name const
//Requires: caml_executable_name
function caml_sys_executable_name(a){
return caml_executable_name
}
//Provides: caml_sys_system_command
//Requires: caml_jsstring_of_string
function caml_sys_system_command(cmd){
var cmd = caml_jsstring_of_string(cmd);
if (typeof require != "undefined"
&& require('child_process')
&& require('child_process').execSync) {
try {require('child_process').execSync(cmd,{stdio: 'inherit'}); return 0}
catch (e) {return 1}
}
else return 127;
}
//Provides: caml_sys_time mutable
var caml_initial_time = (new Date()).getTime() * 0.001;
function caml_sys_time () {
var now = (new Date()).getTime();
return now * 0.001 - caml_initial_time;
}
//Provides: caml_sys_time_include_children
//Requires: caml_sys_time
function caml_sys_time_include_children(b) {
return caml_sys_time();
}
//Provides: caml_sys_random_seed mutable
//The function needs to return an array since OCaml 4.0...
function caml_sys_random_seed () {
if(globalThis.crypto) {
if(typeof globalThis.crypto.getRandomValues === 'function'){
// Webbrowsers
var a = new Uint32Array(1);
globalThis.crypto.getRandomValues(a);
return [0,a[0]];
} else if(globalThis.crypto.randomBytes === 'function'){
// Nodejs
var buff = globalThis.crypto.randomBytes(4);
var a = new Uint32Array(buff);
return [0,a[0]];
}
}
var now = (new Date()).getTime();
var x = now^0xffffffff*Math.random();
return [0,x];
}
//Provides: caml_sys_const_big_endian const
function caml_sys_const_big_endian () { return 0; }
//Provides: caml_sys_const_word_size const
function caml_sys_const_word_size () { return 32; }
//Provides: caml_sys_const_int_size const
function caml_sys_const_int_size () { return 32; }
//Provides: caml_sys_const_max_wosize const
// max_int / 4 so that the following does not overflow
//let max_string_length = word_size / 8 * max_array_length - 1;;
function caml_sys_const_max_wosize () { return (0x7FFFFFFF/4) | 0;}
//Provides: caml_sys_const_ostype_unix const
//Requires: os_type
function caml_sys_const_ostype_unix () { return os_type == "Unix" ? 1 : 0; }
//Provides: caml_sys_const_ostype_win32 const
//Requires: os_type
function caml_sys_const_ostype_win32 () { return os_type == "Win32" ? 1 : 0; }
//Provides: caml_sys_const_ostype_cygwin const
//Requires: os_type
function caml_sys_const_ostype_cygwin () { return os_type == "Cygwin" ? 1 : 0; }
//Provides: caml_sys_const_backend_type const
//Requires: caml_string_of_jsbytes
function caml_sys_const_backend_type () {
return [0, caml_string_of_jsbytes("js_of_ocaml")];
}
//Provides: os_type
var os_type = (globalThis.process &&
globalThis.process.platform &&
globalThis.process.platform == "win32") ? "Cygwin" : "Unix";
//Provides: caml_sys_get_config const
//Requires: caml_string_of_jsbytes, os_type
function caml_sys_get_config () {
return [0, caml_string_of_jsbytes(os_type), 32, 0];
}
//Provides: caml_sys_isatty
function caml_sys_isatty(_chan) {
return 0;
}
//Provides: caml_runtime_variant
//Requires: caml_string_of_jsbytes
function caml_runtime_variant(_unit) {
return caml_string_of_jsbytes("");
}
//Provides: caml_runtime_parameters
//Requires: caml_string_of_jsbytes
function caml_runtime_parameters(_unit) {
return caml_string_of_jsbytes("");
}
//Provides: caml_install_signal_handler const
function caml_install_signal_handler(){return 0}
//Provides: unix_inet_addr_of_string
function unix_inet_addr_of_string () {return 0;}
//Provides: caml_runtime_warnings
var caml_runtime_warnings = 0;
//Provides: caml_ml_enable_runtime_warnings
//Requires: caml_runtime_warnings
function caml_ml_enable_runtime_warnings (bool) {
caml_runtime_warnings = bool;
return 0;
}
//Provides: caml_ml_runtime_warnings_enabled
//Requires: caml_runtime_warnings
function caml_ml_runtime_warnings_enabled (_unit) {
return caml_runtime_warnings;
}
//Provides: caml_spacetime_enabled const (const)
function caml_spacetime_enabled(_unit) {
return 0;
}
//Provides: caml_sys_const_naked_pointers_checked const (const)
function caml_sys_const_naked_pointers_checked(_unit) {
return 0;
}
//Provides: caml_register_channel_for_spacetime const (const)
function caml_register_channel_for_spacetime(_channel) {
return 0;
}
//Provides: caml_spacetime_only_works_for_native_code
//Requires: caml_failwith
function caml_spacetime_only_works_for_native_code() {
caml_failwith("Spacetime profiling only works for native code");
}
//Always
//Requires: caml_fatal_uncaught_exception
function caml_setup_uncaught_exception_handler() {
var process = globalThis.process;
if(process && process.on) {
process.on('uncaughtException', function (err, origin) {
caml_fatal_uncaught_exception(err);
process.exit (2);
})
}
else if(globalThis.addEventListener){
globalThis.addEventListener('error', function(event){
if(event.error){
caml_fatal_uncaught_exception(event.error);
}
});
}
}
caml_setup_uncaught_exception_handler();