[njs] Introduced njs_value_buffer_get().
Dmitry Volyntsev
xeioex at nginx.com
Wed May 3 04:13:26 UTC 2023
details: https://hg.nginx.org/njs/rev/830c81bb573f
branches:
changeset: 2098:830c81bb573f
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Mon May 01 17:54:48 2023 -0700
description:
Introduced njs_value_buffer_get().
diffstat:
external/njs_fs_module.c | 31 ++++++++++++-------------------
src/njs.h | 2 ++
src/njs_buffer.c | 34 ++++++++++++++++++++++++++++++++--
src/njs_buffer.h | 4 ----
4 files changed, 46 insertions(+), 25 deletions(-)
diffs (147 lines):
diff -r 1776189b0de6 -r 830c81bb573f external/njs_fs_module.c
--- a/external/njs_fs_module.c Tue May 02 20:33:30 2023 -0700
+++ b/external/njs_fs_module.c Mon May 01 17:54:48 2023 -0700
@@ -1405,14 +1405,12 @@ static njs_int_t
njs_fs_read(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t calltype, njs_value_t *retval)
{
- int64_t fd, length, pos, offset;
- ssize_t n;
- njs_int_t ret;
- njs_str_t data;
- njs_uint_t fd_offset;
- njs_value_t result, *buffer, *value;
- njs_typed_array_t *array;
- njs_array_buffer_t *array_buffer;
+ int64_t fd, length, pos, offset;
+ ssize_t n;
+ njs_int_t ret;
+ njs_str_t data;
+ njs_uint_t fd_offset;
+ njs_value_t result, *buffer, *value;
fd_offset = !!(calltype == NJS_FS_DIRECT);
@@ -1429,13 +1427,8 @@ njs_fs_read(njs_vm_t *vm, njs_value_t *a
*/
buffer = njs_arg(args, nargs, fd_offset + 1);
- array = njs_buffer_slot(vm, buffer, "buffer");
- if (njs_slow_path(array == NULL)) {
- return NJS_ERROR;
- }
-
- array_buffer = njs_typed_array_writable(vm, array);
- if (njs_slow_path(array_buffer == NULL)) {
+ ret = njs_value_buffer_get(vm, buffer, &data);
+ if (njs_slow_path(ret != NJS_OK)) {
return NJS_ERROR;
}
@@ -1445,14 +1438,14 @@ njs_fs_read(njs_vm_t *vm, njs_value_t *a
return ret;
}
- if (njs_slow_path(offset < 0 || (size_t) offset > array->byte_length)) {
+ if (njs_slow_path(offset < 0 || (size_t) offset > data.length)) {
njs_range_error(vm, "offset is out of range (must be <= %z)",
- array->byte_length);
+ data.length);
return NJS_ERROR;
}
- data.length = array->byte_length - offset;
- data.start = &array_buffer->u.u8[array->offset + offset];
+ data.length -= offset;
+ data.start += offset;
value = njs_arg(args, nargs, fd_offset + 3);
diff -r 1776189b0de6 -r 830c81bb573f src/njs.h
--- a/src/njs.h Tue May 02 20:33:30 2023 -0700
+++ b/src/njs.h Mon May 01 17:54:48 2023 -0700
@@ -423,6 +423,8 @@ NJS_EXPORT njs_int_t njs_vm_string_compa
NJS_EXPORT njs_int_t njs_vm_value_array_buffer_set(njs_vm_t *vm,
njs_value_t *value, const u_char *start, uint32_t size);
+NJS_EXPORT njs_int_t njs_value_buffer_get(njs_vm_t *vm, njs_value_t *value,
+ njs_str_t *dst);
/*
* Sets a Buffer value.
* start data is not copied and should not be freed.
diff -r 1776189b0de6 -r 830c81bb573f src/njs_buffer.c
--- a/src/njs_buffer.c Tue May 02 20:33:30 2023 -0700
+++ b/src/njs_buffer.c Mon May 01 17:54:48 2023 -0700
@@ -183,7 +183,7 @@ njs_buffer_set(njs_vm_t *vm, njs_value_t
}
-njs_typed_array_t *
+static njs_typed_array_t *
njs_buffer_alloc(njs_vm_t *vm, size_t size, njs_bool_t zeroing)
{
njs_value_t value;
@@ -621,7 +621,7 @@ njs_buffer_slot_internal(njs_vm_t *vm, n
}
-njs_typed_array_t *
+static njs_typed_array_t *
njs_buffer_slot(njs_vm_t *vm, njs_value_t *value, const char *name)
{
njs_typed_array_t *array;
@@ -637,6 +637,36 @@ njs_buffer_slot(njs_vm_t *vm, njs_value_
}
+njs_int_t
+njs_value_buffer_get(njs_vm_t *vm, njs_value_t *value, njs_str_t *dst)
+{
+ njs_typed_array_t *array;
+ njs_array_buffer_t *array_buffer;
+
+ if (njs_slow_path(!(njs_is_typed_array(value)
+ || njs_is_data_view(value))))
+ {
+ njs_type_error(vm, "first argument must be a Buffer or DataView");
+ return NJS_ERROR;
+ }
+
+ array = njs_typed_array(value);
+ if (njs_slow_path(array == NULL)) {
+ return NJS_ERROR;
+ }
+
+ array_buffer = njs_typed_array_writable(vm, array);
+ if (njs_slow_path(array_buffer == NULL)) {
+ return NJS_ERROR;
+ }
+
+ dst->length = array->byte_length;
+ dst->start = &array_buffer->u.u8[array->offset];
+
+ return NJS_OK;
+}
+
+
static njs_int_t
njs_buffer_array_range(njs_vm_t *vm, njs_typed_array_t *array,
const njs_value_t *start, const njs_value_t *end, const char *name,
diff -r 1776189b0de6 -r 830c81bb573f src/njs_buffer.h
--- a/src/njs_buffer.h Tue May 02 20:33:30 2023 -0700
+++ b/src/njs_buffer.h Mon May 01 17:54:48 2023 -0700
@@ -21,14 +21,10 @@ typedef struct {
} njs_buffer_encoding_t;
-njs_typed_array_t *njs_buffer_slot(njs_vm_t *vm, njs_value_t *value,
- const char *name);
njs_int_t njs_buffer_set(njs_vm_t *vm, njs_value_t *value, const u_char *start,
uint32_t size);
njs_int_t njs_buffer_new(njs_vm_t *vm, njs_value_t *value, const u_char *start,
uint32_t size);
-njs_typed_array_t *njs_buffer_alloc(njs_vm_t *vm, size_t size,
- njs_bool_t zeroing);
const njs_buffer_encoding_t *njs_buffer_encoding(njs_vm_t *vm,
const njs_value_t *value, njs_bool_t thrw);
More information about the nginx-devel
mailing list