Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
buffer: write strings directly from call
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
trevnorris committed Jun 19, 2013
1 parent bf8dc07 commit f5e13ae
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
13 changes: 7 additions & 6 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
3 changes: 2 additions & 1 deletion src/node_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f5e13ae

Please sign in to comment.