Refactor JSON builtin methods (#3031)
JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
This commit is contained in:
@@ -2568,6 +2568,56 @@ ecma_stringbuilder_grow (ecma_stringbuilder_t *builder_p, /**< string builder */
|
||||
return ((lit_utf8_byte_t *) header_p) + header_p->current_size - required_size;
|
||||
} /* ecma_stringbuilder_grow */
|
||||
|
||||
/**
|
||||
* Get the current size of the string in a string builder
|
||||
*
|
||||
* @return the size of the string data
|
||||
*/
|
||||
lit_utf8_size_t
|
||||
ecma_stringbuilder_get_size (ecma_stringbuilder_t *builder_p) /**< string builder */
|
||||
{
|
||||
ecma_stringbuilder_header_t *header_p = builder_p->header_p;
|
||||
JERRY_ASSERT (header_p != NULL);
|
||||
|
||||
return ECMA_STRINGBUILDER_STRING_SIZE (header_p);
|
||||
} /* ecma_stringbuilder_get_size */
|
||||
|
||||
/**
|
||||
* Get pointer to the raw string data in a string builder
|
||||
*
|
||||
* @return pointer to the string data
|
||||
*/
|
||||
lit_utf8_byte_t *
|
||||
ecma_stringbuilder_get_data (ecma_stringbuilder_t *builder_p) /**< string builder */
|
||||
{
|
||||
ecma_stringbuilder_header_t *header_p = builder_p->header_p;
|
||||
JERRY_ASSERT (header_p != NULL);
|
||||
|
||||
return ECMA_STRINGBUILDER_STRING_PTR (header_p);
|
||||
} /* ecma_stringbuilder_get_data */
|
||||
|
||||
/**
|
||||
* Revert the string builder to a smaller size
|
||||
*/
|
||||
void
|
||||
ecma_stringbuilder_revert (ecma_stringbuilder_t *builder_p, /**< string builder */
|
||||
const lit_utf8_size_t size) /**< new size */
|
||||
{
|
||||
ecma_stringbuilder_header_t *header_p = builder_p->header_p;
|
||||
JERRY_ASSERT (header_p != NULL);
|
||||
|
||||
const lit_utf8_size_t new_size = size + (lit_utf8_size_t) (sizeof (ecma_ascii_string_t));
|
||||
JERRY_ASSERT (new_size <= header_p->current_size);
|
||||
|
||||
#if ENABLED (JERRY_MEM_STATS)
|
||||
jmem_stats_free_string_bytes (header_p->current_size - new_size);
|
||||
#endif /* ENABLED (JERRY_MEM_STATS) */
|
||||
|
||||
header_p = jmem_heap_realloc_block (header_p, header_p->current_size, new_size);
|
||||
header_p->current_size = new_size;
|
||||
builder_p->header_p = header_p;
|
||||
} /* ecma_stringbuilder_revert */
|
||||
|
||||
/**
|
||||
* Append an ecma_string_t to a string builder
|
||||
*/
|
||||
@@ -2623,6 +2673,17 @@ ecma_stringbuilder_append_char (ecma_stringbuilder_t *builder_p, /**< string bui
|
||||
lit_char_to_utf8_bytes (dest_p, c);
|
||||
} /* ecma_stringbuilder_append_char */
|
||||
|
||||
/**
|
||||
* Append a single byte to a string builder
|
||||
*/
|
||||
void
|
||||
ecma_stringbuilder_append_byte (ecma_stringbuilder_t *builder_p, /**< string builder */
|
||||
const lit_utf8_byte_t byte) /**< byte */
|
||||
{
|
||||
lit_utf8_byte_t *dest_p = ecma_stringbuilder_grow (builder_p, 1);
|
||||
*dest_p = byte;
|
||||
} /* ecma_stringbuilder_append_byte */
|
||||
|
||||
/**
|
||||
* Finalize a string builder, returning the created string, and releasing the underlying buffer.
|
||||
*
|
||||
@@ -2675,7 +2736,9 @@ ecma_stringbuilder_finalize (ecma_stringbuilder_t *builder_p) /**< string builde
|
||||
|
||||
const size_t utf8_string_size = string_size + container_size;
|
||||
header_p = jmem_heap_realloc_block (header_p, header_p->current_size, utf8_string_size);
|
||||
memmove (((lit_utf8_byte_t *) header_p + container_size), string_begin_p, string_size);
|
||||
memmove (((lit_utf8_byte_t *) header_p + container_size),
|
||||
ECMA_STRINGBUILDER_STRING_PTR (header_p),
|
||||
string_size);
|
||||
|
||||
#if ENABLED (JERRY_MEM_STATS)
|
||||
jmem_stats_allocate_string_bytes (container_size - sizeof (ecma_ascii_string_t));
|
||||
|
||||
@@ -319,12 +319,16 @@ ecma_string_t *ecma_string_trim (const ecma_string_t *string_p);
|
||||
|
||||
ecma_stringbuilder_t ecma_stringbuilder_create (void);
|
||||
ecma_stringbuilder_t ecma_stringbuilder_create_from (ecma_string_t *string_p);
|
||||
lit_utf8_size_t ecma_stringbuilder_get_size (ecma_stringbuilder_t *builder_p);
|
||||
lit_utf8_byte_t *ecma_stringbuilder_get_data (ecma_stringbuilder_t *builder_p);
|
||||
void ecma_stringbuilder_revert (ecma_stringbuilder_t *builder_p, const lit_utf8_size_t size);
|
||||
void ecma_stringbuilder_append (ecma_stringbuilder_t *builder_p, const ecma_string_t *string_p);
|
||||
void ecma_stringbuilder_append_magic (ecma_stringbuilder_t *builder_p, const lit_magic_string_id_t id);
|
||||
void ecma_stringbuilder_append_raw (ecma_stringbuilder_t *builder_p,
|
||||
const lit_utf8_byte_t *data_p,
|
||||
const lit_utf8_size_t data_size);
|
||||
void ecma_stringbuilder_append_char (ecma_stringbuilder_t *builder_p, const ecma_char_t c);
|
||||
void ecma_stringbuilder_append_byte (ecma_stringbuilder_t *builder_p, const lit_utf8_byte_t);
|
||||
ecma_string_t *ecma_stringbuilder_finalize (ecma_stringbuilder_t *builder_p);
|
||||
void ecma_stringbuilder_destroy (ecma_stringbuilder_t *builder_p);
|
||||
|
||||
|
||||
@@ -64,12 +64,8 @@ ecma_json_has_object_in_stack (ecma_json_occurence_stack_item_t *stack_p, /**< s
|
||||
*/
|
||||
bool
|
||||
ecma_has_string_value_in_collection (ecma_collection_header_t *collection_p, /**< collection */
|
||||
ecma_value_t string_value) /**< string value */
|
||||
ecma_string_t *string_p) /**< string */
|
||||
{
|
||||
JERRY_ASSERT (ecma_is_value_string (string_value));
|
||||
|
||||
ecma_string_t *string_p = ecma_get_string_from_value (string_value);
|
||||
|
||||
ecma_value_t *ecma_value_p = ecma_collection_iterator_init (collection_p);
|
||||
|
||||
while (ecma_value_p != NULL)
|
||||
@@ -85,143 +81,7 @@ ecma_has_string_value_in_collection (ecma_collection_header_t *collection_p, /**
|
||||
}
|
||||
|
||||
return false;
|
||||
} /* ecma_has_string_value_in_collection*/
|
||||
|
||||
/**
|
||||
* Common function to concatenate key-value pairs into an ecma-string.
|
||||
*
|
||||
* See also:
|
||||
* ECMA-262 v5, 15.12.3
|
||||
*
|
||||
* Used by:
|
||||
* - ecma_builtin_helper_json_create_formatted_json step 10.b.ii
|
||||
* - ecma_builtin_helper_json_create_non_formatted_json step 10.a.i
|
||||
*
|
||||
* @return pointer to ecma-string
|
||||
* Returned value must be freed with ecma_deref_ecma_string.
|
||||
*/
|
||||
static ecma_string_t *
|
||||
ecma_builtin_helper_json_create_separated_properties (ecma_collection_header_t *partial_p, /**< key-value pairs*/
|
||||
ecma_string_t *separator_p) /**< separator*/
|
||||
{
|
||||
ecma_string_t *properties_str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY);
|
||||
|
||||
ecma_value_t *ecma_value_p = ecma_collection_iterator_init (partial_p);
|
||||
|
||||
bool first = true;
|
||||
|
||||
while (ecma_value_p != NULL)
|
||||
{
|
||||
ecma_string_t *current_p = ecma_get_string_from_value (*ecma_value_p);
|
||||
ecma_value_p = ecma_collection_iterator_next (ecma_value_p);
|
||||
|
||||
if (JERRY_LIKELY (!first))
|
||||
{
|
||||
properties_str_p = ecma_concat_ecma_strings (properties_str_p, separator_p);
|
||||
}
|
||||
|
||||
properties_str_p = ecma_concat_ecma_strings (properties_str_p, current_p);
|
||||
first = false;
|
||||
}
|
||||
|
||||
return properties_str_p;
|
||||
} /* ecma_builtin_helper_json_create_separated_properties */
|
||||
|
||||
/**
|
||||
* Common function to create a formatted JSON string.
|
||||
*
|
||||
* See also:
|
||||
* ECMA-262 v5, 15.12.3
|
||||
*
|
||||
* Used by:
|
||||
* - ecma_builtin_json_object step 10.b
|
||||
* - ecma_builtin_json_array step 10.b
|
||||
*
|
||||
* @return ecma value
|
||||
* Returned value must be freed with ecma_free_value.
|
||||
*/
|
||||
ecma_value_t
|
||||
ecma_builtin_helper_json_create_formatted_json (lit_utf8_byte_t left_bracket, /**< left bracket character */
|
||||
lit_utf8_byte_t right_bracket, /**< right bracket character */
|
||||
ecma_string_t *stepback_p, /**< stepback*/
|
||||
ecma_collection_header_t *partial_p, /**< key-value pairs*/
|
||||
ecma_json_stringify_context_t *context_p) /**< context*/
|
||||
{
|
||||
JERRY_ASSERT (left_bracket < LIT_UTF8_1_BYTE_CODE_POINT_MAX
|
||||
&& right_bracket < LIT_UTF8_1_BYTE_CODE_POINT_MAX);
|
||||
|
||||
/* 10.b.i */
|
||||
lit_utf8_byte_t chars[2] = { LIT_CHAR_COMMA, LIT_CHAR_LF };
|
||||
|
||||
ecma_string_t *separator_p = ecma_new_ecma_string_from_utf8 (chars, 2);
|
||||
|
||||
separator_p = ecma_concat_ecma_strings (separator_p, context_p->indent_str_p);
|
||||
|
||||
/* 10.b.ii */
|
||||
ecma_string_t *properties_str_p = ecma_builtin_helper_json_create_separated_properties (partial_p, separator_p);
|
||||
ecma_deref_ecma_string (separator_p);
|
||||
|
||||
/* 10.b.iii */
|
||||
chars[0] = left_bracket;
|
||||
|
||||
ecma_string_t *final_str_p = ecma_new_ecma_string_from_utf8 (chars, 2);
|
||||
|
||||
final_str_p = ecma_concat_ecma_strings (final_str_p, context_p->indent_str_p);
|
||||
|
||||
final_str_p = ecma_concat_ecma_strings (final_str_p, properties_str_p);
|
||||
ecma_deref_ecma_string (properties_str_p);
|
||||
|
||||
chars[0] = LIT_CHAR_LF;
|
||||
final_str_p = ecma_append_chars_to_string (final_str_p, chars, 1, 1);
|
||||
|
||||
final_str_p = ecma_concat_ecma_strings (final_str_p, stepback_p);
|
||||
|
||||
chars[0] = right_bracket;
|
||||
final_str_p = ecma_append_chars_to_string (final_str_p, chars, 1, 1);
|
||||
|
||||
return ecma_make_string_value (final_str_p);
|
||||
} /* ecma_builtin_helper_json_create_formatted_json */
|
||||
|
||||
/**
|
||||
* Common function to create a non-formatted JSON string.
|
||||
*
|
||||
* See also:
|
||||
* ECMA-262 v5, 15.12.3
|
||||
*
|
||||
* Used by:
|
||||
* - ecma_builtin_json_object step 10.a
|
||||
* - ecma_builtin_json_array step 10.a
|
||||
*
|
||||
* @return ecma value
|
||||
* Returned value must be freed with ecma_free_value.
|
||||
*/
|
||||
ecma_value_t
|
||||
ecma_builtin_helper_json_create_non_formatted_json (lit_utf8_byte_t left_bracket, /**< left bracket character */
|
||||
lit_utf8_byte_t right_bracket, /**< right bracket character */
|
||||
ecma_collection_header_t *partial_p) /**< key-value pairs */
|
||||
{
|
||||
JERRY_ASSERT (left_bracket < LIT_UTF8_1_BYTE_CODE_POINT_MAX
|
||||
&& right_bracket < LIT_UTF8_1_BYTE_CODE_POINT_MAX);
|
||||
|
||||
/* 10.a */
|
||||
ecma_string_t *comma_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_COMMA_CHAR);
|
||||
ecma_string_t *properties_str_p;
|
||||
|
||||
/* 10.a.i */
|
||||
properties_str_p = ecma_builtin_helper_json_create_separated_properties (partial_p, comma_str_p);
|
||||
|
||||
/* 10.a.ii */
|
||||
ecma_string_t *result_str_p = ecma_new_ecma_string_from_code_unit (left_bracket);
|
||||
|
||||
result_str_p = ecma_concat_ecma_strings (result_str_p, properties_str_p);
|
||||
ecma_deref_ecma_string (properties_str_p);
|
||||
|
||||
lit_utf8_byte_t chars[1] = { right_bracket };
|
||||
|
||||
result_str_p = ecma_append_chars_to_string (result_str_p, chars, 1, 1);
|
||||
|
||||
return ecma_make_string_value (result_str_p);
|
||||
} /* ecma_builtin_helper_json_create_non_formatted_json */
|
||||
} /* ecma_has_string_value_in_collection */
|
||||
|
||||
#endif /* ENABLED (JERRY_BUILTIN_JSON) */
|
||||
|
||||
|
||||
@@ -159,20 +159,23 @@ typedef struct
|
||||
ecma_json_occurence_stack_item_t *occurence_stack_last_p;
|
||||
|
||||
/** The actual indentation text. */
|
||||
ecma_string_t *indent_str_p;
|
||||
ecma_stringbuilder_t indent_builder;
|
||||
|
||||
/** The indentation text. */
|
||||
ecma_string_t *gap_str_p;
|
||||
|
||||
/** The replacer function. */
|
||||
ecma_object_t *replacer_function_p;
|
||||
|
||||
/** Result string builder. */
|
||||
ecma_stringbuilder_t result_builder;
|
||||
} ecma_json_stringify_context_t;
|
||||
|
||||
ecma_value_t ecma_builtin_json_parse_buffer (const lit_utf8_byte_t * str_start_p,
|
||||
lit_utf8_size_t string_size);
|
||||
ecma_value_t ecma_builtin_json_string_from_object (const ecma_value_t arg1);
|
||||
bool ecma_json_has_object_in_stack (ecma_json_occurence_stack_item_t *stack_p, ecma_object_t *object_p);
|
||||
bool ecma_has_string_value_in_collection (ecma_collection_header_t *collection_p, ecma_value_t string_value);
|
||||
bool ecma_has_string_value_in_collection (ecma_collection_header_t *collection_p, ecma_string_t *string_p);
|
||||
|
||||
ecma_value_t
|
||||
ecma_builtin_helper_json_create_formatted_json (lit_utf8_byte_t left_bracket, lit_utf8_byte_t right_bracket,
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -633,10 +633,7 @@ jmem_heap_realloc_block (void *ptr, /**< memory region to reallocate */
|
||||
|
||||
/* jmem_heap_alloc_block_internal may trigger garbage collection, which can create new free blocks
|
||||
* in the heap structure, so we need to look up the previous block again. */
|
||||
if (JERRY_UNLIKELY (JERRY_CONTEXT (ecma_gc_new_objects) == 0))
|
||||
{
|
||||
prev_p = jmem_heap_find_prev (block_p);
|
||||
}
|
||||
|
||||
memcpy (ret_block_p, block_p, old_size);
|
||||
jmem_heap_insert_block (block_p, prev_p, aligned_old_size);
|
||||
|
||||
Reference in New Issue
Block a user