From fa8ba08e58cb4cc86b4484a6b3973610d3810bd9 Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Tue, 2 Jun 2015 12:53:30 -0600 Subject: [PATCH] buffer: make additional changes to native API 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: https://github.com/nodejs/io.js/pull/1825 Reviewed-By: Ben Noordhuis --- src/node_buffer.cc | 8 ++++---- src/node_buffer.h | 41 ++++++++--------------------------------- src/node_crypto.cc | 10 +++++----- src/stream_wrap.cc | 2 +- src/string_bytes.cc | 3 ++- src/tls_wrap.cc | 2 +- src/udp_wrap.cc | 2 +- 7 files changed, 22 insertions(+), 46 deletions(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 0b8b22b1ee3f95..f567ef9373eab0 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -255,7 +255,7 @@ MaybeLocal New(Isolate* isolate, } Local 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. @@ -319,7 +319,7 @@ MaybeLocal New(Environment* env, size_t length) { } -MaybeLocal New(Isolate* isolate, const char* data, size_t length) { +MaybeLocal Copy(Isolate* isolate, const char* data, size_t length) { Environment* env = Environment::GetCurrent(isolate); EscapableHandleScope handle_scope(env->isolate()); Local obj; @@ -435,11 +435,11 @@ MaybeLocal New(Environment* env, } -MaybeLocal Use(Isolate* isolate, char* data, size_t length) { +MaybeLocal New(Isolate* isolate, char* data, size_t length) { Environment* env = Environment::GetCurrent(isolate); EscapableHandleScope handle_scope(env->isolate()); Local obj; - if (Buffer::Use(env, data, length).ToLocal(&obj)) + if (Buffer::New(env, data, length).ToLocal(&obj)) return handle_scope.Escape(obj); return Local(); } diff --git a/src/node_buffer.h b/src/node_buffer.h index d59c44692b12fb..ec442d58b3735e 100644 --- a/src/node_buffer.h +++ b/src/node_buffer.h @@ -23,55 +23,30 @@ NODE_EXTERN char* Data(v8::Handle val); NODE_EXTERN size_t Length(v8::Handle val); NODE_EXTERN size_t Length(v8::Handle val); +// public constructor - data is copied +NODE_EXTERN v8::MaybeLocal Copy(v8::Isolate* isolate, + const char* data, + size_t len); + // public constructor NODE_EXTERN v8::MaybeLocal New(v8::Isolate* isolate, size_t length); -NODE_DEPRECATED("Use New(isolate, ...)", - inline v8::MaybeLocal New(size_t length) { - return New(v8::Isolate::GetCurrent(), length); -}) + // public constructor from string NODE_EXTERN v8::MaybeLocal New(v8::Isolate* isolate, v8::Handle string, enum encoding enc = UTF8); -NODE_DEPRECATED("Use New(isolate, ...)", - inline v8::MaybeLocal New( - v8::Handle 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 New(v8::Isolate* isolate, - const char* data, - size_t len); -NODE_DEPRECATED("Use New(isolate, ...)", - inline v8::MaybeLocal 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 New(v8::Isolate* isolate, char* data, size_t length, FreeCallback callback, void* hint); -NODE_DEPRECATED("Use New(isolate, ...)", - inline v8::MaybeLocal 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 Use(v8::Isolate* isolate, +NODE_EXTERN v8::MaybeLocal New(v8::Isolate* isolate, char* data, size_t len); -NODE_DEPRECATED("Use Use(isolate, ...)", - inline v8::MaybeLocal 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) { diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 2d64d9e1973266..953bf1b767f890 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -2899,7 +2899,7 @@ void CipherBase::GetAuthTag(const FunctionCallbackInfo& args) { unsigned int out_len = 0; if (cipher->GetAuthTag(&out, &out_len)) { - Local buf = Buffer::Use(env, out, out_len).ToLocalChecked(); + Local buf = Buffer::New(env, out, out_len).ToLocalChecked(); args.GetReturnValue().Set(buf); } else { env->ThrowError("Attempting to get auth tag in unsupported state"); @@ -4373,7 +4373,7 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo& args) { return env->ThrowError("Failed to compute ECDH key"); } - Local buf = Buffer::Use(env, out, out_len).ToLocalChecked(); + Local buf = Buffer::New(env, out, out_len).ToLocalChecked(); args.GetReturnValue().Set(buf); } @@ -4411,7 +4411,7 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo& args) { } Local buf = - Buffer::Use(env, reinterpret_cast(out), size).ToLocalChecked(); + Buffer::New(env, reinterpret_cast(out), size).ToLocalChecked(); args.GetReturnValue().Set(buf); } @@ -4438,7 +4438,7 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo& args) { } Local buf = - Buffer::Use(env, reinterpret_cast(out), size).ToLocalChecked(); + Buffer::New(env, reinterpret_cast(out), size).ToLocalChecked(); args.GetReturnValue().Set(buf); } @@ -4841,7 +4841,7 @@ void RandomBytesCheck(RandomBytesRequest* req, Local 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(); } } diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index 3097eac4859761..ae941f5edbd90b 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -223,7 +223,7 @@ void StreamWrap::OnReadImpl(ssize_t nread, CHECK_EQ(pending, UV_UNKNOWN_HANDLE); } - Local obj = Buffer::Use(env, base, nread).ToLocalChecked(); + Local obj = Buffer::New(env, base, nread).ToLocalChecked(); wrap->EmitData(nread, obj, pending_obj); } diff --git a/src/string_bytes.cc b/src/string_bytes.cc index 36525a73ac84c3..0abdbf85bbc40b 100644 --- a/src/string_bytes.cc +++ b/src/string_bytes.cc @@ -704,7 +704,8 @@ Local StringBytes::Encode(Isolate* isolate, switch (encoding) { case BUFFER: { - Local vbuf = Buffer::New(isolate, buf, buflen).ToLocalChecked(); + Local vbuf = + Buffer::Copy(isolate, buf, buflen).ToLocalChecked(); return scope.Escape(vbuf); } diff --git a/src/tls_wrap.cc b/src/tls_wrap.cc index fbb35fdb5e6173..fc19a5ce0bbe38 100644 --- a/src/tls_wrap.cc +++ b/src/tls_wrap.cc @@ -660,7 +660,7 @@ void TLSWrap::OnReadSelf(ssize_t nread, TLSWrap* wrap = static_cast(ctx); Local 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()); } diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index 791ef76848cf43..dd3958ec0e37da 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -408,7 +408,7 @@ void UDPWrap::OnRecv(uv_udp_t* handle, } char* base = static_cast(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); }