mirror of
https://github.com/nodejs/node.git
synced 2026-11-29 14:27:57 -05:00
buffer: allow ARGS_THIS to accept a name
Allowing the name to be passed to the ARGS_THIS macro will make it easier to share code with the Uint8Array implementation. PR-URL: https://github.com/nodejs/io.js/pull/1825 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
1 parent
f49aed1568
commit
1ed4e1328e
1 file changed
+69
-54
+69
-54
@@ -18,13 +18,17 @@
|
||||
if (!(r)) return env->ThrowRangeError("out of range index"); \
|
||||
} while (0)
|
||||
|
||||
#define ARGS_THIS(argT) \
|
||||
Local<Object> obj = argT; \
|
||||
size_t obj_length = obj->GetIndexedPropertiesExternalArrayDataLength(); \
|
||||
char* obj_data = static_cast<char*>( \
|
||||
obj->GetIndexedPropertiesExternalArrayData()); \
|
||||
if (obj_length > 0) \
|
||||
CHECK_NE(obj_data, nullptr);
|
||||
#define ARGS_THIS_DEC(name) \
|
||||
size_t name##_length; \
|
||||
char* name##_data;
|
||||
|
||||
#define ARGS_THIS(argT, name) \
|
||||
Local<Object> name = argT; \
|
||||
name##_length = name->GetIndexedPropertiesExternalArrayDataLength(); \
|
||||
name##_data = static_cast<char*>( \
|
||||
name->GetIndexedPropertiesExternalArrayData()); \
|
||||
if (name##_length > 0) \
|
||||
CHECK_NE(name##_data, nullptr);
|
||||
|
||||
#define SLICE_START_END(start_arg, end_arg, end_max) \
|
||||
size_t start; \
|
||||
@@ -222,24 +226,27 @@ Local<Object> Use(Environment* env, char* data, uint32_t length) {
|
||||
template <encoding encoding>
|
||||
void StringSlice(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
Isolate* isolate = env->isolate();
|
||||
ARGS_THIS_DEC(ts_obj);
|
||||
|
||||
ARGS_THIS(args.This())
|
||||
SLICE_START_END(args[0], args[1], obj_length)
|
||||
ARGS_THIS(args.This(), ts_obj);
|
||||
SLICE_START_END(args[0], args[1], ts_obj_length)
|
||||
|
||||
args.GetReturnValue().Set(
|
||||
StringBytes::Encode(env->isolate(), obj_data + start, length, encoding));
|
||||
StringBytes::Encode(isolate, ts_obj_data + start, length, encoding));
|
||||
}
|
||||
|
||||
|
||||
template <>
|
||||
void StringSlice<UCS2>(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
ARGS_THIS_DEC(ts_obj);
|
||||
|
||||
ARGS_THIS(args.This())
|
||||
SLICE_START_END(args[0], args[1], obj_length)
|
||||
ARGS_THIS(args.This(), ts_obj);
|
||||
SLICE_START_END(args[0], args[1], ts_obj_length)
|
||||
length /= 2;
|
||||
|
||||
const char* data = obj_data + start;
|
||||
const char* data = ts_obj_data + start;
|
||||
const uint16_t* buf;
|
||||
bool release = false;
|
||||
|
||||
@@ -307,8 +314,9 @@ void Copy(const FunctionCallbackInfo<Value> &args) {
|
||||
return env->ThrowTypeError("first arg should be a Buffer");
|
||||
|
||||
Local<Object> target = args[0]->ToObject(env->isolate());
|
||||
ARGS_THIS_DEC(ts_obj);
|
||||
|
||||
ARGS_THIS(args.This())
|
||||
ARGS_THIS(args.This(), ts_obj);
|
||||
size_t target_length = target->GetIndexedPropertiesExternalArrayDataLength();
|
||||
char* target_data = static_cast<char*>(
|
||||
target->GetIndexedPropertiesExternalArrayData());
|
||||
@@ -318,13 +326,13 @@ void Copy(const FunctionCallbackInfo<Value> &args) {
|
||||
|
||||
CHECK_NOT_OOB(ParseArrayIndex(args[1], 0, &target_start));
|
||||
CHECK_NOT_OOB(ParseArrayIndex(args[2], 0, &source_start));
|
||||
CHECK_NOT_OOB(ParseArrayIndex(args[3], obj_length, &source_end));
|
||||
CHECK_NOT_OOB(ParseArrayIndex(args[3], ts_obj_length, &source_end));
|
||||
|
||||
// Copy 0 bytes; we're done
|
||||
if (target_start >= target_length || source_start >= source_end)
|
||||
return args.GetReturnValue().Set(0);
|
||||
|
||||
if (source_start > obj_length)
|
||||
if (source_start > ts_obj_length)
|
||||
return env->ThrowRangeError("out of range index");
|
||||
|
||||
if (source_end - source_start > target_length - target_start)
|
||||
@@ -332,48 +340,49 @@ void Copy(const FunctionCallbackInfo<Value> &args) {
|
||||
|
||||
uint32_t to_copy = MIN(MIN(source_end - source_start,
|
||||
target_length - target_start),
|
||||
obj_length - source_start);
|
||||
ts_obj_length - source_start);
|
||||
|
||||
memmove(target_data + target_start, obj_data + source_start, to_copy);
|
||||
memmove(target_data + target_start, ts_obj_data + source_start, to_copy);
|
||||
args.GetReturnValue().Set(to_copy);
|
||||
}
|
||||
|
||||
|
||||
void Fill(const FunctionCallbackInfo<Value>& args) {
|
||||
ARGS_THIS(args[0].As<Object>())
|
||||
ARGS_THIS_DEC(ts_obj);
|
||||
ARGS_THIS(args[0].As<Object>(), ts_obj);
|
||||
|
||||
size_t start = args[2]->Uint32Value();
|
||||
size_t end = args[3]->Uint32Value();
|
||||
size_t length = end - start;
|
||||
CHECK(length + start <= obj_length);
|
||||
CHECK(length + start <= ts_obj_length);
|
||||
|
||||
if (args[1]->IsNumber()) {
|
||||
int value = args[1]->Uint32Value() & 255;
|
||||
memset(obj_data + start, value, length);
|
||||
memset(ts_obj_data + start, value, length);
|
||||
return;
|
||||
}
|
||||
|
||||
node::Utf8Value str(args.GetIsolate(), args[1]);
|
||||
size_t str_length = str.length();
|
||||
size_t in_there = str_length;
|
||||
char* ptr = obj_data + start + str_length;
|
||||
char* ptr = ts_obj_data + start + str_length;
|
||||
|
||||
if (str_length == 0)
|
||||
return;
|
||||
|
||||
memcpy(obj_data + start, *str, MIN(str_length, length));
|
||||
memcpy(ts_obj_data + start, *str, MIN(str_length, length));
|
||||
|
||||
if (str_length >= length)
|
||||
return;
|
||||
|
||||
while (in_there < length - in_there) {
|
||||
memcpy(ptr, obj_data + start, in_there);
|
||||
memcpy(ptr, ts_obj_data + start, in_there);
|
||||
ptr += in_there;
|
||||
in_there *= 2;
|
||||
}
|
||||
|
||||
if (in_there < length) {
|
||||
memcpy(ptr, obj_data + start, length - in_there);
|
||||
memcpy(ptr, ts_obj_data + start, length - in_there);
|
||||
in_there = length;
|
||||
}
|
||||
}
|
||||
@@ -382,8 +391,9 @@ void Fill(const FunctionCallbackInfo<Value>& args) {
|
||||
template <encoding encoding>
|
||||
void StringWrite(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
ARGS_THIS_DEC(ts_obj);
|
||||
|
||||
ARGS_THIS(args.This())
|
||||
ARGS_THIS(args.This(), ts_obj);
|
||||
|
||||
if (!args[0]->IsString())
|
||||
return env->ThrowTypeError("Argument must be a string");
|
||||
@@ -397,18 +407,18 @@ void StringWrite(const FunctionCallbackInfo<Value>& args) {
|
||||
size_t max_length;
|
||||
|
||||
CHECK_NOT_OOB(ParseArrayIndex(args[1], 0, &offset));
|
||||
CHECK_NOT_OOB(ParseArrayIndex(args[2], obj_length - offset, &max_length));
|
||||
CHECK_NOT_OOB(ParseArrayIndex(args[2], ts_obj_length - offset, &max_length));
|
||||
|
||||
max_length = MIN(obj_length - offset, max_length);
|
||||
max_length = MIN(ts_obj_length - offset, max_length);
|
||||
|
||||
if (max_length == 0)
|
||||
return args.GetReturnValue().Set(0);
|
||||
|
||||
if (offset >= obj_length)
|
||||
if (offset >= ts_obj_length)
|
||||
return env->ThrowRangeError("Offset is out of bounds");
|
||||
|
||||
uint32_t written = StringBytes::Write(env->isolate(),
|
||||
obj_data + offset,
|
||||
ts_obj_data + offset,
|
||||
max_length,
|
||||
str,
|
||||
encoding,
|
||||
@@ -459,10 +469,11 @@ static inline void Swizzle(char* start, unsigned int len) {
|
||||
|
||||
template <typename T, enum Endianness endianness>
|
||||
void ReadFloatGeneric(const FunctionCallbackInfo<Value>& args) {
|
||||
ARGS_THIS(args[0].As<Object>());
|
||||
ARGS_THIS_DEC(ts_obj);
|
||||
ARGS_THIS(args[0].As<Object>(), ts_obj);
|
||||
|
||||
uint32_t offset = args[1]->Uint32Value();
|
||||
CHECK_LE(offset + sizeof(T), obj_length);
|
||||
CHECK_LE(offset + sizeof(T), ts_obj_length);
|
||||
|
||||
union NoAlias {
|
||||
T val;
|
||||
@@ -470,7 +481,7 @@ void ReadFloatGeneric(const FunctionCallbackInfo<Value>& args) {
|
||||
};
|
||||
|
||||
union NoAlias na;
|
||||
const char* ptr = static_cast<const char*>(obj_data) + offset;
|
||||
const char* ptr = static_cast<const char*>(ts_obj_data) + offset;
|
||||
memcpy(na.bytes, ptr, sizeof(na.bytes));
|
||||
if (endianness != GetEndianness())
|
||||
Swizzle(na.bytes, sizeof(na.bytes));
|
||||
@@ -501,11 +512,12 @@ void ReadDoubleBE(const FunctionCallbackInfo<Value>& args) {
|
||||
|
||||
template <typename T, enum Endianness endianness>
|
||||
uint32_t WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) {
|
||||
ARGS_THIS(args[0].As<Object>())
|
||||
ARGS_THIS_DEC(ts_obj);
|
||||
ARGS_THIS(args[0].As<Object>(), ts_obj);
|
||||
|
||||
T val = args[1]->NumberValue();
|
||||
uint32_t offset = args[2]->Uint32Value();
|
||||
CHECK_LE(offset + sizeof(T), obj_length);
|
||||
CHECK_LE(offset + sizeof(T), ts_obj_length);
|
||||
|
||||
union NoAlias {
|
||||
T val;
|
||||
@@ -513,7 +525,7 @@ uint32_t WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) {
|
||||
};
|
||||
|
||||
union NoAlias na = { val };
|
||||
char* ptr = static_cast<char*>(obj_data) + offset;
|
||||
char* ptr = static_cast<char*>(ts_obj_data) + offset;
|
||||
if (endianness != GetEndianness())
|
||||
Swizzle(na.bytes, sizeof(na.bytes));
|
||||
memcpy(ptr, na.bytes, sizeof(na.bytes));
|
||||
@@ -603,28 +615,29 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
|
||||
ASSERT(args[1]->IsString());
|
||||
ASSERT(args[2]->IsNumber());
|
||||
|
||||
ARGS_THIS(args[0].As<Object>());
|
||||
ARGS_THIS_DEC(ts_obj);
|
||||
ARGS_THIS(args[0].As<Object>(), ts_obj);
|
||||
node::Utf8Value str(args.GetIsolate(), args[1]);
|
||||
int32_t offset_i32 = args[2]->Int32Value();
|
||||
uint32_t offset;
|
||||
|
||||
if (offset_i32 < 0) {
|
||||
if (offset_i32 + static_cast<int32_t>(obj_length) < 0)
|
||||
if (offset_i32 + static_cast<int32_t>(ts_obj_length) < 0)
|
||||
offset = 0;
|
||||
else
|
||||
offset = static_cast<uint32_t>(obj_length + offset_i32);
|
||||
offset = static_cast<uint32_t>(ts_obj_length + offset_i32);
|
||||
} else {
|
||||
offset = static_cast<uint32_t>(offset_i32);
|
||||
}
|
||||
|
||||
if (str.length() == 0 ||
|
||||
obj_length == 0 ||
|
||||
ts_obj_length == 0 ||
|
||||
(offset != 0 && str.length() + offset <= str.length()) ||
|
||||
str.length() + offset > obj_length)
|
||||
str.length() + offset > ts_obj_length)
|
||||
return args.GetReturnValue().Set(-1);
|
||||
|
||||
int32_t r =
|
||||
IndexOf(obj_data + offset, obj_length - offset, *str, str.length());
|
||||
IndexOf(ts_obj_data + offset, ts_obj_length - offset, *str, str.length());
|
||||
args.GetReturnValue().Set(r == -1 ? -1 : static_cast<int32_t>(r + offset));
|
||||
}
|
||||
|
||||
@@ -634,7 +647,8 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) {
|
||||
ASSERT(args[1]->IsObject());
|
||||
ASSERT(args[2]->IsNumber());
|
||||
|
||||
ARGS_THIS(args[0].As<Object>());
|
||||
ARGS_THIS_DEC(ts_obj);
|
||||
ARGS_THIS(args[0].As<Object>(), ts_obj);
|
||||
Local<Object> buf = args[1].As<Object>();
|
||||
int32_t offset_i32 = args[2]->Int32Value();
|
||||
size_t buf_length = buf->GetIndexedPropertiesExternalArrayDataLength();
|
||||
@@ -646,22 +660,22 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) {
|
||||
CHECK_NE(buf_data, nullptr);
|
||||
|
||||
if (offset_i32 < 0) {
|
||||
if (offset_i32 + static_cast<int32_t>(obj_length) < 0)
|
||||
if (offset_i32 + static_cast<int32_t>(ts_obj_length) < 0)
|
||||
offset = 0;
|
||||
else
|
||||
offset = static_cast<uint32_t>(obj_length + offset_i32);
|
||||
offset = static_cast<uint32_t>(ts_obj_length + offset_i32);
|
||||
} else {
|
||||
offset = static_cast<uint32_t>(offset_i32);
|
||||
}
|
||||
|
||||
if (buf_length == 0 ||
|
||||
obj_length == 0 ||
|
||||
ts_obj_length == 0 ||
|
||||
(offset != 0 && buf_length + offset <= buf_length) ||
|
||||
buf_length + offset > obj_length)
|
||||
buf_length + offset > ts_obj_length)
|
||||
return args.GetReturnValue().Set(-1);
|
||||
|
||||
int32_t r =
|
||||
IndexOf(obj_data + offset, obj_length - offset, buf_data, buf_length);
|
||||
IndexOf(ts_obj_data + offset, ts_obj_length - offset, buf_data, buf_length);
|
||||
args.GetReturnValue().Set(r == -1 ? -1 : static_cast<int32_t>(r + offset));
|
||||
}
|
||||
|
||||
@@ -671,27 +685,28 @@ void IndexOfNumber(const FunctionCallbackInfo<Value>& args) {
|
||||
ASSERT(args[1]->IsNumber());
|
||||
ASSERT(args[2]->IsNumber());
|
||||
|
||||
ARGS_THIS(args[0].As<Object>());
|
||||
ARGS_THIS_DEC(ts_obj);
|
||||
ARGS_THIS(args[0].As<Object>(), ts_obj);
|
||||
uint32_t needle = args[1]->Uint32Value();
|
||||
int32_t offset_i32 = args[2]->Int32Value();
|
||||
uint32_t offset;
|
||||
|
||||
if (offset_i32 < 0) {
|
||||
if (offset_i32 + static_cast<int32_t>(obj_length) < 0)
|
||||
if (offset_i32 + static_cast<int32_t>(ts_obj_length) < 0)
|
||||
offset = 0;
|
||||
else
|
||||
offset = static_cast<uint32_t>(obj_length + offset_i32);
|
||||
offset = static_cast<uint32_t>(ts_obj_length + offset_i32);
|
||||
} else {
|
||||
offset = static_cast<uint32_t>(offset_i32);
|
||||
}
|
||||
|
||||
if (obj_length == 0 || offset + 1 > obj_length)
|
||||
if (ts_obj_length == 0 || offset + 1 > ts_obj_length)
|
||||
return args.GetReturnValue().Set(-1);
|
||||
|
||||
void* ptr = memchr(obj_data + offset, needle, obj_length - offset);
|
||||
void* ptr = memchr(ts_obj_data + offset, needle, ts_obj_length - offset);
|
||||
char* ptr_char = static_cast<char*>(ptr);
|
||||
args.GetReturnValue().Set(
|
||||
ptr ? static_cast<int32_t>(ptr_char - obj_data) : -1);
|
||||
ptr ? static_cast<int32_t>(ptr_char - ts_obj_data) : -1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user