Skip to content

Commit

Permalink
core: rename TJS_EvalFile to TJS_EvalModule
Browse files Browse the repository at this point in the history
That's how we use it everywhere, make it explicit about it being for
modules only.
  • Loading branch information
saghul committed Feb 24, 2022
1 parent a6c77cb commit 3a0c21e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static int eval_expr(JSContext *ctx, const char *buf) {

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

if (JS_IsException(val)) {
tjs_dump_error(ctx);
Expand Down
2 changes: 1 addition & 1 deletion src/tjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ JSContext *TJS_GetJSContext(TJSRuntime *qrt);
TJSRuntime *TJS_GetRuntime(JSContext *ctx);
int TJS_Run(TJSRuntime *qrt);
void TJS_Stop(TJSRuntime *qrt);
JSValue TJS_EvalFile(JSContext *ctx, const char *filename, int eval_flags, bool is_main);
JSValue TJS_EvalModule(JSContext *ctx, const char *filename, bool is_main);
int TJS_RunRepl(JSContext *ctx);

#endif
39 changes: 11 additions & 28 deletions src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,10 @@ int tjs__load_file(JSContext *ctx, DynBuf *dbuf, const char *filename) {
return r;
}

JSValue TJS_EvalFile(JSContext *ctx, const char *filename, int flags, bool is_main) {
JSValue TJS_EvalModule(JSContext *ctx, const char *filename, bool is_main) {
DynBuf dbuf;
size_t dbuf_size;
int r, eval_flags;
int r;
JSValue ret;

dbuf_init(&dbuf);
Expand All @@ -414,32 +414,15 @@ JSValue TJS_EvalFile(JSContext *ctx, const char *filename, int flags, bool is_ma
/* Add null termination, required by JS_Eval. */
dbuf_putc(&dbuf, '\0');

if (flags == -1) {
if (JS_DetectModule((const char *) dbuf.buf, dbuf_size))
eval_flags = JS_EVAL_TYPE_MODULE;
else
eval_flags = JS_EVAL_TYPE_GLOBAL;
} else {
eval_flags = flags;
}

if ((eval_flags & JS_EVAL_TYPE_MASK) == JS_EVAL_TYPE_MODULE) {
/* for the modules, we compile then run to be able to set import.meta */
ret = JS_Eval(ctx,
(char *) dbuf.buf,
dbuf_size,
filename,
eval_flags | JS_EVAL_FLAG_COMPILE_ONLY);
if (!JS_IsException(ret)) {
js_module_set_import_meta(ctx, ret, TRUE, is_main);
ret = JS_EvalFunction(ctx, ret);
}
} else {
ret = JS_Eval(ctx,
(char *) dbuf.buf,
dbuf_size,
filename,
eval_flags);
/* Compile then run to be able to set import.meta */
ret = JS_Eval(ctx,
(char *) dbuf.buf,
dbuf_size,
filename,
JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
if (!JS_IsException(ret)) {
js_module_set_import_meta(ctx, ret, TRUE, is_main);
ret = JS_EvalFunction(ctx, ret);
}

/* Emit window 'load' event. */
Expand Down
2 changes: 1 addition & 1 deletion src/worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static JSValue worker_eval(JSContext *ctx, int argc, JSValueConst *argv) {
goto error;
}

ret = TJS_EvalFile(ctx, filename, JS_EVAL_TYPE_MODULE, false);
ret = TJS_EvalModule(ctx, filename, false);
JS_FreeCString(ctx, filename);

if (JS_IsException(ret)) {
Expand Down

0 comments on commit 3a0c21e

Please sign in to comment.