@@ -422,7 +422,7 @@ struct JSContext {
422
422
/* if NULL, eval is not supported */
423
423
JSValue (*eval_internal)(JSContext *ctx, JSValue this_obj,
424
424
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);
426
426
void *user_opaque;
427
427
};
428
428
@@ -1236,7 +1236,7 @@ static void js_async_function_resolve_mark(JSRuntime *rt, JSValue val,
1236
1236
JS_MarkFunc *mark_func);
1237
1237
static JSValue JS_EvalInternal(JSContext *ctx, JSValue this_obj,
1238
1238
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);
1240
1240
static void js_free_module_def(JSContext *ctx, JSModuleDef *m);
1241
1241
static void js_mark_module_def(JSRuntime *rt, JSModuleDef *m,
1242
1242
JS_MarkFunc *mark_func);
@@ -33362,12 +33362,12 @@ static __exception int js_parse_program(JSParseState *s)
33362
33362
33363
33363
static void js_parse_init(JSContext *ctx, JSParseState *s,
33364
33364
const char *input, size_t input_len,
33365
- const char *filename)
33365
+ const char *filename, int line )
33366
33366
{
33367
33367
memset(s, 0, sizeof(*s));
33368
33368
s->ctx = ctx;
33369
33369
s->filename = filename;
33370
- s->line_num = 1 ;
33370
+ s->line_num = line ;
33371
33371
s->col_num = 1;
33372
33372
s->buf_start = s->buf_ptr = (const uint8_t *)input;
33373
33373
s->buf_end = s->buf_ptr + input_len;
@@ -33419,7 +33419,7 @@ JSValue JS_EvalFunction(JSContext *ctx, JSValue fun_obj)
33419
33419
/* `export_name` and `input` may be pure ASCII or UTF-8 encoded */
33420
33420
static JSValue __JS_EvalInternal(JSContext *ctx, JSValue this_obj,
33421
33421
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)
33423
33423
{
33424
33424
JSParseState s1, *s = &s1;
33425
33425
int err, eval_type;
@@ -33431,7 +33431,7 @@ static JSValue __JS_EvalInternal(JSContext *ctx, JSValue this_obj,
33431
33431
JSModuleDef *m;
33432
33432
bool is_strict_mode;
33433
33433
33434
- js_parse_init(ctx, s, input, input_len, filename);
33434
+ js_parse_init(ctx, s, input, input_len, filename, line );
33435
33435
skip_shebang(&s->buf_ptr, s->buf_end);
33436
33436
33437
33437
eval_type = flags & JS_EVAL_TYPE_MASK;
@@ -33461,7 +33461,7 @@ static JSValue __JS_EvalInternal(JSContext *ctx, JSValue this_obj,
33461
33461
is_strict_mode = true;
33462
33462
}
33463
33463
}
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);
33465
33465
if (!fd)
33466
33466
goto fail1;
33467
33467
s->cur_func = fd;
@@ -33534,12 +33534,12 @@ static JSValue __JS_EvalInternal(JSContext *ctx, JSValue this_obj,
33534
33534
/* the indirection is needed to make 'eval' optional */
33535
33535
static JSValue JS_EvalInternal(JSContext *ctx, JSValue this_obj,
33536
33536
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)
33538
33538
{
33539
33539
if (unlikely(!ctx->eval_internal)) {
33540
33540
return JS_ThrowTypeError(ctx, "eval is not supported");
33541
33541
}
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,
33543
33543
flags, scope_idx);
33544
33544
}
33545
33545
@@ -33555,7 +33555,7 @@ static JSValue JS_EvalObject(JSContext *ctx, JSValue this_obj,
33555
33555
str = JS_ToCStringLen(ctx, &len, val);
33556
33556
if (!str)
33557
33557
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);
33559
33559
JS_FreeCString(ctx, str);
33560
33560
return ret;
33561
33561
@@ -33565,20 +33565,52 @@ JSValue JS_EvalThis(JSContext *ctx, JSValue this_obj,
33565
33565
const char *input, size_t input_len,
33566
33566
const char *filename, int eval_flags)
33567
33567
{
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
+ }
33568
33590
JSValue ret;
33569
33591
33570
33592
assert((eval_flags & JS_EVAL_TYPE_MASK) == JS_EVAL_TYPE_GLOBAL ||
33571
33593
(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,
33573
33595
eval_flags, -1);
33574
33596
return ret;
33575
33597
}
33576
33598
33577
33599
JSValue JS_Eval(JSContext *ctx, const char *input, size_t input_len,
33578
33600
const char *filename, int eval_flags)
33579
33601
{
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);
33582
33614
}
33583
33615
33584
33616
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
45218
45250
JSParseState s1, *s = &s1;
45219
45251
JSValue val = JS_UNDEFINED;
45220
45252
45221
- js_parse_init(ctx, s, buf, buf_len, filename);
45253
+ js_parse_init(ctx, s, buf, buf_len, filename, 1 );
45222
45254
if (json_next_token(s))
45223
45255
goto fail;
45224
45256
val = json_parse_value(s);
@@ -56161,7 +56193,7 @@ bool JS_DetectModule(const char *input, size_t input_len)
56161
56193
return false;
56162
56194
}
56163
56195
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,
56165
56197
JS_EVAL_TYPE_MODULE|JS_EVAL_FLAG_COMPILE_ONLY, -1);
56166
56198
if (JS_IsException(val)) {
56167
56199
const char *msg = JS_ToCString(ctx, rt->current_exception);
0 commit comments