Skip to content

Commit 674cb40

Browse files
committed
[js] Handle all the modes that can be passed to nqp::open
1 parent 66eae59 commit 674cb40

File tree

1 file changed

+25
-3
lines changed
  • src/vm/js/nqp-runtime

1 file changed

+25
-3
lines changed

src/vm/js/nqp-runtime/io.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,32 @@ class FileHandle extends NQPObject {
174174

175175
exports.FileHandle = FileHandle;
176176

177+
function modeToFlags(mode) {
178+
let flags;
179+
180+
if (mode === 'w') {
181+
flags = fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_TRUNC;
182+
} else {
183+
if (mode[0] === 'r') flags = fs.constants.O_RDONLY;
184+
else if (mode[0] === '-') flags = fs.constants.O_WRONLY;
185+
else if (mode[0] === '+') flags = fs.constants.O_RDWR;
186+
else if (mode[0] === 'w') flags = fs.constants.O_WRONLY | fs.constants.O_CREAT;
187+
else throw 'unknown mode to open: ' + mode;
188+
}
189+
190+
for (let c of mode.substr(1)) {
191+
if (c === 'a') flags |= fs.constants.O_APPEND;
192+
else if (c === 'c') flags |= fs.constants.O_CREAT;
193+
else if (c === 't') flags |= fs.constants.O_TRUNC;
194+
else if (c === 'x') flags |= fs.constants.O_EXCL;
195+
else throw 'unknown mode to open: ' + mode;
196+
}
197+
198+
return flags;
199+
}
200+
177201
op.open = function(name, mode) {
178-
var modes = {r: 'r', w: 'w', wa: 'a', '-ct': 'w', '-ca': 'a'};
179-
if (!modes[mode]) { throw 'unknown mode to open: ' + mode }
180-
var fh = new FileHandle(fs.openSync(name, modes[mode]));
202+
let fh = new FileHandle(fs.openSync(name, modeToFlags(mode)));
181203
fh.encoding = 'utf8';
182204
return fh;
183205
};

0 commit comments

Comments
 (0)