@@ -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);
@@ -33431,12 +33431,12 @@ static __exception int js_parse_program(JSParseState *s)
33431
33431
33432
33432
static void js_parse_init(JSContext *ctx, JSParseState *s,
33433
33433
const char *input, size_t input_len,
33434
- const char *filename)
33434
+ const char *filename, int line )
33435
33435
{
33436
33436
memset(s, 0, sizeof(*s));
33437
33437
s->ctx = ctx;
33438
33438
s->filename = filename;
33439
- s->line_num = 1 ;
33439
+ s->line_num = line ;
33440
33440
s->col_num = 1;
33441
33441
s->buf_start = s->buf_ptr = (const uint8_t *)input;
33442
33442
s->buf_end = s->buf_ptr + input_len;
@@ -33488,7 +33488,7 @@ JSValue JS_EvalFunction(JSContext *ctx, JSValue fun_obj)
33488
33488
/* `export_name` and `input` may be pure ASCII or UTF-8 encoded */
33489
33489
static JSValue __JS_EvalInternal(JSContext *ctx, JSValue this_obj,
33490
33490
const char *input, size_t input_len,
33491
- const char *filename, int flags, int scope_idx)
33491
+ const char *filename, int line, int flags, int scope_idx)
33492
33492
{
33493
33493
JSParseState s1, *s = &s1;
33494
33494
int err, eval_type;
@@ -33500,7 +33500,7 @@ static JSValue __JS_EvalInternal(JSContext *ctx, JSValue this_obj,
33500
33500
JSModuleDef *m;
33501
33501
bool is_strict_mode;
33502
33502
33503
- js_parse_init(ctx, s, input, input_len, filename);
33503
+ js_parse_init(ctx, s, input, input_len, filename, line );
33504
33504
skip_shebang(&s->buf_ptr, s->buf_end);
33505
33505
33506
33506
eval_type = flags & JS_EVAL_TYPE_MASK;
@@ -33530,7 +33530,7 @@ static JSValue __JS_EvalInternal(JSContext *ctx, JSValue this_obj,
33530
33530
is_strict_mode = true;
33531
33531
}
33532
33532
}
33533
- fd = js_new_function_def(ctx, NULL, true, false, filename, 1 , 1);
33533
+ fd = js_new_function_def(ctx, NULL, true, false, filename, line , 1);
33534
33534
if (!fd)
33535
33535
goto fail1;
33536
33536
s->cur_func = fd;
@@ -33603,12 +33603,12 @@ static JSValue __JS_EvalInternal(JSContext *ctx, JSValue this_obj,
33603
33603
/* the indirection is needed to make 'eval' optional */
33604
33604
static JSValue JS_EvalInternal(JSContext *ctx, JSValue this_obj,
33605
33605
const char *input, size_t input_len,
33606
- const char *filename, int flags, int scope_idx)
33606
+ const char *filename, int line, int flags, int scope_idx)
33607
33607
{
33608
33608
if (unlikely(!ctx->eval_internal)) {
33609
33609
return JS_ThrowTypeError(ctx, "eval is not supported");
33610
33610
}
33611
- return ctx->eval_internal(ctx, this_obj, input, input_len, filename,
33611
+ return ctx->eval_internal(ctx, this_obj, input, input_len, filename, line,
33612
33612
flags, scope_idx);
33613
33613
}
33614
33614
@@ -33624,7 +33624,7 @@ static JSValue JS_EvalObject(JSContext *ctx, JSValue this_obj,
33624
33624
str = JS_ToCStringLen(ctx, &len, val);
33625
33625
if (!str)
33626
33626
return JS_EXCEPTION;
33627
- ret = JS_EvalInternal(ctx, this_obj, str, len, "<input>", flags, scope_idx);
33627
+ ret = JS_EvalInternal(ctx, this_obj, str, len, "<input>", 1, flags, scope_idx);
33628
33628
JS_FreeCString(ctx, str);
33629
33629
return ret;
33630
33630
@@ -33634,20 +33634,55 @@ JSValue JS_EvalThis(JSContext *ctx, JSValue this_obj,
33634
33634
const char *input, size_t input_len,
33635
33635
const char *filename, int eval_flags)
33636
33636
{
33637
+ JSEvalOptions options = {
33638
+ .version = JS_EVAL_OPTIONS_VERSION,
33639
+ .filename = filename,
33640
+ .line_num = 1,
33641
+ .eval_flags = eval_flags
33642
+ };
33643
+ return JS_EvalThis2(ctx, this_obj, input, input_len, &options);
33644
+ }
33645
+
33646
+ JSValue JS_EvalThis2(JSContext *ctx, JSValue this_obj,
33647
+ const char *input, size_t input_len,
33648
+ JSEvalOptions *options)
33649
+ {
33650
+ const char *filename = "<unnamed>";
33651
+ int line = 1;
33652
+ int eval_flags = 0;
33653
+ if (options) {
33654
+ if (options->version != JS_EVAL_OPTIONS_VERSION)
33655
+ return JS_ThrowInternalError(ctx, "bad JSEvalOptions version");
33656
+ if (options->filename)
33657
+ filename = options->filename;
33658
+ if (options->line_num != 0)
33659
+ line = options->line_num;
33660
+ eval_flags = options->eval_flags;
33661
+ }
33637
33662
JSValue ret;
33638
33663
33639
33664
assert((eval_flags & JS_EVAL_TYPE_MASK) == JS_EVAL_TYPE_GLOBAL ||
33640
33665
(eval_flags & JS_EVAL_TYPE_MASK) == JS_EVAL_TYPE_MODULE);
33641
- ret = JS_EvalInternal(ctx, this_obj, input, input_len, filename,
33666
+ ret = JS_EvalInternal(ctx, this_obj, input, input_len, filename, line,
33642
33667
eval_flags, -1);
33643
33668
return ret;
33644
33669
}
33645
33670
33646
33671
JSValue JS_Eval(JSContext *ctx, const char *input, size_t input_len,
33647
33672
const char *filename, int eval_flags)
33648
33673
{
33649
- return JS_EvalThis(ctx, ctx->global_obj, input, input_len, filename,
33650
- eval_flags);
33674
+ JSEvalOptions options = {
33675
+ .version = JS_EVAL_OPTIONS_VERSION,
33676
+ .filename = filename,
33677
+ .line_num = 1,
33678
+ .eval_flags = eval_flags
33679
+ };
33680
+ return JS_EvalThis2(ctx, ctx->global_obj, input, input_len, &options);
33681
+ }
33682
+
33683
+ JSValue JS_Eval2(JSContext *ctx, const char *input, size_t input_len, JSEvalOptions *options)
33684
+ {
33685
+ return JS_EvalThis2(ctx, ctx->global_obj, input, input_len, options);
33651
33686
}
33652
33687
33653
33688
int JS_ResolveModule(JSContext *ctx, JSValue obj)
@@ -45293,7 +45328,7 @@ JSValue JS_ParseJSON(JSContext *ctx, const char *buf, size_t buf_len, const char
45293
45328
JSParseState s1, *s = &s1;
45294
45329
JSValue val = JS_UNDEFINED;
45295
45330
45296
- js_parse_init(ctx, s, buf, buf_len, filename);
45331
+ js_parse_init(ctx, s, buf, buf_len, filename, 1 );
45297
45332
if (json_next_token(s))
45298
45333
goto fail;
45299
45334
val = json_parse_value(s);
@@ -56236,7 +56271,7 @@ bool JS_DetectModule(const char *input, size_t input_len)
56236
56271
return false;
56237
56272
}
56238
56273
JS_AddIntrinsicRegExp(ctx); // otherwise regexp literals don't parse
56239
- val = __JS_EvalInternal(ctx, JS_UNDEFINED, input, input_len, "<unnamed>",
56274
+ val = __JS_EvalInternal(ctx, JS_UNDEFINED, input, input_len, "<unnamed>", 1,
56240
56275
JS_EVAL_TYPE_MODULE|JS_EVAL_FLAG_COMPILE_ONLY, -1);
56241
56276
if (JS_IsException(val)) {
56242
56277
const char *msg = JS_ToCString(ctx, rt->current_exception);
0 commit comments