Support reading script source from stdin in qjs#947
Support reading script source from stdin in qjs#947bnoordhuis merged 1 commit intoquickjs-ng:masterfrom
Conversation
Replace js_load_file with something that doesn't choke on special files and don't call realpath(3) on /dev files because that doesn't work. Fixes: quickjs-ng#945
| do { | ||
| n = fread(tmp, 1, sizeof(tmp), f); |
There was a problem hiding this comment.
Why not use a regular loop while ((n = fread(tmp, 1, sizeof(tmp), f)) != 0) { ... }?
I the goal is to make sure buf is allocated for empty files, an explanatory comment would be welcome.
There was a problem hiding this comment.
Isn't that self-evident though? do/while is for when a loop should execute at least once.
There was a problem hiding this comment.
do/while loops are never self evident, IME they have a tendency to hide nasty bugs. Whenever you see one in code, read carefully and you might find a bug.
In this case, the loop runs at least once, and fread will return 0 on empty files, so the case of a 0 return must be handled gracefully. And as an extra bonus, the loop will run one extra time on file whose length is exactly a multiple of 8K. That's 2 corner cases to think about. With a regular while loop, there is no hidden special cases to take into consideration. Less brain space: unambiguous end test and explicit empty file test.
Replace js_load_file with something that doesn't choke on special files and don't call realpath(3) on /dev files because that doesn't work.
Now
echo 'print("hello")' | qjs /dev/stdinworks.Fixes: #945