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 "ecma-builtins.h"
17#include "ecma-gc.h"
18#include "ecma-helpers.h"
19#include "ecma-init-finalize.h"
20#include "ecma-lex-env.h"
21#include "ecma-literal-storage.h"
22#include "jmem.h"
23#include "jcontext.h"
24
25/** \addtogroup ecma ECMA
26 * @{
27 *
28 * \addtogroup ecmainitfinalize Initialization and finalization of ECMA components
29 * @{
30 */
31
32/**
33 * Maximum number of GC loops on cleanup.
34 */
35#define JERRY_GC_LOOP_LIMIT 100
36
37/**
38 * Initialize ECMA components
39 */
40void
41ecma_init (void)
42{
43#if (JERRY_GC_MARK_LIMIT != 0)
44  JERRY_CONTEXT (ecma_gc_mark_recursion_limit) = JERRY_GC_MARK_LIMIT;
45#endif /* (JERRY_GC_MARK_LIMIT != 0) */
46
47  ecma_init_global_environment ();
48
49#if ENABLED (JERRY_PROPRETY_HASHMAP)
50  JERRY_CONTEXT (ecma_prop_hashmap_alloc_state) = ECMA_PROP_HASHMAP_ALLOC_ON;
51  JERRY_CONTEXT (status_flags) &= (uint32_t) ~ECMA_STATUS_HIGH_PRESSURE_GC;
52#endif /* ENABLED (JERRY_PROPRETY_HASHMAP) */
53
54#if (JERRY_STACK_LIMIT != 0)
55  volatile int sp;
56  JERRY_CONTEXT (stack_base) = (uintptr_t) &sp;
57#endif /* (JERRY_STACK_LIMIT != 0) */
58
59#if ENABLED (JERRY_ES2015_BUILTIN_PROMISE)
60  ecma_job_queue_init ();
61#endif /* ENABLED (JERRY_ES2015_BUILTIN_PROMISE) */
62
63#if ENABLED (JERRY_ES2015)
64  JERRY_CONTEXT (current_new_target) = NULL;
65  JERRY_CONTEXT (current_function_obj_p) = NULL;
66#endif /* ENABLED (JERRY_ES2015) */
67} /* ecma_init */
68
69/**
70 * Finalize ECMA components
71 */
72void
73ecma_finalize (void)
74{
75#if ENABLED (JERRY_ES2015)
76  JERRY_ASSERT (JERRY_CONTEXT (current_new_target) == NULL);
77  JERRY_ASSERT (JERRY_CONTEXT (current_function_obj_p) == NULL);
78#endif /* ENABLED (JERRY_ES2015) */
79
80  ecma_finalize_global_environment ();
81  uint8_t runs = 0;
82  do
83  {
84    ecma_finalize_builtins ();
85    ecma_gc_run ();
86    if (++runs >= JERRY_GC_LOOP_LIMIT)
87    {
88      jerry_fatal (ERR_UNTERMINATED_GC_LOOPS);
89    }
90  }
91  while (JERRY_CONTEXT (ecma_gc_new_objects) != 0);
92  ecma_finalize_lit_storage ();
93} /* ecma_finalize */
94
95/**
96 * @}
97 * @}
98 */
99