-
Notifications
You must be signed in to change notification settings - Fork 676
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
Improve location info for syntax errors. #4816
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 |
---|---|---|
|
@@ -5645,6 +5645,64 @@ jerry_free_source_info (jerry_source_info_t *source_info_p) /**< source info blo | |
#endif /* JERRY_FUNCTION_TO_STRING */ | ||
} /* jerry_free_source_info */ | ||
|
||
/** | ||
* Gets the resource name and location info assigned to a SyntaxError object generated by the parser. | ||
* | ||
* @return resource name, if a location info is available | ||
* error, otherwise | ||
*/ | ||
jerry_value_t | ||
jerry_get_syntax_error_location (jerry_value_t value, /**< SyntaxError object */ | ||
jerry_syntax_error_location_t *error_location_p) /**< [out] location info */ | ||
{ | ||
jerry_assert_api_available (); | ||
|
||
#if JERRY_ERROR_MESSAGES | ||
if (ecma_is_value_error_reference (value)) | ||
{ | ||
value = ecma_get_extended_primitive_from_value (value)->u.value; | ||
} | ||
|
||
if (ecma_is_value_object (value)) | ||
{ | ||
ecma_object_t *object_p = ecma_get_object_from_value (value); | ||
|
||
ecma_string_t *name_p = ecma_get_internal_string (LIT_INTERNAL_MAGIC_STRING_SYNTAX_ERROR_LOCATION); | ||
ecma_property_t *property_p = ecma_find_named_property (object_p, name_p); | ||
|
||
if (property_p != NULL) | ||
{ | ||
ecma_value_t error_property_value = ECMA_PROPERTY_VALUE_PTR (property_p)->value; | ||
uint8_t *location_p = ECMA_GET_NON_NULL_POINTER_FROM_POINTER_TAG (uint8_t, error_property_value); | ||
ecma_value_t result = *(ecma_value_t *) location_p; | ||
|
||
if (error_location_p != NULL) | ||
{ | ||
size_t size_data = error_property_value & ECMA_SYNTAX_ERROR_ALLOCATION_SIZE_MASK; | ||
location_p += ((size_data + 1) << ECMA_SYNTAX_ERROR_ALLOCATION_UNIT_SHIFT); | ||
|
||
error_location_p->line = ecma_extended_info_decode_vlq (&location_p); | ||
error_location_p->column_start = ecma_extended_info_decode_vlq (&location_p); | ||
|
||
uint32_t difference = ecma_extended_info_decode_vlq (&location_p); | ||
|
||
error_location_p->column_end = error_location_p->column_start + difference; | ||
} | ||
|
||
ecma_ref_ecma_string (ecma_get_string_from_value (result)); | ||
return result; | ||
} | ||
} | ||
|
||
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("Location is not available"))); | ||
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. I'm wondering wether it would be better to simply return with undefined if there is no location, or with a pre-defined magic string. And similarly when error messages are disabled. It feels to me like the location info is an added bonus, it can be used when it's available, but if it's not, then that's not really a problem. Or at least not something that would warrant an error. |
||
#else /* !JERRY_ERROR_MESSAGES */ | ||
JERRY_UNUSED (value); | ||
JERRY_UNUSED (error_location_p); | ||
|
||
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("Location is not available"))); | ||
#endif /* JERRY_ERROR_MESSAGES */ | ||
} /* jerry_get_syntax_error_location */ | ||
|
||
/** | ||
* Replaces the currently active realm with another realm. | ||
* | ||
|
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.
How about not making this syntax error specific, and changing the name to be generic for errors. Functionally it already seems to be generic. Also in the future we could possibly include location info for other error types as well.
Same for the
jerry_syntax_error_location_t
type, I would just call itjerry_error_location_t
.