Rework the public API (#4829)

Related to #4186.

Some notable changes:
  - The term 'Error' now strictly refers to native Error objects defined in
    the ECMA standard, which are ordinary objects. All other uses of
    'error' or 'error reference' where the term refers to a thrown value is
    now called 'exception'.

  - Simplified the naming scheme of many String API functions. These functions
    will now also take an 'encoding' argument to specify the desired
    encoding in which to operate.

  - Removed the substring-copy-to-buffer functions. These functions
    behaved awkwardly, as they use character index to specify the
    start/end positions, and were mostly used incorrectly with byte
    offsets instead. The functionality can still be replicated with
    other functions if necessary.

  - String-to-buffer functions will no longer fail if the buffer is not
    sufficiently large, the string will instead be cropped.

  - Fixed the usage of the '_sz' prefix in many API functions. The term
    'sz' means zero-terminated string in hungarian notation, this was
    used incorrectly in many cases.

  - Renamed most of the public API functions to have shorter, more on-point
    names, rather than the often too long descriptive names. Functions are now
    also grouped by the type of value they operate on, where this makes
    sense.

JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
This commit is contained in:
Dániel Bátyai
2021-12-06 10:20:09 +01:00
committed by GitHub
parent 81d2319144
commit 9860d66a56
180 changed files with 10738 additions and 11025 deletions
+10 -3
View File
@@ -31,6 +31,7 @@ ECMA_ERROR_DEF (ECMA_ERR_OBJECT_EXPECTED, "Object expected")
#if JERRY_BUILTIN_ANNEXB && JERRY_BUILTIN_REGEXP
ECMA_ERROR_DEF (ECMA_ERR_INVALID_ARGUMENT, "Invalid argument")
#endif /* JERRY_BUILTIN_ANNEXB && JERRY_BUILTIN_REGEXP */
ECMA_ERROR_DEF (ECMA_ERR_INVALID_ENCODING, "Invalid encoding")
#if JERRY_BUILTIN_REGEXP
ECMA_ERROR_DEF (ECMA_ERR_NOTHING_TO_REPEAT, "Nothing to repeat")
#endif /* JERRY_BUILTIN_REGEXP */
@@ -189,6 +190,9 @@ ECMA_ERROR_DEF (ECMA_ERR_THE_STRUCTURE_IS_CYCLICAL, "The structure is cyclical")
#if JERRY_BUILTIN_REGEXP
ECMA_ERROR_DEF (ECMA_ERR_UNEXPECTED_END_OF_PATTERN, "Unexpected end of pattern")
#endif /* JERRY_BUILTIN_REGEXP */
#if JERRY_SNAPSHOT_SAVE
ECMA_ERROR_DEF (ECMA_ERR_SNAPSHOT_UNSUPPORTED_COMPILED_CODE, "Unsupported compiled code")
#endif /* JERRY_SNAPSHOT_SAVE */
#if !(JERRY_BUILTIN_BIGINT)
ECMA_ERROR_DEF (ECMA_ERR_BIGINT_NOT_SUPPORTED, "BigInt support is disabled")
#endif /* !(JERRY_BUILTIN_BIGINT) */
@@ -305,6 +309,9 @@ ECMA_ERROR_DEF (ECMA_ERR_PRIVATE_METHOD_IS_NOT_WRITABLE, "Private method is not
ECMA_ERROR_DEF (ECMA_ERR_RADIX_IS_OUT_OF_RANGE, "Radix must be between 2 and 36")
#endif /* JERRY_BUILTIN_BIGINT \
|| JERRY_BUILTIN_NUMBER */
#if !(JERRY_SNAPSHOT_EXEC)
ECMA_ERROR_DEF (ECMA_ERR_SNAPSHOT_EXEC_DISABLED, "Snapshot execution is disabled")
#endif /* !(JERRY_SNAPSHOT_EXEC) */
#if !(JERRY_BUILTIN_TYPEDARRAY)
ECMA_ERROR_DEF (ECMA_ERR_TYPED_ARRAY_NOT_SUPPORTED, "TypedArray support is disabled")
#endif /* !(JERRY_BUILTIN_TYPEDARRAY) */
@@ -318,6 +325,9 @@ ECMA_ERROR_DEF (ECMA_ERR_ITERATOR_NEXT_IS_NOT_CALLABLE, "Iterator 'next' is not
ECMA_ERROR_DEF (ECMA_ERR_ITERATOR_VALUE_IS_NOT_AN_OBJECT, "Iterator value is not an object")
ECMA_ERROR_DEF (ECMA_ERR_RESOLVE_METHOD_MUST_BE_CALLABLE, "Resolve method must be callable")
#endif /* JERRY_ESNEXT */
#if !(JERRY_SNAPSHOT_SAVE)
ECMA_ERROR_DEF (ECMA_ERR_SNAPSHOT_SAVE_DISABLED, "Snapshot generation is disabled")
#endif /* !(JERRY_SNAPSHOT_SAVE) */
#if !(JERRY_PARSER)
ECMA_ERROR_DEF (ECMA_ERR_PARSER_NOT_SUPPORTED, "Source code parsing is disabled")
#endif /* !(JERRY_PARSER) */
@@ -384,9 +394,6 @@ ECMA_ERROR_DEF (ECMA_ERR_CALLBACK_IS_NOT_CALLABLE, "Callback function is not cal
#if JERRY_BUILTIN_TYPEDARRAY
ECMA_ERROR_DEF (ECMA_ERR_INITIAL_VALUE_CANNOT_BE_UNDEFINED, "Initial value cannot be undefined")
#endif /* JERRY_BUILTIN_TYPEDARRAY */
#if JERRY_BUILTIN_REGEXP
ECMA_ERROR_DEF (ECMA_ERR_INPUT_MUST_BE_A_VALID_UTF8_STRING, "Input must be a valid utf8 string")
#endif /* JERRY_BUILTIN_REGEXP */
#if JERRY_BUILTIN_SHAREDARRAYBUFFER
ECMA_ERROR_DEF (ECMA_ERR_INVALID_SHARED_ARRAYBUFFER_LENGTH, "Invalid Shared ArrayBuffer length")
#endif /* JERRY_BUILTIN_SHAREDARRAYBUFFER */
+4 -1
View File
@@ -120,7 +120,6 @@ ECMA_ERR_INCORRECT_RETURN_PROXY_GET_TRAP = "Incorrect value is returned by a Pro
ECMA_ERR_INCORRECT_RETURN_PROXY_SET_TRAP = "Incorrect value is returned by a Proxy 'set' trap"
ECMA_ERR_INFINITY_OR_NAN_CANNOT_BE_CONVERTED_TO_BIGINT = "Infinity or NaN cannot be converted to BigInt"
ECMA_ERR_INITIAL_VALUE_CANNOT_BE_UNDEFINED = "Initial value cannot be undefined"
ECMA_ERR_INPUT_MUST_BE_A_VALID_UTF8_STRING = "Input must be a valid utf8 string"
ECMA_ERR_INVALID_ARRAYBUFFER_LENGTH = "Invalid ArrayBuffer length"
ECMA_ERR_INVALID_JSON_FORMAT = "Invalid JSON format"
ECMA_ERR_INVALID_REGEXP_FLAGS = "Invalid RegExp flags"
@@ -129,6 +128,7 @@ ECMA_ERR_INVALID_TYPEDARRAY_LENGTH = "Invalid TypedArray length"
ECMA_ERR_INVALID_UTF8_CHARACTER = "Invalid UTF8 character"
ECMA_ERR_INVALID_UTF8_CODEPOINT = "Invalid UTF8 codepoint"
ECMA_ERR_INVALID_UTF8_STRING = "Invalid UTF8 string"
ECMA_ERR_INVALID_ENCODING = "Invalid encoding"
ECMA_ERR_INVALID_ARGUMENT = "Invalid argument"
ECMA_ERR_INVALID_ARGUMENT_IS_PASSED_TO_REGEXP_FUNCTION = "Invalid argument is passed to RegExp function"
ECMA_ERR_INVALID_ARGUMENT_TYPE_IN_TOPRIMITIVE = "Invalid argument type in toPrimitive"
@@ -305,7 +305,10 @@ ECMA_ERR_LET_CONST_NOT_INITIALIZED = "Variables declared by let/const must be in
ECMA_ERR_MAXIMUM_SNAPSHOT_SIZE = "Maximum snapshot size reached"
ECMA_ERR_REGULAR_EXPRESSION_NOT_SUPPORTED = "Regular expression literals are not supported"
ECMA_ERR_SNAPSHOT_BUFFER_SMALL = "Snapshot buffer too small"
ECMA_ERR_SNAPSHOT_UNSUPPORTED_COMPILED_CODE = "Unsupported compiled code"
ECMA_ERR_SNAPSHOT_FLAG_NOT_SUPPORTED = "Unsupported generate snapshot flags specified"
ECMA_ERR_SNAPSHOT_SAVE_DISABLED = "Snapshot generation is disabled"
ECMA_ERR_SNAPSHOT_EXEC_DISABLED = "Snapshot execution is disabled"
ECMA_ERR_CANNOT_ALLOCATE_MEMORY_LITERALS = "Cannot allocate memory for literals"
ECMA_ERR_TAGGED_TEMPLATE_LITERALS = "Unsupported feature: tagged template literals"
ECMA_ERR_CONTAINER_NEEDED = "Value is not a Container or Iterator"
+7 -7
View File
@@ -21,15 +21,15 @@
*/
typedef struct
{
lit_utf8_byte_t *text; /* Text of ecma error message. */
char *text; /* Text of ecma error message. */
uint8_t size; /* Size of ecma error message. */
} ecma_error_message_t;
/* Error message texts with size. */
static ecma_error_message_t ecma_error_messages[] JERRY_ATTR_CONST_DATA = {
{ (lit_utf8_byte_t *) "", 0 }, /* ECMA_ERR_EMPTY */
{ "", 0 }, /* ECMA_ERR_EMPTY */
/** @cond doxygen_suppress */
#define ECMA_ERROR_DEF(id, utf8_string) { (lit_utf8_byte_t *) utf8_string, sizeof (utf8_string) - 1 },
#define ECMA_ERROR_DEF(id, string) { string, sizeof (string) - 1 },
#include "ecma-error-messages.inc.h"
#undef ECMA_ERROR_DEF
/** @endcond */
@@ -41,8 +41,8 @@ static ecma_error_message_t ecma_error_messages[] JERRY_ATTR_CONST_DATA = {
*
* @return pointer to zero-terminated ecma error
*/
const lit_utf8_byte_t *
ecma_get_error_utf8 (uint32_t id) /**< ecma error id */
const char *
ecma_get_error_msg (ecma_error_msg_t id) /**< ecma error id */
{
JERRY_ASSERT (id != ECMA_IS_VALID_CONSTRUCTOR);
@@ -51,7 +51,7 @@ ecma_get_error_utf8 (uint32_t id) /**< ecma error id */
#else /* !JERRY_ERROR_MESSAGES */
return NULL;
#endif /* JERRY_ERROR_MESSAGES */
} /* ecma_get_error_utf8 */
} /* ecma_get_error_msg */
/**
* Get size of specified ecma error
@@ -59,7 +59,7 @@ ecma_get_error_utf8 (uint32_t id) /**< ecma error id */
* @return size in bytes
*/
lit_utf8_size_t
ecma_get_error_size (uint32_t id) /**< ecma error id */
ecma_get_error_size (ecma_error_msg_t id) /**< ecma error id */
{
JERRY_ASSERT (id != ECMA_IS_VALID_CONSTRUCTOR);
+2 -2
View File
@@ -33,7 +33,7 @@ typedef enum
ECMA_IS_VALID_CONSTRUCTOR /* used as return value when checking constructor */
} ecma_error_msg_t;
const lit_utf8_byte_t* ecma_get_error_utf8 (uint32_t id);
lit_utf8_size_t ecma_get_error_size (uint32_t id);
const char* ecma_get_error_msg (ecma_error_msg_t id);
lit_utf8_size_t ecma_get_error_size (ecma_error_msg_t id);
#endif /* !ECMA_ERRORS_H */
+2 -2
View File
@@ -1300,7 +1300,7 @@ ecma_gc_free_native_pointer (ecma_property_t property, /**< property descriptor
if (native_pointer_p->native_info_p != NULL)
{
jerry_object_native_free_callback_t free_cb = native_pointer_p->native_info_p->free_cb;
jerry_object_native_free_cb_t free_cb = native_pointer_p->native_info_p->free_cb;
if (free_cb != NULL)
{
@@ -1324,7 +1324,7 @@ ecma_gc_free_native_pointer (ecma_property_t property, /**< property descriptor
{
if (item_p->data.native_info_p != NULL)
{
jerry_object_native_free_callback_t free_cb = item_p->data.native_info_p->free_cb;
jerry_object_native_free_cb_t free_cb = item_p->data.native_info_p->free_cb;
if (free_cb != NULL)
{
+8 -8
View File
@@ -56,7 +56,7 @@
*/
typedef enum
{
ECMA_STATUS_API_AVAILABLE = (1u << 0), /**< api available */
ECMA_STATUS_API_ENABLED = (1u << 0), /**< api available */
ECMA_STATUS_DIRECT_EVAL = (1u << 1), /**< eval is called directly */
#if JERRY_PROPERTY_HASHMAP
ECMA_STATUS_HIGH_PRESSURE_GC = (1u << 2), /**< last gc was under high pressure */
@@ -1763,14 +1763,14 @@ typedef struct
#endif /* !defined (JERRY_BUILTIN_BIGINT) */
/**
* Abort flag for errors in C API.
* Flags for exception values.
*/
#define ECMA_ERROR_API_ABORT (1u << 0)
/**
* Throw captured flag for errors in C API.
*/
#define ECMA_ERROR_API_THROW_CAPTURED (1u << 1)
typedef enum
{
ECMA_ERROR_API_FLAG_NONE = 0,
ECMA_ERROR_API_FLAG_ABORT = (1u << 0), /**< abort flag */
ECMA_ERROR_API_FLAG_THROW_CAPTURED = (1u << 1), /**< throw captured flag */
} ecma_error_api_flags_t;
/**
* Representation of a thrown value on API level.
+69 -299
View File
@@ -488,7 +488,7 @@ ecma_new_ecma_external_string_from_cesu8 (const lit_utf8_byte_t *string_p, /**<
/* Normal strings are created for short strings. */
ecma_string_t *string_desc_p = ecma_new_ecma_string_from_utf8 (string_p, string_size);
jerry_external_string_free_callback_t free_cb = JERRY_CONTEXT (external_string_free_callback_p);
jerry_external_string_free_cb_t free_cb = JERRY_CONTEXT (external_string_free_callback_p);
if (free_cb != NULL)
{
@@ -501,7 +501,7 @@ ecma_new_ecma_external_string_from_cesu8 (const lit_utf8_byte_t *string_p, /**<
if (string_desc_p != NULL)
{
jerry_external_string_free_callback_t free_cb = JERRY_CONTEXT (external_string_free_callback_p);
jerry_external_string_free_cb_t free_cb = JERRY_CONTEXT (external_string_free_callback_p);
if (free_cb != NULL)
{
@@ -974,7 +974,7 @@ ecma_destroy_ecma_string (ecma_string_t *string_p) /**< ecma-string */
}
ecma_external_string_t *external_string_p = (ecma_external_string_t *) string_p;
jerry_external_string_free_callback_t free_cb = JERRY_CONTEXT (external_string_free_callback_p);
jerry_external_string_free_cb_t free_cb = JERRY_CONTEXT (external_string_free_callback_p);
if (free_cb != NULL)
{
@@ -1078,31 +1078,44 @@ ecma_string_get_array_index (const ecma_string_t *str_p) /**< ecma-string */
} /* ecma_string_get_array_index */
/**
* Convert ecma-string's contents to a cesu-8 string and put it to the buffer.
* It is the caller's responsibility to make sure that the string fits in the buffer.
* Copy digits of uint32 number, truncating if buffer is not large enough.
*
* @return number of bytes, actually copied to the buffer.
* @return number of digits copied
*/
static lit_utf8_size_t
ecma_uint32_to_buffer (uint32_t num, /**< number */
lit_utf8_byte_t *buffer_p /**< destination buffer */,
lit_utf8_size_t buffer_size /**< buffer size */)
{
lit_utf8_byte_t digits[ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32];
lit_utf8_size_t digit_count = ecma_uint32_to_utf8_string (num, digits, ECMA_MAX_CHARS_IN_STRINGIFIED_UINT32);
digit_count = JERRY_MIN (buffer_size, digit_count);
memcpy (buffer_p, digits, digit_count);
return digit_count;
} /* ecma_uint32_to_buffer */
/**
* Convert ecma-string's contents to the specified encoding and copy it to the buffer.
* String data will be truncated to fit the buffer.
*
* @return number of bytes copied to the buffer.
*/
lit_utf8_size_t JERRY_ATTR_WARN_UNUSED_RESULT
ecma_string_copy_to_cesu8_buffer (const ecma_string_t *string_p, /**< ecma-string descriptor */
lit_utf8_byte_t *buffer_p, /**< destination buffer pointer
* (can be NULL if buffer_size == 0) */
lit_utf8_size_t buffer_size) /**< size of buffer */
ecma_string_copy_to_buffer (const ecma_string_t *string_p, /**< ecma-string descriptor */
lit_utf8_byte_t *buffer_p, /**< destination buffer pointer
* (can be NULL if buffer_size == 0) */
lit_utf8_size_t buffer_size, /**< size of buffer */
jerry_encoding_t encoding) /**< encoding */
{
JERRY_ASSERT (string_p != NULL);
JERRY_ASSERT (buffer_p != NULL || buffer_size == 0);
JERRY_ASSERT (ecma_string_get_size (string_p) <= buffer_size);
lit_utf8_size_t size;
if (ECMA_IS_DIRECT_STRING (string_p))
{
if (ECMA_IS_DIRECT_STRING_WITH_TYPE (string_p, ECMA_DIRECT_STRING_UINT))
{
uint32_t uint32_number = (uint32_t) ECMA_GET_DIRECT_STRING_VALUE (string_p);
size = ecma_uint32_to_utf8_string (uint32_number, buffer_p, buffer_size);
JERRY_ASSERT (size <= buffer_size);
return size;
return ecma_uint32_to_buffer ((uint32_t) ECMA_GET_DIRECT_STRING_VALUE (string_p), buffer_p, buffer_size);
}
}
else
@@ -1111,295 +1124,55 @@ ecma_string_copy_to_cesu8_buffer (const ecma_string_t *string_p, /**< ecma-strin
if (ECMA_STRING_GET_CONTAINER (string_p) == ECMA_STRING_CONTAINER_UINT32_IN_DESC)
{
uint32_t uint32_number = string_p->u.uint32_number;
size = ecma_uint32_to_utf8_string (uint32_number, buffer_p, buffer_size);
JERRY_ASSERT (size <= buffer_size);
return size;
return ecma_uint32_to_buffer (string_p->u.uint32_number, buffer_p, buffer_size);
}
}
const lit_utf8_byte_t *chars_p = ecma_string_get_chars_fast (string_p, &size);
JERRY_ASSERT (chars_p != NULL);
JERRY_ASSERT (size <= buffer_size);
memcpy (buffer_p, chars_p, size);
return size;
} /* ecma_string_copy_to_cesu8_buffer */
/**
* Convert ecma-string's contents to an utf-8 string and put it to the buffer.
* It is the caller's responsibility to make sure that the string fits in the buffer.
*
* @return number of bytes, actually copied to the buffer.
*/
lit_utf8_size_t JERRY_ATTR_WARN_UNUSED_RESULT
ecma_string_copy_to_utf8_buffer (const ecma_string_t *string_p, /**< ecma-string descriptor */
lit_utf8_byte_t *buffer_p, /**< destination buffer pointer
* (can be NULL if buffer_size == 0) */
lit_utf8_size_t buffer_size) /**< size of buffer */
{
JERRY_ASSERT (string_p != NULL);
JERRY_ASSERT (buffer_p != NULL || buffer_size == 0);
JERRY_ASSERT (ecma_string_get_utf8_size (string_p) <= buffer_size);
lit_utf8_size_t size;
if (ECMA_IS_DIRECT_STRING (string_p))
{
if (ECMA_IS_DIRECT_STRING_WITH_TYPE (string_p, ECMA_DIRECT_STRING_UINT))
{
uint32_t uint32_number = (uint32_t) ECMA_GET_DIRECT_STRING_VALUE (string_p);
size = ecma_uint32_to_utf8_string (uint32_number, buffer_p, buffer_size);
JERRY_ASSERT (size <= buffer_size);
return size;
}
}
else
{
JERRY_ASSERT (string_p->refs_and_container >= ECMA_STRING_REF_ONE);
if (ECMA_STRING_GET_CONTAINER (string_p) == ECMA_STRING_CONTAINER_UINT32_IN_DESC)
{
uint32_t uint32_number = string_p->u.uint32_number;
size = ecma_uint32_to_utf8_string (uint32_number, buffer_p, buffer_size);
JERRY_ASSERT (size <= buffer_size);
return size;
}
}
uint8_t flags = ECMA_STRING_FLAG_IS_ASCII;
const lit_utf8_byte_t *chars_p = ecma_string_get_chars (string_p, &size, NULL, NULL, &flags);
lit_utf8_size_t string_size;
const lit_utf8_byte_t *chars_p = ecma_string_get_chars_fast (string_p, &string_size);
lit_utf8_size_t copy_size = 0;
JERRY_ASSERT (chars_p != NULL);
if (flags & ECMA_STRING_FLAG_IS_ASCII)
switch (encoding)
{
JERRY_ASSERT (size <= buffer_size);
memcpy (buffer_p, chars_p, size);
return size;
}
size = lit_convert_cesu8_string_to_utf8_string (chars_p, size, buffer_p, buffer_size);
if (flags & ECMA_STRING_FLAG_MUST_BE_FREED)
{
jmem_heap_free_block ((void *) chars_p, size);
}
JERRY_ASSERT (size <= buffer_size);
return size;
} /* ecma_string_copy_to_utf8_buffer */
/**
* Convert ecma-string's contents to a cesu-8 string, extract the parts of the converted string between the specified
* start position and the end position (or the end of the string, whichever comes first), and copy these characters
* into the buffer.
*
* @return number of bytes, actually copied to the buffer.
*/
lit_utf8_size_t
ecma_substring_copy_to_cesu8_buffer (const ecma_string_t *string_desc_p, /**< ecma-string descriptor */
lit_utf8_size_t start_pos, /**< position of the first character */
lit_utf8_size_t end_pos, /**< position of the last character */
lit_utf8_byte_t *buffer_p, /**< destination buffer pointer
* (can be NULL if buffer_size == 0) */
lit_utf8_size_t buffer_size) /**< size of buffer */
{
JERRY_ASSERT (string_desc_p != NULL);
JERRY_ASSERT (buffer_p != NULL || buffer_size == 0);
lit_utf8_size_t string_length = ecma_string_get_length (string_desc_p);
lit_utf8_size_t size = 0;
if (start_pos >= string_length || start_pos >= end_pos)
{
return 0;
}
if (end_pos > string_length)
{
end_pos = string_length;
}
ECMA_STRING_TO_UTF8_STRING (string_desc_p, utf8_str_p, utf8_str_size);
const lit_utf8_byte_t *start_p = utf8_str_p;
if (string_length == utf8_str_size)
{
start_p += start_pos;
size = end_pos - start_pos;
if (size > buffer_size)
case JERRY_ENCODING_CESU8:
{
size = buffer_size;
}
copy_size = JERRY_MIN (string_size, buffer_size);
memcpy (buffer_p, start_p, size);
}
else
{
end_pos -= start_pos;
while (start_pos--)
{
start_p += lit_get_unicode_char_size_by_utf8_first_byte (*start_p);
}
const lit_utf8_byte_t *end_p = start_p;
while (end_pos--)
{
lit_utf8_size_t code_unit_size = lit_get_unicode_char_size_by_utf8_first_byte (*end_p);
if ((size + code_unit_size) > buffer_size)
if (copy_size < string_size)
{
/* Do not copy partial characters */
while ((chars_p[copy_size] & LIT_UTF8_EXTRA_BYTE_MASK) == LIT_UTF8_EXTRA_BYTE_MARKER)
{
copy_size--;
}
}
memcpy (buffer_p, chars_p, copy_size);
break;
}
case JERRY_ENCODING_UTF8:
{
if (string_size == ecma_string_get_length (string_p))
{
copy_size = JERRY_MIN (string_size, buffer_size);
memcpy (buffer_p, chars_p, copy_size);
break;
}
end_p += code_unit_size;
size += code_unit_size;
copy_size = lit_convert_cesu8_string_to_utf8_string (chars_p, string_size, buffer_p, buffer_size);
break;
}
memcpy (buffer_p, start_p, size);
}
ECMA_FINALIZE_UTF8_STRING (utf8_str_p, utf8_str_size);
JERRY_ASSERT (size <= buffer_size);
return size;
} /* ecma_substring_copy_to_cesu8_buffer */
/**
* Convert ecma-string's contents to an utf-8 string, extract the parts of the converted string between the specified
* start position and the end position (or the end of the string, whichever comes first), and copy these characters
* into the buffer.
*
* @return number of bytes, actually copied to the buffer.
*/
lit_utf8_size_t
ecma_substring_copy_to_utf8_buffer (const ecma_string_t *string_desc_p, /**< ecma-string descriptor */
lit_utf8_size_t start_pos, /**< position of the first character */
lit_utf8_size_t end_pos, /**< position of the last character */
lit_utf8_byte_t *buffer_p, /**< destination buffer pointer
* (can be NULL if buffer_size == 0) */
lit_utf8_size_t buffer_size) /**< size of buffer */
{
JERRY_ASSERT (string_desc_p != NULL);
JERRY_ASSERT (ECMA_IS_DIRECT_STRING (string_desc_p) || string_desc_p->refs_and_container >= ECMA_STRING_REF_ONE);
JERRY_ASSERT (buffer_p != NULL || buffer_size == 0);
lit_utf8_size_t size = 0;
lit_utf8_size_t utf8_str_length = ecma_string_get_utf8_length (string_desc_p);
if (start_pos >= utf8_str_length || start_pos >= end_pos)
{
return 0;
}
if (end_pos > utf8_str_length)
{
end_pos = utf8_str_length;
}
ECMA_STRING_TO_UTF8_STRING (string_desc_p, cesu8_str_p, cesu8_str_size);
lit_utf8_size_t cesu8_str_length = ecma_string_get_length (string_desc_p);
if (cesu8_str_length == cesu8_str_size)
{
cesu8_str_p += start_pos;
size = end_pos - start_pos;
if (size > buffer_size)
default:
{
size = buffer_size;
break;
}
memcpy (buffer_p, cesu8_str_p, size);
}
else
{
const lit_utf8_byte_t *cesu8_end_pos = cesu8_str_p + cesu8_str_size;
end_pos -= start_pos;
while (start_pos--)
{
ecma_char_t ch;
lit_utf8_size_t code_unit_size = lit_read_code_unit_from_cesu8 (cesu8_str_p, &ch);
cesu8_str_p += code_unit_size;
if ((cesu8_str_p != cesu8_end_pos) && lit_is_code_point_utf16_high_surrogate (ch))
{
ecma_char_t next_ch;
lit_utf8_size_t next_ch_size = lit_read_code_unit_from_cesu8 (cesu8_str_p, &next_ch);
if (lit_is_code_point_utf16_low_surrogate (next_ch))
{
JERRY_ASSERT (code_unit_size == next_ch_size);
cesu8_str_p += code_unit_size;
}
}
}
const lit_utf8_byte_t *cesu8_pos = cesu8_str_p;
lit_utf8_byte_t *utf8_pos = buffer_p;
lit_utf8_byte_t *utf8_end_pos = buffer_p + buffer_size;
while (end_pos--)
{
ecma_char_t ch;
lit_utf8_size_t code_unit_size = lit_read_code_unit_from_cesu8 (cesu8_pos, &ch);
if ((size + code_unit_size) > buffer_size)
{
break;
}
if (((cesu8_pos + code_unit_size) != cesu8_end_pos) && lit_is_code_point_utf16_high_surrogate (ch))
{
ecma_char_t next_ch;
lit_utf8_size_t next_ch_size = lit_read_code_unit_from_cesu8 (cesu8_pos + code_unit_size, &next_ch);
if (lit_is_code_point_utf16_low_surrogate (next_ch))
{
JERRY_ASSERT (code_unit_size == next_ch_size);
if ((size + code_unit_size + 1) > buffer_size)
{
break;
}
cesu8_pos += next_ch_size;
lit_code_point_t code_point = lit_convert_surrogate_pair_to_code_point (ch, next_ch);
lit_code_point_to_utf8 (code_point, utf8_pos);
size += (code_unit_size + 1);
}
else
{
memcpy (utf8_pos, cesu8_pos, code_unit_size);
size += code_unit_size;
}
}
else
{
memcpy (utf8_pos, cesu8_pos, code_unit_size);
size += code_unit_size;
}
utf8_pos = buffer_p + size;
cesu8_pos += code_unit_size;
}
JERRY_ASSERT (utf8_pos <= utf8_end_pos);
}
ECMA_FINALIZE_UTF8_STRING (cesu8_str_p, cesu8_str_size);
JERRY_ASSERT (size <= buffer_size);
return size;
} /* ecma_substring_copy_to_utf8_buffer */
return copy_size;
} /* ecma_string_copy_to_buffer */
/**
* Convert ecma-string's contents to a cesu-8 string and put it to the buffer.
@@ -1407,14 +1180,14 @@ ecma_substring_copy_to_utf8_buffer (const ecma_string_t *string_desc_p, /**< ecm
* Check if the size of the string is equal with the size of the buffer.
*/
extern inline void JERRY_ATTR_ALWAYS_INLINE
ecma_string_to_utf8_bytes (const ecma_string_t *string_desc_p, /**< ecma-string descriptor */
lit_utf8_byte_t *buffer_p, /**< destination buffer pointer
* (can be NULL if buffer_size == 0) */
lit_utf8_size_t buffer_size) /**< size of buffer */
ecma_string_to_cesu8_bytes (const ecma_string_t *string_desc_p, /**< ecma-string descriptor */
lit_utf8_byte_t *buffer_p, /**< destination buffer pointer
* (can be NULL if buffer_size == 0) */
lit_utf8_size_t buffer_size) /**< size of buffer */
{
const lit_utf8_size_t size = ecma_string_copy_to_cesu8_buffer (string_desc_p, buffer_p, buffer_size);
const lit_utf8_size_t size = ecma_string_copy_to_buffer (string_desc_p, buffer_p, buffer_size, JERRY_ENCODING_CESU8);
JERRY_ASSERT (size == buffer_size);
} /* ecma_string_to_utf8_bytes */
} /* ecma_string_to_cesu8_bytes */
/**
* Get size of the uint32 number stored locally in the string's descriptor
@@ -2707,9 +2480,7 @@ ecma_stringbuilder_create_from (ecma_string_t *string_p) /**< ecma string */
jmem_stats_allocate_string_bytes (initial_size);
#endif /* JERRY_MEM_STATS */
size_t copied_size =
ecma_string_copy_to_cesu8_buffer (string_p, ECMA_STRINGBUILDER_STRING_PTR (header_p), string_size);
JERRY_ASSERT (copied_size == string_size);
ecma_string_to_cesu8_bytes (string_p, ECMA_STRINGBUILDER_STRING_PTR (header_p), string_size);
ecma_stringbuilder_t ret = { .header_p = header_p };
return ret;
@@ -2822,8 +2593,7 @@ ecma_stringbuilder_append (ecma_stringbuilder_t *builder_p, /**< string builder
const lit_utf8_size_t string_size = ecma_string_get_size (string_p);
lit_utf8_byte_t *dest_p = ecma_stringbuilder_grow (builder_p, string_size);
size_t copied_size = ecma_string_copy_to_cesu8_buffer (string_p, dest_p, string_size);
JERRY_ASSERT (copied_size == string_size);
ecma_string_to_cesu8_bytes (string_p, dest_p, string_size);
} /* ecma_stringbuilder_append */
/**
+2 -2
View File
@@ -424,10 +424,10 @@ ecma_is_value_object (ecma_value_t value) /**< ecma value */
* false - otherwise
*/
extern inline bool JERRY_ATTR_CONST JERRY_ATTR_ALWAYS_INLINE
ecma_is_value_error_reference (ecma_value_t value) /**< ecma value */
ecma_is_value_exception (ecma_value_t value) /**< ecma value */
{
return (ecma_get_value_type_field (value) == ECMA_TYPE_ERROR);
} /* ecma_is_value_error_reference */
} /* ecma_is_value_exception */
/**
* Debug assertion that specified value's type is one of ECMA-defined
+26 -26
View File
@@ -1254,7 +1254,7 @@ ecma_ref_extended_primitive (ecma_extended_primitive_t *primitve_p) /**< extende
* Decrease ref count of an error reference.
*/
void
ecma_deref_error_reference (ecma_extended_primitive_t *error_ref_p) /**< error reference */
ecma_deref_exception (ecma_extended_primitive_t *error_ref_p) /**< error reference */
{
JERRY_ASSERT (error_ref_p->refs_and_type >= ECMA_EXTENDED_PRIMITIVE_REF_ONE);
@@ -1265,7 +1265,7 @@ ecma_deref_error_reference (ecma_extended_primitive_t *error_ref_p) /**< error r
ecma_free_value (error_ref_p->u.value);
jmem_pools_free (error_ref_p, sizeof (ecma_extended_primitive_t));
}
} /* ecma_deref_error_reference */
} /* ecma_deref_exception */
#if JERRY_BUILTIN_BIGINT
@@ -1303,8 +1303,8 @@ ecma_deref_bigint (ecma_extended_primitive_t *bigint_p) /**< bigint value */
* @return error reference value
*/
ecma_value_t
ecma_create_error_reference (ecma_value_t value, /**< referenced value */
uint32_t options) /**< ECMA_ERROR_API_* options */
ecma_create_exception (ecma_value_t value, /**< referenced value */
uint32_t options) /**< ECMA_ERROR_API_* options */
{
ecma_extended_primitive_t *error_ref_p;
error_ref_p = (ecma_extended_primitive_t *) jmem_pools_alloc (sizeof (ecma_extended_primitive_t));
@@ -1312,7 +1312,7 @@ ecma_create_error_reference (ecma_value_t value, /**< referenced value */
error_ref_p->refs_and_type = ECMA_EXTENDED_PRIMITIVE_REF_ONE | options;
error_ref_p->u.value = value;
return ecma_make_extended_primitive_value (error_ref_p, ECMA_TYPE_ERROR);
} /* ecma_create_error_reference */
} /* ecma_create_exception */
/**
* Create an error reference from the currently thrown error value.
@@ -1320,47 +1320,47 @@ ecma_create_error_reference (ecma_value_t value, /**< referenced value */
* @return error reference value
*/
ecma_value_t
ecma_create_error_reference_from_context (void)
ecma_create_exception_from_context (void)
{
uint32_t options = 0;
uint32_t status_flags = JERRY_CONTEXT (status_flags);
if (status_flags & ECMA_STATUS_ABORT)
{
options |= ECMA_ERROR_API_ABORT;
options |= ECMA_ERROR_API_FLAG_ABORT;
}
#if JERRY_VM_THROW
if (status_flags & ECMA_STATUS_ERROR_THROWN)
{
options |= ECMA_ERROR_API_THROW_CAPTURED;
options |= ECMA_ERROR_API_FLAG_THROW_CAPTURED;
}
#endif /* JERRY_VM_THROW */
return ecma_create_error_reference (jcontext_take_exception (), options);
} /* ecma_create_error_reference_from_context */
return ecma_create_exception (jcontext_take_exception (), options);
} /* ecma_create_exception_from_context */
/**
* Create an error reference from a given object.
* Create an exception from a given object.
*
* Note:
* Reference of the value is taken.
* Reference of the object is taken.
*
* @return error reference value
* @return exception value
*/
extern inline ecma_value_t JERRY_ATTR_ALWAYS_INLINE
ecma_create_error_object_reference (ecma_object_t *object_p) /**< referenced object */
ecma_create_exception_from_object (ecma_object_t *object_p) /**< referenced object */
{
return ecma_create_error_reference (ecma_make_object_value (object_p), 0);
} /* ecma_create_error_object_reference */
return ecma_create_exception (ecma_make_object_value (object_p), 0);
} /* ecma_create_exception_from_object */
/**
* Raise error from the given error reference.
* Raise a new exception from the argument exception value.
*
* Note: the error reference's ref count is also decreased
* Note: the argument exceptions reference count is decreased
*/
void
ecma_raise_error_from_error_reference (ecma_value_t value) /**< error reference */
ecma_throw_exception (ecma_value_t value) /**< error reference */
{
JERRY_ASSERT (!jcontext_has_pending_exception () && !jcontext_has_pending_abort ());
ecma_extended_primitive_t *error_ref_p = ecma_get_extended_primitive_from_value (value);
@@ -1376,13 +1376,13 @@ ecma_raise_error_from_error_reference (ecma_value_t value) /**< error reference
#endif /* JERRY_VM_THROW */
| ECMA_STATUS_ABORT);
if (!(error_ref_p->refs_and_type & ECMA_ERROR_API_ABORT))
if (!(error_ref_p->refs_and_type & ECMA_ERROR_API_FLAG_ABORT))
{
status_flags &= ~(uint32_t) ECMA_STATUS_ABORT;
}
#if JERRY_VM_THROW
if (!(error_ref_p->refs_and_type & ECMA_ERROR_API_THROW_CAPTURED))
if (!(error_ref_p->refs_and_type & ECMA_ERROR_API_FLAG_THROW_CAPTURED))
{
status_flags &= ~(uint32_t) ECMA_STATUS_ERROR_THROWN;
}
@@ -1401,7 +1401,7 @@ ecma_raise_error_from_error_reference (ecma_value_t value) /**< error reference
}
JERRY_CONTEXT (error_value) = referenced_value;
} /* ecma_raise_error_from_error_reference */
} /* ecma_throw_exception */
/**
* Decrease the reference counter of a script value.
@@ -1434,7 +1434,7 @@ ecma_script_deref (ecma_value_t script_value) /**< script value */
}
#if JERRY_RESOURCE_NAME
ecma_deref_ecma_string (ecma_get_string_from_value (script_p->resource_name));
ecma_deref_ecma_string (ecma_get_string_from_value (script_p->source_name));
#endif /* JERRY_RESOURCE_NAME */
#if JERRY_MODULE_SYSTEM
@@ -1778,7 +1778,7 @@ ecma_compiled_code_get_line_info (const ecma_compiled_code_t *bytecode_header_p)
* @return resource name value
*/
ecma_value_t
ecma_get_resource_name (const ecma_compiled_code_t *bytecode_p) /**< compiled code */
ecma_get_source_name (const ecma_compiled_code_t *bytecode_p) /**< compiled code */
{
#if JERRY_RESOURCE_NAME
#if JERRY_SNAPSHOT_EXEC
@@ -1789,12 +1789,12 @@ ecma_get_resource_name (const ecma_compiled_code_t *bytecode_p) /**< compiled co
#endif /* JERRY_SNAPSHOT_EXEC */
ecma_value_t script_value = ((cbc_uint8_arguments_t *) bytecode_p)->script_value;
return ECMA_GET_INTERNAL_VALUE_POINTER (cbc_script_t, script_value)->resource_name;
return ECMA_GET_INTERNAL_VALUE_POINTER (cbc_script_t, script_value)->source_name;
#else /* !JERRY_RESOURCE_NAME */
JERRY_UNUSED (bytecode_p);
return ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON);
#endif /* !JERRY_RESOURCE_NAME */
} /* ecma_get_resource_name */
} /* ecma_get_source_name */
#if (JERRY_STACK_LIMIT != 0)
/**
+12 -24
View File
@@ -227,7 +227,7 @@ bool JERRY_ATTR_CONST ecma_is_value_prop_name (ecma_value_t value);
bool JERRY_ATTR_CONST ecma_is_value_direct_string (ecma_value_t value);
bool JERRY_ATTR_CONST ecma_is_value_non_direct_string (ecma_value_t value);
bool JERRY_ATTR_CONST ecma_is_value_object (ecma_value_t value);
bool JERRY_ATTR_CONST ecma_is_value_error_reference (ecma_value_t value);
bool JERRY_ATTR_CONST ecma_is_value_exception (ecma_value_t value);
ecma_value_t ecma_is_value_array (ecma_value_t arg);
void ecma_check_value_type_is_spec_defined (ecma_value_t value);
@@ -316,24 +316,12 @@ void ecma_destroy_ecma_string (ecma_string_t *string_p);
ecma_number_t ecma_string_to_number (const ecma_string_t *str_p);
uint32_t ecma_string_get_array_index (const ecma_string_t *str_p);
lit_utf8_size_t JERRY_ATTR_WARN_UNUSED_RESULT ecma_string_copy_to_cesu8_buffer (const ecma_string_t *string_desc_p,
lit_utf8_byte_t *buffer_p,
lit_utf8_size_t buffer_size);
lit_utf8_size_t JERRY_ATTR_WARN_UNUSED_RESULT ecma_string_copy_to_utf8_buffer (const ecma_string_t *string_desc_p,
lit_utf8_byte_t *buffer_p,
lit_utf8_size_t buffer_size);
lit_utf8_size_t ecma_substring_copy_to_cesu8_buffer (const ecma_string_t *string_desc_p,
lit_utf8_size_t start_pos,
lit_utf8_size_t end_pos,
lit_utf8_byte_t *buffer_p,
lit_utf8_size_t buffer_size);
lit_utf8_size_t ecma_substring_copy_to_utf8_buffer (const ecma_string_t *string_desc_p,
lit_utf8_size_t start_pos,
lit_utf8_size_t end_pos,
lit_utf8_byte_t *buffer_p,
lit_utf8_size_t buffer_size);
lit_utf8_size_t JERRY_ATTR_WARN_UNUSED_RESULT ecma_string_copy_to_buffer (const ecma_string_t *string_desc_p,
lit_utf8_byte_t *buffer_p,
lit_utf8_size_t buffer_size,
jerry_encoding_t encoding);
void
ecma_string_to_utf8_bytes (const ecma_string_t *string_desc_p, lit_utf8_byte_t *buffer_p, lit_utf8_size_t buffer_size);
ecma_string_to_cesu8_bytes (const ecma_string_t *string_desc_p, lit_utf8_byte_t *buffer_p, lit_utf8_size_t buffer_size);
const lit_utf8_byte_t *ecma_string_get_chars (const ecma_string_t *string_p,
lit_utf8_size_t *size_p,
lit_utf8_size_t *length_p,
@@ -498,15 +486,15 @@ ecma_property_descriptor_t ecma_make_empty_property_descriptor (void);
void ecma_free_property_descriptor (ecma_property_descriptor_t *prop_desc_p);
void ecma_ref_extended_primitive (ecma_extended_primitive_t *primitve_p);
void ecma_deref_error_reference (ecma_extended_primitive_t *error_ref_p);
void ecma_deref_exception (ecma_extended_primitive_t *exception_p);
#if JERRY_BUILTIN_BIGINT
void ecma_deref_bigint (ecma_extended_primitive_t *bigint_p);
#endif /* JERRY_BUILTIN_BIGINT */
ecma_value_t ecma_create_error_reference (ecma_value_t value, uint32_t options);
ecma_value_t ecma_create_error_reference_from_context (void);
ecma_value_t ecma_create_error_object_reference (ecma_object_t *object_p);
void ecma_raise_error_from_error_reference (ecma_value_t value);
ecma_value_t ecma_create_exception (ecma_value_t value, uint32_t options);
ecma_value_t ecma_create_exception_from_context (void);
ecma_value_t ecma_create_exception_from_object (ecma_object_t *object_p);
void ecma_throw_exception (ecma_value_t value);
void ecma_script_deref (ecma_value_t script_value);
void ecma_bytecode_ref (ecma_compiled_code_t *bytecode_p);
@@ -520,7 +508,7 @@ ecma_collection_t *ecma_compiled_code_get_tagged_template_collection (const ecma
#if JERRY_LINE_INFO
uint8_t *ecma_compiled_code_get_line_info (const ecma_compiled_code_t *bytecode_header_p);
#endif /* JERRY_LINE_INFO */
ecma_value_t ecma_get_resource_name (const ecma_compiled_code_t *bytecode_p);
ecma_value_t ecma_get_source_name (const ecma_compiled_code_t *bytecode_p);
#if (JERRY_STACK_LIMIT != 0)
uintptr_t ecma_get_current_stack_usage (void);
#endif /* (JERRY_STACK_LIMIT != 0) */
+1 -1
View File
@@ -116,7 +116,7 @@ ecma_line_info_free (uint8_t *line_info_p) /**< line info buffer */
void
ecma_line_info_get (uint8_t *line_info_p, /**< line info buffer */
uint32_t offset, /**< byte code offset */
jerry_backtrace_location_t *location_p) /**< [out] location */
jerry_frame_location_t *location_p) /**< [out] location */
{
uint32_t line = 1;
uint32_t column = ECMA_LINE_INFO_COLUMN_DEFAULT;
+1 -1
View File
@@ -98,7 +98,7 @@ uint32_t ecma_line_info_difference_update (uint32_t current_value, uint32_t diff
/* General functions. */
void ecma_line_info_free (uint8_t *line_info_p);
void ecma_line_info_get (uint8_t *line_info_p, uint32_t offset, jerry_backtrace_location_t *location_p);
void ecma_line_info_get (uint8_t *line_info_p, uint32_t offset, jerry_frame_location_t *location_p);
#if JERRY_PARSER_DUMP_BYTE_CODE
void ecma_line_info_dump (uint8_t *line_info_p);
+1 -1
View File
@@ -633,7 +633,7 @@ ecma_save_literals_for_snapshot (ecma_collection_t *lit_pool_p, /**< list of kno
*(uint16_t *) destination_p = (uint16_t) length;
ecma_string_to_utf8_bytes (string_p, destination_p + sizeof (uint16_t), length);
ecma_string_to_cesu8_bytes (string_p, destination_p + sizeof (uint16_t), length);
length = JERRY_ALIGNUP (sizeof (uint16_t) + length, JERRY_SNAPSHOT_LITERAL_ALIGNMENT);
}
+7 -7
View File
@@ -563,9 +563,9 @@ ecma_module_evaluate (ecma_module_t *module_p) /**< module */
{
ret_value = module_p->u.callback (ecma_make_object_value (&module_p->header.object));
if (JERRY_UNLIKELY (ecma_is_value_error_reference (ret_value)))
if (JERRY_UNLIKELY (ecma_is_value_exception (ret_value)))
{
ecma_raise_error_from_error_reference (ret_value);
ecma_throw_exception (ret_value);
ret_value = ECMA_VALUE_ERROR;
}
}
@@ -1099,7 +1099,7 @@ typedef struct ecma_module_stack_item_t
*/
ecma_value_t
ecma_module_link (ecma_module_t *module_p, /**< root module */
jerry_module_resolve_callback_t callback, /**< resolve module callback */
jerry_module_resolve_cb_t callback, /**< resolve module callback */
void *user_p) /**< pointer passed to the resolve callback */
{
if (module_p->header.u.cls.u1.module_state != JERRY_MODULE_STATE_UNLINKED)
@@ -1141,9 +1141,9 @@ restart:
ecma_value_t resolve_result = callback (node_p->u.path_or_module, module_val, user_p);
if (JERRY_UNLIKELY (ecma_is_value_error_reference (resolve_result)))
if (JERRY_UNLIKELY (ecma_is_value_exception (resolve_result)))
{
ecma_raise_error_from_error_reference (resolve_result);
ecma_throw_exception (resolve_result);
goto error;
}
@@ -1373,9 +1373,9 @@ ecma_module_import (ecma_value_t specifier, /**< module specifier */
JERRY_CONTEXT (module_import_callback_user_p));
ecma_deref_ecma_string (specifier_p);
if (JERRY_UNLIKELY (ecma_is_value_error_reference (result)))
if (JERRY_UNLIKELY (ecma_is_value_exception (result)))
{
ecma_raise_error_from_error_reference (result);
ecma_throw_exception (result);
goto error;
}
+2 -2
View File
@@ -68,7 +68,7 @@ typedef struct ecma_module
union
{
ecma_compiled_code_t *compiled_code_p; /**< compiled code for the module */
jerry_native_module_evaluate_callback_t callback; /**< callback for evaluating native modules */
jerry_native_module_evaluate_cb_t callback; /**< callback for evaluating native modules */
} u;
} ecma_module_t;
@@ -117,7 +117,7 @@ typedef struct ecma_module_resolve_stack
ecma_value_t ecma_module_initialize (ecma_module_t *module_p);
ecma_module_t *ecma_module_get_resolved_module (ecma_value_t module_val);
ecma_value_t ecma_module_link (ecma_module_t *module_p, jerry_module_resolve_callback_t callback_p, void *user_p);
ecma_value_t ecma_module_link (ecma_module_t *module_p, jerry_module_resolve_cb_t callback_p, void *user_p);
ecma_value_t ecma_module_evaluate (ecma_module_t *module_p);
ecma_value_t ecma_module_import (ecma_value_t specifier, ecma_value_t user_value);