forked from ocsigen/js_of_ocaml
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.js
492 lines (440 loc) · 14.6 KB
/
io.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
// Js_of_ocaml runtime support
// http://www.ocsigen.org/js_of_ocaml/
// Copyright (C) 2014 Jérôme Vouillon, Hugo Heuzard
// 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.
///////////// Io
//Provides: caml_sys_close
//Requires: caml_global_data
function caml_sys_close(fd) {
delete caml_global_data.fds[fd];
return 0;
}
//Provides: caml_std_output
//Requires: caml_string_of_jsbytes, caml_ml_string_length, caml_ml_channels
function caml_std_output(chanid,s){
var chan = caml_ml_channels[chanid];
var str = caml_string_of_jsbytes(s);
var slen = caml_ml_string_length(str);
chan.file.write(chan.offset, str, 0, slen);
chan.offset += slen;
return 0;
}
//Provides: caml_sys_open
//Requires: caml_raise_sys_error, caml_global_data
//Requires: caml_create_bytes,MlFakeFile
//Requires: js_print_stderr, js_print_stdout
//Requires: caml_std_output
//Requires: resolve_fs_device
//Requires: caml_jsbytes_of_string
function caml_sys_open_internal(idx,output,file,flags) {
if(caml_global_data.fds === undefined) caml_global_data.fds = new Array();
flags=flags?flags:{};
var info = {};
info.file = file;
info.offset = flags.append?file.length():0;
info.flags = flags;
info.output = output;
caml_global_data.fds[idx] = info;
if(!caml_global_data.fd_last_idx || idx > caml_global_data.fd_last_idx)
caml_global_data.fd_last_idx = idx;
return idx;
}
function caml_sys_open (name, flags, _perms) {
var f = {};
while(flags){
switch(flags[1]){
case 0: f.rdonly = 1;break;
case 1: f.wronly = 1;break;
case 2: f.append = 1;break;
case 3: f.create = 1;break;
case 4: f.truncate = 1;break;
case 5: f.excl = 1; break;
case 6: f.binary = 1;break;
case 7: f.text = 1;break;
case 8: f.nonblock = 1;break;
}
flags=flags[2];
}
if(f.rdonly && f.wronly)
caml_raise_sys_error(caml_jsbytes_of_string(name) + " : flags Open_rdonly and Open_wronly are not compatible");
if(f.text && f.binary)
caml_raise_sys_error(caml_jsbytes_of_string(name) + " : flags Open_text and Open_binary are not compatible");
var root = resolve_fs_device(name);
var file = root.device.open(root.rest,f);
var idx = caml_global_data.fd_last_idx?caml_global_data.fd_last_idx:0;
return caml_sys_open_internal (idx+1,caml_std_output,file,f);
}
caml_sys_open_internal(0,caml_std_output, new MlFakeFile(caml_create_bytes(0)), {rdonly:1}); //stdin
caml_sys_open_internal(1,js_print_stdout, new MlFakeFile(caml_create_bytes(0)), {buffered:2}); //stdout
caml_sys_open_internal(2,js_print_stderr, new MlFakeFile(caml_create_bytes(0)), {buffered:2}); //stderr
// ocaml Channels
//Provides: caml_ml_set_channel_name
function caml_ml_set_channel_name() {
return 0
}
//Provides: caml_ml_channels
var caml_ml_channels = new Array();
//Provides: caml_ml_out_channels_list
//Requires: caml_ml_channels
function caml_ml_out_channels_list () {
var l = 0;
for(var c = 0; c < caml_ml_channels.length; c++){
if(caml_ml_channels[c] && caml_ml_channels[c].opened && caml_ml_channels[c].out)
l=[0,caml_ml_channels[c].fd,l];
}
return l;
}
//Provides: caml_ml_open_descriptor_out
//Requires: caml_ml_channels, caml_global_data
//Requires: caml_raise_sys_error
function caml_ml_open_descriptor_out (fd) {
var data = caml_global_data.fds[fd];
if(data.flags.rdonly) caml_raise_sys_error("fd "+ fd + " is readonly");
var buffered = (data.flags.buffered !== undefined) ? data.flags.buffered : 1;
var channel = {
file:data.file,
offset:data.offset,
fd:fd,
opened:true,
out:true,
buffer:"",
buffered:buffered
};
caml_ml_channels[channel.fd]=channel;
return channel.fd;
}
//Provides: caml_ml_open_descriptor_in
//Requires: caml_global_data,caml_sys_open,caml_raise_sys_error, caml_ml_channels
//Requires: fs_node_supported, caml_string_of_jsstring
function caml_ml_open_descriptor_in (fd) {
var data = caml_global_data.fds[fd];
if(data.flags.wronly) caml_raise_sys_error("fd "+ fd + " is writeonly");
var refill = null;
if(fd == 0 && fs_node_supported()){
var fs = require('fs');
refill = function () {
return caml_string_of_jsstring(fs.readFileSync(0, 'utf8'))};
}
var channel = {
file:data.file,
offset:data.offset,
fd:fd,
opened:true,
out: false,
refill:refill
};
caml_ml_channels[channel.fd]=channel;
return channel.fd;
}
//Provides: caml_channel_descriptor
//Requires: caml_global_data, caml_ml_channels
function caml_channel_descriptor(chanid){
var chan = caml_ml_channels[chanid];
return chan.fd;
}
//Provides: win_filedescr_of_channel
//Requires: caml_channel_descriptor
var win_filedescr_of_channel = caml_channel_descriptor
//Provides: caml_ml_set_binary_mode
//Requires: caml_global_data, caml_ml_channels
function caml_ml_set_binary_mode(chanid,mode){
var chan = caml_ml_channels[chanid];
var data = caml_global_data.fds[chan.fd];
data.flags.text = !mode
data.flags.binary = mode
return 0;
}
//Input from in_channel
//Provides: caml_ml_close_channel
//Requires: caml_ml_flush, caml_ml_channels
//Requires: caml_sys_close
function caml_ml_close_channel (chanid) {
var chan = caml_ml_channels[chanid];
caml_ml_flush(chanid);
chan.opened = false;
chan.file.close();
caml_sys_close(chan.fd)
return 0;
}
//Provides: caml_ml_channel_size
//Requires: caml_ml_channels
function caml_ml_channel_size(chanid) {
var chan = caml_ml_channels[chanid];
return chan.file.length();
}
//Provides: caml_ml_channel_size_64
//Requires: caml_int64_of_float,caml_ml_channels
function caml_ml_channel_size_64(chanid) {
var chan = caml_ml_channels[chanid];
return caml_int64_of_float(chan.file.length ());
}
//Provides: caml_ml_set_channel_output
//Requires: caml_ml_channels, caml_global_data
function caml_ml_set_channel_output(chanid,f) {
var chan = caml_ml_channels[chanid];
caml_global_data.fds[chan.fd].output = f;
return 0;
}
//Provides: caml_ml_set_channel_refill
//Requires: caml_ml_channels, caml_global_data
function caml_ml_set_channel_refill(chanid,f) {
caml_ml_channels[chanid].refill = f;
return 0;
}
//Provides: caml_ml_refill_input
//Requires: caml_ml_string_length
function caml_ml_refill_input (chan) {
var str = chan.refill();
var str_len = caml_ml_string_length(str);
if (str_len == 0) chan.refill = null;
chan.file.write(chan.file.length(), str, 0, str_len);
return str_len;
}
//Provides: caml_ml_may_refill_input
//Requires: caml_ml_refill_input, caml_ml_channels
function caml_ml_may_refill_input (chanid) {
var chan = caml_ml_channels[chanid];
if (chan.refill == null) return;
if (chan.file.length() != chan.offset) return;
caml_ml_refill_input (chan);
}
//Provides: caml_ml_input
//Requires: caml_ml_refill_input, caml_ml_channels
function caml_ml_input (chanid, s, i, l) {
var chan = caml_ml_channels[chanid];
var l2 = chan.file.length() - chan.offset;
if (l2 == 0 && chan.refill != null) l2 = caml_ml_refill_input(chan);
if (l2 < l) l = l2;
chan.file.read(chan.offset, s, i, l);
chan.offset += l;
return l;
}
//Provides: caml_input_value
//Requires: caml_marshal_data_size, caml_input_value_from_bytes, caml_create_bytes, caml_ml_channels
function caml_input_value (chanid) {
var chan = caml_ml_channels[chanid];
var buf = caml_create_bytes(8);
chan.file.read(chan.offset,buf,0,8);
// Header is 20 bytes
var len = caml_marshal_data_size (buf, 0) + 20;
var buf = caml_create_bytes(len);
chan.file.read(chan.offset,buf,0,len);
var offset = [0];
var res = caml_input_value_from_bytes(buf, offset);
chan.offset = chan.offset + offset[0];
return res;
}
//Provides: caml_input_value_to_outside_heap
//Requires: caml_input_value
function caml_input_value_to_outside_heap(c) {
return caml_input_value(c);
}
//Provides: caml_ml_input_char
//Requires: caml_raise_end_of_file, caml_array_bound_error
//Requires: caml_ml_may_refill_input, caml_ml_channels
function caml_ml_input_char (chanid) {
var chan = caml_ml_channels[chanid];
caml_ml_may_refill_input(chanid);
if (chan.offset >= chan.file.length())
caml_raise_end_of_file();
var res = chan.file.read_one(chan.offset);
chan.offset++;
return res;
}
//Provides: caml_ml_input_int
//Requires: caml_raise_end_of_file
//Requires: caml_ml_refill_input, caml_ml_channels
function caml_ml_input_int (chanid) {
var chan = caml_ml_channels[chanid];
var file = chan.file;
while ((chan.offset + 3) >= file.length()) {
var l = caml_ml_refill_input(chan);
if (l == 0) caml_raise_end_of_file();
}
var o = chan.offset;
var r =(file.read_one(o ) << 24)
| (file.read_one(o+1) << 16)
| (file.read_one(o+2) << 8)
| (file.read_one(o+3));
chan.offset+=4;
return r;
}
//Provides: caml_ml_seek_in
//Requires: caml_raise_sys_error, caml_ml_channels
function caml_ml_seek_in(chanid,pos){
var chan = caml_ml_channels[chanid];
if (chan.refill != null) caml_raise_sys_error("Illegal seek");
chan.offset = pos;
return 0;
}
//Provides: caml_ml_seek_in_64
//Requires: caml_int64_to_float, caml_raise_sys_error, caml_ml_channels
function caml_ml_seek_in_64(chanid,pos){
var chan = caml_ml_channels[chanid];
if (chan.refill != null) caml_raise_sys_error("Illegal seek");
chan.offset = caml_int64_to_float(pos);
return 0;
}
//Provides: caml_ml_pos_in
//Requires: caml_ml_channels
function caml_ml_pos_in(chanid) {return caml_ml_channels[chanid].offset}
//Provides: caml_ml_pos_in_64
//Requires: caml_int64_of_float, caml_ml_channels
function caml_ml_pos_in_64(chanid) {return caml_int64_of_float(caml_ml_channels[chanid].offset)}
//Provides: caml_ml_input_scan_line
//Requires: caml_array_bound_error
//Requires: caml_ml_may_refill_input, caml_ml_channels
function caml_ml_input_scan_line(chanid){
var chan = caml_ml_channels[chanid];
caml_ml_may_refill_input(chanid);
var p = chan.offset;
var len = chan.file.length();
if(p >= len) { return 0;}
while(true) {
if(p >= len) return - (p - chan.offset);
if(chan.file.read_one(p) == 10) return p - chan.offset + 1;
p++;
}
}
//Provides: caml_ml_flush
//Requires: caml_raise_sys_error, caml_global_data, caml_ml_channels
function caml_ml_flush (chanid) {
var chan = caml_ml_channels[chanid];
if(! chan.opened) caml_raise_sys_error("Cannot flush a closed channel");
if(!chan.buffer || chan.buffer == "") return 0;
if(chan.fd
&& caml_global_data.fds[chan.fd]
&& caml_global_data.fds[chan.fd].output) {
var output = caml_global_data.fds[chan.fd].output;
switch(output.length){
case 2: output(chanid,chan.buffer);break;
default: output(chan.buffer)
};
}
chan.buffer = "";
return 0;
}
//output to out_channel
//Provides: caml_ml_output_bytes
//Requires: caml_ml_flush,caml_ml_bytes_length
//Requires: caml_create_bytes, caml_blit_bytes, caml_raise_sys_error, caml_ml_channels, caml_string_of_bytes
//Requires: caml_jsbytes_of_string
function caml_ml_output_bytes(chanid,buffer,offset,len) {
var chan = caml_ml_channels[chanid];
if(! chan.opened) caml_raise_sys_error("Cannot output to a closed channel");
var bytes;
if(offset == 0 && caml_ml_bytes_length(buffer) == len)
bytes = buffer;
else {
bytes = caml_create_bytes(len);
caml_blit_bytes(buffer,offset,bytes,0,len);
}
var string = caml_string_of_bytes(bytes);
var jsstring = caml_jsbytes_of_string(string);
switch(chan.buffered){
case 0: // Unbuffered
chan.buffer+=jsstring
caml_ml_flush (chanid);
break
case 1: // Buffered (the default)
chan.buffer+=jsstring;
if(chan.buffer.length > 65536)
caml_ml_flush (chanid);
break;
case 2: // Buffered (only for stdout and stderr)
var id = jsstring.lastIndexOf("\n");
if(id < 0) {
chan.buffer+=jsstring;
if(chan.buffer.length > 65536)
caml_ml_flush (chanid);
}
else {
chan.buffer+=jsstring.substr(0,id+1);
caml_ml_flush (chanid);
chan.buffer += jsstring.substr(id+1);
}
break;
}
return 0;
}
//Provides: caml_ml_output
//Requires: caml_ml_output_bytes, caml_bytes_of_string
function caml_ml_output(chanid,buffer,offset,len){
return caml_ml_output_bytes(chanid,caml_bytes_of_string(buffer),offset,len);
}
//Provides: caml_ml_output_char
//Requires: caml_ml_output
//Requires: caml_string_of_jsbytes
function caml_ml_output_char (chanid,c) {
var s = caml_string_of_jsbytes(String.fromCharCode(c));
caml_ml_output(chanid,s,0,1);
return 0;
}
//Provides: caml_output_value
//Requires: caml_output_value_to_string, caml_ml_output,caml_ml_string_length
function caml_output_value (chanid,v,flags) {
var s = caml_output_value_to_string(v, flags);
caml_ml_output(chanid,s,0,caml_ml_string_length(s));
return 0;
}
//Provides: caml_ml_seek_out
//Requires: caml_ml_channels, caml_ml_flush
function caml_ml_seek_out(chanid,pos){
caml_ml_flush(chanid);
caml_ml_channels[chanid].offset = pos;
return 0;
}
//Provides: caml_ml_seek_out_64
//Requires: caml_int64_to_float, caml_ml_channels, caml_ml_flush
function caml_ml_seek_out_64(chanid,pos){
caml_ml_flush(chanid);
caml_ml_channels[chanid].offset = caml_int64_to_float(pos);
return 0;
}
//Provides: caml_ml_pos_out
//Requires: caml_ml_channels, caml_ml_flush
function caml_ml_pos_out(chanid) {
caml_ml_flush(chanid);
return caml_ml_channels[chanid].offset
}
//Provides: caml_ml_pos_out_64
//Requires: caml_int64_of_float, caml_ml_channels, caml_ml_flush
function caml_ml_pos_out_64(chanid) {
caml_ml_flush(chanid);
return caml_int64_of_float (caml_ml_channels[chanid].offset);
}
//Provides: caml_ml_output_int
//Requires: caml_ml_output
//Requires: caml_string_of_array
function caml_ml_output_int (chanid,i) {
var arr = [(i>>24) & 0xFF,(i>>16) & 0xFF,(i>>8) & 0xFF,i & 0xFF ];
var s = caml_string_of_array(arr);
caml_ml_output(chanid,s,0,4);
return 0
}
//Provides: caml_ml_is_buffered
//Requires: caml_ml_channels
function caml_ml_is_buffered(chanid) {
return caml_ml_channels[chanid].buffered ? 1 : 0
}
//Provides: caml_ml_set_buffered
//Requires: caml_ml_channels, caml_ml_flush
function caml_ml_set_buffered(chanid,v) {
caml_ml_channels[chanid].buffered = v;
if(!v) caml_ml_flush(chanid);
return 0
}