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