Skip to content

Commit 38330d2

Browse files
committed
Add JS_Eval* overloads taking line
1 parent f45e4f0 commit 38330d2

File tree

2 files changed

+62
-16
lines changed

2 files changed

+62
-16
lines changed

quickjs.c

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ struct JSContext {
422422
/* if NULL, eval is not supported */
423423
JSValue (*eval_internal)(JSContext *ctx, JSValue this_obj,
424424
const char *input, size_t input_len,
425-
const char *filename, int flags, int scope_idx);
425+
const char *filename, int line, int flags, int scope_idx);
426426
void *user_opaque;
427427
};
428428

@@ -1236,7 +1236,7 @@ static void js_async_function_resolve_mark(JSRuntime *rt, JSValue val,
12361236
JS_MarkFunc *mark_func);
12371237
static JSValue JS_EvalInternal(JSContext *ctx, JSValue this_obj,
12381238
const char *input, size_t input_len,
1239-
const char *filename, int flags, int scope_idx);
1239+
const char *filename, int line, int flags, int scope_idx);
12401240
static void js_free_module_def(JSContext *ctx, JSModuleDef *m);
12411241
static void js_mark_module_def(JSRuntime *rt, JSModuleDef *m,
12421242
JS_MarkFunc *mark_func);
@@ -33362,12 +33362,12 @@ static __exception int js_parse_program(JSParseState *s)
3336233362

3336333363
static void js_parse_init(JSContext *ctx, JSParseState *s,
3336433364
const char *input, size_t input_len,
33365-
const char *filename)
33365+
const char *filename, int line)
3336633366
{
3336733367
memset(s, 0, sizeof(*s));
3336833368
s->ctx = ctx;
3336933369
s->filename = filename;
33370-
s->line_num = 1;
33370+
s->line_num = line;
3337133371
s->col_num = 1;
3337233372
s->buf_start = s->buf_ptr = (const uint8_t *)input;
3337333373
s->buf_end = s->buf_ptr + input_len;
@@ -33419,7 +33419,7 @@ JSValue JS_EvalFunction(JSContext *ctx, JSValue fun_obj)
3341933419
/* `export_name` and `input` may be pure ASCII or UTF-8 encoded */
3342033420
static JSValue __JS_EvalInternal(JSContext *ctx, JSValue this_obj,
3342133421
const char *input, size_t input_len,
33422-
const char *filename, int flags, int scope_idx)
33422+
const char *filename, int line, int flags, int scope_idx)
3342333423
{
3342433424
JSParseState s1, *s = &s1;
3342533425
int err, eval_type;
@@ -33431,7 +33431,7 @@ static JSValue __JS_EvalInternal(JSContext *ctx, JSValue this_obj,
3343133431
JSModuleDef *m;
3343233432
bool is_strict_mode;
3343333433

33434-
js_parse_init(ctx, s, input, input_len, filename);
33434+
js_parse_init(ctx, s, input, input_len, filename, line);
3343533435
skip_shebang(&s->buf_ptr, s->buf_end);
3343633436

3343733437
eval_type = flags & JS_EVAL_TYPE_MASK;
@@ -33461,7 +33461,7 @@ static JSValue __JS_EvalInternal(JSContext *ctx, JSValue this_obj,
3346133461
is_strict_mode = true;
3346233462
}
3346333463
}
33464-
fd = js_new_function_def(ctx, NULL, true, false, filename, 1, 1);
33464+
fd = js_new_function_def(ctx, NULL, true, false, filename, line, 1);
3346533465
if (!fd)
3346633466
goto fail1;
3346733467
s->cur_func = fd;
@@ -33534,12 +33534,12 @@ static JSValue __JS_EvalInternal(JSContext *ctx, JSValue this_obj,
3353433534
/* the indirection is needed to make 'eval' optional */
3353533535
static JSValue JS_EvalInternal(JSContext *ctx, JSValue this_obj,
3353633536
const char *input, size_t input_len,
33537-
const char *filename, int flags, int scope_idx)
33537+
const char *filename, int line, int flags, int scope_idx)
3353833538
{
3353933539
if (unlikely(!ctx->eval_internal)) {
3354033540
return JS_ThrowTypeError(ctx, "eval is not supported");
3354133541
}
33542-
return ctx->eval_internal(ctx, this_obj, input, input_len, filename,
33542+
return ctx->eval_internal(ctx, this_obj, input, input_len, filename, line,
3354333543
flags, scope_idx);
3354433544
}
3354533545

@@ -33555,7 +33555,7 @@ static JSValue JS_EvalObject(JSContext *ctx, JSValue this_obj,
3355533555
str = JS_ToCStringLen(ctx, &len, val);
3355633556
if (!str)
3355733557
return JS_EXCEPTION;
33558-
ret = JS_EvalInternal(ctx, this_obj, str, len, "<input>", flags, scope_idx);
33558+
ret = JS_EvalInternal(ctx, this_obj, str, len, "<input>", 1, flags, scope_idx);
3355933559
JS_FreeCString(ctx, str);
3356033560
return ret;
3356133561

@@ -33565,20 +33565,52 @@ JSValue JS_EvalThis(JSContext *ctx, JSValue this_obj,
3356533565
const char *input, size_t input_len,
3356633566
const char *filename, int eval_flags)
3356733567
{
33568+
JSEvalOptions options = {
33569+
.version = JS_EVAL_OPTIONS_VERSION,
33570+
.filename = filename,
33571+
.line_num = 1,
33572+
.eval_flags = eval_flags
33573+
};
33574+
return JS_EvalThis2(ctx, this_obj, input, input_len, &options);
33575+
}
33576+
33577+
JSValue JS_EvalThis2(JSContext *ctx, JSValue this_obj,
33578+
const char *input, size_t input_len,
33579+
JSEvalOptions *options)
33580+
{
33581+
const char *filename = "<unnamed>";
33582+
int line = 1;
33583+
int eval_flags = 0;
33584+
if (options) {
33585+
if (options->filename)
33586+
filename = options->filename;
33587+
line = options->line_num;
33588+
eval_flags = options->eval_flags;
33589+
}
3356833590
JSValue ret;
3356933591

3357033592
assert((eval_flags & JS_EVAL_TYPE_MASK) == JS_EVAL_TYPE_GLOBAL ||
3357133593
(eval_flags & JS_EVAL_TYPE_MASK) == JS_EVAL_TYPE_MODULE);
33572-
ret = JS_EvalInternal(ctx, this_obj, input, input_len, filename,
33594+
ret = JS_EvalInternal(ctx, this_obj, input, input_len, filename, line,
3357333595
eval_flags, -1);
3357433596
return ret;
3357533597
}
3357633598

3357733599
JSValue JS_Eval(JSContext *ctx, const char *input, size_t input_len,
3357833600
const char *filename, int eval_flags)
3357933601
{
33580-
return JS_EvalThis(ctx, ctx->global_obj, input, input_len, filename,
33581-
eval_flags);
33602+
JSEvalOptions options = {
33603+
.version = JS_EVAL_OPTIONS_VERSION,
33604+
.filename = filename,
33605+
.line_num = 1,
33606+
.eval_flags = eval_flags
33607+
};
33608+
return JS_EvalThis2(ctx, ctx->global_obj, input, input_len, &options);
33609+
}
33610+
33611+
JSValue JS_Eval2(JSContext *ctx, const char *input, size_t input_len, JSEvalOptions *options)
33612+
{
33613+
return JS_EvalThis2(ctx, ctx->global_obj, input, input_len, options);
3358233614
}
3358333615

3358433616
int JS_ResolveModule(JSContext *ctx, JSValue obj)
@@ -45218,7 +45250,7 @@ JSValue JS_ParseJSON(JSContext *ctx, const char *buf, size_t buf_len, const char
4521845250
JSParseState s1, *s = &s1;
4521945251
JSValue val = JS_UNDEFINED;
4522045252

45221-
js_parse_init(ctx, s, buf, buf_len, filename);
45253+
js_parse_init(ctx, s, buf, buf_len, filename, 1);
4522245254
if (json_next_token(s))
4522345255
goto fail;
4522445256
val = json_parse_value(s);
@@ -56161,7 +56193,7 @@ bool JS_DetectModule(const char *input, size_t input_len)
5616156193
return false;
5616256194
}
5616356195
JS_AddIntrinsicRegExp(ctx); // otherwise regexp literals don't parse
56164-
val = __JS_EvalInternal(ctx, JS_UNDEFINED, input, input_len, "<unnamed>",
56196+
val = __JS_EvalInternal(ctx, JS_UNDEFINED, input, input_len, "<unnamed>", 1,
5616556197
JS_EVAL_TYPE_MODULE|JS_EVAL_FLAG_COMPILE_ONLY, -1);
5616656198
if (JS_IsException(val)) {
5616756199
const char *msg = JS_ToCString(ctx, rt->current_exception);

quickjs.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,16 @@ typedef struct JSClassDef {
540540
JSClassExoticMethods *exotic;
541541
} JSClassDef;
542542

543+
#define JS_EVAL_OPTIONS_VERSION 1
544+
545+
typedef struct JSEvalOptions {
546+
int version;
547+
int eval_flags;
548+
const char *filename;
549+
int line_num;
550+
// can add new fields in ABI-compatible manner by incrementing JS_EVAL2_VERSION
551+
} JSEvalOptions;
552+
543553
#define JS_INVALID_CLASS_ID 0
544554
JS_EXTERN JSClassID JS_NewClassID(JSRuntime *rt, JSClassID *pclass_id);
545555
/* Returns the class ID if `v` is an object, otherwise returns JS_INVALID_CLASS_ID. */
@@ -794,10 +804,14 @@ JS_EXTERN bool JS_DetectModule(const char *input, size_t input_len);
794804
/* 'input' must be zero terminated i.e. input[input_len] = '\0'. */
795805
JS_EXTERN JSValue JS_Eval(JSContext *ctx, const char *input, size_t input_len,
796806
const char *filename, int eval_flags);
797-
/* same as JS_Eval() but with an explicit 'this_obj' parameter */
807+
JS_EXTERN JSValue JS_Eval2(JSContext *ctx, const char *input, size_t input_len,
808+
JSEvalOptions *options);
798809
JS_EXTERN JSValue JS_EvalThis(JSContext *ctx, JSValue this_obj,
799810
const char *input, size_t input_len,
800811
const char *filename, int eval_flags);
812+
JS_EXTERN JSValue JS_EvalThis2(JSContext *ctx, JSValue this_obj,
813+
const char *input, size_t input_len,
814+
JSEvalOptions *options);
801815
JS_EXTERN JSValue JS_GetGlobalObject(JSContext *ctx);
802816
JS_EXTERN int JS_IsInstanceOf(JSContext *ctx, JSValue val, JSValue obj);
803817
JS_EXTERN int JS_DefineProperty(JSContext *ctx, JSValue this_obj,

0 commit comments

Comments
 (0)