Skip to content

Instantly share code, notes, and snippets.

@xeioex
Created February 27, 2019 15:49
Show Gist options
  • Save xeioex/f170ee865fe2bb89212b416670194d91 to your computer and use it in GitHub Desktop.
Save xeioex/f170ee865fe2bb89212b416670194d91 to your computer and use it in GitHub Desktop.
# HG changeset patch
# User Dmitry Volyntsev <[email protected]>
# Date 1551275635 -10800
# Wed Feb 27 16:53:55 2019 +0300
# Node ID d9354fe610cbb0e5d38f4286a655d1e480716ee1
# Parent fc69d402ec6deb2efdd7152d78875c93f7c8a606
Modules: returing undefined value for absent values.
Instead of empty string.
diff --git a/nginx/ngx_http_js_module.c b/nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c
+++ b/nginx/ngx_http_js_module.c
@@ -922,7 +922,8 @@ ngx_http_js_ext_get_header_out(njs_vm_t
h = ngx_http_js_get_header(&r->headers_out.headers.part, v->start,
v->length);
if (h == NULL) {
- return njs_vm_value_string_set(vm, value, NULL, 0);
+ njs_value_undefined_set(value);
+ return NJS_OK;
}
return njs_vm_value_string_set(vm, value, h->value.data, h->value.len);
@@ -1433,8 +1434,8 @@ ngx_http_js_ext_get_request_body(njs_vm_
}
if (r->request_body == NULL || r->request_body->bufs == NULL) {
- njs_vm_error(vm, "request body is unavailable");
- return NJS_ERROR;
+ njs_value_undefined_set(value);
+ return NJS_OK;
}
if (r->request_body->temp_file) {
@@ -1502,7 +1503,8 @@ ngx_http_js_ext_get_header_in(njs_vm_t *
h = ngx_http_js_get_header(&r->headers_in.headers.part, v->start,
v->length);
if (h == NULL) {
- return njs_vm_value_string_set(vm, value, NULL, 0);
+ njs_value_undefined_set(value);
+ return NJS_OK;
}
return njs_vm_value_string_set(vm, value, h->value.data, h->value.len);
@@ -1531,7 +1533,9 @@ ngx_http_js_ext_get_arg(njs_vm_t *vm, nj
return njs_vm_value_string_set(vm, value, arg.data, arg.len);
}
- return njs_vm_value_string_set(vm, value, NULL, 0);
+ njs_value_undefined_set(value);
+
+ return NJS_OK;
}
@@ -1618,7 +1622,8 @@ ngx_http_js_ext_get_variable(njs_vm_t *v
vv = ngx_http_get_variable(r, &name, key);
if (vv == NULL || vv->not_found) {
- return njs_vm_value_string_set(vm, value, NULL, 0);
+ njs_value_undefined_set(value);
+ return NJS_OK;
}
return njs_vm_value_string_set(vm, value, vv->data, vv->len);
diff --git a/nginx/ngx_stream_js_module.c b/nginx/ngx_stream_js_module.c
--- a/nginx/ngx_stream_js_module.c
+++ b/nginx/ngx_stream_js_module.c
@@ -1203,7 +1203,8 @@ ngx_stream_js_ext_get_variable(njs_vm_t
vv = ngx_stream_get_variable(s, &name, key);
if (vv == NULL || vv->not_found) {
- return njs_vm_value_string_set(vm, value, NULL, 0);
+ njs_value_undefined_set(value);
+ return NJS_OK;
}
return njs_vm_value_string_set(vm, value, vv->data, vv->len);
# HG changeset patch
# User Dmitry Volyntsev <[email protected]>
# Date 1551275635 -10800
# Wed Feb 27 16:53:55 2019 +0300
# Node ID c6dcbc8ef3194d3f3d4a5f99c8dba38c8f350d07
# Parent d9354fe610cbb0e5d38f4286a655d1e480716ee1
Treating null and undefined as empty string for external prop set.
diff --git a/njs/njs_object.c b/njs/njs_object.c
--- a/njs/njs_object.c
+++ b/njs/njs_object.c
@@ -682,9 +682,14 @@ njs_external_property_set(njs_vm_t *vm,
pq = (njs_property_query_t *) vm->stash;
- ret = njs_vm_value_to_ext_string(vm, &s, setval, 0);
- if (nxt_slow_path(ret != NXT_OK)) {
- return ret;
+ if (!njs_is_null_or_void(setval)) {
+ ret = njs_vm_value_to_ext_string(vm, &s, setval, 0);
+ if (nxt_slow_path(ret != NXT_OK)) {
+ return ret;
+ }
+
+ } else {
+ s = nxt_string_value("");
}
*retval = *setval;
# HG changeset patch
# User Dmitry Volyntsev <[email protected]>
# Date 1551275636 -10800
# Wed Feb 27 16:53:56 2019 +0300
# Node ID b22bd6730b59c5d48898080068ffbcf3606cae92
# Parent c6dcbc8ef3194d3f3d4a5f99c8dba38c8f350d07
Renaming njs_value_is_void() to njs_value_is_undefined().
diff --git a/njs/njs.h b/njs/njs.h
--- a/njs/njs.h
+++ b/njs/njs.h
@@ -265,7 +265,8 @@ NXT_EXPORT void *njs_value_data(const nj
NXT_EXPORT njs_function_t *njs_value_function(const njs_value_t *value);
NXT_EXPORT nxt_int_t njs_value_is_null(const njs_value_t *value);
-NXT_EXPORT nxt_int_t njs_value_is_void(const njs_value_t *value);
+NXT_EXPORT nxt_int_t njs_value_is_undefined(const njs_value_t *value);
+NXT_EXPORT nxt_int_t njs_value_is_null_or_undefined(const njs_value_t *value);
NXT_EXPORT nxt_int_t njs_value_is_boolean(const njs_value_t *value);
NXT_EXPORT nxt_int_t njs_value_is_number(const njs_value_t *value);
NXT_EXPORT nxt_int_t njs_value_is_valid_number(const njs_value_t *value);
diff --git a/njs/njs_shell.c b/njs/njs_shell.c
--- a/njs/njs_shell.c
+++ b/njs/njs_shell.c
@@ -914,7 +914,7 @@ njs_ext_console_time(njs_vm_t *vm, njs_v
{
njs_console_t *console;
- if (!njs_value_is_void(njs_arg(args, nargs, 1))) {
+ if (!njs_value_is_undefined(njs_arg(args, nargs, 1))) {
njs_vm_error(vm, "labels not implemented");
return NJS_ERROR;
}
@@ -941,7 +941,7 @@ njs_ext_console_time_end(njs_vm_t *vm, n
ns = nxt_time();
- if (!njs_value_is_void(njs_arg(args, nargs, 1))) {
+ if (!njs_value_is_undefined(njs_arg(args, nargs, 1))) {
njs_vm_error(vm, "labels not implemented");
return NJS_ERROR;
}
diff --git a/njs/njs_vm.c b/njs/njs_vm.c
--- a/njs/njs_vm.c
+++ b/njs/njs_vm.c
@@ -3395,13 +3395,20 @@ njs_value_is_null(const njs_value_t *val
nxt_noinline nxt_int_t
-njs_value_is_void(const njs_value_t *value)
+njs_value_is_undefined(const njs_value_t *value)
{
return njs_is_void(value);
}
nxt_noinline nxt_int_t
+njs_value_is_null_or_undefined(const njs_value_t *value)
+{
+ return njs_is_null_or_void(value);
+}
+
+
+nxt_noinline nxt_int_t
njs_value_is_boolean(const njs_value_t *value)
{
return njs_is_boolean(value);
# HG changeset patch
# User Dmitry Volyntsev <[email protected]>
# Date 1551282464 -10800
# Wed Feb 27 18:47:44 2019 +0300
# Node ID a758f1b548e26b3afc484af52248b24b8d97ef8d
# Parent b22bd6730b59c5d48898080068ffbcf3606cae92
HTTP: improved working with arguments.
According to ES 5.1:10.5 direct checking of the number
of arguments should be avoided.
diff --git a/nginx/ngx_http_js_module.c b/nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c
+++ b/nginx/ngx_http_js_module.c
@@ -138,6 +138,8 @@ static void ngx_http_js_clear_timer(njs_
static void ngx_http_js_timer_handler(ngx_event_t *ev);
static void ngx_http_js_handle_event(ngx_http_request_t *r,
njs_vm_event_t vm_event, njs_value_t *args, nxt_uint_t nargs);
+static njs_ret_t ngx_http_js_string(njs_vm_t *vm, const njs_value_t *value,
+ nxt_str_t *str);
static char *ngx_http_js_include(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
@@ -1205,17 +1207,12 @@ ngx_http_js_ext_return(njs_vm_t *vm, njs
{
nxt_str_t text;
ngx_int_t status;
- njs_value_t *value;
ngx_http_js_ctx_t *ctx;
ngx_http_request_t *r;
+ const njs_value_t *value;
ngx_http_complex_value_t cv;
- if (nargs < 2) {
- njs_vm_error(vm, "too few arguments");
- return NJS_ERROR;
- }
-
- value = njs_argument(args, 1);
+ value = njs_arg(args, nargs, 1);
if (!njs_value_is_valid_number(value)) {
njs_vm_error(vm, "code is not a number");
return NJS_ERROR;
@@ -1228,21 +1225,13 @@ ngx_http_js_ext_return(njs_vm_t *vm, njs
return NJS_ERROR;
}
- if (nargs < 3) {
- text.start = NULL;
- text.length = 0;
-
- } else {
- if (njs_vm_value_to_ext_string(vm, &text, njs_argument(args, 2), 0)
- == NJS_ERROR)
- {
- njs_vm_error(vm, "failed to convert text");
- return NJS_ERROR;
- }
+ r = njs_vm_external(vm, njs_arg(args, nargs, 0));
+ if (nxt_slow_path(r == NULL)) {
+ return NJS_ERROR;
}
- r = njs_vm_external(vm, njs_argument(args, 0));
- if (nxt_slow_path(r == NULL)) {
+ if (ngx_http_js_string(vm, njs_arg(args, nargs, 2), &text) != NJS_OK) {
+ njs_vm_error(vm, "failed to convert text");
return NJS_ERROR;
}
@@ -1277,21 +1266,14 @@ ngx_http_js_ext_internal_redirect(njs_vm
ngx_http_js_ctx_t *ctx;
ngx_http_request_t *r;
- if (nargs < 2) {
- njs_vm_error(vm, "too few arguments");
- return NJS_ERROR;
- }
-
- r = njs_vm_external(vm, njs_argument(args, 0));
+ r = njs_vm_external(vm, njs_arg(args, nargs, 0));
if (nxt_slow_path(r == NULL)) {
return NJS_ERROR;
}
ctx = ngx_http_get_module_ctx(r, ngx_http_js_module);
- if (njs_vm_value_to_ext_string(vm, &uri, njs_argument(args, 1), 0)
- == NJS_ERROR)
- {
+ if (ngx_http_js_string(vm, njs_arg(args, nargs, 1), &uri) != NJS_OK) {
njs_vm_error(vm, "failed to convert uri arg");
return NJS_ERROR;
}
@@ -1705,10 +1687,11 @@ ngx_http_js_ext_subrequest(njs_vm_t *vm,
{
ngx_int_t rc;
nxt_str_t uri_arg, args_arg, method_name, body_arg;
- ngx_uint_t cb_index, method, n, has_body;
- njs_value_t *arg2, *options, *value;
+ ngx_uint_t method, n, has_body;
+ njs_value_t *value;
njs_function_t *callback;
ngx_http_js_ctx_t *ctx;
+ const njs_value_t *arg2, *options;
ngx_http_request_t *r, *sr;
ngx_http_request_body_t *rb;
@@ -1737,12 +1720,7 @@ ngx_http_js_ext_subrequest(njs_vm_t *vm,
static const nxt_str_t method_key = nxt_string("method");
static const nxt_str_t body_key = nxt_string("body");
- if (nargs < 2) {
- njs_vm_error(vm, "too few arguments");
- return NJS_ERROR;
- }
-
- r = njs_vm_external(vm, njs_argument(args, 0));
+ r = njs_vm_external(vm, njs_arg(args, nargs, 0));
if (nxt_slow_path(r == NULL)) {
return NJS_ERROR;
}
@@ -1755,51 +1733,42 @@ ngx_http_js_ext_subrequest(njs_vm_t *vm,
return NJS_ERROR;
}
- if (njs_vm_value_to_ext_string(vm, &uri_arg, njs_argument(args, 1), 0)
- == NJS_ERROR)
- {
+ if (ngx_http_js_string(vm, njs_arg(args, nargs, 1), &uri_arg) != NJS_OK) {
njs_vm_error(vm, "failed to convert uri arg");
return NJS_ERROR;
}
options = NULL;
+ callback = NULL;
method = 0;
args_arg.length = 0;
args_arg.start = NULL;
has_body = 0;
- if (nargs > 2 && !njs_value_is_function(njs_argument(args, 2))) {
- arg2 = njs_argument(args, 2);
-
- if (njs_value_is_object(arg2)) {
- options = arg2;
-
- } else if (njs_value_is_string(arg2)) {
- if (njs_vm_value_to_ext_string(vm, &args_arg, arg2, 0)
- == NJS_ERROR)
- {
- njs_vm_error(vm, "failed to convert args");
- return NJS_ERROR;
- }
-
- } else {
+ arg2 = njs_arg(args, nargs, 2);
+
+ if (njs_value_is_string(arg2)) {
+ if (ngx_http_js_string(vm, arg2, &args_arg) != NJS_OK) {
njs_vm_error(vm, "failed to convert args");
return NJS_ERROR;
}
- cb_index = 3;
-
- } else {
- cb_index = 2;
+ } else if (njs_value_is_function(arg2)) {
+ callback = njs_value_function(arg2);
+
+ } else if (njs_value_is_object(arg2)) {
+ options = arg2;
+
+ } else if (!njs_value_is_undefined(arg2)) {
+ njs_vm_error(vm, "failed to convert args");
+ return NJS_ERROR;
}
if (options != NULL) {
value = njs_vm_object_prop(vm, options, &args_key);
if (value != NULL) {
- if (njs_vm_value_to_ext_string(vm, &args_arg, value, 0)
- == NJS_ERROR)
- {
+ if (ngx_http_js_string(vm, value, &args_arg) != NJS_OK) {
njs_vm_error(vm, "failed to convert options.args");
return NJS_ERROR;
}
@@ -1807,9 +1776,7 @@ ngx_http_js_ext_subrequest(njs_vm_t *vm,
value = njs_vm_object_prop(vm, options, &method_key);
if (value != NULL) {
- if (njs_vm_value_to_ext_string(vm, &method_name, value, 0)
- == NJS_ERROR)
- {
+ if (ngx_http_js_string(vm, value, &method_name) != NJS_OK) {
njs_vm_error(vm, "failed to convert options.method");
return NJS_ERROR;
}
@@ -1836,9 +1803,7 @@ ngx_http_js_ext_subrequest(njs_vm_t *vm,
value = njs_vm_object_prop(vm, options, &body_key);
if (value != NULL) {
- if (njs_vm_value_to_ext_string(vm, &body_arg, value, 0)
- == NJS_ERROR)
- {
+ if (ngx_http_js_string(vm, value, &body_arg) != NJS_OK) {
njs_vm_error(vm, "failed to convert options.body");
return NJS_ERROR;
}
@@ -1847,15 +1812,13 @@ ngx_http_js_ext_subrequest(njs_vm_t *vm,
}
}
- callback = NULL;
-
- if (cb_index < nargs) {
- if (!njs_value_is_function(njs_argument(args, cb_index))) {
+ if (callback == NULL && !njs_value_is_undefined(njs_arg(args, nargs, 3))) {
+ if (!njs_value_is_function(njs_argument(args, 3))) {
njs_vm_error(vm, "callback is not a function");
return NJS_ERROR;
} else {
- callback = njs_value_function(njs_argument(args, cb_index));
+ callback = njs_value_function(njs_argument(args, 3));
}
}
@@ -2172,6 +2135,23 @@ ngx_http_js_handle_event(ngx_http_reques
}
+static njs_ret_t
+ngx_http_js_string(njs_vm_t *vm, const njs_value_t *value, nxt_str_t *str)
+{
+ if (!njs_value_is_null_or_undefined(value)) {
+ if (njs_vm_value_to_ext_string(vm, str, value, 0) == NJS_ERROR) {
+ return NJS_ERROR;
+ }
+
+ } else {
+ str->start = NULL;
+ str->length = 0;
+ }
+
+ return NJS_OK;
+}
+
+
static char *
ngx_http_js_include(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment