From f5e13ae9b5ca6eaacbcf9aad36552d9cd6c8bde6 Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Wed, 19 Jun 2013 13:07:24 -0700 Subject: [PATCH] buffer: write strings directly from call Buffer() used to pass the string to js where it would then be passed back to cpp for processing. Now only the buffer object instantiation is done in js and the string is processed in cpp. Also added a Buffer api that also accepts the encoding. --- src/node_buffer.cc | 13 +++++++------ src/node_buffer.h | 3 ++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index d7f63b2ec2..d304063abc 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -113,15 +113,16 @@ size_t Length(Handle obj) { } -// TODO(trevnorris): would be more efficient to use StringBytes to calculate the -// length and write out the data beforehand then do the same as New(). -Local New(Handle string) { +Local New(Handle string, enum encoding enc) { HandleScope scope(node_isolate); - Handle argv[1] = { string }; - Local obj = p_buffer_fn->NewInstance(1, argv); + size_t length = StringBytes::Size(string, enc); - return scope.Close(obj); + Local buf = New(length); + char* data = Buffer::Data(buf); + StringBytes::Write(data, length, string, enc); + + return scope.Close(buf); } diff --git a/src/node_buffer.h b/src/node_buffer.h index fe2abb9e5e..37468f2d37 100644 --- a/src/node_buffer.h +++ b/src/node_buffer.h @@ -42,7 +42,8 @@ NODE_EXTERN size_t Length(v8::Handle val); // public constructor NODE_EXTERN v8::Local New(size_t length); // public constructor from string -NODE_EXTERN v8::Local New(v8::Handle string); +NODE_EXTERN v8::Local New(v8::Handle string, + enum encoding enc = UTF8); // public constructor - data is copied // TODO(trevnorris): should be something like Copy() NODE_EXTERN v8::Local New(const char* data, size_t len);