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#include "ecma-array-object.h"
17425bb815Sopenharmony_ci#include "ecma-helpers.h"
18425bb815Sopenharmony_ci#include "ecma-objects.h"
19425bb815Sopenharmony_ci#include "jcontext.h"
20425bb815Sopenharmony_ci#include "lit-char-helpers.h"
21425bb815Sopenharmony_ci#include "vm.h"
22425bb815Sopenharmony_ci
23425bb815Sopenharmony_ci/**
24425bb815Sopenharmony_ci * Check whether currently executed code is strict mode code
25425bb815Sopenharmony_ci *
26425bb815Sopenharmony_ci * @return true - current code is executed in strict mode,
27425bb815Sopenharmony_ci *         false - otherwise
28425bb815Sopenharmony_ci */
29425bb815Sopenharmony_cibool
30425bb815Sopenharmony_civm_is_strict_mode (void)
31425bb815Sopenharmony_ci{
32425bb815Sopenharmony_ci  JERRY_ASSERT (JERRY_CONTEXT (vm_top_context_p) != NULL);
33425bb815Sopenharmony_ci
34425bb815Sopenharmony_ci  return JERRY_CONTEXT (vm_top_context_p)->bytecode_header_p->status_flags & CBC_CODE_FLAGS_STRICT_MODE;
35425bb815Sopenharmony_ci} /* vm_is_strict_mode */
36425bb815Sopenharmony_ci
37425bb815Sopenharmony_ci/**
38425bb815Sopenharmony_ci * Check whether currently performed call (on top of call-stack) is performed in form,
39425bb815Sopenharmony_ci * meeting conditions of 'Direct Call to Eval' (see also: ECMA-262 v5, 15.1.2.1.1)
40425bb815Sopenharmony_ci *
41425bb815Sopenharmony_ci * Warning:
42425bb815Sopenharmony_ci *         the function should only be called from implementation
43425bb815Sopenharmony_ci *         of built-in 'eval' routine of Global object
44425bb815Sopenharmony_ci *
45425bb815Sopenharmony_ci * @return true - currently performed call is performed through 'eval' identifier,
46425bb815Sopenharmony_ci *                without 'this' argument,
47425bb815Sopenharmony_ci *         false - otherwise
48425bb815Sopenharmony_ci */
49425bb815Sopenharmony_ciinline bool JERRY_ATTR_ALWAYS_INLINE
50425bb815Sopenharmony_civm_is_direct_eval_form_call (void)
51425bb815Sopenharmony_ci{
52425bb815Sopenharmony_ci  return (JERRY_CONTEXT (status_flags) & ECMA_STATUS_DIRECT_EVAL) != 0;
53425bb815Sopenharmony_ci} /* vm_is_direct_eval_form_call */
54425bb815Sopenharmony_ci
55425bb815Sopenharmony_ci#if defined(JERRY_FUNCTION_BACKTRACE) && !defined(__APPLE__)
56425bb815Sopenharmony_cistatic ecma_string_t*
57425bb815Sopenharmony_civm_get_function_name_string (vm_frame_ctx_t *context_p)
58425bb815Sopenharmony_ci{
59425bb815Sopenharmony_ci  ecma_stringbuilder_t func_name_builder;
60425bb815Sopenharmony_ci  if (context_p->prev_context_p != NULL) {
61425bb815Sopenharmony_ci    vm_frame_ctx_t* prev_ctx_p = context_p->prev_context_p;
62425bb815Sopenharmony_ci    ecma_object_t* func_obj = ecma_get_object_from_value (prev_ctx_p->callee_value);
63425bb815Sopenharmony_ci    ecma_object_type_t func_obj_type = ecma_get_object_type (func_obj);
64425bb815Sopenharmony_ci    if (func_obj_type == ECMA_OBJECT_TYPE_FUNCTION
65425bb815Sopenharmony_ci	|| func_obj_type == ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION
66425bb815Sopenharmony_ci	|| func_obj_type == ECMA_OBJECT_TYPE_BOUND_FUNCTION) {
67425bb815Sopenharmony_ci      ecma_string_t* name_prop = ecma_get_magic_string (LIT_MAGIC_STRING_NAME);
68425bb815Sopenharmony_ci      ecma_value_t func_name_value = ecma_op_object_get (func_obj, name_prop);
69425bb815Sopenharmony_ci      if (func_name_value == ECMA_VALUE_UNDEFINED) {
70425bb815Sopenharmony_ci        func_name_builder = ecma_stringbuilder_create_raw ((lit_utf8_byte_t *) ("<unknown>"), 9);
71425bb815Sopenharmony_ci      } else {
72425bb815Sopenharmony_ci        func_name_builder = ecma_stringbuilder_create_from(ecma_get_string_from_value (func_name_value));
73425bb815Sopenharmony_ci      }
74425bb815Sopenharmony_ci      ecma_deref_ecma_string (name_prop);
75425bb815Sopenharmony_ci    } else {
76425bb815Sopenharmony_ci      func_name_builder = ecma_stringbuilder_create_raw ((lit_utf8_byte_t *) ("<erroneous>"), 11);
77425bb815Sopenharmony_ci    }
78425bb815Sopenharmony_ci  } else {
79425bb815Sopenharmony_ci    func_name_builder = ecma_stringbuilder_create_raw ((lit_utf8_byte_t *) ("<GLOBAL>"), 8);
80425bb815Sopenharmony_ci  }
81425bb815Sopenharmony_ci  ecma_string_t* lbracket = ecma_new_ecma_string_from_utf8((const lit_utf8_byte_t *) ("("), 1);
82425bb815Sopenharmony_ci  ecma_string_t* rbracket = ecma_new_ecma_string_from_utf8((const lit_utf8_byte_t *) (")"), 1);
83425bb815Sopenharmony_ci  ecma_stringbuilder_append (&func_name_builder, lbracket);
84425bb815Sopenharmony_ci  ecma_stringbuilder_append (&func_name_builder, rbracket);
85425bb815Sopenharmony_ci  ecma_stringbuilder_append_byte (&func_name_builder, LIT_CHAR_COMMA);
86425bb815Sopenharmony_ci  ecma_stringbuilder_append_byte (&func_name_builder, LIT_CHAR_SP);
87425bb815Sopenharmony_ci  ecma_deref_ecma_string(rbracket);
88425bb815Sopenharmony_ci  ecma_deref_ecma_string(lbracket);
89425bb815Sopenharmony_ci  return ecma_stringbuilder_finalize (&func_name_builder);
90425bb815Sopenharmony_ci}
91425bb815Sopenharmony_ci#endif
92425bb815Sopenharmony_ci
93425bb815Sopenharmony_ci/**
94425bb815Sopenharmony_ci * Get backtrace. The backtrace is an array of strings where
95425bb815Sopenharmony_ci * each string contains the position of the corresponding frame.
96425bb815Sopenharmony_ci * The array length is zero if the backtrace is not available.
97425bb815Sopenharmony_ci *
98425bb815Sopenharmony_ci * @return array ecma value
99425bb815Sopenharmony_ci */
100425bb815Sopenharmony_ciecma_value_t
101425bb815Sopenharmony_civm_get_backtrace (uint32_t max_depth) /**< maximum backtrace depth, 0 = unlimited */
102425bb815Sopenharmony_ci{
103425bb815Sopenharmony_ci#if ENABLED (JERRY_LINE_INFO)
104425bb815Sopenharmony_ci  ecma_value_t result_array = ecma_op_create_array_object (NULL, 0, false);
105425bb815Sopenharmony_ci
106425bb815Sopenharmony_ci  if (max_depth == 0)
107425bb815Sopenharmony_ci  {
108425bb815Sopenharmony_ci    max_depth = UINT32_MAX;
109425bb815Sopenharmony_ci  }
110425bb815Sopenharmony_ci
111425bb815Sopenharmony_ci  vm_frame_ctx_t *context_p = JERRY_CONTEXT (vm_top_context_p);
112425bb815Sopenharmony_ci  ecma_object_t *array_p = ecma_get_object_from_value (result_array);
113425bb815Sopenharmony_ci  JERRY_ASSERT (ecma_op_object_is_fast_array (array_p));
114425bb815Sopenharmony_ci  uint32_t index = 0;
115425bb815Sopenharmony_ci
116425bb815Sopenharmony_ci  while (context_p != NULL)
117425bb815Sopenharmony_ci  {
118425bb815Sopenharmony_ci    ecma_string_t *str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY);
119425bb815Sopenharmony_ci
120425bb815Sopenharmony_ci#if defined(JERRY_FUNCTION_BACKTRACE) && !defined(__APPLE__)
121425bb815Sopenharmony_ci    ecma_string_t* func_name = vm_get_function_name_string (context_p);
122425bb815Sopenharmony_ci    func_name = ecma_concat_ecma_strings (func_name, str_p);
123425bb815Sopenharmony_ci
124425bb815Sopenharmony_ci    ecma_fast_array_set_property (array_p, index, ecma_make_string_value (func_name));
125425bb815Sopenharmony_ci    ecma_deref_ecma_string(str_p);
126425bb815Sopenharmony_ci    ecma_deref_ecma_string(func_name);
127425bb815Sopenharmony_ci#else
128425bb815Sopenharmony_ci    ecma_fast_array_set_property (array_p, index, ecma_make_string_value (str_p));
129425bb815Sopenharmony_ci    ecma_deref_ecma_string (str_p);
130425bb815Sopenharmony_ci#endif
131425bb815Sopenharmony_ci
132425bb815Sopenharmony_ci    context_p = context_p->prev_context_p;
133425bb815Sopenharmony_ci    index++;
134425bb815Sopenharmony_ci
135425bb815Sopenharmony_ci    if (index >= max_depth)
136425bb815Sopenharmony_ci    {
137425bb815Sopenharmony_ci      break;
138425bb815Sopenharmony_ci    }
139425bb815Sopenharmony_ci  }
140425bb815Sopenharmony_ci
141425bb815Sopenharmony_ci  return result_array;
142425bb815Sopenharmony_ci#else /* !ENABLED (JERRY_LINE_INFO) */
143425bb815Sopenharmony_ci  JERRY_UNUSED (max_depth);
144425bb815Sopenharmony_ci
145425bb815Sopenharmony_ci  return ecma_op_create_array_object (NULL, 0, false);
146425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_LINE_INFO) */
147425bb815Sopenharmony_ci} /* vm_get_backtrace */
148