[njs] Improved API for value creation from the outside of VM.

Dmitry Volyntsev xeioex at nginx.com
Thu Feb 21 13:49:17 UTC 2019


details:   https://hg.nginx.org/njs/rev/2a6f5ffd5e96
branches:  
changeset: 793:2a6f5ffd5e96
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Feb 21 16:35:52 2019 +0300
description:
Improved API for value creation from the outside of VM.

diffstat:

 nginx/ngx_http_js_module.c   |  34 ++++++++++++++++++----------------
 nginx/ngx_stream_js_module.c |  13 +++++++------
 njs/njs.h                    |  22 ++++++++++++----------
 njs/njs_string.c             |   8 ++++----
 njs/njs_string.h             |   4 ++++
 njs/njs_vm.c                 |  21 ++++++++++++++++++---
 njs/test/njs_unit_test.c     |  18 +++++++++---------
 7 files changed, 72 insertions(+), 48 deletions(-)

diffs (383 lines):

diff -r 736cfe916b2b -r 2a6f5ffd5e96 nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Thu Feb 21 00:30:40 2019 +0800
+++ b/nginx/ngx_http_js_module.c	Thu Feb 21 16:35:52 2019 +0300
@@ -805,7 +805,7 @@ ngx_http_js_ext_get_string(njs_vm_t *vm,
 
     field = (ngx_str_t *) (p + data);
 
-    return njs_string_create(vm, value, field->data, field->len, 0);
+    return njs_vm_value_string_set(vm, value, field->data, field->len);
 }
 
 
@@ -864,7 +864,7 @@ ngx_http_js_ext_next_header(njs_vm_t *vm
             continue;
         }
 
-        return njs_string_create(vm, value, h->key.data, h->key.len, 0);
+        return njs_vm_value_string_set(vm, value, h->key.data, h->key.len);
     }
 
     return NJS_DONE;
@@ -920,10 +920,10 @@ 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_string_create(vm, value, NULL, 0, 0);
+        return njs_vm_value_string_set(vm, value, NULL, 0);
     }
 
-    return njs_string_create(vm, value, h->value.data, h->value.len, 0);
+    return njs_vm_value_string_set(vm, value, h->value.data, h->value.len);
 }
 
 
@@ -1122,7 +1122,8 @@ ngx_http_js_ext_send(njs_vm_t *vm, njs_v
         next = 0;
 
         for ( ;; ) {
-            ret = njs_value_string_copy(vm, &s, njs_argument(args, n), &next);
+            ret = njs_vm_value_string_copy(vm, &s, njs_argument(args, n),
+                                           &next);
 
             if (ret == NJS_DECLINED) {
                 break;
@@ -1387,7 +1388,7 @@ ngx_http_js_ext_get_http_version(njs_vm_
         break;
     }
 
-    return njs_string_create(vm, value, v.data, v.len, 0);
+    return njs_vm_value_string_set(vm, value, v.data, v.len);
 }
 
 
@@ -1401,7 +1402,8 @@ ngx_http_js_ext_get_remote_address(njs_v
     r = (ngx_http_request_t *) obj;
     c = r->connection;
 
-    return njs_string_create(vm, value, c->addr_text.data, c->addr_text.len, 0);
+    return njs_vm_value_string_set(vm, value, c->addr_text.data,
+                                   c->addr_text.len);
 }
 
 
@@ -1472,7 +1474,7 @@ ngx_http_js_ext_get_request_body(njs_vm_
 
 done:
 
-    ret = njs_string_create(vm, request_body, body, len, 0);
+    ret = njs_vm_value_string_set(vm, request_body, body, len);
 
     if (ret != NXT_OK) {
         return NJS_ERROR;
@@ -1498,10 +1500,10 @@ 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_string_create(vm, value, NULL, 0, 0);
+        return njs_vm_value_string_set(vm, value, NULL, 0);
     }
 
-    return njs_string_create(vm, value, h->value.data, h->value.len, 0);
+    return njs_vm_value_string_set(vm, value, h->value.data, h->value.len);
 }
 
 
@@ -1524,10 +1526,10 @@ ngx_http_js_ext_get_arg(njs_vm_t *vm, nj
     v = (nxt_str_t *) data;
 
     if (ngx_http_arg(r, v->start, v->length, &arg) == NGX_OK) {
-        return njs_string_create(vm, value, arg.data, arg.len, 0);
+        return njs_vm_value_string_set(vm, value, arg.data, arg.len);
     }
 
-    return njs_string_create(vm, value, NULL, 0, 0);
+    return njs_vm_value_string_set(vm, value, NULL, 0);
 }
 
 
@@ -1590,7 +1592,7 @@ ngx_http_js_ext_next_arg(njs_vm_t *vm, n
         entry->len = 0;
     }
 
-    return njs_string_create(vm, value, start, len, 0);
+    return njs_vm_value_string_set(vm, value, start, len);
 }
 
 
@@ -1614,10 +1616,10 @@ 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_string_create(vm, value, NULL, 0, 0);
+        return njs_vm_value_string_set(vm, value, NULL, 0);
     }
 
-    return njs_string_create(vm, value, vv->data, vv->len, 0);
+    return njs_vm_value_string_set(vm, value, vv->data, vv->len);
 }
 
 
@@ -1980,7 +1982,7 @@ ngx_http_js_ext_get_reply_body(njs_vm_t 
 
     len = b ? b->last - b->pos : 0;
 
-    p = njs_string_alloc(vm, value, len, 0);
+    p = njs_vm_value_string_alloc(vm, value, len);
     if (p == NULL) {
         return NJS_ERROR;
     }
diff -r 736cfe916b2b -r 2a6f5ffd5e96 nginx/ngx_stream_js_module.c
--- a/nginx/ngx_stream_js_module.c	Thu Feb 21 00:30:40 2019 +0800
+++ b/nginx/ngx_stream_js_module.c	Thu Feb 21 16:35:52 2019 +0300
@@ -794,7 +794,7 @@ ngx_stream_js_buffer_arg(ngx_stream_sess
 
     len = b ? b->last - b->pos : 0;
 
-    p = njs_string_alloc(ctx->vm, buffer, len, 0);
+    p = njs_vm_value_string_alloc(ctx->vm, buffer, len);
     if (p == NULL) {
         return NJS_ERROR;
     }
@@ -821,8 +821,8 @@ ngx_stream_js_flags_arg(ngx_stream_sessi
 
     ctx = ngx_stream_get_module_ctx(s, ngx_stream_js_module);
 
-    njs_string_create(ctx->vm, njs_value_arg(&last_key), last_str.start,
-                      last_str.length, 0);
+    njs_vm_value_string_set(ctx->vm, njs_value_arg(&last_key), last_str.start,
+                            last_str.length);
 
     c = s->connection;
 
@@ -884,7 +884,8 @@ ngx_stream_js_ext_get_remote_address(njs
     s = (ngx_stream_session_t *) obj;
     c = s->connection;
 
-    return njs_string_create(vm, value, c->addr_text.data, c->addr_text.len, 0);
+    return njs_vm_value_string_set(vm, value, c->addr_text.data,
+                                   c->addr_text.len);
 }
 
 
@@ -1200,10 +1201,10 @@ 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_string_create(vm, value, NULL, 0, 0);
+        return njs_vm_value_string_set(vm, value, NULL, 0);
     }
 
-    return njs_string_create(vm, value, vv->data, vv->len, 0);
+    return njs_vm_value_string_set(vm, value, vv->data, vv->len);
 }
 
 
diff -r 736cfe916b2b -r 2a6f5ffd5e96 njs/njs.h
--- a/njs/njs.h	Thu Feb 21 00:30:40 2019 +0800
+++ b/njs/njs.h	Thu Feb 21 16:35:52 2019 +0300
@@ -57,7 +57,7 @@ extern const njs_value_t            njs_
 
 
 #define njs_vm_error(vm, fmt, ...)                                            \
-    njs_value_error_set(vm, njs_vm_retval(vm), fmt, ##__VA_ARGS__)
+    njs_vm_value_error_set(vm, njs_vm_retval(vm), fmt, ##__VA_ARGS__)
 
 
 typedef njs_ret_t (*njs_extern_get_t)(njs_vm_t *vm, njs_value_t *value,
@@ -231,14 +231,16 @@ NXT_EXPORT njs_function_t *njs_vm_functi
 NXT_EXPORT njs_value_t *njs_vm_retval(njs_vm_t *vm);
 NXT_EXPORT void njs_vm_retval_set(njs_vm_t *vm, const njs_value_t *value);
 
-NXT_EXPORT u_char * njs_string_alloc(njs_vm_t *vm, njs_value_t *value,
-    uint32_t size, uint32_t length);
-NXT_EXPORT njs_ret_t njs_string_create(njs_vm_t *vm, njs_value_t *value,
-    const u_char *start, uint32_t size, uint32_t length);
-
-NXT_EXPORT nxt_int_t njs_value_string_copy(njs_vm_t *vm, nxt_str_t *retval,
+/*
+ * Sets a byte string value.
+ *   start data is not copied and should not be freed.
+ */
+NXT_EXPORT njs_ret_t njs_vm_value_string_set(njs_vm_t *vm, njs_value_t *value,
+    const u_char *start, uint32_t size);
+NXT_EXPORT u_char *njs_vm_value_string_alloc(njs_vm_t *vm, njs_value_t *value,
+    uint32_t size);
+NXT_EXPORT nxt_int_t njs_vm_value_string_copy(njs_vm_t *vm, nxt_str_t *retval,
     const njs_value_t *value, uintptr_t *next);
-
 NXT_EXPORT njs_ret_t njs_vm_value_to_ext_string(njs_vm_t *vm, nxt_str_t *dst,
     const njs_value_t *src, nxt_uint_t handle_exception);
 NXT_EXPORT njs_ret_t njs_vm_retval_to_ext_string(njs_vm_t *vm, nxt_str_t *dst);
@@ -248,14 +250,14 @@ NXT_EXPORT njs_ret_t njs_vm_value_dump(n
 NXT_EXPORT njs_ret_t njs_vm_retval_dump(njs_vm_t *vm, nxt_str_t *dst,
     nxt_uint_t indent);
 
+NXT_EXPORT void njs_vm_value_error_set(njs_vm_t *vm, njs_value_t *value,
+    const char *fmt, ...);
 NXT_EXPORT void njs_vm_memory_error(njs_vm_t *vm);
 
 NXT_EXPORT void njs_value_void_set(njs_value_t *value);
 NXT_EXPORT void njs_value_boolean_set(njs_value_t *value, int yn);
 NXT_EXPORT void njs_value_number_set(njs_value_t *value, double num);
 NXT_EXPORT void njs_value_data_set(njs_value_t *value, void *data);
-NXT_EXPORT void njs_value_error_set(njs_vm_t *vm, njs_value_t *value,
-    const char *fmt, ...);
 
 NXT_EXPORT uint8_t njs_value_bool(const njs_value_t *value);
 NXT_EXPORT double njs_value_number(const njs_value_t *value);
diff -r 736cfe916b2b -r 2a6f5ffd5e96 njs/njs_string.c
--- a/njs/njs_string.c	Thu Feb 21 00:30:40 2019 +0800
+++ b/njs/njs_string.c	Thu Feb 21 16:35:52 2019 +0300
@@ -111,8 +111,8 @@ static njs_ret_t njs_string_decode(njs_v
 
 
 njs_ret_t
-njs_string_create(njs_vm_t *vm, njs_value_t *value, const u_char *start,
-    uint32_t size, uint32_t length)
+njs_string_set(njs_vm_t *vm, njs_value_t *value, const u_char *start,
+    uint32_t size)
 {
     u_char        *dst;
     const u_char  *src;
@@ -123,7 +123,7 @@ njs_string_create(njs_vm_t *vm, njs_valu
 
     if (size <= NJS_STRING_SHORT) {
         value->short_string.size = size;
-        value->short_string.length = length;
+        value->short_string.length = 0;
 
         dst = value->short_string.start;
         src = start;
@@ -155,7 +155,7 @@ njs_string_create(njs_vm_t *vm, njs_valu
         value->long_string.data = string;
 
         string->start = (u_char *) start;
-        string->length = length;
+        string->length = 0;
         string->retain = 1;
     }
 
diff -r 736cfe916b2b -r 2a6f5ffd5e96 njs/njs_string.h
--- a/njs/njs_string.h	Thu Feb 21 00:30:40 2019 +0800
+++ b/njs/njs_string.h	Thu Feb 21 16:35:52 2019 +0300
@@ -121,6 +121,10 @@ njs_string_length(njs_utf8_t utf8, const
 }
 
 
+njs_ret_t njs_string_set(njs_vm_t *vm, njs_value_t *value, const u_char *start,
+    uint32_t size);
+u_char *njs_string_alloc(njs_vm_t *vm, njs_value_t *value, uint32_t size,
+    uint32_t length);
 njs_ret_t njs_string_new(njs_vm_t *vm, njs_value_t *value, const u_char *start,
     uint32_t size, uint32_t length);
 njs_ret_t njs_string_hex(njs_vm_t *vm, njs_value_t *value,
diff -r 736cfe916b2b -r 2a6f5ffd5e96 njs/njs_vm.c
--- a/njs/njs_vm.c	Thu Feb 21 00:30:40 2019 +0800
+++ b/njs/njs_vm.c	Thu Feb 21 16:35:52 2019 +0300
@@ -3326,8 +3326,23 @@ njs_value_data_set(njs_value_t *value, v
 }
 
 
+nxt_noinline njs_ret_t
+njs_vm_value_string_set(njs_vm_t *vm, njs_value_t *value, const u_char *start,
+    uint32_t size)
+{
+    return njs_string_set(vm, value, start, size);
+}
+
+
+nxt_noinline u_char *
+njs_vm_value_string_alloc(njs_vm_t *vm, njs_value_t *value, uint32_t size)
+{
+    return njs_string_alloc(vm, value, size, 0);
+}
+
+
 void
-njs_value_error_set(njs_vm_t *vm, njs_value_t *value, const char *fmt, ...)
+njs_vm_value_error_set(njs_vm_t *vm, njs_value_t *value, const char *fmt, ...)
 {
     va_list  args;
     u_char   buf[NXT_MAX_ERROR_STR], *p;
@@ -3431,8 +3446,8 @@ njs_value_is_function(const njs_value_t 
 
 
 nxt_int_t
-njs_value_string_copy(njs_vm_t *vm, nxt_str_t *retval, const njs_value_t *value,
-    uintptr_t *next)
+njs_vm_value_string_copy(njs_vm_t *vm, nxt_str_t *retval,
+    const njs_value_t *value, uintptr_t *next)
 {
     uintptr_t    n;
     njs_array_t  *array;
diff -r 736cfe916b2b -r 2a6f5ffd5e96 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Thu Feb 21 00:30:40 2019 +0800
+++ b/njs/test/njs_unit_test.c	Thu Feb 21 16:35:52 2019 +0300
@@ -11324,7 +11324,7 @@ njs_unit_test_r_get_uri_external(njs_vm_
 
     field = (nxt_str_t *) (p + data);
 
-    return njs_string_create(vm, value, field->start, field->length, 0);
+    return njs_vm_value_string_set(vm, value, field->start, field->length);
 }
 
 
@@ -11355,7 +11355,7 @@ njs_unit_test_r_get_a_external(njs_vm_t 
 
     p = nxt_sprintf(buf, buf + nxt_length(buf), "%uD", r->a);
 
-    return njs_string_create(vm, value, buf, p - buf, 0);
+    return njs_vm_value_string_set(vm, value, buf, p - buf);
 }
 
 
@@ -11373,7 +11373,7 @@ static njs_ret_t
 njs_unit_test_host_external(njs_vm_t *vm, njs_value_t *value, void *obj,
     uintptr_t data)
 {
-    return njs_string_create(vm, value, (u_char *) "АБВГДЕЁЖЗИЙ", 22, 0);
+    return njs_vm_value_string_set(vm, value, (u_char *) "АБВГДЕЁЖЗИЙ", 22);
 }
 
 
@@ -11427,8 +11427,8 @@ njs_unit_test_r_set_vars(njs_vm_t *vm, v
         return NXT_ERROR;
     }
 
-    njs_string_create(vm, &name, key->start, key->length, 0);
-    njs_string_create(vm, &val, value->start, value->length, 0);
+    njs_vm_value_string_set(vm, &name, key->start, key->length);
+    njs_vm_value_string_set(vm, &val, value->start, value->length);
 
     prop = lvlhsh_unit_test_alloc(vm->mem_pool, &name, &val);
     if (prop == NULL) {
@@ -11492,7 +11492,7 @@ njs_unit_test_header_external(njs_vm_t *
 
     size = 7 + h->length;
 
-    p = njs_string_alloc(vm, value, size, 0);
+    p = njs_vm_value_string_alloc(vm, value, size);
     if (p == NULL) {
         return NJS_ERROR;
     }
@@ -11531,7 +11531,7 @@ njs_unit_test_header_next_external(njs_v
         return NXT_DONE;
     }
 
-    return njs_string_create(vm, value, s, 2, 0);
+    return njs_vm_value_string_set(vm, value, s, 2);
 }
 
 
@@ -11550,8 +11550,8 @@ njs_unit_test_method_external(njs_vm_t *
 
     ret = njs_vm_value_to_ext_string(vm, &s, njs_arg(args, nargs, 1), 0);
     if (ret == NXT_OK && s.length == 3 && memcmp(s.start, "YES", 3) == 0) {
-        return njs_string_create(vm, njs_vm_retval(vm), r->uri.start,
-                                 r->uri.length, 0);
+        return njs_vm_value_string_set(vm, njs_vm_retval(vm), r->uri.start,
+                                       r->uri.length);
     }
 
     vm->retval = njs_value_void;


More information about the nginx-devel mailing list