Browse Source

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.
v0.11.3-release
Trevor Norris 12 years ago
parent
commit
f5e13ae9b5
  1. 13
      src/node_buffer.cc
  2. 3
      src/node_buffer.h

13
src/node_buffer.cc

@ -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);
}

3
src/node_buffer.h

@ -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);

Loading…
Cancel
Save