Skip to content

Commit

Permalink
[js] Implement nqp::readlinechompfh and nqp::setinputlineseps.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmurias committed Nov 4, 2015
1 parent c332679 commit 502fd48
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/vm/js/QAST/Compiler.nqp
Expand Up @@ -574,11 +574,13 @@ class QAST::OperationsJS {
sub ($fh, $offset, $whence) { "nqp.op.seekfh($fh, $offset, $whence, $*CTX)" }, :sideffects);
add_simple_op('eoffh', $T_INT, [$T_OBJ], :sideffects);
add_simple_op('readlinefh', $T_STR, [$T_OBJ], :sideffects);
add_simple_op('readlinechompfh', $T_STR, [$T_OBJ], :sideffects);
add_simple_op('readallfh', $T_STR, [$T_OBJ], :sideffects);
add_simple_op('printfh', $T_OBJ, [$T_OBJ, $T_STR], :sideffects);
add_simple_op('flushfh', $T_OBJ, [$T_OBJ], :sideffects);
add_simple_op('closefh', $T_OBJ, [$T_OBJ], :sideffects);
add_simple_op('setinputlinesep', $T_OBJ, [$T_OBJ, $T_STR], :sideffects);
add_simple_op('setinputlineseps', $T_OBJ, [$T_OBJ, $T_OBJ], :sideffects);

add_simple_op('bootarray', $T_OBJ, []);

Expand Down
4 changes: 3 additions & 1 deletion src/vm/js/bin/run_tests.pl
Expand Up @@ -14,6 +14,8 @@

my @nqp_tests = glob "t/nqp/{01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,25,26,27,28,29,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,50,51,53,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,83,84,85,88,89,90,91,92,93,94,95,96}*.t";

my @moar_tests = glob "t/moar/03*.t";

my @runtime_unit_tests = qw(t/js/varint.js);

my $node_version = `node -v`;
Expand All @@ -24,4 +26,4 @@
@nqp_tests = grep {!/19|78/} @nqp_tests;
}

$harness->runtests(@runtime_unit_tests, @nqp_tests, @regex, @serialization, @qast, @js_specific);
$harness->runtests(@runtime_unit_tests, @nqp_tests, @moar_tests, @regex, @serialization, @qast, @js_specific);
49 changes: 40 additions & 9 deletions src/vm/js/nqp-runtime/io.js
Expand Up @@ -128,10 +128,22 @@ op.setencoding = function(fh, encoding) {
};

op.setinputlinesep = function(fh, sep) {
fh.sep = sep;
fh.seps = [sep];
};

op.setinputlineseps = function(fh, seps) {
fh.seps = seps;
};

op.readlinefh = function(fh) {
return readline(fh, false);
};

op.readlinechompfh = function(fh) {
return readline(fh, true);
};

function readline(fh, chomp) {
var line = '';
var buffer = new Buffer(16);
var starting = fs.seekSync(fh.fd, 0, 1);
Expand All @@ -147,24 +159,43 @@ op.readlinefh = function(fh) {
var newline;
// TODO think/ask about a "" sep
var offset;
if (fh.sep) {
newline = string.indexOf(fh.sep);
if (newline != -1) {
newline += fh.sep.length - 1;
var sep;
var newline = -1;
if (fh.seps) {
for (var i=0; i < fh.seps.length; i++) {
var offset = string.indexOf(fh.seps[i]);
if (offset != -1 && (newline == -1 || offset < newline)) {
newline = offset;
sep = fh.seps[i];
}
}

} else {
var cr = string.indexOf('\r');
var nl = string.indexOf('\n');
newline = (cr != -1 ? (cr < nl ? (cr + 1 == nl ? nl : cr) : nl) : nl);
if (cr != -1 && cr < nl) {
if (cr < nl) {
newline = cr;
if (cr + 1 == nl) {
sep = "\r\n";
} else {
sep = "\r";
}
}
} else {
newline = nl;
sep = "\n";
}
}

if (newline != -1) {
var up_to_newline = string.slice(0, newline + 1);
var up_to_newline = string.slice(0, newline);
// THINK ABOUT decoding and encoding might give a different offset

fs.seekSync(fh.fd,
Buffer.byteLength(up_to_newline, fh.encoding) + starting, 0);
return up_to_newline;
Buffer.byteLength(up_to_newline + sep, fh.encoding) + starting, 0);

return (chomp ? up_to_newline : up_to_newline + sep);
}
}

Expand Down

0 comments on commit 502fd48

Please sign in to comment.