[njs] Added njs_vm_array_alloc() and njs_vm_array_push() public API.

Dmitry Volyntsev xeioex at nginx.com
Wed Oct 23 11:47:15 UTC 2019


details:   https://hg.nginx.org/njs/rev/5e136f9a5954
branches:  
changeset: 1196:5e136f9a5954
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed Oct 23 14:42:38 2019 +0300
description:
Added njs_vm_array_alloc() and njs_vm_array_push() public API.

diffstat:

 src/njs.h    |   4 ++++
 src/njs_vm.c |  39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 0 deletions(-)

diffs (63 lines):

diff -r 47cdd4680fc2 -r 5e136f9a5954 src/njs.h
--- a/src/njs.h	Tue Oct 22 19:58:52 2019 +0300
+++ b/src/njs.h	Wed Oct 23 14:42:38 2019 +0300
@@ -305,6 +305,10 @@ NJS_EXPORT njs_int_t njs_vm_object_alloc
 NJS_EXPORT njs_value_t *njs_vm_object_prop(njs_vm_t *vm,
     const njs_value_t *value, const njs_str_t *key);
 
+NJS_EXPORT njs_int_t njs_vm_array_alloc(njs_vm_t *vm, njs_value_t *retval,
+    uint32_t spare);
+NJS_EXPORT njs_value_t *njs_vm_array_push(njs_vm_t *vm, njs_value_t *value);
+
 NJS_EXPORT njs_int_t njs_vm_json_parse(njs_vm_t *vm, njs_value_t *args,
     njs_uint_t nargs);
 NJS_EXPORT njs_int_t njs_vm_json_stringify(njs_vm_t *vm, njs_value_t *args,
diff -r 47cdd4680fc2 -r 5e136f9a5954 src/njs_vm.c
--- a/src/njs_vm.c	Tue Oct 22 19:58:52 2019 +0300
+++ b/src/njs_vm.c	Wed Oct 23 14:42:38 2019 +0300
@@ -938,6 +938,45 @@ done:
 }
 
 
+njs_int_t
+njs_vm_array_alloc(njs_vm_t *vm, njs_value_t *retval, uint32_t spare)
+{
+    njs_array_t  *array;
+
+    array = njs_array_alloc(vm, 0, spare);
+
+    if (njs_slow_path(array == NULL)) {
+        return NJS_ERROR;
+    }
+
+    njs_set_array(retval, array);
+
+    return NJS_OK;
+}
+
+
+njs_value_t *
+njs_vm_array_push(njs_vm_t *vm, njs_value_t *value)
+{
+    njs_int_t    ret;
+    njs_array_t  *array;
+
+    if (njs_slow_path(!njs_is_array(value))) {
+        njs_type_error(vm, "njs_vm_array_push() argument is not array");
+        return NULL;
+    }
+
+    array = njs_array(value);
+
+    ret = njs_array_expand(vm, array, 0, 1);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return NULL;
+    }
+
+    return &array->start[array->length++];
+}
+
+
 njs_value_t *
 njs_vm_object_prop(njs_vm_t *vm, const njs_value_t *value, const njs_str_t *key)
 {


More information about the nginx-devel mailing list