Skip to content

Commit

Permalink
buffer: make additional changes to native API
Browse files Browse the repository at this point in the history
Address comments and deprecations left in source files. These changes
include:

* Remove the deprecated API.
* Change Buffer::New() that did a copy of the data to Buffer::Copy()
* Change Buffer::Use() to Buffer::New()

PR-URL: #1825
Reviewed-By: Ben Noordhuis <[email protected]>
  • Loading branch information
trevnorris authored and rvagg committed Jul 22, 2015
1 parent d6fb710 commit 54e9a0b
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 46 deletions.
8 changes: 4 additions & 4 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ MaybeLocal<Object> New(Isolate* isolate,
}

Local<Object> buf;
if (Use(isolate, data, actual).ToLocal(&buf))
if (New(isolate, data, actual).ToLocal(&buf))
return scope.Escape(buf);

// Object failed to be created. Clean up resources.
Expand Down Expand Up @@ -319,7 +319,7 @@ MaybeLocal<Object> New(Environment* env, size_t length) {
}


MaybeLocal<Object> New(Isolate* isolate, const char* data, size_t length) {
MaybeLocal<Object> Copy(Isolate* isolate, const char* data, size_t length) {
Environment* env = Environment::GetCurrent(isolate);
EscapableHandleScope handle_scope(env->isolate());
Local<Object> obj;
Expand Down Expand Up @@ -435,11 +435,11 @@ MaybeLocal<Object> New(Environment* env,
}


MaybeLocal<Object> Use(Isolate* isolate, char* data, size_t length) {
MaybeLocal<Object> New(Isolate* isolate, char* data, size_t length) {
Environment* env = Environment::GetCurrent(isolate);
EscapableHandleScope handle_scope(env->isolate());
Local<Object> obj;
if (Buffer::Use(env, data, length).ToLocal(&obj))
if (Buffer::New(env, data, length).ToLocal(&obj))
return handle_scope.Escape(obj);
return Local<Object>();
}
Expand Down
41 changes: 8 additions & 33 deletions src/node_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,55 +23,30 @@ NODE_EXTERN char* Data(v8::Handle<v8::Object> val);
NODE_EXTERN size_t Length(v8::Handle<v8::Value> val);
NODE_EXTERN size_t Length(v8::Handle<v8::Object> val);

// public constructor - data is copied
NODE_EXTERN v8::MaybeLocal<v8::Object> Copy(v8::Isolate* isolate,
const char* data,
size_t len);

// public constructor
NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate, size_t length);
NODE_DEPRECATED("Use New(isolate, ...)",
inline v8::MaybeLocal<v8::Object> New(size_t length) {
return New(v8::Isolate::GetCurrent(), length);
})

// public constructor from string
NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate,
v8::Handle<v8::String> string,
enum encoding enc = UTF8);
NODE_DEPRECATED("Use New(isolate, ...)",
inline v8::MaybeLocal<v8::Object> New(
v8::Handle<v8::String> string,
enum encoding enc = UTF8) {
return New(v8::Isolate::GetCurrent(), string, enc);
})
// public constructor - data is copied
// TODO(trevnorris): should be something like Copy()
NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate,
const char* data,
size_t len);
NODE_DEPRECATED("Use New(isolate, ...)",
inline v8::MaybeLocal<v8::Object> New(const char* data,
size_t len) {
return New(v8::Isolate::GetCurrent(), data, len);
})

// public constructor - data is used, callback is passed data on object gc
NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate,
char* data,
size_t length,
FreeCallback callback,
void* hint);
NODE_DEPRECATED("Use New(isolate, ...)",
inline v8::MaybeLocal<v8::Object> New(char* data,
size_t length,
FreeCallback callback,
void* hint) {
return New(v8::Isolate::GetCurrent(), data, length, callback, hint);
})

// public constructor - data is used.
// TODO(trevnorris): should be New() for consistency
NODE_EXTERN v8::MaybeLocal<v8::Object> Use(v8::Isolate* isolate,
NODE_EXTERN v8::MaybeLocal<v8::Object> New(v8::Isolate* isolate,
char* data,
size_t len);
NODE_DEPRECATED("Use Use(isolate, ...)",
inline v8::MaybeLocal<v8::Object> Use(char* data, size_t len) {
return Use(v8::Isolate::GetCurrent(), data, len);
})

// This is verbose to be explicit with inline commenting
static inline bool IsWithinBounds(size_t off, size_t len, size_t max) {
Expand Down
10 changes: 5 additions & 5 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2892,7 +2892,7 @@ void CipherBase::GetAuthTag(const FunctionCallbackInfo<Value>& args) {
unsigned int out_len = 0;

if (cipher->GetAuthTag(&out, &out_len)) {
Local<Object> buf = Buffer::Use(env, out, out_len).ToLocalChecked();
Local<Object> buf = Buffer::New(env, out, out_len).ToLocalChecked();
args.GetReturnValue().Set(buf);
} else {
env->ThrowError("Attempting to get auth tag in unsupported state");
Expand Down Expand Up @@ -4366,7 +4366,7 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
return env->ThrowError("Failed to compute ECDH key");
}

Local<Object> buf = Buffer::Use(env, out, out_len).ToLocalChecked();
Local<Object> buf = Buffer::New(env, out, out_len).ToLocalChecked();
args.GetReturnValue().Set(buf);
}

Expand Down Expand Up @@ -4404,7 +4404,7 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
}

Local<Object> buf =
Buffer::Use(env, reinterpret_cast<char*>(out), size).ToLocalChecked();
Buffer::New(env, reinterpret_cast<char*>(out), size).ToLocalChecked();
args.GetReturnValue().Set(buf);
}

Expand All @@ -4431,7 +4431,7 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
}

Local<Object> buf =
Buffer::Use(env, reinterpret_cast<char*>(out), size).ToLocalChecked();
Buffer::New(env, reinterpret_cast<char*>(out), size).ToLocalChecked();
args.GetReturnValue().Set(buf);
}

Expand Down Expand Up @@ -4834,7 +4834,7 @@ void RandomBytesCheck(RandomBytesRequest* req, Local<Value> argv[2]) {
size_t size;
req->return_memory(&data, &size);
argv[0] = Null(req->env()->isolate());
argv[1] = Buffer::Use(req->env(), data, size).ToLocalChecked();
argv[1] = Buffer::New(req->env(), data, size).ToLocalChecked();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/stream_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ void StreamWrap::OnReadImpl(ssize_t nread,
CHECK_EQ(pending, UV_UNKNOWN_HANDLE);
}

Local<Object> obj = Buffer::Use(env, base, nread).ToLocalChecked();
Local<Object> obj = Buffer::New(env, base, nread).ToLocalChecked();
wrap->EmitData(nread, obj, pending_obj);
}

Expand Down
3 changes: 2 additions & 1 deletion src/string_bytes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,8 @@ Local<Value> StringBytes::Encode(Isolate* isolate,
switch (encoding) {
case BUFFER:
{
Local<Object> vbuf = Buffer::New(isolate, buf, buflen).ToLocalChecked();
Local<Object> vbuf =
Buffer::Copy(isolate, buf, buflen).ToLocalChecked();
return scope.Escape(vbuf);
}

Expand Down
2 changes: 1 addition & 1 deletion src/tls_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ void TLSWrap::OnReadSelf(ssize_t nread,
TLSWrap* wrap = static_cast<TLSWrap*>(ctx);
Local<Object> buf_obj;
if (buf != nullptr)
buf_obj = Buffer::Use(wrap->env(), buf->base, buf->len).ToLocalChecked();
buf_obj = Buffer::New(wrap->env(), buf->base, buf->len).ToLocalChecked();
wrap->EmitData(nread, buf_obj, Local<Object>());
}

Expand Down
2 changes: 1 addition & 1 deletion src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ void UDPWrap::OnRecv(uv_udp_t* handle,
}

char* base = static_cast<char*>(realloc(buf->base, nread));
argv[2] = Buffer::Use(env, base, nread).ToLocalChecked();
argv[2] = Buffer::New(env, base, nread).ToLocalChecked();
argv[3] = AddressToJS(env, addr);
wrap->MakeCallback(env->onmessage_string(), ARRAY_SIZE(argv), argv);
}
Expand Down

0 comments on commit 54e9a0b

Please sign in to comment.