Implement function.toString operation (#4752)

May increase the memory consumtpion heavily.

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2021-08-31 13:37:25 +02:00
committed by GitHub
parent 1c6b18ecdf
commit 6649940ea6
37 changed files with 1002 additions and 192 deletions
+152
View File
@@ -0,0 +1,152 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "byte-code.h"
#include "ecma-helpers.h"
#include "ecma-extended-info.h"
#if JERRY_ESNEXT || JERRY_FUNCTION_TO_STRING
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmaextendedinfo Extended info
* @{
*/
/**
* Decodes an uint32_t number, and updates the buffer position.
*
* @return the decoded value
*/
uint32_t
ecma_extended_info_decode_vlq (uint8_t **buffer_p) /**< [in/out] target buffer */
{
uint8_t *source_p = *buffer_p;
uint32_t value = 0;
do
{
source_p--;
value = (value << ECMA_EXTENDED_INFO_VLQ_SHIFT) | (*source_p & ECMA_EXTENDED_INFO_VLQ_MASK);
}
while (*source_p & ECMA_EXTENDED_INFO_VLQ_CONTINUE);
*buffer_p = source_p;
return value;
} /* ecma_extended_info_decode_vlq */
/**
* Encodes an uint32_t number into a buffer.
*/
void
ecma_extended_info_encode_vlq (uint8_t **buffer_p, /**< target buffer */
uint32_t value) /**< encoded value */
{
uint8_t *destination_p = *buffer_p - 1;
if (value <= ECMA_EXTENDED_INFO_VLQ_MASK)
{
*destination_p = (uint8_t) value;
*buffer_p = destination_p;
return;
}
uint32_t length = 0;
uint32_t current_value = value >> ECMA_EXTENDED_INFO_VLQ_SHIFT;
do
{
current_value >>= ECMA_EXTENDED_INFO_VLQ_SHIFT;
length++;
}
while (current_value > 0);
destination_p -= length;
*buffer_p = destination_p;
do
{
*destination_p++ = (uint8_t) (value | ECMA_EXTENDED_INFO_VLQ_CONTINUE);
value >>= ECMA_EXTENDED_INFO_VLQ_SHIFT;
}
while (value > 0);
**buffer_p &= ECMA_EXTENDED_INFO_VLQ_MASK;
} /* ecma_extended_info_encode_vlq */
/**
* Gets the encoded length of a number.
*
* @return encoded length
*/
uint32_t
ecma_extended_info_get_encoded_length (uint32_t value) /**< encoded value */
{
uint32_t length = 0;
do
{
value >>= ECMA_EXTENDED_INFO_VLQ_SHIFT;
length++;
}
while (value > 0);
return length;
} /* ecma_extended_info_get_encoded_length */
/**
* Get the extended info from a byte code
*
* @return pointer to the extended info
*/
uint8_t *
ecma_compiled_code_resolve_extended_info (const ecma_compiled_code_t *bytecode_header_p) /**< compiled code */
{
JERRY_ASSERT (bytecode_header_p != NULL);
JERRY_ASSERT (bytecode_header_p->status_flags & CBC_CODE_FLAGS_HAS_EXTENDED_INFO);
ecma_value_t *base_p = ecma_compiled_code_resolve_arguments_start (bytecode_header_p);
#if JERRY_ESNEXT
if (CBC_FUNCTION_GET_TYPE (bytecode_header_p->status_flags) != CBC_FUNCTION_CONSTRUCTOR)
{
base_p--;
}
if (bytecode_header_p->status_flags & CBC_CODE_FLAGS_HAS_TAGGED_LITERALS)
{
base_p--;
}
#endif /* JERRY_ESNEXT */
#if JERRY_LINE_INFO
if (bytecode_header_p->status_flags & CBC_CODE_FLAGS_HAS_LINE_INFO)
{
base_p--;
}
#endif /* JERRY_LINE_INFO */
JERRY_ASSERT (((uint8_t *) base_p)[-1] != 0);
return ((uint8_t *) base_p) - 1;
} /* ecma_compiled_code_resolve_extended_info */
#endif /* JERRY_ESNEXT || JERRY_FUNCTION_TO_STRING */
/**
* @}
* @}
*/
+58
View File
@@ -0,0 +1,58 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ECMA_EXTENDED_INFO_H
#define ECMA_EXTENDED_INFO_H
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmaextendedinfo Extended info
* @{
*/
#if JERRY_ESNEXT || JERRY_FUNCTION_TO_STRING
#include "ecma-globals.h"
/**
* Vlq encoding: flag which is set for all bytes except the last one.
*/
#define ECMA_EXTENDED_INFO_VLQ_CONTINUE 0x80
/**
* Vlq encoding: mask to decode the number fragment.
*/
#define ECMA_EXTENDED_INFO_VLQ_MASK 0x7f
/**
* Vlq encoding: number of bits stored in a byte.
*/
#define ECMA_EXTENDED_INFO_VLQ_SHIFT 7
uint32_t ecma_extended_info_decode_vlq (uint8_t **buffer_p);
void ecma_extended_info_encode_vlq (uint8_t **buffer_p, uint32_t value);
uint32_t ecma_extended_info_get_encoded_length (uint32_t value);
uint8_t *ecma_compiled_code_resolve_extended_info (const ecma_compiled_code_t *bytecode_header_p);
#endif /* JERRY_ESNEXT || JERRY_FUNCTION_TO_STRING */
/**
* @}
* @}
*/
#endif /* !ECMA_EXTENDED_INFO_H */
+3 -3
View File
@@ -430,10 +430,10 @@ ecma_gc_mark_compiled_code (const ecma_compiled_code_t *compiled_code_p) /**< co
if (CBC_SCRIPT_GET_TYPE (script_p) == CBC_SCRIPT_USER_OBJECT)
{
cbc_script_user_t *script_user_p = (cbc_script_user_t *) script_p;
ecma_value_t user_value = CBC_SCRIPT_GET_USER_VALUE (script_p);
JERRY_ASSERT (ecma_is_value_object (script_user_p->user_value));
ecma_gc_set_object_visited (ecma_get_object_from_value (script_user_p->user_value));
JERRY_ASSERT (ecma_is_value_object (user_value));
ecma_gc_set_object_visited (ecma_get_object_from_value (user_value));
}
#if JERRY_BUILTIN_REALMS
+3
View File
@@ -130,6 +130,9 @@ typedef enum
#if JERRY_ESNEXT
ECMA_PARSE_INTERNAL_PRE_SCANNING = (1u << 16), /**< the parser is in pre-scanning mode */
#endif /* JERRY_ESNEXT */
#if JERRY_FUNCTION_TO_STRING
ECMA_PARSE_INTERNAL_HAS_4_BYTE_MARKER = (1u << 17), /**< source has 4 byte marker */
#endif /* JERRY_FUNCTION_TO_STRING */
#ifndef JERRY_NDEBUG
/**
* This flag represents an error in for in/of statements, which cannot be set
+1 -1
View File
@@ -443,7 +443,7 @@ ecma_new_ecma_string_from_utf8_converted_to_cesu8 (const lit_utf8_byte_t *string
converted_string_size += string_size;
JERRY_ASSERT (lit_is_valid_utf8_string (string_p, string_size));
JERRY_ASSERT (lit_is_valid_utf8_string (string_p, string_size, false));
lit_utf8_byte_t *data_p;
ecma_string_t *string_desc_p = ecma_new_ecma_string_from_utf8_buffer (converted_string_length,
+15 -27
View File
@@ -1527,14 +1527,14 @@ ecma_bytecode_deref (ecma_compiled_code_t *bytecode_p) /**< byte code pointer */
if (type != CBC_SCRIPT_GENERIC)
{
script_size = sizeof (cbc_script_user_t);
script_size += sizeof (ecma_value_t);
if (type == CBC_SCRIPT_USER_VALUE)
{
cbc_script_user_t *script_user_p = (cbc_script_user_t *) script_p;
ecma_value_t user_value = CBC_SCRIPT_GET_USER_VALUE (script_p);
JERRY_ASSERT (!ecma_is_value_object (script_user_p->user_value));
ecma_free_value (script_user_p->user_value);
JERRY_ASSERT (!ecma_is_value_object (user_value));
ecma_free_value (user_value);
}
}
@@ -1542,6 +1542,16 @@ ecma_bytecode_deref (ecma_compiled_code_t *bytecode_p) /**< byte code pointer */
ecma_deref_ecma_string (ecma_get_string_from_value (script_p->resource_name));
#endif /* JERRY_RESOURCE_NAME */
#if JERRY_FUNCTION_TO_STRING
ecma_deref_ecma_string (ecma_get_string_from_value (script_p->source_code));
if (script_p->refs_and_type & CBC_SCRIPT_HAS_FUNCTION_ARGUMENTS)
{
ecma_deref_ecma_string (ecma_get_string_from_value (CBC_SCRIPT_GET_FUNCTION_ARGUMENTS (script_p, type)));
script_size += sizeof (ecma_value_t);
}
#endif /* JERRY_FUNCTION_TO_STRING */
jmem_heap_free_block (script_p, script_size);
}
@@ -1728,21 +1738,6 @@ ecma_compiled_code_resolve_function_name (const ecma_compiled_code_t *bytecode_h
return base_p;
} /* ecma_compiled_code_resolve_function_name */
/**
* Get the extended info from a byte code
*
* @return extended info value
*/
uint32_t
ecma_compiled_code_resolve_extended_info (const ecma_compiled_code_t *bytecode_header_p) /**< compiled code */
{
JERRY_ASSERT (bytecode_header_p != NULL);
JERRY_ASSERT (bytecode_header_p->status_flags & CBC_CODE_FLAGS_HAS_EXTENDED_INFO);
ecma_value_t *base_p = ecma_compiled_code_resolve_function_name (bytecode_header_p);
return base_p[-1];
} /* ecma_compiled_code_resolve_extended_info */
/**
* Get the tagged template collection of the compiled code
*
@@ -1755,9 +1750,7 @@ ecma_compiled_code_get_tagged_template_collection (const ecma_compiled_code_t *b
JERRY_ASSERT (bytecode_header_p->status_flags & CBC_CODE_FLAGS_HAS_TAGGED_LITERALS);
ecma_value_t *base_p = ecma_compiled_code_resolve_function_name (bytecode_header_p);
int offset = (bytecode_header_p->status_flags & CBC_CODE_FLAGS_HAS_EXTENDED_INFO) ? -2 : -1;
return ECMA_GET_INTERNAL_VALUE_POINTER (ecma_collection_t, base_p[offset]);
return ECMA_GET_INTERNAL_VALUE_POINTER (ecma_collection_t, base_p[-1]);
} /* ecma_compiled_code_get_tagged_template_collection */
#endif /* JERRY_ESNEXT */
@@ -1783,11 +1776,6 @@ ecma_compiled_code_get_line_info (const ecma_compiled_code_t *bytecode_header_p)
base_p--;
}
if (bytecode_header_p->status_flags & CBC_CODE_FLAGS_HAS_EXTENDED_INFO)
{
base_p--;
}
if (bytecode_header_p->status_flags & CBC_CODE_FLAGS_HAS_TAGGED_LITERALS)
{
base_p--;
-1
View File
@@ -539,7 +539,6 @@ const ecma_compiled_code_t *ecma_bytecode_get_from_value (ecma_value_t value);
ecma_value_t *ecma_compiled_code_resolve_arguments_start (const ecma_compiled_code_t *bytecode_header_p);
#if JERRY_ESNEXT
ecma_value_t *ecma_compiled_code_resolve_function_name (const ecma_compiled_code_t *bytecode_header_p);
uint32_t ecma_compiled_code_resolve_extended_info (const ecma_compiled_code_t *bytecode_header_p);
ecma_collection_t *ecma_compiled_code_get_tagged_template_collection (const ecma_compiled_code_t *bytecode_header_p);
#endif /* JERRY_ESNEXT */
#if JERRY_LINE_INFO
@@ -18,6 +18,7 @@
#include "ecma-builtins.h"
#include "ecma-conversion.h"
#include "ecma-exceptions.h"
#include "ecma-extended-info.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
@@ -79,9 +80,116 @@ enum
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_function_prototype_object_to_string (void)
ecma_builtin_function_prototype_object_to_string (ecma_object_t *func_obj_p) /**< this argument object */
{
return ecma_make_magic_string_value (LIT_MAGIC_STRING__FUNCTION_TO_STRING);
if (ecma_get_object_type (func_obj_p) != ECMA_OBJECT_TYPE_FUNCTION)
{
return ecma_make_magic_string_value (LIT_MAGIC_STRING_FUNCTION_TO_STRING_NATIVE);
}
#if JERRY_FUNCTION_TO_STRING
const ecma_compiled_code_t *bytecode_p;
bytecode_p = ecma_op_function_get_compiled_code ((ecma_extended_object_t *) func_obj_p);
ecma_value_t script_value = ((cbc_uint8_arguments_t *) bytecode_p)->script_value;
cbc_script_t *script_p = ECMA_GET_INTERNAL_VALUE_POINTER (cbc_script_t, script_value);
if (bytecode_p->status_flags & CBC_CODE_FLAGS_HAS_EXTENDED_INFO)
{
uint8_t *extended_info_p = ecma_compiled_code_resolve_extended_info (bytecode_p);
uint8_t extended_info = *extended_info_p;
if (extended_info & CBC_EXTENDED_CODE_FLAGS_HAS_SOURCE_CODE_RANGE)
{
#if JERRY_ESNEXT
if (extended_info & CBC_EXTENDED_CODE_FLAGS_HAS_ARGUMENT_LENGTH)
{
ecma_extended_info_decode_vlq (&extended_info_p);
}
#endif /* JERRY_ESNEXT */
uint32_t range_start = ecma_extended_info_decode_vlq (&extended_info_p);
uint32_t range_size = ecma_extended_info_decode_vlq (&extended_info_p);
ecma_value_t source_code;
if (!(extended_info & CBC_EXTENDED_CODE_FLAGS_SOURCE_CODE_IN_ARGUMENTS))
{
source_code = script_p->source_code;
#if JERRY_SNAPSHOT_EXEC
if (ecma_is_value_magic_string (source_code, LIT_MAGIC_STRING__EMPTY))
{
return ecma_make_magic_string_value (LIT_MAGIC_STRING_FUNCTION_TO_STRING_ECMA);
}
#endif /* JERRY_SNAPSHOT_EXEC */
}
else
{
#if JERRY_SNAPSHOT_EXEC
if (!(script_p->refs_and_type & CBC_SCRIPT_HAS_FUNCTION_ARGUMENTS))
{
return ecma_make_magic_string_value (LIT_MAGIC_STRING_FUNCTION_TO_STRING_ECMA);
}
#else /* !JERRY_SNAPSHOT_EXEC */
JERRY_ASSERT (script_p->refs_and_type & CBC_SCRIPT_HAS_FUNCTION_ARGUMENTS);
#endif /* JERRY_SNAPSHOT_EXEC */
source_code = CBC_SCRIPT_GET_FUNCTION_ARGUMENTS (script_p, CBC_SCRIPT_GET_TYPE (script_p));
}
ecma_string_t *result_string_p;
ECMA_STRING_TO_UTF8_STRING (ecma_get_string_from_value (source_code), source_p, source_size);
result_string_p = ecma_new_ecma_string_from_utf8 (source_p + range_start, range_size);
ECMA_FINALIZE_UTF8_STRING (source_p, source_size);
return ecma_make_string_value (result_string_p);
}
}
#if JERRY_SNAPSHOT_EXEC
if (!(script_p->refs_and_type & CBC_SCRIPT_HAS_FUNCTION_ARGUMENTS))
{
return ecma_make_magic_string_value (LIT_MAGIC_STRING_FUNCTION_TO_STRING_ECMA);
}
#else /* !JERRY_SNAPSHOT_EXEC */
JERRY_ASSERT (script_p->refs_and_type & CBC_SCRIPT_HAS_FUNCTION_ARGUMENTS);
#endif /* JERRY_SNAPSHOT_EXEC */
lit_magic_string_id_t header_id = LIT_MAGIC_STRING_FUNCTION_TO_STRING_ANON;
#if JERRY_ESNEXT
switch (CBC_FUNCTION_GET_TYPE (bytecode_p->status_flags))
{
case CBC_FUNCTION_GENERATOR:
{
header_id = LIT_MAGIC_STRING_FUNCTION_TO_STRING_ANON_GENERATOR;
break;
}
case CBC_FUNCTION_ASYNC_GENERATOR:
{
header_id = LIT_MAGIC_STRING_FUNCTION_TO_STRING_ANON_ASYNC_GENERATOR;
break;
}
case CBC_FUNCTION_ASYNC:
{
header_id = LIT_MAGIC_STRING_FUNCTION_TO_STRING_ANON_ASYNC;
break;
}
}
#endif /* JERRY_ESNEXT */
ecma_stringbuilder_t builder = ecma_stringbuilder_create_from (ecma_get_magic_string (header_id));
ecma_value_t function_arguments = CBC_SCRIPT_GET_FUNCTION_ARGUMENTS (script_p, CBC_SCRIPT_GET_TYPE (script_p));
ecma_stringbuilder_append (&builder, ecma_get_string_from_value (function_arguments));
ecma_stringbuilder_append_raw (&builder, (const lit_utf8_byte_t *) "\n) {\n", 5);
ecma_stringbuilder_append (&builder, ecma_get_string_from_value (script_p->source_code));
ecma_stringbuilder_append_raw (&builder, (const lit_utf8_byte_t *) "\n}", 2);
return ecma_make_string_value (ecma_stringbuilder_finalize (&builder));
#else /* !JERRY_FUNCTION_TO_STRING */
return ecma_make_magic_string_value (LIT_MAGIC_STRING_FUNCTION_TO_STRING_ECMA);
#endif /* JERRY_FUNCTION_TO_STRING */
} /* ecma_builtin_function_prototype_object_to_string */
/**
@@ -440,7 +548,7 @@ ecma_builtin_function_prototype_dispatch_routine (uint8_t builtin_routine_id, /*
{
case ECMA_FUNCTION_PROTOTYPE_TO_STRING:
{
return ecma_builtin_function_prototype_object_to_string ();
return ecma_builtin_function_prototype_object_to_string (func_obj_p);
}
case ECMA_FUNCTION_PROTOTYPE_APPLY:
{
@@ -284,7 +284,7 @@ ecma_builtin_global_object_decode_uri_helper (lit_utf8_byte_t *input_start_p, /*
}
if (!is_valid
|| !lit_is_valid_utf8_string (octets, bytes_count))
|| !lit_is_valid_utf8_string (octets, bytes_count, true))
{
ecma_stringbuilder_destroy (&builder);
return ecma_raise_uri_error (ECMA_ERR_MSG ("Invalid UTF8 string"));
@@ -17,6 +17,7 @@
#include "ecma-builtin-helpers.h"
#include "ecma-builtin-handlers.h"
#include "ecma-exceptions.h"
#include "ecma-extended-info.h"
#include "ecma-function-object.h"
#include "ecma-gc.h"
#include "ecma-helpers.h"
@@ -1739,11 +1740,7 @@ ecma_op_function_try_to_lazy_instantiate_property (ecma_object_t *object_p, /**<
const ecma_compiled_code_t *bytecode_data_p = ecma_op_function_get_compiled_code (ext_func_p);
uint32_t len;
if (bytecode_data_p->status_flags & CBC_CODE_FLAGS_HAS_EXTENDED_INFO)
{
len = CBC_EXTENDED_INFO_GET_LENGTH (ecma_compiled_code_resolve_extended_info (bytecode_data_p));
}
else if (bytecode_data_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS)
if (bytecode_data_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS)
{
cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) bytecode_data_p;
len = args_p->argument_end;
@@ -1754,6 +1751,16 @@ ecma_op_function_try_to_lazy_instantiate_property (ecma_object_t *object_p, /**<
len = args_p->argument_end;
}
if (bytecode_data_p->status_flags & CBC_CODE_FLAGS_HAS_EXTENDED_INFO)
{
uint8_t *extended_info_p = ecma_compiled_code_resolve_extended_info (bytecode_data_p);
if (*extended_info_p & CBC_EXTENDED_CODE_FLAGS_HAS_ARGUMENT_LENGTH)
{
len = ecma_extended_info_decode_vlq (&extended_info_p);
}
}
/* Set tag bit to represent initialized 'length' property */
ECMA_SET_FIRST_BIT_TO_POINTER_TAG (ext_func_p->u.function.scope_cp);
ecma_property_t *value_prop_p;