1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 17:57:17 +00:00

file/read on eof will return nil.

Also add documentation for :exit in import.
Address issue #91
Partially adress issue #93
This commit is contained in:
Calvin Rose 2019-05-14 11:04:17 -04:00
parent 805b3bbb88
commit f2313b9959
2 changed files with 8 additions and 2 deletions

View File

@ -1715,7 +1715,9 @@
symbols into the current environment, prepending a given prefix as needed. symbols into the current environment, prepending a given prefix as needed.
(use the :as or :prefix option to set a prefix). If no prefix is provided, (use the :as or :prefix option to set a prefix). If no prefix is provided,
use the name of the module as a prefix. One can also use :export true use the name of the module as a prefix. One can also use :export true
to re-export the imported symbols." to re-export the imported symbols. If :exit true is given as an argument,
any errors encountered at the top level in the module will cause (os/exit 1)
to be called."
[path & args] [path & args]
(def argm (map (fn [x] (def argm (map (fn [x]
(if (keyword? x) (if (keyword? x)

View File

@ -160,7 +160,7 @@ static Janet cfun_io_fopen(int32_t argc, Janet *argv) {
return f ? makef(f, flags) : janet_wrap_nil(); return f ? makef(f, flags) : janet_wrap_nil();
} }
/* Read up to n bytes into buffer. Return error string if error. */ /* Read up to n bytes into buffer. */
static void read_chunk(IOFile *iof, JanetBuffer *buffer, int32_t nBytesMax) { static void read_chunk(IOFile *iof, JanetBuffer *buffer, int32_t nBytesMax) {
if (!(iof->flags & (IO_READ | IO_UPDATE))) if (!(iof->flags & (IO_READ | IO_UPDATE)))
janet_panic("file is not readable"); janet_panic("file is not readable");
@ -183,6 +183,7 @@ static Janet cfun_io_fread(int32_t argc, Janet *argv) {
} else { } else {
buffer = janet_getbuffer(argv, 2); buffer = janet_getbuffer(argv, 2);
} }
int32_t bufstart = buffer->count;
if (janet_checktype(argv[1], JANET_KEYWORD)) { if (janet_checktype(argv[1], JANET_KEYWORD)) {
const uint8_t *sym = janet_unwrap_keyword(argv[1]); const uint8_t *sym = janet_unwrap_keyword(argv[1]);
if (!janet_cstrcmp(sym, "all")) { if (!janet_cstrcmp(sym, "all")) {
@ -207,6 +208,8 @@ static Janet cfun_io_fread(int32_t argc, Janet *argv) {
fseek(iof->file, 0, SEEK_SET); fseek(iof->file, 0, SEEK_SET);
read_chunk(iof, buffer, (int32_t) fsize); read_chunk(iof, buffer, (int32_t) fsize);
} }
/* Never return nil for :all */
return janet_wrap_buffer(buffer);
} else if (!janet_cstrcmp(sym, "line")) { } else if (!janet_cstrcmp(sym, "line")) {
for (;;) { for (;;) {
int x = fgetc(iof->file); int x = fgetc(iof->file);
@ -221,6 +224,7 @@ static Janet cfun_io_fread(int32_t argc, Janet *argv) {
if (len < 0) janet_panic("expected positive integer"); if (len < 0) janet_panic("expected positive integer");
read_chunk(iof, buffer, len); read_chunk(iof, buffer, len);
} }
if (bufstart == buffer->count) return janet_wrap_nil();
return janet_wrap_buffer(buffer); return janet_wrap_buffer(buffer);
} }