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 "jerryscript-port.h"
17#include "jerryscript-port-default.h"
18
19#if (JERRY_EXTERNAL_CONTEXT == 1)
20
21extern jerry_context_t *jerry_dynamic_global_context_p;
22
23#if defined (JERRY_FOR_IAR_CONFIG)
24
25#include "generate-bytecode.h"
26#include "los_task.h"
27
28/**
29 * use dynamic size array to record the correspondence between task id and jerry-heap/context
30 */
31extern ContextRecord* g_contextRecords;
32
33/**
34 * obtain the task ID with the highest priority in the task readiness queue
35 */
36extern UINT32 LOS_NextTaskIDGet(VOID);
37
38void jerry_switch_context();
39
40/**
41 * set context function: store task id and context
42 */
43void
44jerry_port_default_set_current_context (jerry_context_t *context_p) /**< store created context */
45{
46  uint32_t curTaskId = LOS_CurTaskIDGet();
47  g_contextRecords[curTaskId].context_p = context_p;
48  jerry_dynamic_global_context_p = context_p;
49}
50
51void jerry_switch_context()
52{
53  jerry_dynamic_global_context_p = g_contextRecords[LOS_NextTaskIDGet()].context_p;
54}
55
56/**
57 * when task ends, the context_pointer point to NULL
58 */
59void
60jerry_port_default_remove_current_context_record () /**< remove current task's context record in Array */
61{
62  uint32_t curTaskId = LOS_CurTaskIDGet();
63  g_contextRecords[curTaskId].context_p = NULL;
64  jerry_dynamic_global_context_p = NULL;
65}
66
67/**
68 * key: global dynamic context_p for current context
69 */
70jerry_context_t *
71jerry_port_get_current_context (void) /**< points to current task's context */
72{
73  return jerry_dynamic_global_context_p;
74}
75
76#else // not defined JERRY_FOR_IAR_CONFIG, but enabled JERRY_EXTERNAL_CONTEXT
77
78/**
79 * Set the current_context_p as the passed pointer.
80 */
81void
82jerry_port_default_set_current_context (jerry_context_t *context_p) /**< points to the created context */
83{
84  jerry_dynamic_global_context_p = context_p;
85} /* jerry_port_default_set_current_context */
86
87/**
88 * Get the current context.
89 *
90 * @return the pointer to the current context
91 */
92jerry_context_t *
93jerry_port_get_current_context (void)
94{
95  return jerry_dynamic_global_context_p;
96} /* jerry_port_get_current_context */
97
98#endif // defined (JERRY_FOR_IAR_CONFIG)
99
100#else // (JERRY_EXTERNAL_CONTEXT == 0)
101
102static jerry_context_t *current_context_p = NULL;
103
104/**
105 * Set the current_context_p as the passed pointer.
106 */
107void
108jerry_port_default_set_current_context (jerry_context_t *context_p) /**< points to the created context */
109{
110  current_context_p = context_p;
111} /* jerry_port_default_set_current_context */
112
113/**
114 * Get the current context.
115 *
116 * @return the pointer to the current context
117 */
118jerry_context_t *
119jerry_port_get_current_context (void)
120{
121  return current_context_p;
122} /* jerry_port_get_current_context */
123
124#endif  // (JERRY_EXTERNAL_CONTEXT == 0)
125