Skip to content

Commit

Permalink
cli: evaluate stdin if it's not a TTY
Browse files Browse the repository at this point in the history
  • Loading branch information
saghul committed Feb 24, 2022
1 parent eca0e9e commit 31c84b9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ configure_file(
${CMAKE_SOURCE_DIR}/src/js/bundle.js
${CMAKE_CURRENT_BINARY_DIR}/bundle.js
COPYONLY)
configure_file(
${CMAKE_SOURCE_DIR}/src/js/eval-stdin.js
${CMAKE_CURRENT_BINARY_DIR}/eval-stdin.js
COPYONLY)
configure_file(
${CMAKE_SOURCE_DIR}/src/js/repl.js
${CMAKE_CURRENT_BINARY_DIR}/repl.js
Expand Down
27 changes: 6 additions & 21 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,6 @@ static int eval_expr(JSContext *ctx, const char *buf) {
return ret;
}

static int eval_module(JSContext *ctx, const char *filepath) {
int ret = 0;
JSValue val = TJS_EvalModule(ctx, filepath, true);

if (JS_IsException(val)) {
tjs_dump_error(ctx);
ret = -1;
}
JS_FreeValue(ctx, val);
return ret;
}

static void print_help(void) {
printf("Usage: tjs [options] [file]\n"
"\n"
Expand Down Expand Up @@ -239,16 +227,13 @@ int main(int argc, char **argv) {
exit_code = EXIT_FAILURE;
goto exit;
}
} else if (optind >= argc) {
/* interactive mode */
if (TJS_RunRepl(ctx)) {
exit_code = EXIT_FAILURE;
goto exit;
}
} else {
/* evaluate file */
const char *filepath = argv[optind];
if (eval_module(ctx, filepath)) {
const char *filepath = NULL;
if (optind < argc) {
filepath = argv[optind];
}

if (TJS_RunMain(qrt, filepath)) {
exit_code = EXIT_FAILURE;
goto exit;
}
Expand Down
19 changes: 19 additions & 0 deletions src/js/eval-stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const gEval = globalThis.eval;
const decoder = new TextDecoder();

(async () => {
const readBuf = new Uint8Array(4096);
const buf = [];

while (true) {
const n = await tjs.stdin.read(readBuf);

if (n === 0) {
break;
}

buf.push(...readBuf.subarray(0, n));
}

gEval(decoder.decode(new Uint8Array(buf)));
})();
2 changes: 1 addition & 1 deletion src/tjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ TJSRuntime *TJS_GetRuntime(JSContext *ctx);
int TJS_Run(TJSRuntime *qrt);
void TJS_Stop(TJSRuntime *qrt);
JSValue TJS_EvalModule(JSContext *ctx, const char *filename, bool is_main);
int TJS_RunRepl(JSContext *ctx);
int TJS_RunMain(TJSRuntime *qrt, const char *filename);

#endif
34 changes: 31 additions & 3 deletions src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,21 @@
#include "tjs.h"

#include <string.h>
#include <unistd.h>

#define TJS__DEFAULT_STACK_SIZE 1048576

INCTXT(eval_stdin, "eval-stdin.js");

/**
* These are defined now:
*
* const unsigned char tjs__code_eval_stdin_data[];
* const unsigned char *const tjs__code_eval_stdin_end;
* const unsigned int tjs__code_eval_stdin_size;
*
*/

INCTXT(repl, "repl.js");

/**
Expand Down Expand Up @@ -438,6 +450,22 @@ JSValue TJS_EvalModule(JSContext *ctx, const char *filename, bool is_main) {
return ret;
}

int TJS_RunRepl(JSContext *ctx) {
return tjs__eval_text(ctx, tjs__code_repl_data, tjs__code_repl_size, "repl.js");
}
int TJS_RunMain(TJSRuntime *qrt, const char *filename) {
JSContext *ctx = qrt->ctx;

if (filename) {
int r = 0;
JSValue val = TJS_EvalModule(ctx, filename, true);

if (JS_IsException(val)) {
tjs_dump_error(ctx);
r = -1;
}
JS_FreeValue(ctx, val);
return r;
} else if (uv_guess_handle(STDIN_FILENO) == UV_TTY) {
return tjs__eval_text(ctx, tjs__code_repl_data, tjs__code_repl_size, "repl.js");
} else {
return tjs__eval_text(ctx, tjs__code_eval_stdin_data, tjs__code_eval_stdin_size, "eval-stdin.js");
}
};

0 comments on commit 31c84b9

Please sign in to comment.