• 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 #include "target_config.h"
28 
29 /**
30  * use array to record the correspondence between task id and jerry-heap/context
31  */
32 #define MAX_CONTEXT_NUM (LOSCFG_BASE_CORE_TSK_LIMIT+1)
33 ContextRecord g_contextRecords[MAX_CONTEXT_NUM] = {0};
34 
35 void jerry_switch_context();
36 
37 /**
38  * set context function: store task id and context
39  */
40 void
jerry_port_default_set_current_context(jerry_context_t * context_p)41 jerry_port_default_set_current_context (jerry_context_t *context_p) /**< store created context */
42 {
43   uint32_t curTaskId = LOS_CurTaskIDGet();
44   g_contextRecords[curTaskId].context_p = context_p;
45   jerry_dynamic_global_context_p = context_p;
46 }
47 
jerry_switch_context()48 void jerry_switch_context()
49 {
50   jerry_dynamic_global_context_p = g_contextRecords[LOS_NextTaskIDGet()].context_p;
51 }
52 
53 /**
54  * when task ends, the context_pointer point to NULL
55  */
56 void
jerry_port_default_remove_current_context_record()57 jerry_port_default_remove_current_context_record () /**< remove current task's context record in Array */
58 {
59   uint32_t curTaskId = LOS_CurTaskIDGet();
60   g_contextRecords[curTaskId].context_p = NULL;
61   jerry_dynamic_global_context_p = NULL;
62 }
63 
64 /**
65  * key: global dynamic context_p for current context
66  */
67 jerry_context_t *
jerry_port_get_current_context(void)68 jerry_port_get_current_context (void) /**< points to current task's context */
69 {
70   return jerry_dynamic_global_context_p;
71 }
72 
73 #else // not defined JERRY_FOR_IAR_CONFIG, but enabled JERRY_EXTERNAL_CONTEXT
74 
75 /**
76  * Set the current_context_p as the passed pointer.
77  */
78 void
jerry_port_default_set_current_context(jerry_context_t * context_p)79 jerry_port_default_set_current_context (jerry_context_t *context_p) /**< points to the created context */
80 {
81   jerry_dynamic_global_context_p = context_p;
82 } /* jerry_port_default_set_current_context */
83 
84 /**
85  * Get the current context.
86  *
87  * @return the pointer to the current context
88  */
89 jerry_context_t *
jerry_port_get_current_context(void)90 jerry_port_get_current_context (void)
91 {
92   return jerry_dynamic_global_context_p;
93 } /* jerry_port_get_current_context */
94 
95 #endif // defined (JERRY_FOR_IAR_CONFIG)
96 
97 #endif // (JERRY_EXTERNAL_CONTEXT == 1)
98