From: Dmitry Volyntsev Date: Fri, 28 Dec 2018 09:44:56 +0000 (+0300) Subject: Added setImmediate(). X-Git-Tag: 0.2.8~98 X-Git-Url: http://git.kaiwu.me/sitemap.xml?a=commitdiff_plain;h=71e7d988ee1db0c422bc0410cd78ac95b65d696e;p=njs.git Added setImmediate(). --- diff --git a/njs/njs.c b/njs/njs.c index b88f497e..bf7ee186 100644 --- a/njs/njs.c +++ b/njs/njs.c @@ -521,7 +521,7 @@ njs_vm_call(njs_vm_t *vm, njs_function_t *function, const njs_value_t *args, 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 --git a/njs/njs.h b/njs/njs.h index ac088427..638b886d 100644 --- a/njs/njs.h +++ b/njs/njs.h @@ -125,15 +125,15 @@ typedef void * njs_vm_event_t; 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_t *vm, njs_external_ptr_t external); 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 --git a/njs/njs_builtin.c b/njs/njs_builtin.c index 2b3ee097..4605e655 100644 --- a/njs/njs_builtin.c +++ b/njs/njs_builtin.c @@ -111,6 +111,7 @@ const njs_object_init_t *njs_function_init[] = { &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_functions[] = { { 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 --git a/njs/njs_event.h b/njs/njs_event.h index e09cdbc0..405c607e 100644 --- a/njs/njs_event.h +++ b/njs/njs_event.h @@ -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 --git a/njs/njs_generator.c b/njs/njs_generator.c index 6ca1e116..f73f5da6 100644 --- a/njs/njs_generator.c +++ b/njs/njs_generator.c @@ -406,6 +406,7 @@ njs_generator(njs_vm_t *vm, njs_generator_t *generator, njs_parser_node_t *node) 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 --git a/njs/njs_lexer_keyword.c b/njs/njs_lexer_keyword.c index 039c3a59..ba7dd9b7 100644 --- a/njs/njs_lexer_keyword.c +++ b/njs/njs_lexer_keyword.c @@ -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 --git a/njs/njs_parser.c b/njs/njs_parser.c index abf7a518..f09cda0b 100644 --- a/njs/njs_parser.c +++ b/njs/njs_parser.c @@ -2075,6 +2075,7 @@ njs_parser_terminal(njs_vm_t *vm, njs_parser_t *parser, njs_token_t token) 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 --git a/njs/njs_parser.h b/njs/njs_parser.h index ab10c0c0..a992cb77 100644 --- a/njs/njs_parser.h +++ b/njs/njs_parser.h @@ -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 --git a/njs/njs_time.c b/njs/njs_time.c index 08aa587d..39caaacb 100644 --- a/njs/njs_time.c +++ b/njs/njs_time.c @@ -10,10 +10,11 @@ #include -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_t *args, nxt_uint_t nargs, 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_t *args, nxt_uint_t nargs, 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_t *args, nxt_uint_t nargs, 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; } @@ -77,6 +80,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_function_init = { 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 --git a/njs/njs_time.h b/njs/njs_time.h index 86185be5..868719e4 100644 --- a/njs/njs_time.h +++ b/njs/njs_time.h @@ -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 --git a/njs/njs_vm.h b/njs/njs_vm.h index fd7cc240..7dd4fe06 100644 --- a/njs/njs_vm.h +++ b/njs/njs_vm.h @@ -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) };