1/*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
3 * Description: New context and heap for JS && BMS task in JUPITER.
4 * Create: 2020/11/17
5 */
6
7#if defined (JERRY_IAR_JUPITER) && (JERRY_EXTERNAL_CONTEXT == 1)
8
9#include "jcontext.h"
10#include "config-jupiter.h"
11
12#pragma data_alignment = JMEM_ALIGNMENT
13extern uint8_t* js_context_and_heap;
14
15// heap for bms task
16extern uint8_t* bms_context_and_heap;
17
18/**
19 * The alloc function passed to jerry_create_context for bms
20 */
21static void * bms_task_context_alloc (size_t total_size, void *cb_data_p)
22{
23  (void) cb_data_p; /* unused */
24
25  if (total_size > (BMS_TASK_CONTEXT_AND_HEAP_SIZE * CONVERTION_RATIO)) {
26    /* out of setted space */
27    return NULL;
28  }
29  return bms_context_and_heap;
30} /* bms_task_context_alloc */
31
32/**
33 * Create and set the default external context for bms task
34 */
35void bms_task_context_init (void)
36{
37  jerry_context_t * bms_task_context = jerry_create_context (BMS_TASK_HEAP_SIZE * CONVERTION_RATIO,
38                                        bms_task_context_alloc, NULL);
39
40  jerry_port_default_set_current_context (bms_task_context);
41} /* bms_task_context_init */
42
43
44/**
45 * The alloc function passed to jerry_create_context for js
46 */
47static void * js_task_context_alloc (size_t total_size, void *cb_data_p)
48{
49  (void) cb_data_p; /* unused */
50
51  if (total_size > (JS_TASK_CONTEXT_AND_HEAP_SIZE_BYTE)) {
52    /* out of setted space */
53    return NULL;
54  }
55  return js_context_and_heap;
56} /* js_task_context_alloc */
57
58/**
59 * Create and set the default external context for js task., need free after task
60 */
61void js_task_context_init (void)
62{
63  jerry_context_t * js_task_context = jerry_create_context (JS_TASK_HEAP_SIZE * CONVERTION_RATIO,
64                                        js_task_context_alloc, NULL);
65
66  jerry_port_default_set_current_context (js_task_context);
67} /* js_task_context_init */
68
69/**
70 * Unified interface for Creating external context
71 */
72void
73jerry_external_context_init (uint32_t heap_size, /**< the size of heap */
74                    jerry_context_alloc_t alloc, /**< the alloc function */
75                    void *cb_data_p) /**< the cb_data for alloc function */
76{
77  jerry_context_t * external_context = jerry_create_context (heap_size, alloc, NULL);
78  jerry_port_default_set_current_context (external_context);
79}
80
81#endif // defined (JERRY_IAR_JUPITER) && (JERRY_EXTERNAL_CONTEXT == 1)
82