Skip to content

Commit 34d1ed5

Browse files
committed
Runtime: cleanup and fixes
1 parent 6bde8cd commit 34d1ed5

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

runtime/js/fs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ function caml_read_file_content(name) {
358358
var file = root.device.open(root.rest, { rdonly: 1 });
359359
var len = file.length();
360360
var buf = new Uint8Array(len);
361-
file.read(buf, 0, len);
361+
file.read(buf, 0, len, false);
362362
return caml_string_of_uint8_array(buf);
363363
}
364364
caml_raise_no_such_file(caml_jsstring_of_string(name));

runtime/js/fs_fake.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ class MlFakeDevice {
293293
this.nm(name),
294294
);
295295
file = this.content[name];
296-
if (f.truncate) file.truncate();
296+
if (f.truncate) file.truncate(0);
297297
} else if (f.create) {
298298
this.create_dir_if_needed(name);
299299
this.content[name] = new MlFakeFile(caml_create_bytes(0));
@@ -362,8 +362,9 @@ class MlFakeFile extends MlFile {
362362

363363
truncate(len) {
364364
var old = this.data;
365+
var old_len = caml_ml_bytes_length(old);
365366
this.data = caml_create_bytes(len | 0);
366-
caml_blit_bytes(old, 0, this.data, 0, len);
367+
caml_blit_bytes(old, 0, this.data, 0, Math.min(len, old_len));
367368
}
368369

369370
length() {
@@ -393,12 +394,13 @@ class MlFakeFile extends MlFile {
393394
if (offset + len >= clen) {
394395
len = clen - offset;
395396
}
396-
if (len) {
397+
if (len > 0) {
397398
var data = caml_create_bytes(len | 0);
398399
caml_blit_bytes(this.data, offset, data, 0, len);
399400
buf.set(caml_uint8_array_of_bytes(data), pos);
401+
return len;
400402
}
401-
return len;
403+
return 0;
402404
}
403405
}
404406

runtime/js/fs_node.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,12 +403,13 @@ class MlNodeFd extends MlFile {
403403
var stats = this.fs.fstatSync(fd);
404404
flags.noSeek =
405405
stats.isCharacterDevice() || stats.isFIFO() || stats.isSocket();
406+
this.offset = this.flags.append ? stats.size : 0;
406407
} catch (err) {
407408
// The fstat will fail on standard streams under Windows with node
408409
// 18 (and lower). See https://github.com/libuv/libuv/pull/3811.
409410
flags.noSeek = true;
411+
this.offset = 0;
410412
}
411-
this.offset = this.flags.append ? stats.size : 0;
412413
this.seeked = false;
413414
}
414415

runtime/js/io.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var caml_sys_fds = new Array(3);
2727
function caml_sys_close(fd) {
2828
var x = caml_sys_fds[fd];
2929
if (x) {
30-
x.file.close();
30+
x.file.close(false);
3131
delete caml_sys_fds[fd];
3232
}
3333
return 0;
@@ -360,6 +360,7 @@ function caml_refill(chan) {
360360
chan.buffer,
361361
chan.buffer_max,
362362
chan.buffer.length - chan.buffer_max,
363+
false,
363364
);
364365
chan.offset += nread;
365366
chan.buffer_max += nread;
@@ -568,7 +569,7 @@ function caml_ml_flush(chanid) {
568569
);
569570
} else {
570571
for (var pos = 0; pos < chan.buffer_curr; ) {
571-
pos += chan.file.write(chan.buffer, pos, chan.buffer_curr - pos);
572+
pos += chan.file.write(chan.buffer, pos, chan.buffer_curr - pos, false);
572573
}
573574
}
574575
chan.offset += chan.buffer_curr;

0 commit comments

Comments
 (0)