From: Dmitry Volyntsev Date: Wed, 23 Oct 2019 11:42:38 +0000 (+0300) Subject: Added njs_vm_array_alloc() and njs_vm_array_push() public API. X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/static/gitweb.js?a=commitdiff_plain;h=a808134055c7839ba5f54a10fd7b6a9d32170236;p=njs.git Added njs_vm_array_alloc() and njs_vm_array_push() public API. --- diff --git a/src/njs.h b/src/njs.h index 6afab906..2d37fee3 100644 --- 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, diff --git a/src/njs_vm.c b/src/njs_vm.c index e9c453da..8564e2f3 100644 --- a/src/njs_vm.c +++ b/src/njs_vm.c @@ -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) {