-
Notifications
You must be signed in to change notification settings - Fork 348
Add a column offset to JSEvalOptions #1656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -615,7 +615,8 @@ struct JSContext { | |
| /* if NULL, eval is not supported */ | ||
| JSValue (*eval_internal)(JSContext *ctx, JSValueConst this_obj, | ||
| const char *input, size_t input_len, | ||
| const char *filename, int line, int flags, int scope_idx); | ||
| const char *filename, int line, int col, int flags, | ||
| int scope_idx); | ||
| void *user_opaque; | ||
| }; | ||
|
|
||
|
|
@@ -1469,7 +1470,8 @@ static void js_async_function_resolve_mark(JSRuntime *rt, JSValueConst val, | |
| JS_MarkFunc *mark_func); | ||
| static JSValue JS_EvalInternal(JSContext *ctx, JSValueConst this_obj, | ||
| const char *input, size_t input_len, | ||
| const char *filename, int line, int flags, int scope_idx); | ||
| const char *filename, int line, int col, | ||
| int flags, int scope_idx); | ||
| static void js_free_module_def(JSContext *ctx, JSModuleDef *m); | ||
| static int js_module_attributes_equal(JSContext *ctx, JSValueConst attr1, | ||
| JSValueConst attr2); | ||
|
|
@@ -38088,21 +38090,31 @@ static __exception int js_parse_program(JSParseState *s) | |
|
|
||
| static void js_parse_init(JSContext *ctx, JSParseState *s, | ||
| const char *input, size_t input_len, | ||
| const char *filename, int line) | ||
| const char *filename, int line, int col) | ||
| { | ||
| int col_off; | ||
|
|
||
| memset(s, 0, sizeof(*s)); | ||
| s->ctx = ctx; | ||
| s->filename = filename; | ||
| s->line_num = line; | ||
| /* number the first line from `col`, but only while every column of the | ||
| source still fits an int; anything else is numbered from 1 */ | ||
|
Comment on lines
+38101
to
+38102
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please drop the "add 1 to i" comment. |
||
| s->col_num = 1; | ||
| if (col > 0 && input_len < (size_t)(INT32_MAX - col)) | ||
| s->col_num = col; | ||
|
Comment on lines
+38104
to
+38105
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s->col_num = max_int(1, col);I don't like how the current code conflates the offset (human visible) with the position in the buffer (machine visible), please do something about that. (In a previous age I would've made concrete suggestions but since it's going into a slop bot that feels like wasted effort.) |
||
| s->buf_start = s->buf_ptr = (const uint8_t *)input; | ||
| s->buf_end = s->buf_ptr + input_len; | ||
| s->line_start = s->buf_ptr; | ||
| /* the first line starts at column `col` of the enclosing document, so | ||
| back the two column origins up by that much; both are reset at the | ||
| first line terminator, so only the first line is affected */ | ||
| col_off = s->col_num - 1; | ||
| s->line_start = s->buf_ptr - col_off; | ||
| s->mark = s->buf_ptr + min_int(1, input_len); | ||
| s->eol = s->buf_ptr; | ||
| s->eol = s->buf_ptr - col_off; | ||
| s->token.val = ' '; | ||
| s->token.line_num = 1; | ||
| s->token.col_num = 1; | ||
| s->token.line_num = line; | ||
| s->token.col_num = s->col_num; | ||
| } | ||
|
|
||
| static JSValue JS_EvalFunctionInternal(JSContext *ctx, JSValue fun_obj, | ||
|
|
@@ -38148,7 +38160,8 @@ JSValue JS_EvalFunction(JSContext *ctx, JSValue fun_obj) | |
| /* `export_name` and `input` may be pure ASCII or UTF-8 encoded */ | ||
| static JSValue __JS_EvalInternal(JSContext *ctx, JSValueConst this_obj, | ||
| const char *input, size_t input_len, | ||
| const char *filename, int line, int flags, int scope_idx) | ||
| const char *filename, int line, int col, | ||
| int flags, int scope_idx) | ||
| { | ||
| JSParseState s1, *s = &s1; | ||
| int err, eval_type; | ||
|
|
@@ -38160,7 +38173,7 @@ static JSValue __JS_EvalInternal(JSContext *ctx, JSValueConst this_obj, | |
| JSModuleDef *m; | ||
| bool is_strict_mode; | ||
|
|
||
| js_parse_init(ctx, s, input, input_len, filename, line); | ||
| js_parse_init(ctx, s, input, input_len, filename, line, col); | ||
| skip_shebang(&s->buf_ptr, s->buf_end); | ||
|
|
||
| eval_type = flags & JS_EVAL_TYPE_MASK; | ||
|
|
@@ -38265,7 +38278,8 @@ static JSValue __JS_EvalInternal(JSContext *ctx, JSValueConst this_obj, | |
| /* the indirection is needed to make 'eval' optional */ | ||
| static JSValue JS_EvalInternal(JSContext *ctx, JSValueConst this_obj, | ||
| const char *input, size_t input_len, | ||
| const char *filename, int line, int flags, int scope_idx) | ||
| const char *filename, int line, int col, | ||
| int flags, int scope_idx) | ||
| { | ||
| JSRuntime *rt = ctx->rt; | ||
|
|
||
|
|
@@ -38277,7 +38291,7 @@ static JSValue JS_EvalInternal(JSContext *ctx, JSValueConst this_obj, | |
| ctx->error_back_trace = JS_UNDEFINED; | ||
| } | ||
| return ctx->eval_internal(ctx, this_obj, input, input_len, filename, line, | ||
| flags, scope_idx); | ||
| col, flags, scope_idx); | ||
| } | ||
|
|
||
| static JSValue JS_EvalObject(JSContext *ctx, JSValueConst this_obj, | ||
|
|
@@ -38292,7 +38306,8 @@ static JSValue JS_EvalObject(JSContext *ctx, JSValueConst this_obj, | |
| str = JS_ToCStringLen(ctx, &len, val); | ||
| if (!str) | ||
| return JS_EXCEPTION; | ||
| ret = JS_EvalInternal(ctx, this_obj, str, len, "<input>", 1, flags, scope_idx); | ||
| ret = JS_EvalInternal(ctx, this_obj, str, len, "<input>", 1, 1, flags, | ||
| scope_idx); | ||
| JS_FreeCString(ctx, str); | ||
| return ret; | ||
|
|
||
|
|
@@ -38317,22 +38332,25 @@ JSValue JS_EvalThis2(JSContext *ctx, JSValueConst this_obj, | |
| { | ||
| const char *filename = "<unnamed>"; | ||
| int line = 1; | ||
| int col = 1; | ||
| int eval_flags = 0; | ||
| if (options) { | ||
| if (options->version != JS_EVAL_OPTIONS_VERSION) | ||
| if (options->version < 1 || options->version > JS_EVAL_OPTIONS_VERSION) | ||
| return JS_ThrowInternalError(ctx, "bad JSEvalOptions version"); | ||
| if (options->filename) | ||
| filename = options->filename; | ||
| if (options->line_num != 0) | ||
| line = options->line_num; | ||
| if (options->version >= 2 && options->col_num != 0) | ||
| col = options->col_num; | ||
| eval_flags = options->eval_flags; | ||
| } | ||
| JSValue ret; | ||
|
|
||
| assert((eval_flags & JS_EVAL_TYPE_MASK) == JS_EVAL_TYPE_GLOBAL || | ||
| (eval_flags & JS_EVAL_TYPE_MASK) == JS_EVAL_TYPE_MODULE); | ||
| ret = JS_EvalInternal(ctx, this_obj, input, input_len, filename, line, | ||
| eval_flags, -1); | ||
| col, eval_flags, -1); | ||
| return ret; | ||
| } | ||
|
|
||
|
|
@@ -51178,7 +51196,7 @@ static JSValue JS_ParseJSON_internal(JSContext *ctx, const char *buf, size_t buf | |
| JSParseState s1, *s = &s1; | ||
| JSValue val = JS_UNDEFINED; | ||
|
|
||
| js_parse_init(ctx, s, buf, buf_len, filename, 1); | ||
| js_parse_init(ctx, s, buf, buf_len, filename, 1, 1); | ||
| if (json_next_token(s)) | ||
| goto fail; | ||
| val = json_parse_value(s, pr); | ||
|
|
@@ -64776,7 +64794,8 @@ bool JS_DetectModule(const char *input, size_t input_len) | |
| return false; | ||
| } | ||
| JS_AddIntrinsicRegExpCompiler(ctx); // otherwise regexp literals don't parse | ||
| val = __JS_EvalInternal(ctx, JS_UNDEFINED, input, input_len, "<unnamed>", 1, | ||
| val = __JS_EvalInternal(ctx, JS_UNDEFINED, input, input_len, "<unnamed>", | ||
| 1, 1, | ||
| JS_EVAL_TYPE_MODULE|JS_EVAL_FLAG_COMPILE_ONLY, -1); | ||
| if (JS_IsException(val)) { | ||
| const char *msg = JS_ToCString(ctx, rt->current_exception); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For all that is holy, please make this a function.