buffer: write strings directly from call

Buffer(<String>) 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.
pull/5010/head
Trevor Norris 2013-06-19 13:07:24 -07:00
parent bf8dc0762a
commit f5e13ae9b5
2 changed files with 9 additions and 7 deletions

View File

@ -113,15 +113,16 @@ size_t Length(Handle<Object> 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<Object> New(Handle<String> string) {
Local<Object> New(Handle<String> string, enum encoding enc) {
HandleScope scope(node_isolate);
Handle<Value> argv[1] = { string };
Local<Object> obj = p_buffer_fn->NewInstance(1, argv);
size_t length = StringBytes::Size(string, enc);
return scope.Close(obj);
Local<Object> buf = New(length);
char* data = Buffer::Data(buf);
StringBytes::Write(data, length, string, enc);
return scope.Close(buf);
}

View File

@ -42,7 +42,8 @@ NODE_EXTERN size_t Length(v8::Handle<v8::Object> val);
// public constructor
NODE_EXTERN v8::Local<v8::Object> New(size_t length);
// public constructor from string
NODE_EXTERN v8::Local<v8::Object> New(v8::Handle<v8::String> string);
NODE_EXTERN v8::Local<v8::Object> New(v8::Handle<v8::String> string,
enum encoding enc = UTF8);
// public constructor - data is copied
// TODO(trevnorris): should be something like Copy()
NODE_EXTERN v8::Local<v8::Object> New(const char* data, size_t len);