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.
This commit is contained in:
parent
bf8dc0762a
commit
f5e13ae9b5
@ -113,15 +113,16 @@ size_t Length(Handle<Object> obj) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// TODO(trevnorris): would be more efficient to use StringBytes to calculate the
|
Local<Object> New(Handle<String> string, enum encoding enc) {
|
||||||
// length and write out the data beforehand then do the same as New().
|
|
||||||
Local<Object> New(Handle<String> string) {
|
|
||||||
HandleScope scope(node_isolate);
|
HandleScope scope(node_isolate);
|
||||||
|
|
||||||
Handle<Value> argv[1] = { string };
|
size_t length = StringBytes::Size(string, enc);
|
||||||
Local<Object> obj = p_buffer_fn->NewInstance(1, argv);
|
|
||||||
|
|
||||||
return scope.Close(obj);
|
Local<Object> buf = New(length);
|
||||||
|
char* data = Buffer::Data(buf);
|
||||||
|
StringBytes::Write(data, length, string, enc);
|
||||||
|
|
||||||
|
return scope.Close(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -42,7 +42,8 @@ NODE_EXTERN size_t Length(v8::Handle<v8::Object> val);
|
|||||||
// public constructor
|
// public constructor
|
||||||
NODE_EXTERN v8::Local<v8::Object> New(size_t length);
|
NODE_EXTERN v8::Local<v8::Object> New(size_t length);
|
||||||
// public constructor from string
|
// 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
|
// public constructor - data is copied
|
||||||
// TODO(trevnorris): should be something like Copy()
|
// TODO(trevnorris): should be something like Copy()
|
||||||
NODE_EXTERN v8::Local<v8::Object> New(const char* data, size_t len);
|
NODE_EXTERN v8::Local<v8::Object> New(const char* data, size_t len);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user