]> git.kaiwu.me - njs.git/commitdiff
Added njs_vm_array_alloc() and njs_vm_array_push() public API.
authorDmitry Volyntsev <xeioex@nginx.com>
Wed, 23 Oct 2019 11:42:38 +0000 (14:42 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Wed, 23 Oct 2019 11:42:38 +0000 (14:42 +0300)
src/njs.h
src/njs_vm.c

index 6afab90691ae589b222cf1d3e2575fba8eda5906..2d37fee3a2d54451cf5bc253505fbc6d4908f0b8 100644 (file)
--- a/src/njs.h
+++ b/src/njs.h
@@ -305,6 +305,10 @@ NJS_EXPORT njs_int_t njs_vm_object_alloc(njs_vm_t *vm, njs_value_t *retval,
 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,
index e9c453dac5f30b98e806b31263efac99de8b8ac2..8564e2f339ed5f39b7d7fdc1951e5c2ab3c66fc5 100644 (file)
@@ -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)
 {