Files
jerryscript/jerry-core/ecma/operations/ecma-eval.c
T
Akos Kiss 25b0750756 Fix problems arising from incorrect use of various size types
E.g.,
* `ssize_t` was used where `lit_utf8_size_t` or `jerry_api_size_t`
  would have been correct,
* `lit_utf8_size_t` was used where `ecma_length_t` would have been
  correct.

Note, the patch also includes internal and public API changes:
* `ecma_string_to_utf8_string` does not return negative value if
   output buffer is not large enough to contain the string; the
   buffer is expected to be large enough. (`ecma_string_get_size`
   can be used to retrieve the required size.)
* `jerry_api_string_to_char_buffer` adapts the same logic (and
  `jerry_api_get_string_size` can be used to determine the
  required size of the buffer).

Related issue: #942

JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
2016-03-08 15:22:07 +01:00

124 lines
3.6 KiB
C

/* Copyright 2015-2016 Samsung Electronics Co., Ltd.
*
* 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 "ecma-builtins.h"
#include "ecma-exceptions.h"
#include "ecma-eval.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-lex-env.h"
#include "js-parser.h"
#include "vm.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup eval eval
*/
/**
* Perform 'eval' with code stored in ecma-string
*
* See also:
* ecma_op_eval_chars_buffer
* ECMA-262 v5, 15.1.2.1 (steps 2 to 8)
*
* @return ecma value
*/
ecma_value_t
ecma_op_eval (ecma_string_t *code_p, /**< code string */
bool is_direct, /**< is eval called directly (ECMA-262 v5, 15.1.2.1.1) */
bool is_called_from_strict_mode_code) /**< is eval is called from strict mode code */
{
ecma_value_t ret_value;
lit_utf8_size_t chars_num = ecma_string_get_size (code_p);
if (chars_num == 0)
{
ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
}
else
{
MEM_DEFINE_LOCAL_ARRAY (code_utf8_buffer_p,
chars_num,
lit_utf8_byte_t);
lit_utf8_size_t buffer_size_req = ecma_string_to_utf8_string (code_p,
code_utf8_buffer_p,
chars_num);
JERRY_ASSERT (buffer_size_req == chars_num);
ret_value = ecma_op_eval_chars_buffer ((jerry_api_char_t *) code_utf8_buffer_p,
chars_num,
is_direct,
is_called_from_strict_mode_code);
MEM_FINALIZE_LOCAL_ARRAY (code_utf8_buffer_p);
}
return ret_value;
} /* ecma_op_eval */
/**
* Perform 'eval' with code stored in continuous character buffer
*
* See also:
* ecma_op_eval
* ECMA-262 v5, 15.1.2.1 (steps 2 to 8)
*
* @return ecma value
*/
ecma_value_t
ecma_op_eval_chars_buffer (const jerry_api_char_t *code_p, /**< code characters buffer */
size_t code_buffer_size, /**< size of the buffer */
bool is_direct, /**< is eval called directly (ECMA-262 v5, 15.1.2.1.1) */
bool is_called_from_strict_mode_code) /**< is eval is called from strict mode code */
{
JERRY_ASSERT (code_p != NULL);
ecma_value_t ret_value;
ecma_compiled_code_t *bytecode_data_p;
jsp_status_t parse_status;
bool is_strict_call = (is_direct && is_called_from_strict_mode_code);
jerry_api_object_t *error_obj_p = NULL;
parse_status = parser_parse_eval (code_p,
code_buffer_size,
is_strict_call,
&bytecode_data_p,
&error_obj_p);
if (parse_status == JSP_STATUS_OK)
{
ret_value = vm_run_eval (bytecode_data_p, is_direct);
}
else
{
JERRY_ASSERT (parse_status == JSP_STATUS_SYNTAX_ERROR);
ret_value = ecma_make_error_obj_value (error_obj_p);
}
return ret_value;
} /* ecma_op_eval_chars_buffer */
/**
* @}
* @}
*/