Skip to content

Commit 16d5d6f

Browse files
authored
Add a simple Job queue in default port. (#1685)
Job queue (event loop) is the basis of Promise. Each port should implement their own job queue. JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang zidong.jiang@intel.com
1 parent 21cec3e commit 16d5d6f

File tree

7 files changed

+247
-0
lines changed

7 files changed

+247
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 "ecma-globals.h"
17+
#include "ecma-helpers.h"
18+
#include "ecma-jobqueue.h"
19+
#include "jerry-port.h"
20+
21+
#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
22+
23+
/** \addtogroup ecma ECMA
24+
* @{
25+
*
26+
* \addtogroup ecmajobqueue ECMA Job Queue related routines
27+
* @{
28+
*/
29+
30+
/**
31+
* The processor for PromiseReactionJob
32+
*/
33+
static ecma_value_t __attribute__ ((unused))
34+
ecma_job_process_promise_reaction_job (void *job_p) /**< the job to be operated */
35+
{
36+
JERRY_UNUSED (job_p);
37+
/** TODO: implement the function body */
38+
return ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
39+
} /* ecma_job_process_promise_reaction_job */
40+
41+
/**
42+
* The processor for PromiseResolveThenableJob
43+
*/
44+
static ecma_value_t __attribute__ ((unused))
45+
ecma_job_process_promise_thenable_job (void *job_p) /**< the job to be operated */
46+
{
47+
JERRY_UNUSED (job_p);
48+
/** TODO: implement the function body */
49+
return ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
50+
} /* ecma_job_process_promise_thenable_job */
51+
52+
/**
53+
* @}
54+
* @}
55+
*/
56+
#endif /* !CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
#ifndef ECMA_JOB_QUEUE_H
17+
#define ECMA_JOB_QUEUE_H
18+
19+
#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
20+
21+
/** \addtogroup ecma ECMA
22+
* @{
23+
*
24+
* \addtogroup ecmajobqueue ECMA Job Queue related routines
25+
* @{
26+
*/
27+
28+
/**
29+
* @}
30+
* @}
31+
*/
32+
#endif /* !CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */
33+
#endif /* !ECMA_JOB_QUEUE_H */

jerry-core/jerry-port.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define JERRY_PORT_H
1818

1919
#include <stdbool.h>
20+
#include <stdint.h>
2021
#include <stdio.h>
2122

2223
#ifdef __cplusplus
@@ -139,6 +140,14 @@ bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p);
139140
*/
140141
double jerry_port_get_current_time (void);
141142

143+
#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
144+
145+
typedef uint32_t (*jerry_job_handler_t) (void *);
146+
147+
void jerry_port_jobqueue_enqueue (jerry_job_handler_t handler, void *job_p);
148+
149+
#endif /* !CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */
150+
142151
/**
143152
* @}
144153
*/

jerry-core/profiles/es5.1.profile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
CONFIG_DISABLE_ES2015_ARRAYBUFFER_BUILTIN
22
CONFIG_DISABLE_ES2015_BUILTIN
3+
CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
34
CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN

jerry-core/profiles/minimal.profile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CONFIG_DISABLE_DATE_BUILTIN
55
CONFIG_DISABLE_ERROR_BUILTINS
66
CONFIG_DISABLE_ES2015_ARRAYBUFFER_BUILTIN
77
CONFIG_DISABLE_ES2015_BUILTIN
8+
CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
89
CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN
910
CONFIG_DISABLE_JSON_BUILTIN
1011
CONFIG_DISABLE_MATH_BUILTIN
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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 */

targets/default/jerry-port-default.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#ifndef JERRY_PORT_DEFAULT_H
1717
#define JERRY_PORT_DEFAULT_H
1818

19+
#include "jerryscript.h"
1920
#include "jerry-port.h"
2021

2122
#include <stdbool.h>
@@ -36,6 +37,11 @@ bool jerry_port_default_is_abort_on_fail (void);
3637
jerry_log_level_t jerry_port_default_get_log_level (void);
3738
void jerry_port_default_set_log_level (jerry_log_level_t level);
3839

40+
#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
41+
void jerry_port_jobqueue_init (void);
42+
jerry_value_t jerry_port_jobqueue_run (void);
43+
#endif /* !CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */
44+
3945
/**
4046
* @}
4147
*/

0 commit comments

Comments
 (0)