• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 extern 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  */
31 extern ContextRecord* g_contextRecords;
32 
33 void jerry_switch_context();
34 
35 /**
36  * set context function: store task id and context
37  */
38 void
jerry_port_default_set_current_context(jerry_context_t * context_p)39 jerry_port_default_set_current_context (jerry_context_t *context_p) /**< store created context */
40 {
41   uint32_t curTaskId = LOS_CurTaskIDGet();
42   g_contextRecords[curTaskId].context_p = context_p;
43   jerry_dynamic_global_context_p = context_p;
44 }
45 
jerry_switch_context()46 void jerry_switch_context()
47 {
48   jerry_dynamic_global_context_p = g_contextRecords[LOS_NextTaskIDGet()].context_p;
49 }
50 
51 /**
52  * when task ends, the context_pointer point to NULL
53  */
54 void
jerry_port_default_remove_current_context_record()55 jerry_port_default_remove_current_context_record () /**< remove current task's context record in Array */
56 {
57   uint32_t curTaskId = LOS_CurTaskIDGet();
58   g_contextRecords[curTaskId].context_p = NULL;
59   jerry_dynamic_global_context_p = NULL;
60 }
61 
62 /**
63  * key: global dynamic context_p for current context
64  */
65 jerry_context_t *
jerry_port_get_current_context(void)66 jerry_port_get_current_context (void) /**< points to current task's context */
67 {
68   return jerry_dynamic_global_context_p;
69 }
70 
71 #else // not defined JERRY_FOR_IAR_CONFIG, but enabled JERRY_EXTERNAL_CONTEXT
72 
73 /**
74  * Set the current_context_p as the passed pointer.
75  */
76 void
jerry_port_default_set_current_context(jerry_context_t * context_p)77 jerry_port_default_set_current_context (jerry_context_t *context_p) /**< points to the created context */
78 {
79   jerry_dynamic_global_context_p = context_p;
80 } /* jerry_port_default_set_current_context */
81 
82 /**
83  * Get the current context.
84  *
85  * @return the pointer to the current context
86  */
87 jerry_context_t *
jerry_port_get_current_context(void)88 jerry_port_get_current_context (void)
89 {
90   return jerry_dynamic_global_context_p;
91 } /* jerry_port_get_current_context */
92 
93 #endif // defined (JERRY_FOR_IAR_CONFIG)
94 
95 #else // (JERRY_EXTERNAL_CONTEXT == 0)
96 
97 static jerry_context_t *current_context_p = NULL;
98 
99 /**
100  * Set the current_context_p as the passed pointer.
101  */
102 void
jerry_port_default_set_current_context(jerry_context_t * context_p)103 jerry_port_default_set_current_context (jerry_context_t *context_p) /**< points to the created context */
104 {
105   current_context_p = context_p;
106 } /* jerry_port_default_set_current_context */
107 
108 /**
109  * Get the current context.
110  *
111  * @return the pointer to the current context
112  */
113 jerry_context_t *
jerry_port_get_current_context(void)114 jerry_port_get_current_context (void)
115 {
116   return current_context_p;
117 } /* jerry_port_get_current_context */
118 
119 #endif  // (JERRY_EXTERNAL_CONTEXT == 0)
120