1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci *
3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci * You may obtain a copy of the License at
6425bb815Sopenharmony_ci *
7425bb815Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci *
9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci * limitations under the License.
14425bb815Sopenharmony_ci */
15425bb815Sopenharmony_ci
16425bb815Sopenharmony_ci#ifndef ECMA_TRY_CATCH_MACRO_H
17425bb815Sopenharmony_ci#define ECMA_TRY_CATCH_MACRO_H
18425bb815Sopenharmony_ci
19425bb815Sopenharmony_ci#include "ecma-helpers.h"
20425bb815Sopenharmony_ci
21425bb815Sopenharmony_ci/**
22425bb815Sopenharmony_ci * The macro defines try-block that initializes variable 'var' with 'op'
23425bb815Sopenharmony_ci * and checks for exceptions that might be thrown during initialization.
24425bb815Sopenharmony_ci *
25425bb815Sopenharmony_ci * If no exception was thrown, then code after the try-block is executed.
26425bb815Sopenharmony_ci * Otherwise, throw-completion value is just copied to return_value.
27425bb815Sopenharmony_ci *
28425bb815Sopenharmony_ci * Note:
29425bb815Sopenharmony_ci *      Each ECMA_TRY_CATCH should have it's own corresponding ECMA_FINALIZE
30425bb815Sopenharmony_ci *      statement with same argument as corresponding ECMA_TRY_CATCH's first argument.
31425bb815Sopenharmony_ci */
32425bb815Sopenharmony_ci#define ECMA_TRY_CATCH(var, op, return_value) \
33425bb815Sopenharmony_ci  JERRY_ASSERT (return_value == ECMA_VALUE_EMPTY); \
34425bb815Sopenharmony_ci  ecma_value_t var ## _completion = op; \
35425bb815Sopenharmony_ci  if (ECMA_IS_VALUE_ERROR (var ## _completion)) \
36425bb815Sopenharmony_ci  { \
37425bb815Sopenharmony_ci    return_value = var ## _completion; \
38425bb815Sopenharmony_ci  } \
39425bb815Sopenharmony_ci  else \
40425bb815Sopenharmony_ci  { \
41425bb815Sopenharmony_ci    ecma_value_t var = var ## _completion; \
42425bb815Sopenharmony_ci    JERRY_UNUSED (var);
43425bb815Sopenharmony_ci
44425bb815Sopenharmony_ci/**
45425bb815Sopenharmony_ci * The macro marks end of code block that is defined by corresponding
46425bb815Sopenharmony_ci * ECMA_TRY_CATCH and frees variable, initialized by the ECMA_TRY_CATCH.
47425bb815Sopenharmony_ci *
48425bb815Sopenharmony_ci * Note:
49425bb815Sopenharmony_ci *      Each ECMA_TRY_CATCH should be followed by ECMA_FINALIZE with same argument
50425bb815Sopenharmony_ci *      as corresponding ECMA_TRY_CATCH's first argument.
51425bb815Sopenharmony_ci */
52425bb815Sopenharmony_ci#define ECMA_FINALIZE(var) \
53425bb815Sopenharmony_ci    ecma_free_value (var ## _completion); \
54425bb815Sopenharmony_ci  }
55425bb815Sopenharmony_ci
56425bb815Sopenharmony_ci/**
57425bb815Sopenharmony_ci * The macro defines try-block that tries to perform ToNumber operation on given value
58425bb815Sopenharmony_ci * and checks for exceptions that might be thrown during the operation.
59425bb815Sopenharmony_ci *
60425bb815Sopenharmony_ci * If no exception was thrown, then code after the try-block is executed.
61425bb815Sopenharmony_ci * Otherwise, throw-completion value is just copied to return_value.
62425bb815Sopenharmony_ci *
63425bb815Sopenharmony_ci * Note:
64425bb815Sopenharmony_ci *      Each ECMA_OP_TO_NUMBER_TRY_CATCH should have it's own corresponding ECMA_OP_TO_NUMBER_FINALIZE
65425bb815Sopenharmony_ci *      statement with same argument as corresponding ECMA_OP_TO_NUMBER_TRY_CATCH's first argument.
66425bb815Sopenharmony_ci */
67425bb815Sopenharmony_ci#define ECMA_OP_TO_NUMBER_TRY_CATCH(num_var, value, return_value) \
68425bb815Sopenharmony_ci  JERRY_ASSERT (return_value == ECMA_VALUE_EMPTY); \
69425bb815Sopenharmony_ci  ecma_number_t num_var; \
70425bb815Sopenharmony_ci  return_value = ecma_get_number (value, &num_var); \
71425bb815Sopenharmony_ci  \
72425bb815Sopenharmony_ci  if (JERRY_LIKELY (ecma_is_value_empty (return_value))) \
73425bb815Sopenharmony_ci  {
74425bb815Sopenharmony_ci
75425bb815Sopenharmony_ci/**
76425bb815Sopenharmony_ci * The macro marks end of code block that is defined by corresponding ECMA_OP_TO_NUMBER_TRY_CATCH.
77425bb815Sopenharmony_ci *
78425bb815Sopenharmony_ci * Note:
79425bb815Sopenharmony_ci *      Each ECMA_OP_TO_NUMBER_TRY_CATCH should be followed by ECMA_OP_TO_NUMBER_FINALIZE
80425bb815Sopenharmony_ci *      with same argument as corresponding ECMA_OP_TO_NUMBER_TRY_CATCH's first argument.
81425bb815Sopenharmony_ci */
82425bb815Sopenharmony_ci#define ECMA_OP_TO_NUMBER_FINALIZE(num_var) }
83425bb815Sopenharmony_ci
84425bb815Sopenharmony_ci#endif /* !ECMA_TRY_CATCH_MACRO_H */
85