njs_typed_array_t *buffer, *array;
array = njs_typed_array(value);
+ if (njs_slow_path(njs_is_detached_buffer(array->buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
length = njs_typed_array_length(array);
buffer = njs_buffer_alloc_array(vm, length, 0);
const njs_value_t *start, const njs_value_t *end, const char *name,
uint8_t **out_start, uint8_t **out_end)
{
- uint8_t *u8;
- uint64_t num_start, num_end;
- njs_int_t ret;
+ uint64_t num_start, num_end;
+ njs_int_t ret;
+ njs_array_buffer_t *buffer;
num_start = 0;
num_end = num_start;
}
- u8 = njs_typed_array_buffer(array)->u.u8;
- *out_start = &u8[array->offset + num_start];
- *out_end = &u8[array->offset + num_end];
+ buffer = njs_typed_array_buffer(array);
+ if (njs_slow_path(njs_is_detached_buffer(buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
+ *out_start = &buffer->u.u8[array->offset + num_start];
+ *out_end = &buffer->u.u8[array->offset + num_end];
return NJS_OK;
}
}
arr = njs_typed_array(value);
+ if (njs_slow_path(njs_is_detached_buffer(arr->buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
if (njs_slow_path((SIZE_MAX - len) < arr->byte_length)) {
njs_type_error(vm, "Invalid length");
}
arr = njs_typed_array(&retval);
+ if (njs_slow_path(njs_is_detached_buffer(arr->buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
if (njs_slow_path((SIZE_MAX - len) < arr->byte_length)) {
njs_type_error(vm, "Invalid length");
#endif
buffer = array->buffer;
+ if (njs_slow_path(njs_is_detached_buffer(buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
u8 = &buffer->u.u8[index + array->offset];
switch (size) {
#endif
buffer = array->buffer;
+ if (njs_slow_path(njs_is_detached_buffer(buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
u8 = &buffer->u.u8[index + array->offset];
switch (size) {
return ret;
}
+ if (njs_slow_path(njs_is_detached_buffer(buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
memset(start, njs_number_to_uint32(num) & 0xff, end - offset);
}
end = njs_min(end, array->byte_length);
}
+ if (njs_slow_path(njs_is_detached_buffer(array->buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
str.start = &njs_typed_array_buffer(array)->u.u8[array->offset + start];
str.length = end - start;
njs_buffer_prototype_index_of(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t last)
{
- uint8_t *u8, byte;
+ uint8_t byte;
int64_t from, to, increment, length, offset, index, i;
njs_int_t ret;
njs_str_t str;
njs_value_t *this, *value, *value_from, *enc, dst;
+ const uint8_t *u8;
njs_typed_array_t *array, *src;
+ njs_array_buffer_t *buffer;
const njs_buffer_encoding_t *encoding;
encoding = njs_buffer_utf8_encoding();
}
}
- u8 = &njs_typed_array_buffer(array)->u.u8[0];
+ buffer = njs_typed_array_buffer(array);
+ if (njs_slow_path(njs_is_detached_buffer(buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
+ u8 = &buffer->u.u8[0];
offset = array->offset;
switch (value->type) {
goto fail;
}
+ if (njs_slow_path(njs_is_detached_buffer(src->buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
str.start = &src->buffer->u.u8[src->offset];
str.length = src->byte_length;
}
njs_buffer_prototype_to_json(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused)
{
- u_char *p, *end;
- njs_int_t ret;
- njs_value_t *value;
- njs_value_t object, array;
- njs_array_t *arr;
- njs_object_t *obj;
- njs_typed_array_t *ta;
+ u_char *p, *end;
+ njs_int_t ret;
+ njs_value_t *value;
+ njs_value_t object, array;
+ njs_array_t *arr;
+ njs_object_t *obj;
+ njs_typed_array_t *ta;
+ njs_array_buffer_t *buffer;
static const njs_value_t string_buffer = njs_string("Buffer");
return NJS_ERROR;
}
- p = &njs_typed_array_buffer(ta)->u.u8[ta->offset];
+ buffer = njs_typed_array_buffer(ta);
+ if (njs_slow_path(njs_is_detached_buffer(buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
+ p = &buffer->u.u8[ta->offset];
end = p + ta->byte_length;
value = arr->start;
return NULL;
}
- if (!njs_is_undefined(njs_arg(args, nargs, 2))) {
+ if (njs_is_defined(njs_arg(args, nargs, 2))) {
+ ret = njs_value_to_index(vm, njs_argument(args, 2), &size);
+ if (njs_slow_path(ret != NJS_OK)) {
+ return NULL;
+ }
+ }
+
+ if (njs_slow_path(njs_is_detached_buffer(buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NULL;
+ }
+
+ if (njs_is_defined(njs_arg(args, nargs, 2))) {
ret = njs_value_to_index(vm, njs_argument(args, 2), &size);
if (njs_slow_path(ret != NJS_OK)) {
return NULL;
} else if (njs_is_typed_array(value)) {
src_tarray = njs_typed_array(value);
+ if (njs_slow_path(njs_is_detached_buffer(src_tarray->buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NULL;
+ }
+
size = (uint64_t) njs_typed_array_length(src_tarray) * element_size;
} else if (njs_is_object(value)) {
njs_typed_array_create(njs_vm_t *vm, njs_value_t *constructor,
njs_value_t *args, njs_uint_t nargs, njs_value_t *retval)
{
- njs_int_t ret;
- njs_value_t this;
- njs_object_t *object;
+ njs_int_t ret;
+ njs_value_t this;
+ njs_object_t *object;
+ njs_typed_array_t *array;
object = njs_function_new_object(vm, constructor);
if (njs_slow_path(object == NULL)) {
return NJS_ERROR;
}
+ array = njs_typed_array(retval);
+ if (njs_slow_path(njs_is_detached_buffer(array->buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
if (njs_slow_path(nargs == 1 && njs_is_number(&args[0])
- && njs_typed_array_length(njs_typed_array(retval))
+ && njs_typed_array_length(array)
< njs_number(&args[0])))
{
njs_type_error(vm, "Derived TypedArray constructor "
njs_array_buffer_t *buffer;
buffer = array->buffer;
+ if (njs_slow_path(njs_is_detached_buffer(buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NULL;
+ }
ret = njs_array_buffer_writable(vm, buffer);
if (njs_slow_path(ret != NJS_OK)) {
njs_typed_array_prototype_length(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t unused)
{
+ uint32_t length;
njs_value_t *this;
njs_typed_array_t *array;
}
array = njs_typed_array(this);
+ length = njs_typed_array_length(array);
- njs_set_number(&vm->retval, njs_typed_array_length(array));
+ if (njs_slow_path(njs_is_detached_buffer(array->buffer))) {
+ length = 0;
+ }
+
+ njs_set_number(&vm->retval, length);
return NJS_OK;
}
njs_typed_array_prototype_byte_length(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t unused)
{
+ size_t byte_length;
njs_value_t *this;
njs_typed_array_t *array;
}
array = njs_typed_array(this);
+ byte_length = array->byte_length;
- njs_set_number(&vm->retval, array->byte_length);
+ if (njs_slow_path(njs_is_detached_buffer(array->buffer))) {
+ if (njs_is_data_view(this)) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
+ byte_length = 0;
+ }
+
+ njs_set_number(&vm->retval, byte_length);
return NJS_OK;
}
njs_typed_array_prototype_byte_offset(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t unused)
{
+ size_t byte_offset;
njs_value_t *this;
njs_typed_array_t *array;
}
array = njs_typed_array(this);
+ byte_offset = njs_typed_array_offset(array);
+
+ if (njs_slow_path(njs_is_detached_buffer(array->buffer))) {
+ if (njs_is_data_view(this)) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
+ byte_offset = 0;
+ }
- njs_set_number(&vm->retval, njs_typed_array_offset(array));
+ njs_set_number(&vm->retval, byte_offset);
return NJS_OK;
}
if (njs_is_typed_array(src)) {
src_tarray = njs_typed_array(src);
+ if (njs_slow_path(njs_is_detached_buffer(src_tarray->buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
src_length = njs_typed_array_length(src_tarray);
if (njs_slow_path((src_length > length)
}
}
+ if (njs_slow_path(njs_is_detached_buffer(buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
njs_typed_array_prop_set(vm, self, offset + i, num);
}
}
}
array = njs_typed_array(this);
+ if (njs_slow_path(njs_is_detached_buffer(array->buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
length = njs_typed_array_length(array);
setval = njs_lvalue_arg(&lvalue, args, nargs, 1);
array = njs_typed_array(this);
length = njs_typed_array_length(array);
buffer = njs_typed_array_buffer(array);
+ if (njs_slow_path(copy && njs_is_detached_buffer(buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
ret = njs_value_to_integer(vm, njs_arg(args, nargs, 1), &start);
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}
- new_array = njs_typed_array(&vm->retval);
- if (njs_typed_array_length(new_array) < count) {
- njs_type_error(vm, "Derived TypedArray constructor is "
- "too small array");
+ if (count == 0) {
+ return NJS_OK;
+ }
+
+ if (njs_slow_path(njs_is_detached_buffer(buffer))) {
+ njs_type_error(vm, "detached buffer");
return NJS_ERROR;
}
+ new_array = njs_typed_array(&vm->retval);
new_buffer = njs_typed_array_buffer(new_array);
element_size = njs_typed_array_element_size(array->type);
}
array = njs_typed_array(this);
+ if (njs_slow_path(njs_is_detached_buffer(array->buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
length = njs_typed_array_length(array);
value = njs_arg(args, nargs, 1);
value = njs_arg(args, nargs, 3);
- if (njs_is_undefined(value)) {
- final = length;
+ final = length;
- } else {
+ if (njs_is_defined(value)) {
ret = njs_value_to_integer(vm, value, &final);
if (njs_slow_path(ret != NJS_OK)) {
return NJS_ERROR;
njs_typed_array_prototype_iterator(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t type)
{
- double val;
- int64_t i, length;
- njs_int_t ret;
- njs_arr_t results;
- njs_value_t *this, *this_arg, *r;
- njs_value_t arguments[4], retval;
- njs_function_t *function;
- njs_typed_array_t *array, *dst;
+ double val;
+ int64_t i, length;
+ njs_int_t ret;
+ njs_arr_t results;
+ njs_value_t *this, *this_arg, *r;
+ njs_value_t arguments[4], retval;
+ njs_function_t *function;
+ njs_typed_array_t *array, *dst;
+ njs_array_buffer_t *buffer;
this = njs_argument(args, 0);
if (njs_slow_path(!njs_is_typed_array(this))) {
function = njs_function(njs_argument(args, 1));
this_arg = njs_arg(args, nargs, 2);
+ buffer = array->buffer;
results.separate = 0;
results.pointer = 0;
}
for (i = 0; i < length; i++) {
+ if (njs_slow_path(njs_is_detached_buffer(buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
val = njs_typed_array_prop(array, i);
arguments[0] = *this_arg;
}
}
+ if (njs_slow_path(njs_is_detached_buffer(array->buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
v = njs_number(njs_argument(args, 1));
i64 = v;
njs_typed_array_prototype_reduce(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t right)
{
- int64_t i, from, to, increment, length;
- njs_int_t ret;
- njs_value_t *this, accumulator;
- njs_value_t arguments[5];
- njs_function_t *function;
- njs_typed_array_t *array;
+ int64_t i, from, to, increment, length;
+ njs_int_t ret;
+ njs_value_t *this, accumulator;
+ njs_value_t arguments[5];
+ njs_function_t *function;
+ njs_typed_array_t *array;
+ njs_array_buffer_t *buffer;
this = njs_argument(args, 0);
if (njs_slow_path(!njs_is_typed_array(this))) {
increment = 1;
}
+ buffer = array->buffer;
+
if (nargs > 2) {
accumulator = *njs_argument(args, 2);
} else {
+ if (njs_slow_path(njs_is_detached_buffer(buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
njs_set_number(&accumulator, njs_typed_array_prop(array, from));
from += increment;
}
for (i = from; i != to; i += increment) {
+ if (njs_slow_path(njs_is_detached_buffer(buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
njs_set_undefined(&arguments[0]);
arguments[1] = accumulator;
njs_set_number(&arguments[2], njs_typed_array_prop(array, i));
typedef struct {
njs_vm_t *vm;
+ njs_array_buffer_t *buffer;
njs_function_t *function;
njs_bool_t exception;
double (*get)(const void *v);
goto exception;
}
+ if (njs_slow_path(njs_is_detached_buffer(ctx->buffer))) {
+ njs_type_error(ctx->vm, "detached buffer");
+ goto exception;
+ }
+
if (njs_slow_path(isnan(num))) {
return 0;
}
return NJS_ERROR;
}
+ array = njs_typed_array(this);
+ if (njs_slow_path(njs_is_detached_buffer(array->buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
ctx.vm = vm;
+ ctx.buffer = array->buffer;
ctx.exception = 0;
comparefn = njs_arg(args, nargs, 1);
ctx.function = NULL;
}
- array = njs_typed_array(this);
-
switch (array->type) {
case NJS_OBJ_TYPE_UINT8_ARRAY:
case NJS_OBJ_TYPE_UINT8_CLAMPED_ARRAY:
}
array = njs_typed_array(this);
+ if (njs_slow_path(njs_is_detached_buffer(array->buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
arr_length = njs_typed_array_length(array);
separator = njs_arg(args, nargs, 1);
size = 0;
offset = 0;
- buffer = njs_array_buffer(njs_argument(args, 1));
-
ret = njs_value_to_index(vm, njs_arg(args, nargs, 2), &offset);
if (njs_slow_path(ret != NJS_OK)) {
return NJS_ERROR;
}
+ buffer = njs_array_buffer(njs_argument(args, 1));
+ if (njs_slow_path(njs_is_detached_buffer(buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
+
if (!njs_is_undefined(njs_arg(args, nargs, 3))) {
ret = njs_value_to_index(vm, njs_argument(args, 3), &size);
if (njs_slow_path(ret != NJS_OK)) {
#endif
view = njs_data_view(this);
+ if (njs_slow_path(njs_is_detached_buffer(view->buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
if (njs_typed_array_element_size(type) + index > view->byte_length) {
njs_range_error(vm, "index %uL is outside the bound of the buffer",
#endif
view = njs_data_view(this);
+ if (njs_slow_path(njs_is_detached_buffer(view->buffer))) {
+ njs_type_error(vm, "detached buffer");
+ return NJS_ERROR;
+ }
if (njs_typed_array_element_size(type) + index > view->byte_length) {
njs_range_error(vm, "index %uL is outside the bound of the buffer",