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

Commit f5e13ae

Browse files
committed
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.
1 parent bf8dc07 commit f5e13ae

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/node_buffer.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,16 @@ size_t Length(Handle<Object> obj) {
113113
}
114114

115115

116-
// TODO(trevnorris): would be more efficient to use StringBytes to calculate the
117-
// length and write out the data beforehand then do the same as New().
118-
Local<Object> New(Handle<String> string) {
116+
Local<Object> New(Handle<String> string, enum encoding enc) {
119117
HandleScope scope(node_isolate);
120118

121-
Handle<Value> argv[1] = { string };
122-
Local<Object> obj = p_buffer_fn->NewInstance(1, argv);
119+
size_t length = StringBytes::Size(string, enc);
123120

124-
return scope.Close(obj);
121+
Local<Object> buf = New(length);
122+
char* data = Buffer::Data(buf);
123+
StringBytes::Write(data, length, string, enc);
124+
125+
return scope.Close(buf);
125126
}
126127

127128

src/node_buffer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ NODE_EXTERN size_t Length(v8::Handle<v8::Object> val);
4242
// public constructor
4343
NODE_EXTERN v8::Local<v8::Object> New(size_t length);
4444
// public constructor from string
45-
NODE_EXTERN v8::Local<v8::Object> New(v8::Handle<v8::String> string);
45+
NODE_EXTERN v8::Local<v8::Object> New(v8::Handle<v8::String> string,
46+
enum encoding enc = UTF8);
4647
// public constructor - data is copied
4748
// TODO(trevnorris): should be something like Copy()
4849
NODE_EXTERN v8::Local<v8::Object> New(const char* data, size_t len);

0 commit comments

Comments
 (0)