[njs] Added setImmediate().
Dmitry Volyntsev
xeioex at nginx.com
Fri Dec 28 09:46:26 UTC 2018
details: https://hg.nginx.org/njs/rev/269fe12bbabe
branches:
changeset: 704:269fe12bbabe
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Fri Dec 28 12:44:56 2018 +0300
description:
Added setImmediate().
diffstat:
njs/njs.c | 2 +-
njs/njs.h | 10 +++++-----
njs/njs_builtin.c | 3 +++
njs/njs_event.h | 18 +++++++++---------
njs/njs_generator.c | 1 +
njs/njs_lexer_keyword.c | 1 +
njs/njs_parser.c | 1 +
njs/njs_parser.h | 1 +
njs/njs_time.c | 41 +++++++++++++++++++++++++++++++++--------
njs/njs_time.h | 3 +++
njs/njs_vm.h | 1 +
11 files changed, 59 insertions(+), 23 deletions(-)
diffs (258 lines):
diff -r f08c5fc1dea8 -r 269fe12bbabe njs/njs.c
--- a/njs/njs.c Fri Dec 28 12:43:36 2018 +0300
+++ b/njs/njs.c Fri Dec 28 12:44:56 2018 +0300
@@ -521,7 +521,7 @@ njs_vm_call(njs_vm_t *vm, njs_function_t
njs_vm_event_t
njs_vm_add_event(njs_vm_t *vm, njs_function_t *function, nxt_uint_t once,
- njs_host_event_t host_ev, njs_event_destructor destructor)
+ njs_host_event_t host_ev, njs_event_destructor_t destructor)
{
njs_event_t *event;
diff -r f08c5fc1dea8 -r 269fe12bbabe njs/njs.h
--- a/njs/njs.h Fri Dec 28 12:43:36 2018 +0300
+++ b/njs/njs.h Fri Dec 28 12:44:56 2018 +0300
@@ -125,15 +125,15 @@ typedef void * njs_
typedef void * njs_host_event_t;
typedef void * njs_external_ptr_t;
-typedef njs_host_event_t (*njs_set_timer)(njs_external_ptr_t external,
+typedef njs_host_event_t (*njs_set_timer_t)(njs_external_ptr_t external,
uint64_t delay, njs_vm_event_t vm_event);
-typedef void (*njs_event_destructor)(njs_external_ptr_t external,
+typedef void (*njs_event_destructor_t)(njs_external_ptr_t external,
njs_host_event_t event);
typedef struct {
- njs_set_timer set_timer;
- njs_event_destructor clear_timer;
+ njs_set_timer_t set_timer;
+ njs_event_destructor_t clear_timer;
} njs_vm_ops_t;
@@ -165,7 +165,7 @@ NXT_EXPORT njs_vm_t *njs_vm_clone(njs_vm
NXT_EXPORT njs_vm_event_t njs_vm_add_event(njs_vm_t *vm,
njs_function_t *function, nxt_uint_t once, njs_host_event_t host_ev,
- njs_event_destructor destructor);
+ njs_event_destructor_t destructor);
NXT_EXPORT void njs_vm_del_event(njs_vm_t *vm, njs_vm_event_t vm_event);
NXT_EXPORT nxt_int_t njs_vm_post_event(njs_vm_t *vm, njs_vm_event_t vm_event,
const njs_value_t *args, nxt_uint_t nargs);
diff -r f08c5fc1dea8 -r 269fe12bbabe njs/njs_builtin.c
--- a/njs/njs_builtin.c Fri Dec 28 12:43:36 2018 +0300
+++ b/njs/njs_builtin.c Fri Dec 28 12:44:56 2018 +0300
@@ -111,6 +111,7 @@ const njs_object_init_t *njs_function
&njs_decode_uri_component_function_init,
&njs_require_function_init,
&njs_set_timeout_function_init,
+ &njs_set_immediate_function_init,
&njs_clear_timeout_function_init,
NULL
};
@@ -132,6 +133,8 @@ const njs_function_init_t njs_native_fu
{ njs_module_require, { NJS_SKIP_ARG, NJS_STRING_ARG } },
{ njs_set_timeout,
{ NJS_SKIP_ARG, NJS_FUNCTION_ARG, NJS_NUMBER_ARG } },
+ { njs_set_immediate,
+ { NJS_SKIP_ARG, NJS_FUNCTION_ARG } },
{ njs_clear_timeout, { NJS_SKIP_ARG, NJS_NUMBER_ARG } },
};
diff -r f08c5fc1dea8 -r 269fe12bbabe njs/njs_event.h
--- a/njs/njs_event.h Fri Dec 28 12:43:36 2018 +0300
+++ b/njs/njs_event.h Fri Dec 28 12:44:56 2018 +0300
@@ -18,17 +18,17 @@
typedef struct {
- njs_function_t *function;
- njs_value_t *args;
- nxt_uint_t nargs;
- njs_host_event_t host_event;
- njs_event_destructor destructor;
+ njs_function_t *function;
+ njs_value_t *args;
+ nxt_uint_t nargs;
+ njs_host_event_t host_event;
+ njs_event_destructor_t destructor;
- njs_value_t id;
- nxt_queue_link_t link;
+ njs_value_t id;
+ nxt_queue_link_t link;
- unsigned posted:1;
- unsigned once:1;
+ unsigned posted:1;
+ unsigned once:1;
} njs_event_t;
diff -r f08c5fc1dea8 -r 269fe12bbabe njs/njs_generator.c
--- a/njs/njs_generator.c Fri Dec 28 12:43:36 2018 +0300
+++ b/njs/njs_generator.c Fri Dec 28 12:44:56 2018 +0300
@@ -406,6 +406,7 @@ njs_generator(njs_vm_t *vm, njs_generato
case NJS_TOKEN_DECODE_URI_COMPONENT:
case NJS_TOKEN_REQUIRE:
case NJS_TOKEN_SET_TIMEOUT:
+ case NJS_TOKEN_SET_IMMEDIATE:
case NJS_TOKEN_CLEAR_TIMEOUT:
return njs_generate_builtin_object(vm, generator, node);
diff -r f08c5fc1dea8 -r 269fe12bbabe njs/njs_lexer_keyword.c
--- a/njs/njs_lexer_keyword.c Fri Dec 28 12:43:36 2018 +0300
+++ b/njs/njs_lexer_keyword.c Fri Dec 28 12:44:56 2018 +0300
@@ -90,6 +90,7 @@ static const njs_keyword_t njs_keywords
{ nxt_string("decodeURIComponent"), NJS_TOKEN_DECODE_URI_COMPONENT, 0 },
{ nxt_string("require"), NJS_TOKEN_REQUIRE, 0 },
{ nxt_string("setTimeout"), NJS_TOKEN_SET_TIMEOUT, 0 },
+ { nxt_string("setImmediate"), NJS_TOKEN_SET_IMMEDIATE, 0 },
{ nxt_string("clearTimeout"), NJS_TOKEN_CLEAR_TIMEOUT, 0 },
/* Reserved words. */
diff -r f08c5fc1dea8 -r 269fe12bbabe njs/njs_parser.c
--- a/njs/njs_parser.c Fri Dec 28 12:43:36 2018 +0300
+++ b/njs/njs_parser.c Fri Dec 28 12:44:56 2018 +0300
@@ -2075,6 +2075,7 @@ njs_parser_terminal(njs_vm_t *vm, njs_pa
case NJS_TOKEN_DECODE_URI_COMPONENT:
case NJS_TOKEN_REQUIRE:
case NJS_TOKEN_SET_TIMEOUT:
+ case NJS_TOKEN_SET_IMMEDIATE:
case NJS_TOKEN_CLEAR_TIMEOUT:
return njs_parser_builtin_function(vm, parser, node);
diff -r f08c5fc1dea8 -r 269fe12bbabe njs/njs_parser.h
--- a/njs/njs_parser.h Fri Dec 28 12:43:36 2018 +0300
+++ b/njs/njs_parser.h Fri Dec 28 12:44:56 2018 +0300
@@ -199,6 +199,7 @@ typedef enum {
NJS_TOKEN_DECODE_URI_COMPONENT,
NJS_TOKEN_REQUIRE,
NJS_TOKEN_SET_TIMEOUT,
+ NJS_TOKEN_SET_IMMEDIATE,
NJS_TOKEN_CLEAR_TIMEOUT,
NJS_TOKEN_RESERVED,
diff -r f08c5fc1dea8 -r 269fe12bbabe njs/njs_time.c
--- a/njs/njs_time.c Fri Dec 28 12:43:36 2018 +0300
+++ b/njs/njs_time.c Fri Dec 28 12:44:56 2018 +0300
@@ -10,10 +10,11 @@
#include <stdio.h>
-njs_ret_t
-njs_set_timeout(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
- njs_index_t unused)
+static njs_ret_t
+njs_set_timer(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+ njs_index_t unused, nxt_bool_t immediate)
{
+ nxt_uint_t n;
uint64_t delay;
njs_event_t *event;
njs_vm_ops_t *ops;
@@ -36,7 +37,7 @@ njs_set_timeout(njs_vm_t *vm, njs_value_
delay = 0;
- if (nargs >= 3 && njs_is_number(&args[2])) {
+ if (!immediate && nargs >= 3 && njs_is_number(&args[2])) {
delay = args[2].data.u.number;
}
@@ -45,9 +46,11 @@ njs_set_timeout(njs_vm_t *vm, njs_value_
goto memory_error;
}
- event->destructor = ops->clear_timer;
+ n = immediate ? 2 : 3;
+
+ event->destructor = (ops != NULL) ? ops->clear_timer : NULL;
event->function = args[1].data.u.function;
- event->nargs = (nargs >= 3) ? nargs - 3 : 0;
+ event->nargs = (nargs >= n) ? nargs - n : 0;
event->once = 1;
event->posted = 0;
@@ -58,11 +61,11 @@ njs_set_timeout(njs_vm_t *vm, njs_value_
goto memory_error;
}
- memcpy(event->args, &args[3], sizeof(njs_value_t) * event->nargs);
+ memcpy(event->args, &args[n], sizeof(njs_value_t) * event->nargs);
}
event->host_event = ops->set_timer(vm->external, delay, event);
- if (event->host_event == NULL) {
+ if (nxt_slow_path(event->host_event == NULL)) {
njs_internal_error(vm, "set_timer() failed");
return NJS_ERROR;
}
@@ -78,6 +81,22 @@ memory_error:
njs_ret_t
+njs_set_timeout(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+ njs_index_t unused)
+{
+ return njs_set_timer(vm, args, nargs, unused, 0);
+}
+
+
+njs_ret_t
+njs_set_immediate(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
+ njs_index_t unused)
+{
+ return njs_set_timer(vm, args, nargs, unused, 1);
+}
+
+
+njs_ret_t
njs_clear_timeout(njs_vm_t *vm, njs_value_t *args, nxt_uint_t nargs,
njs_index_t unused)
{
@@ -116,6 +135,12 @@ const njs_object_init_t njs_set_timeout
0,
};
+const njs_object_init_t njs_set_immediate_function_init = {
+ nxt_string("setImmediate"),
+ NULL,
+ 0,
+};
+
const njs_object_init_t njs_clear_timeout_function_init = {
nxt_string("clearTimeout"),
diff -r f08c5fc1dea8 -r 269fe12bbabe njs/njs_time.h
--- a/njs/njs_time.h Fri Dec 28 12:43:36 2018 +0300
+++ b/njs/njs_time.h Fri Dec 28 12:44:56 2018 +0300
@@ -10,11 +10,14 @@
njs_ret_t njs_set_timeout(njs_vm_t *vm, njs_value_t *args,
nxt_uint_t nargs, njs_index_t unused);
+njs_ret_t njs_set_immediate(njs_vm_t *vm, njs_value_t *args,
+ nxt_uint_t nargs, njs_index_t unused);
njs_ret_t njs_clear_timeout(njs_vm_t *vm, njs_value_t *args,
nxt_uint_t nargs, njs_index_t unused);
extern const njs_object_init_t njs_set_timeout_function_init;
+extern const njs_object_init_t njs_set_immediate_function_init;
extern const njs_object_init_t njs_clear_timeout_function_init;
#endif /* _NJS_TIMEOUT_H_INCLUDED_ */
diff -r f08c5fc1dea8 -r 269fe12bbabe njs/njs_vm.h
--- a/njs/njs_vm.h Fri Dec 28 12:43:36 2018 +0300
+++ b/njs/njs_vm.h Fri Dec 28 12:44:56 2018 +0300
@@ -950,6 +950,7 @@ enum njs_function_e {
NJS_FUNCTION_STRING_DECODE_URI_COMPONENT,
NJS_FUNCTION_REQUIRE,
NJS_FUNCTION_SET_TIMEOUT,
+ NJS_FUNCTION_SET_IMMEDIATE,
NJS_FUNCTION_CLEAR_TIMEOUT,
#define NJS_FUNCTION_MAX (NJS_FUNCTION_CLEAR_TIMEOUT + 1)
};
More information about the nginx-devel
mailing list