Skip to content

Commit

Permalink
Make seekfh throw exceptions when passed an invalid offset or whence …
Browse files Browse the repository at this point in the history
…0 and a negative offset.
  • Loading branch information
pmurias committed Sep 7, 2015
1 parent f282ebe commit ca3389d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/vm/js/QAST/Compiler.nqp
Expand Up @@ -544,7 +544,8 @@ class QAST::OperationsJS {
add_simple_op('open', $T_OBJ, [$T_STR, $T_STR], :sideffects);

add_simple_op('tellfh', $T_INT, [$T_OBJ], :sideffects);
add_simple_op('seekfh', $T_INT, [$T_OBJ, $T_INT, $T_INT], :sideffects);
add_simple_op('seekfh', $T_INT, [$T_OBJ, $T_INT, $T_INT],
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('readallfh', $T_STR, [$T_OBJ], :sideffects);
Expand Down
8 changes: 7 additions & 1 deletion src/vm/js/nqp-runtime/io.js
Expand Up @@ -166,7 +166,13 @@ op.readallfh = function(fh) {
return iconv.decode(all, fh.encoding);
};

op.seekfh = function(fh, offset, whence) {
op.seekfh = function(fh, offset, whence, ctx) {
if (whence == 0 && offset < 0) {
ctx.die("Can't seek to position: " + offset);
}
if (!(whence == 0 || whence == 1 || whence == 2)) {
ctx.die("Invalid whence passed to seekfh: " + whence);
}
fs.seekSync(fh.fd, offset, whence);
};

Expand Down

0 comments on commit ca3389d

Please sign in to comment.