static njs_typed_array_t *
-njs_buffer_slot(njs_vm_t *vm, njs_value_t *value, const char *name)
+njs_buffer_slot_internal(njs_vm_t *vm, njs_value_t *value)
{
njs_typed_array_t *array;
- if (njs_slow_path(!njs_is_object(value))) {
- goto failed;
+ if (njs_is_object(value)) {
+ array = njs_object_proto_lookup(njs_object(value), NJS_TYPED_ARRAY,
+ njs_typed_array_t);
+
+ if (array != NULL && array->type == NJS_OBJ_TYPE_UINT8_ARRAY) {
+ return array;
+ }
}
- array = njs_object_proto_lookup(njs_object(value), NJS_TYPED_ARRAY,
- njs_typed_array_t);
+ return NULL;
+}
- if (njs_slow_path(array != NULL
- && array->type != NJS_OBJ_TYPE_UINT8_ARRAY))
- {
- goto failed;
- }
- return array;
+static njs_typed_array_t *
+njs_buffer_slot(njs_vm_t *vm, njs_value_t *value, const char *name)
+{
+ njs_typed_array_t *array;
-failed:
+ array = njs_buffer_slot_internal(vm, value);
+ if (njs_slow_path(array == NULL)) {
+ njs_type_error(vm, "\"%s\" argument must be an instance "
+ "of Buffer or Uint8Array", name);
+ return NULL;
+ }
- njs_type_error(vm, "\"%s\" argument must be an instance "
- "of Buffer or Uint8Array", name);
- return NULL;
+ return array;
}
{
njs_typed_array_t *array;
- array = njs_buffer_slot(vm, value, "this");
+ array = njs_buffer_slot_internal(vm, value);
if (njs_slow_path(array == NULL)) {
njs_set_undefined(retval);
return NJS_DECLINED;
"})"),
njs_str("true") },
+ { njs_str("Buffer.from([1,2]).equals(new ArrayBuffer(1))"),
+ njs_str("TypeError: \"target\" argument must be an instance of Buffer or Uint8Array") },
+
+ { njs_str("Buffer.from([1,2]).equals(1)"),
+ njs_str("TypeError: \"target\" argument must be an instance of Buffer or Uint8Array") },
+
{ njs_str("var buf = Buffer.alloc(4);"
"buf.fill('ZXZpbA==', 'base64')"),
njs_str("evil") },