Skip to content

Commit

Permalink
[js] Handle all the modes that can be passed to nqp::open
Browse files Browse the repository at this point in the history
  • Loading branch information
pmurias committed May 8, 2017
1 parent 66eae59 commit 674cb40
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/vm/js/nqp-runtime/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,32 @@ class FileHandle extends NQPObject {

exports.FileHandle = FileHandle;

function modeToFlags(mode) {
let flags;

if (mode === 'w') {
flags = fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_TRUNC;
} else {
if (mode[0] === 'r') flags = fs.constants.O_RDONLY;
else if (mode[0] === '-') flags = fs.constants.O_WRONLY;
else if (mode[0] === '+') flags = fs.constants.O_RDWR;
else if (mode[0] === 'w') flags = fs.constants.O_WRONLY | fs.constants.O_CREAT;
else throw 'unknown mode to open: ' + mode;
}

for (let c of mode.substr(1)) {
if (c === 'a') flags |= fs.constants.O_APPEND;
else if (c === 'c') flags |= fs.constants.O_CREAT;
else if (c === 't') flags |= fs.constants.O_TRUNC;
else if (c === 'x') flags |= fs.constants.O_EXCL;
else throw 'unknown mode to open: ' + mode;
}

return flags;
}

op.open = function(name, mode) {
var modes = {r: 'r', w: 'w', wa: 'a', '-ct': 'w', '-ca': 'a'};
if (!modes[mode]) { throw 'unknown mode to open: ' + mode }
var fh = new FileHandle(fs.openSync(name, modes[mode]));
let fh = new FileHandle(fs.openSync(name, modeToFlags(mode)));
fh.encoding = 'utf8';
return fh;
};
Expand Down

0 comments on commit 674cb40

Please sign in to comment.