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#ifndef ECMA_TRY_CATCH_MACRO_H
17#define ECMA_TRY_CATCH_MACRO_H
18
19#include "ecma-helpers.h"
20
21/**
22 * The macro defines try-block that initializes variable 'var' with 'op'
23 * and checks for exceptions that might be thrown during initialization.
24 *
25 * If no exception was thrown, then code after the try-block is executed.
26 * Otherwise, throw-completion value is just copied to return_value.
27 *
28 * Note:
29 *      Each ECMA_TRY_CATCH should have it's own corresponding ECMA_FINALIZE
30 *      statement with same argument as corresponding ECMA_TRY_CATCH's first argument.
31 */
32#define ECMA_TRY_CATCH(var, op, return_value) \
33  JERRY_ASSERT (return_value == ECMA_VALUE_EMPTY); \
34  ecma_value_t var ## _completion = op; \
35  if (ECMA_IS_VALUE_ERROR (var ## _completion)) \
36  { \
37    return_value = var ## _completion; \
38  } \
39  else \
40  { \
41    ecma_value_t var = var ## _completion; \
42    JERRY_UNUSED (var);
43
44/**
45 * The macro marks end of code block that is defined by corresponding
46 * ECMA_TRY_CATCH and frees variable, initialized by the ECMA_TRY_CATCH.
47 *
48 * Note:
49 *      Each ECMA_TRY_CATCH should be followed by ECMA_FINALIZE with same argument
50 *      as corresponding ECMA_TRY_CATCH's first argument.
51 */
52#define ECMA_FINALIZE(var) \
53    ecma_free_value (var ## _completion); \
54  }
55
56/**
57 * The macro defines try-block that tries to perform ToNumber operation on given value
58 * and checks for exceptions that might be thrown during the operation.
59 *
60 * If no exception was thrown, then code after the try-block is executed.
61 * Otherwise, throw-completion value is just copied to return_value.
62 *
63 * Note:
64 *      Each ECMA_OP_TO_NUMBER_TRY_CATCH should have it's own corresponding ECMA_OP_TO_NUMBER_FINALIZE
65 *      statement with same argument as corresponding ECMA_OP_TO_NUMBER_TRY_CATCH's first argument.
66 */
67#define ECMA_OP_TO_NUMBER_TRY_CATCH(num_var, value, return_value) \
68  JERRY_ASSERT (return_value == ECMA_VALUE_EMPTY); \
69  ecma_number_t num_var; \
70  return_value = ecma_get_number (value, &num_var); \
71  \
72  if (JERRY_LIKELY (ecma_is_value_empty (return_value))) \
73  {
74
75/**
76 * The macro marks end of code block that is defined by corresponding ECMA_OP_TO_NUMBER_TRY_CATCH.
77 *
78 * Note:
79 *      Each ECMA_OP_TO_NUMBER_TRY_CATCH should be followed by ECMA_OP_TO_NUMBER_FINALIZE
80 *      with same argument as corresponding ECMA_OP_TO_NUMBER_TRY_CATCH's first argument.
81 */
82#define ECMA_OP_TO_NUMBER_FINALIZE(num_var) }
83
84#endif /* !ECMA_TRY_CATCH_MACRO_H */
85