Move the job queue from the ports into jerry-core (#1804)
* Move the job queue from the ports into jerry-core JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu * Remove port notification and keep `jerry_run_all_enqueued_jobs` API only JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
This commit is contained in:
@@ -432,6 +432,26 @@ jerry_eval (const jerry_char_t *source_p, /**< source code */
|
||||
is_strict);
|
||||
} /* jerry_eval */
|
||||
|
||||
/**
|
||||
* Run enqueued Promise jobs until the first thrown error or until all get executed.
|
||||
*
|
||||
* Note:
|
||||
* returned value must be freed with jerry_release_value, when it is no longer needed.
|
||||
*
|
||||
* @return result of last executed job, may be error value.
|
||||
*/
|
||||
jerry_value_t
|
||||
jerry_run_all_enqueued_jobs (void)
|
||||
{
|
||||
jerry_assert_api_available ();
|
||||
|
||||
#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
|
||||
return ecma_process_all_enqueued_jobs ();
|
||||
#else /* CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */
|
||||
return ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
|
||||
#endif /* CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */
|
||||
} /* jerry_run_all_enqueued_jobs */
|
||||
|
||||
/**
|
||||
* Get global object
|
||||
*
|
||||
|
||||
@@ -46,6 +46,9 @@ ecma_init (void)
|
||||
JERRY_CONTEXT (ecma_prop_hashmap_alloc_last_is_hs_gc) = false;
|
||||
#endif /* !CONFIG_ECMA_PROPERTY_HASHMAP_DISABLE */
|
||||
|
||||
#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
|
||||
ecma_job_queue_init ();
|
||||
#endif /* CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */
|
||||
} /* ecma_init */
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
#include "ecma-jobqueue.h"
|
||||
#include "ecma-objects.h"
|
||||
#include "ecma-promise-object.h"
|
||||
#include "jerryscript-port.h"
|
||||
#include "jcontext.h"
|
||||
|
||||
#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
|
||||
|
||||
@@ -49,6 +49,15 @@ typedef struct
|
||||
ecma_value_t then; /** 'then' function */
|
||||
} ecma_job_promise_resolve_thenable_t;
|
||||
|
||||
/**
|
||||
* Initialize the jobqueue.
|
||||
*/
|
||||
void ecma_job_queue_init (void)
|
||||
{
|
||||
JERRY_CONTEXT (job_queue_head_p) = NULL;
|
||||
JERRY_CONTEXT (job_queue_tail_p) = NULL;
|
||||
} /* ecma_job_queue_init */
|
||||
|
||||
/**
|
||||
* Create a PromiseReactionJob.
|
||||
*
|
||||
@@ -257,18 +266,42 @@ ecma_process_promise_resolve_thenable_job (void *obj_p) /**< the job to be opera
|
||||
} /* ecma_process_promise_resolve_thenable_job */
|
||||
|
||||
/**
|
||||
* Enqueue a PromiseReactionJob into a jobqueue.
|
||||
* Enqueue a Promise job into the jobqueue.
|
||||
*/
|
||||
static void
|
||||
ecma_enqueue_job (ecma_job_handler_t handler, /**< the handler for the job */
|
||||
void *job_p) /**< the job */
|
||||
{
|
||||
ecma_job_queueitem_t *item_p = jmem_heap_alloc_block (sizeof (ecma_job_queueitem_t));
|
||||
item_p->job_p = job_p;
|
||||
item_p->handler = handler;
|
||||
item_p->next_p = NULL;
|
||||
|
||||
if (JERRY_CONTEXT (job_queue_head_p) == NULL)
|
||||
{
|
||||
JERRY_CONTEXT (job_queue_head_p) = item_p;
|
||||
JERRY_CONTEXT (job_queue_tail_p) = item_p;
|
||||
}
|
||||
else
|
||||
{
|
||||
JERRY_CONTEXT (job_queue_tail_p)->next_p = item_p;
|
||||
JERRY_CONTEXT (job_queue_tail_p) = item_p;
|
||||
}
|
||||
} /* ecma_enqueue_job */
|
||||
|
||||
/**
|
||||
* Enqueue a PromiseReactionJob into the jobqueue.
|
||||
*/
|
||||
void
|
||||
ecma_enqueue_promise_reaction_job (ecma_value_t reaction, /**< PromiseReaction */
|
||||
ecma_value_t argument) /**< argument for the reaction */
|
||||
{
|
||||
ecma_job_promise_reaction_t *job_p = ecma_create_promise_reaction_job (reaction, argument);
|
||||
jerry_port_jobqueue_enqueue (ecma_process_promise_reaction_job, job_p);
|
||||
ecma_enqueue_job (ecma_process_promise_reaction_job, job_p);
|
||||
} /* ecma_enqueue_promise_reaction_job */
|
||||
|
||||
/**
|
||||
* Enqueue a PromiseResolveThenableJob into a jobqueue.
|
||||
* Enqueue a PromiseResolveThenableJob into the jobqueue.
|
||||
*/
|
||||
void
|
||||
ecma_enqueue_promise_resolve_thenable_job (ecma_value_t promise, /**< promise to be resolved */
|
||||
@@ -278,9 +311,37 @@ ecma_enqueue_promise_resolve_thenable_job (ecma_value_t promise, /**< promise to
|
||||
ecma_job_promise_resolve_thenable_t *job_p = ecma_create_promise_resolve_thenable_job (promise,
|
||||
thenable,
|
||||
then);
|
||||
jerry_port_jobqueue_enqueue (ecma_process_promise_resolve_thenable_job, job_p);
|
||||
ecma_enqueue_job (ecma_process_promise_resolve_thenable_job, job_p);
|
||||
} /* ecma_enqueue_promise_resolve_thenable_job */
|
||||
|
||||
/**
|
||||
* Process enqueued Promise jobs until the first thrown error or until the
|
||||
* jobqueue becomes empty.
|
||||
*
|
||||
* @return result of the last processed job - if the jobqueue was non-empty,
|
||||
* undefined - otherwise.
|
||||
*/
|
||||
ecma_value_t
|
||||
ecma_process_all_enqueued_jobs (void)
|
||||
{
|
||||
ecma_value_t ret = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
|
||||
|
||||
while (JERRY_CONTEXT (job_queue_head_p) != NULL && !ECMA_IS_VALUE_ERROR (ret))
|
||||
{
|
||||
ecma_job_queueitem_t *item_p = JERRY_CONTEXT (job_queue_head_p);
|
||||
JERRY_CONTEXT (job_queue_head_p) = JERRY_CONTEXT (job_queue_head_p)->next_p;
|
||||
|
||||
void *job_p = item_p->job_p;
|
||||
ecma_job_handler_t handler = item_p->handler;
|
||||
jmem_heap_free_block (item_p, sizeof (ecma_job_queueitem_t));
|
||||
|
||||
ecma_free_value (ret);
|
||||
ret = handler (job_p);
|
||||
}
|
||||
|
||||
return ret;
|
||||
} /* ecma_process_all_enqueued_jobs */
|
||||
|
||||
/**
|
||||
* @}
|
||||
* @}
|
||||
|
||||
@@ -25,9 +25,28 @@
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Jerry job handler function type
|
||||
*/
|
||||
typedef ecma_value_t (*ecma_job_handler_t) (void *job_p);
|
||||
|
||||
/**
|
||||
* Description of the job queue item.
|
||||
*/
|
||||
typedef struct ecma_job_queueitem_t
|
||||
{
|
||||
struct ecma_job_queueitem_t *next_p; /**< points to next item */
|
||||
ecma_job_handler_t handler; /**< the handler for the job*/
|
||||
void *job_p; /**< points to the job */
|
||||
} ecma_job_queueitem_t;
|
||||
|
||||
void ecma_job_queue_init (void);
|
||||
|
||||
void ecma_enqueue_promise_reaction_job (ecma_value_t reaction, ecma_value_t argument);
|
||||
void ecma_enqueue_promise_resolve_thenable_job (ecma_value_t promise, ecma_value_t thenable, ecma_value_t then);
|
||||
|
||||
ecma_value_t ecma_process_all_enqueued_jobs (void);
|
||||
|
||||
/**
|
||||
* @}
|
||||
* @}
|
||||
|
||||
@@ -237,6 +237,8 @@ jerry_value_t jerry_parse_named_resource (const jerry_char_t *name_p, size_t nam
|
||||
jerry_value_t jerry_run (const jerry_value_t func_val);
|
||||
jerry_value_t jerry_eval (const jerry_char_t *source_p, size_t source_size, bool is_strict);
|
||||
|
||||
jerry_value_t jerry_run_all_enqueued_jobs (void);
|
||||
|
||||
/**
|
||||
* Get the global context.
|
||||
*/
|
||||
|
||||
@@ -126,31 +126,6 @@ bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p);
|
||||
*/
|
||||
double jerry_port_get_current_time (void);
|
||||
|
||||
/*
|
||||
* JobQueue Port API
|
||||
*/
|
||||
|
||||
/**
|
||||
* Jerry job handler function type
|
||||
*/
|
||||
typedef uint32_t (*jerry_job_handler_t) (void *);
|
||||
|
||||
/**
|
||||
* Enqueue a job described by a pair of function and data pointers. The port is
|
||||
* expected to call the handler function with the given data at some (later)
|
||||
* point of time.
|
||||
*
|
||||
* @param handler the pointer of the handler function associated with the job.
|
||||
* @param job_p the data pointer to be passed to handler when called.
|
||||
*
|
||||
* Note:
|
||||
* This port function is only called by the implementation of the Promise
|
||||
* builtin (mandated by the ES2015 standard). If the engine is built with
|
||||
* Promise disabled (e.g., with ES5.1 profile), then the port does not have
|
||||
* to implement this function.
|
||||
*/
|
||||
void jerry_port_jobqueue_enqueue (jerry_job_handler_t handler, void *job_p);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#define JCONTEXT_H
|
||||
|
||||
#include "ecma-builtins.h"
|
||||
#include "ecma-jobqueue.h"
|
||||
#include "jerry-debugger.h"
|
||||
#include "jmem.h"
|
||||
#include "re-bytecode.h"
|
||||
@@ -84,6 +85,11 @@ typedef struct
|
||||
uint8_t re_cache_idx; /**< evicted item index when regex cache is full (round-robin) */
|
||||
#endif /* !CONFIG_DISABLE_REGEXP_BUILTIN */
|
||||
|
||||
#ifndef CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
|
||||
ecma_job_queueitem_t *job_queue_head_p; /**< points to the head item of the jobqueue */
|
||||
ecma_job_queueitem_t *job_queue_tail_p; /**< points to the tail item of the jobqueue*/
|
||||
#endif /* CONFIG_DISABLE_ES2015_PROMISE_BUILTIN */
|
||||
|
||||
#ifdef JERRY_VM_EXEC_STOP
|
||||
uint32_t vm_exec_stop_frequency; /**< reset value for vm_exec_stop_counter */
|
||||
uint32_t vm_exec_stop_counter; /**< down counter for reducing the calls of vm_exec_stop_cb */
|
||||
|
||||
Reference in New Issue
Block a user