|
| 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.h" |
| 17 | +#include "jerry-port.h" |
| 18 | +#include "jerry-port-default.h" |
| 19 | +#include "jmem.h" |
| 20 | +#include "jrt.h" |
| 21 | + |
| 22 | +#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN |
| 23 | + |
| 24 | +typedef struct jerry_port_queueitem_t jerry_port_queueitem_t; |
| 25 | + |
| 26 | +/** |
| 27 | + * Description of the queue item. |
| 28 | + */ |
| 29 | +struct jerry_port_queueitem_t |
| 30 | +{ |
| 31 | + jerry_port_queueitem_t *next_p; /**< points to next item */ |
| 32 | + jerry_job_handler_t handler; /**< the handler for the job*/ |
| 33 | + void *job_p; /**< points to the job */ |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * Description of a job queue (FIFO) |
| 38 | + */ |
| 39 | +typedef struct |
| 40 | +{ |
| 41 | + jerry_port_queueitem_t *head_p; /**< points to the head item of the queue */ |
| 42 | + jerry_port_queueitem_t *tail_p; /**< points to the tail item of the queue*/ |
| 43 | +} jerry_port_jobqueue_t; |
| 44 | + |
| 45 | +static jerry_port_jobqueue_t queue; |
| 46 | + |
| 47 | +/** |
| 48 | + * Initialize the job queue |
| 49 | + */ |
| 50 | +void jerry_port_jobqueue_init (void) |
| 51 | +{ |
| 52 | + queue.head_p = NULL; |
| 53 | + queue.tail_p = NULL; |
| 54 | +} /* jerry_port_jobqueue_init */ |
| 55 | + |
| 56 | +/** |
| 57 | + * Enqueue a job |
| 58 | + */ |
| 59 | +void jerry_port_jobqueue_enqueue (jerry_job_handler_t handler, /**< the handler for the job */ |
| 60 | + void *job_p) /**< the job */ |
| 61 | +{ |
| 62 | + jerry_port_queueitem_t *item_p = jmem_heap_alloc_block (sizeof (jerry_port_queueitem_t)); |
| 63 | + item_p->job_p = job_p; |
| 64 | + item_p->handler = handler; |
| 65 | + |
| 66 | + if (queue.head_p == NULL) |
| 67 | + { |
| 68 | + JERRY_ASSERT (queue.tail_p == NULL); |
| 69 | + |
| 70 | + queue.head_p = item_p; |
| 71 | + item_p->next_p = NULL; |
| 72 | + queue.tail_p = item_p; |
| 73 | + |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + JERRY_ASSERT (queue.tail_p != NULL); |
| 78 | + |
| 79 | + queue.tail_p->next_p = item_p; |
| 80 | + queue.tail_p = item_p; |
| 81 | +} /* jerry_port_jobqueue_enqueue */ |
| 82 | + |
| 83 | +/** |
| 84 | + * Dequeue and get the job. |
| 85 | + * @return pointer to jerry_port_queueitem_t |
| 86 | + * It should be freed with jmem_heap_free_block |
| 87 | + */ |
| 88 | +static jerry_port_queueitem_t * |
| 89 | +jerry_port_jobqueue_dequeue (void) |
| 90 | +{ |
| 91 | + if (queue.head_p == NULL) |
| 92 | + { |
| 93 | + JERRY_ASSERT (queue.tail_p == NULL); |
| 94 | + |
| 95 | + return NULL; |
| 96 | + } |
| 97 | + |
| 98 | + JERRY_ASSERT (queue.tail_p != NULL); |
| 99 | + |
| 100 | + jerry_port_queueitem_t *item_p = queue.head_p; |
| 101 | + queue.head_p = queue.head_p->next_p; |
| 102 | + |
| 103 | + return item_p; |
| 104 | +} /* jerry_port_jobqueue_dequeue */ |
| 105 | + |
| 106 | +/** |
| 107 | + * Start the jobqueue. |
| 108 | + * @return jerry value |
| 109 | + * If exception happens in the handler, stop the queue |
| 110 | + * and return the exception. |
| 111 | + * Otherwise, return undefined |
| 112 | + */ |
| 113 | +jerry_value_t |
| 114 | +jerry_port_jobqueue_run (void) |
| 115 | +{ |
| 116 | + jerry_value_t ret; |
| 117 | + |
| 118 | + while (true) |
| 119 | + { |
| 120 | + jerry_port_queueitem_t *item_p = jerry_port_jobqueue_dequeue (); |
| 121 | + |
| 122 | + if (item_p == NULL) |
| 123 | + { |
| 124 | + return jerry_create_undefined (); |
| 125 | + } |
| 126 | + |
| 127 | + void *job_p = item_p->job_p; |
| 128 | + jerry_job_handler_t handler = item_p->handler; |
| 129 | + jmem_heap_free_block (item_p, sizeof (jerry_port_queueitem_t)); |
| 130 | + ret = handler (job_p); |
| 131 | + |
| 132 | + if (jerry_value_has_error_flag (ret)) |
| 133 | + { |
| 134 | + return ret; |
| 135 | + } |
| 136 | + |
| 137 | + jerry_release_value (ret); |
| 138 | + } |
| 139 | +} /* jerry_port_jobqueue_run */ |
| 140 | + |
| 141 | +#endif /* !CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */ |
0 commit comments