Skip to content

Commit 78bdbef

Browse files
ABBAPOHsaghul
authored andcommitted
Add JS_Eval* overloads taking line
1 parent 92961d7 commit 78bdbef

File tree

2 files changed

+65
-16
lines changed

2 files changed

+65
-16
lines changed

quickjs.c

+50-15
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);
@@ -33431,12 +33431,12 @@ static __exception int js_parse_program(JSParseState *s)
3343133431

3343233432
static void js_parse_init(JSContext *ctx, JSParseState *s,
3343333433
const char *input, size_t input_len,
33434-
const char *filename)
33434+
const char *filename, int line)
3343533435
{
3343633436
memset(s, 0, sizeof(*s));
3343733437
s->ctx = ctx;
3343833438
s->filename = filename;
33439-
s->line_num = 1;
33439+
s->line_num = line;
3344033440
s->col_num = 1;
3344133441
s->buf_start = s->buf_ptr = (const uint8_t *)input;
3344233442
s->buf_end = s->buf_ptr + input_len;
@@ -33488,7 +33488,7 @@ JSValue JS_EvalFunction(JSContext *ctx, JSValue fun_obj)
3348833488
/* `export_name` and `input` may be pure ASCII or UTF-8 encoded */
3348933489
static JSValue __JS_EvalInternal(JSContext *ctx, JSValue this_obj,
3349033490
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)
3349233492
{
3349333493
JSParseState s1, *s = &s1;
3349433494
int err, eval_type;
@@ -33500,7 +33500,7 @@ static JSValue __JS_EvalInternal(JSContext *ctx, JSValue this_obj,
3350033500
JSModuleDef *m;
3350133501
bool is_strict_mode;
3350233502

33503-
js_parse_init(ctx, s, input, input_len, filename);
33503+
js_parse_init(ctx, s, input, input_len, filename, line);
3350433504
skip_shebang(&s->buf_ptr, s->buf_end);
3350533505

3350633506
eval_type = flags & JS_EVAL_TYPE_MASK;
@@ -33530,7 +33530,7 @@ static JSValue __JS_EvalInternal(JSContext *ctx, JSValue this_obj,
3353033530
is_strict_mode = true;
3353133531
}
3353233532
}
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);
3353433534
if (!fd)
3353533535
goto fail1;
3353633536
s->cur_func = fd;
@@ -33603,12 +33603,12 @@ static JSValue __JS_EvalInternal(JSContext *ctx, JSValue this_obj,
3360333603
/* the indirection is needed to make 'eval' optional */
3360433604
static JSValue JS_EvalInternal(JSContext *ctx, JSValue this_obj,
3360533605
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)
3360733607
{
3360833608
if (unlikely(!ctx->eval_internal)) {
3360933609
return JS_ThrowTypeError(ctx, "eval is not supported");
3361033610
}
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,
3361233612
flags, scope_idx);
3361333613
}
3361433614

@@ -33624,7 +33624,7 @@ static JSValue JS_EvalObject(JSContext *ctx, JSValue this_obj,
3362433624
str = JS_ToCStringLen(ctx, &len, val);
3362533625
if (!str)
3362633626
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);
3362833628
JS_FreeCString(ctx, str);
3362933629
return ret;
3363033630

@@ -33634,20 +33634,55 @@ JSValue JS_EvalThis(JSContext *ctx, JSValue this_obj,
3363433634
const char *input, size_t input_len,
3363533635
const char *filename, int eval_flags)
3363633636
{
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+
}
3363733662
JSValue ret;
3363833663

3363933664
assert((eval_flags & JS_EVAL_TYPE_MASK) == JS_EVAL_TYPE_GLOBAL ||
3364033665
(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,
3364233667
eval_flags, -1);
3364333668
return ret;
3364433669
}
3364533670

3364633671
JSValue JS_Eval(JSContext *ctx, const char *input, size_t input_len,
3364733672
const char *filename, int eval_flags)
3364833673
{
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);
3365133686
}
3365233687

3365333688
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
4529345328
JSParseState s1, *s = &s1;
4529445329
JSValue val = JS_UNDEFINED;
4529545330

45296-
js_parse_init(ctx, s, buf, buf_len, filename);
45331+
js_parse_init(ctx, s, buf, buf_len, filename, 1);
4529745332
if (json_next_token(s))
4529845333
goto fail;
4529945334
val = json_parse_value(s);
@@ -56236,7 +56271,7 @@ bool JS_DetectModule(const char *input, size_t input_len)
5623656271
return false;
5623756272
}
5623856273
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,
5624056275
JS_EVAL_TYPE_MODULE|JS_EVAL_FLAG_COMPILE_ONLY, -1);
5624156276
if (JS_IsException(val)) {
5624256277
const char *msg = JS_ToCString(ctx, rt->current_exception);

quickjs.h

+15-1
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_EVAL_OPTIONS_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. */
@@ -803,10 +813,14 @@ JS_EXTERN bool JS_DetectModule(const char *input, size_t input_len);
803813
/* 'input' must be zero terminated i.e. input[input_len] = '\0'. */
804814
JS_EXTERN JSValue JS_Eval(JSContext *ctx, const char *input, size_t input_len,
805815
const char *filename, int eval_flags);
806-
/* same as JS_Eval() but with an explicit 'this_obj' parameter */
816+
JS_EXTERN JSValue JS_Eval2(JSContext *ctx, const char *input, size_t input_len,
817+
JSEvalOptions *options);
807818
JS_EXTERN JSValue JS_EvalThis(JSContext *ctx, JSValue this_obj,
808819
const char *input, size_t input_len,
809820
const char *filename, int eval_flags);
821+
JS_EXTERN JSValue JS_EvalThis2(JSContext *ctx, JSValue this_obj,
822+
const char *input, size_t input_len,
823+
JSEvalOptions *options);
810824
JS_EXTERN JSValue JS_GetGlobalObject(JSContext *ctx);
811825
JS_EXTERN int JS_IsInstanceOf(JSContext *ctx, JSValue val, JSValue obj);
812826
JS_EXTERN int JS_DefineProperty(JSContext *ctx, JSValue this_obj,

0 commit comments

Comments
 (0)