|
| 1 | +/* Copyright JS Foundation and other contributors, http://js.foundation |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +#include "byte-code.h" |
| 17 | +#include "ecma-helpers.h" |
| 18 | +#include "ecma-extended-info.h" |
| 19 | + |
| 20 | +#if JERRY_ESNEXT || JERRY_FUNCTION_TO_STRING |
| 21 | + |
| 22 | +/** \addtogroup ecma ECMA |
| 23 | + * @{ |
| 24 | + * |
| 25 | + * \addtogroup ecmaextendedinfo Extended info |
| 26 | + * @{ |
| 27 | + */ |
| 28 | + |
| 29 | +/** |
| 30 | + * Decodes an uint32_t number, and updates the buffer position. |
| 31 | + * |
| 32 | + * @return the decoded value |
| 33 | + */ |
| 34 | +uint32_t |
| 35 | +ecma_extended_info_decode_vlq (uint8_t **buffer_p) /**< [in/out] target buffer */ |
| 36 | +{ |
| 37 | + uint8_t *source_p = *buffer_p; |
| 38 | + uint32_t value = 0; |
| 39 | + |
| 40 | + do |
| 41 | + { |
| 42 | + source_p--; |
| 43 | + value = (value << ECMA_EXTENDED_INFO_VLQ_SHIFT) | (*source_p & ECMA_EXTENDED_INFO_VLQ_MASK); |
| 44 | + } |
| 45 | + while (*source_p & ECMA_EXTENDED_INFO_VLQ_CONTINUE); |
| 46 | + |
| 47 | + *buffer_p = source_p; |
| 48 | + return value; |
| 49 | +} /* ecma_extended_info_decode_vlq */ |
| 50 | + |
| 51 | +/** |
| 52 | + * Encodes an uint32_t number into a buffer. |
| 53 | + */ |
| 54 | +void |
| 55 | +ecma_extended_info_encode_vlq (uint8_t **buffer_p, /**< target buffer */ |
| 56 | + uint32_t value) /**< encoded value */ |
| 57 | +{ |
| 58 | + uint8_t *destination_p = *buffer_p - 1; |
| 59 | + |
| 60 | + if (value <= ECMA_EXTENDED_INFO_VLQ_MASK) |
| 61 | + { |
| 62 | + *destination_p = (uint8_t) value; |
| 63 | + *buffer_p = destination_p; |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + uint32_t length = 0; |
| 68 | + uint32_t current_value = value >> ECMA_EXTENDED_INFO_VLQ_SHIFT; |
| 69 | + |
| 70 | + do |
| 71 | + { |
| 72 | + current_value >>= ECMA_EXTENDED_INFO_VLQ_SHIFT; |
| 73 | + length++; |
| 74 | + } |
| 75 | + while (current_value > 0); |
| 76 | + |
| 77 | + destination_p -= length; |
| 78 | + *buffer_p = destination_p; |
| 79 | + |
| 80 | + do |
| 81 | + { |
| 82 | + *destination_p++ = (uint8_t) (value | ECMA_EXTENDED_INFO_VLQ_CONTINUE); |
| 83 | + value >>= ECMA_EXTENDED_INFO_VLQ_SHIFT; |
| 84 | + } |
| 85 | + while (value > 0); |
| 86 | + |
| 87 | + **buffer_p &= ECMA_EXTENDED_INFO_VLQ_MASK; |
| 88 | +} /* ecma_extended_info_encode_vlq */ |
| 89 | + |
| 90 | +/** |
| 91 | + * Gets the encoded length of a number. |
| 92 | + * |
| 93 | + * @return encoded length |
| 94 | + */ |
| 95 | +uint32_t |
| 96 | +ecma_extended_info_get_encoded_length (uint32_t value) /**< encoded value */ |
| 97 | +{ |
| 98 | + uint32_t length = 0; |
| 99 | + |
| 100 | + do |
| 101 | + { |
| 102 | + value >>= ECMA_EXTENDED_INFO_VLQ_SHIFT; |
| 103 | + length++; |
| 104 | + } |
| 105 | + while (value > 0); |
| 106 | + |
| 107 | + return length; |
| 108 | +} /* ecma_extended_info_get_encoded_length */ |
| 109 | + |
| 110 | +/** |
| 111 | + * Get the extended info from a byte code |
| 112 | + * |
| 113 | + * @return pointer to the extended info |
| 114 | + */ |
| 115 | +uint8_t * |
| 116 | +ecma_compiled_code_resolve_extended_info (const ecma_compiled_code_t *bytecode_header_p) /**< compiled code */ |
| 117 | +{ |
| 118 | + JERRY_ASSERT (bytecode_header_p != NULL); |
| 119 | + JERRY_ASSERT (bytecode_header_p->status_flags & CBC_CODE_FLAGS_HAS_EXTENDED_INFO); |
| 120 | + |
| 121 | + ecma_value_t *base_p = ecma_compiled_code_resolve_arguments_start (bytecode_header_p); |
| 122 | + |
| 123 | +#if JERRY_ESNEXT |
| 124 | + if (CBC_FUNCTION_GET_TYPE (bytecode_header_p->status_flags) != CBC_FUNCTION_CONSTRUCTOR) |
| 125 | + { |
| 126 | + base_p--; |
| 127 | + } |
| 128 | + |
| 129 | + if (bytecode_header_p->status_flags & CBC_CODE_FLAGS_HAS_TAGGED_LITERALS) |
| 130 | + { |
| 131 | + base_p--; |
| 132 | + } |
| 133 | +#endif /* JERRY_ESNEXT */ |
| 134 | + |
| 135 | +#if JERRY_LINE_INFO |
| 136 | + if (bytecode_header_p->status_flags & CBC_CODE_FLAGS_HAS_LINE_INFO) |
| 137 | + { |
| 138 | + base_p--; |
| 139 | + } |
| 140 | +#endif /* JERRY_LINE_INFO */ |
| 141 | + |
| 142 | + JERRY_ASSERT (((uint8_t *) base_p)[-1] != 0); |
| 143 | + |
| 144 | + return ((uint8_t *) base_p) - 1; |
| 145 | +} /* ecma_compiled_code_resolve_extended_info */ |
| 146 | + |
| 147 | +#endif /* JERRY_ESNEXT || JERRY_FUNCTION_TO_STRING */ |
| 148 | + |
| 149 | +/** |
| 150 | + * @} |
| 151 | + * @} |
| 152 | + */ |
0 commit comments