From bec924f0ffac2b8809fd915bb8d8796db8068c3b Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Tue, 26 May 2015 14:34:12 -0600 Subject: [PATCH] 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 --- src/node_buffer.cc | 139 ++++++++++++++++++++++++++------------------- 1 file changed, 81 insertions(+), 58 deletions(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 5133140a8d0d23..be7cb50ffe992c 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -24,13 +24,17 @@ return env->ThrowTypeError("argument should be a Buffer"); \ } while (0) -#define ARGS_THIS(argT) \ - Local obj = argT; \ - size_t obj_length = obj->GetIndexedPropertiesExternalArrayDataLength(); \ - char* obj_data = static_cast( \ - 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 name = argT; \ + name##_length = name->GetIndexedPropertiesExternalArrayDataLength(); \ + name##_data = static_cast( \ + name->GetIndexedPropertiesExternalArrayData()); \ + if (name##_length > 0) \ + CHECK_NE(name##_data, nullptr); #define SLICE_START_END(start_arg, end_arg, end_max) \ size_t start; \ @@ -228,34 +232,40 @@ Local Use(Environment* env, char* data, uint32_t length) { template void StringSlice(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); + Isolate* isolate = env->isolate(); THROW_AND_RETURN_UNLESS_BUFFER(env, args.This()); - ARGS_THIS(args.This()) - if (obj_length == 0) + ARGS_THIS_DEC(ts_obj); + ARGS_THIS(args.This(), ts_obj); + + if (ts_obj_length == 0) return args.GetReturnValue().SetEmptyString(); - SLICE_START_END(args[0], args[1], obj_length) + 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(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); + ARGS_THIS_DEC(ts_obj); THROW_AND_RETURN_UNLESS_BUFFER(env, args.This()); - ARGS_THIS(args.This()) + ARGS_THIS(args.This(), ts_obj); + + + SLICE_START_END(args[0], args[1], ts_obj_length) - if (obj_length == 0) + if (ts_obj_length == 0) return args.GetReturnValue().SetEmptyString(); - SLICE_START_END(args[0], args[1], obj_length) length /= 2; - const char* data = obj_data + start; + const char* data = ts_obj_data + start; const uint16_t* buf; bool release = false; @@ -322,10 +332,13 @@ void Copy(const FunctionCallbackInfo &args) { if (!HasInstance(args[0])) return env->ThrowTypeError("first arg should be a Buffer"); - Local target = args[0].As(); - THROW_AND_RETURN_UNLESS_BUFFER(env, args.This()); - ARGS_THIS(args.This()) + + Local target = args[0]->ToObject(env->isolate()); + ARGS_THIS_DEC(ts_obj); + + ARGS_THIS(args.This(), ts_obj); + size_t target_length = target->GetIndexedPropertiesExternalArrayDataLength(); char* target_data = static_cast( target->GetIndexedPropertiesExternalArrayData()); @@ -335,13 +348,13 @@ void Copy(const FunctionCallbackInfo &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) @@ -349,49 +362,50 @@ void Copy(const FunctionCallbackInfo &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& args) { THROW_AND_RETURN_UNLESS_BUFFER(Environment::GetCurrent(args), args[0]); - ARGS_THIS(args[0].As()) + ARGS_THIS_DEC(ts_obj); + ARGS_THIS(args[0].As(), 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; } } @@ -400,9 +414,10 @@ void Fill(const FunctionCallbackInfo& args) { template void StringWrite(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); + ARGS_THIS_DEC(ts_obj); THROW_AND_RETURN_UNLESS_BUFFER(env, args.This()); - ARGS_THIS(args.This()) + ARGS_THIS(args.This(), ts_obj); if (!args[0]->IsString()) return env->ThrowTypeError("Argument must be a string"); @@ -416,18 +431,18 @@ void StringWrite(const FunctionCallbackInfo& 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, @@ -479,10 +494,11 @@ static inline void Swizzle(char* start, unsigned int len) { template void ReadFloatGeneric(const FunctionCallbackInfo& args) { THROW_AND_RETURN_UNLESS_BUFFER(Environment::GetCurrent(args), args[0]); - ARGS_THIS(args[0].As()); + ARGS_THIS_DEC(ts_obj); + ARGS_THIS(args[0].As(), 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; @@ -490,7 +506,7 @@ void ReadFloatGeneric(const FunctionCallbackInfo& args) { }; union NoAlias na; - const char* ptr = static_cast(obj_data) + offset; + const char* ptr = static_cast(ts_obj_data) + offset; memcpy(na.bytes, ptr, sizeof(na.bytes)); if (endianness != GetEndianness()) Swizzle(na.bytes, sizeof(na.bytes)); @@ -521,11 +537,12 @@ void ReadDoubleBE(const FunctionCallbackInfo& args) { template uint32_t WriteFloatGeneric(const FunctionCallbackInfo& args) { - ARGS_THIS(args[0].As()) + ARGS_THIS_DEC(ts_obj); + ARGS_THIS(args[0].As(), 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; @@ -533,7 +550,7 @@ uint32_t WriteFloatGeneric(const FunctionCallbackInfo& args) { }; union NoAlias na = { val }; - char* ptr = static_cast(obj_data) + offset; + char* ptr = static_cast(ts_obj_data) + offset; if (endianness != GetEndianness()) Swizzle(na.bytes, sizeof(na.bytes)); memcpy(ptr, na.bytes, sizeof(na.bytes)); @@ -631,28 +648,30 @@ void IndexOfString(const FunctionCallbackInfo& args) { ASSERT(args[2]->IsNumber()); THROW_AND_RETURN_UNLESS_BUFFER(Environment::GetCurrent(args), args[0]); - ARGS_THIS(args[0].As()); + ARGS_THIS_DEC(ts_obj); + ARGS_THIS(args[0].As(), 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(obj_length) < 0) + if (offset_i32 + static_cast(ts_obj_length) < 0) offset = 0; else - offset = static_cast(obj_length + offset_i32); + offset = static_cast(ts_obj_length + offset_i32); } else { offset = static_cast(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(r + offset)); } @@ -662,7 +681,9 @@ void IndexOfBuffer(const FunctionCallbackInfo& args) { ASSERT(args[2]->IsNumber()); THROW_AND_RETURN_UNLESS_BUFFER(Environment::GetCurrent(args), args[0]); - ARGS_THIS(args[0].As()); + ARGS_THIS_DEC(ts_obj); + ARGS_THIS(args[0].As(), ts_obj); + Local buf = args[1].As(); int32_t offset_i32 = args[2]->Int32Value(); size_t buf_length = buf->GetIndexedPropertiesExternalArrayDataLength(); @@ -674,22 +695,22 @@ void IndexOfBuffer(const FunctionCallbackInfo& args) { CHECK_NE(buf_data, nullptr); if (offset_i32 < 0) { - if (offset_i32 + static_cast(obj_length) < 0) + if (offset_i32 + static_cast(ts_obj_length) < 0) offset = 0; else - offset = static_cast(obj_length + offset_i32); + offset = static_cast(ts_obj_length + offset_i32); } else { offset = static_cast(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(r + offset)); } @@ -699,27 +720,29 @@ void IndexOfNumber(const FunctionCallbackInfo& args) { ASSERT(args[2]->IsNumber()); THROW_AND_RETURN_UNLESS_BUFFER(Environment::GetCurrent(args), args[0]); - ARGS_THIS(args[0].As()); + ARGS_THIS_DEC(ts_obj); + ARGS_THIS(args[0].As(), 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(obj_length) < 0) + if (offset_i32 + static_cast(ts_obj_length) < 0) offset = 0; else - offset = static_cast(obj_length + offset_i32); + offset = static_cast(ts_obj_length + offset_i32); } else { offset = static_cast(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(ptr); args.GetReturnValue().Set( - ptr ? static_cast(ptr_char - obj_data) : -1); + ptr ? static_cast(ptr_char - ts_obj_data) : -1); }