Skip to content

Commit

Permalink
[js] Implement (or fix) nqp::stat, nqp::stat_time, nqp::lstat_time, n…
Browse files Browse the repository at this point in the history
…qp::lstat.
  • Loading branch information
pmurias committed Jan 11, 2016
1 parent 3baf0b6 commit a40c4c5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/vm/js/QAST/Compiler.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,10 @@ class QAST::OperationsJS {
add_simple_op('radix', $T_OBJ, [$T_INT, $T_STR, $T_INT, $T_INT]);

add_simple_op('stat', $T_INT, [$T_STR, $T_INT]);
add_simple_op('stat_time', $T_NUM, [$T_STR, $T_INT]);

add_simple_op('lstat', $T_INT, [$T_STR, $T_INT]);
add_simple_op('lstat_time', $T_NUM, [$T_STR, $T_INT]);

add_simple_op('defined', $T_INT, [$T_OBJ]);
%ops<isconcrete> := %ops<defined>;
Expand Down
25 changes: 23 additions & 2 deletions src/vm/js/nqp-runtime/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ op.say = function(arg) {
}
};

op.stat = function(file, code) {
function stat(file, code, lstat) {
var EXISTS = 0;
var FILESIZE = 1;
var ISDIR = 2;
Expand All @@ -54,7 +54,11 @@ op.stat = function(file, code) {

// we can't use fs.existsSync(file) as it follows symlinks
try {
var stats = fs.lstatSync(file);
if (lstat) {
var stats = fs.lstatSync(file);
} else {
var stats = fs.statSync(file);
}
} catch (err) {
if (code == EXISTS && err.code === 'ENOENT') {
return 0;
Expand Down Expand Up @@ -86,6 +90,23 @@ op.stat = function(file, code) {
case PLATFORM_BLOCKSIZE: return stats.blksize;
case PLATFORM_BLOCKS: return stats.blocks;
}
}


op.stat = function(file, code) {
return stat(file, code, false) | 0;
};

op.stat_time = function(file, code) {
return stat(file, code, false);
};

op.lstat = function(file, code) {
return stat(file, code, true) | 0;
};

op.lstat_time = function(file, code) {
return stat(file, code, true);
};

function FileHandle(fd) {
Expand Down

0 comments on commit a40c4c5

Please sign in to comment.