diff --git a/lib/buffer.js b/lib/buffer.js index 6d753eb1dd8..fc6b6573db8 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -31,8 +31,6 @@ exports.Buffer = Buffer; exports.SlowBuffer = SlowBuffer; exports.INSPECT_MAX_BYTES = 50; -// add methods to Buffer prototype -buffer.setupBufferJS(Buffer, internal); Buffer.poolSize = 8 * 1024; var poolSize = Buffer.poolSize; @@ -57,13 +55,6 @@ function Buffer(subject, encoding) { this.length = Buffer.byteLength(subject, encoding = encoding || 'utf8'); else if (util.isObject(subject)) this.length = +subject.length > 0 ? Math.floor(+subject.length) : 0; - else if (util.isUndefined(subject)) { - // undef first arg returns unallocated buffer, also assumes length passed. - // this is a stop-gap for now while look for better architecture. - // for internal use only. - this.length = encoding; - return; - } else throw new TypeError('must start with number, buffer, array or string'); @@ -99,12 +90,25 @@ function Buffer(subject, encoding) { function SlowBuffer(length) { length = ~~length; - var b = new Buffer(undefined, length); + var b = new NativeBuffer(length); alloc(b, length); return b; } +// Bypass all checks for instantiating unallocated Buffer required for +// Objects created in C++. Significantly faster than calling the Buffer +// function. +function NativeBuffer(length) { + this.length = length; +} +NativeBuffer.prototype = Buffer.prototype; + + +// add methods to Buffer prototype +buffer.setupBufferJS(NativeBuffer, internal); + + // Static methods Buffer.isBuffer = function isBuffer(b) { @@ -386,7 +390,7 @@ Buffer.prototype.slice = function(start, end) { if (end < start) end = start; - var buf = new Buffer(); + var buf = new NativeBuffer(); sliceOnto(this, buf, start, end); buf.length = end - start; if (buf.length > 0) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index e90e6a37bb4..df3d878f716 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -124,19 +124,13 @@ Local New(Handle string, enum encoding enc) { } -// TODO(trevnorris): these have a flaw by needing to call the Buffer inst then -// Alloc. continue to look for a better architecture. Local New(size_t length) { HandleScope scope(node_isolate); assert(length <= kMaxLength); - Handle argv[2]; - // this is safe b/c Undefined and length fits in an SMI, so there's no risk - // of GC reclaiming the values prematurely. - argv[0] = Undefined(node_isolate); - argv[1] = Uint32::New(length, node_isolate); - Local obj = NewInstance(p_buffer_fn, ARRAY_SIZE(argv), argv); + Local arg = Uint32::NewFromUnsigned(length, node_isolate); + Local obj = NewInstance(p_buffer_fn, 1, &arg); // TODO(trevnorris): done like this to handle HasInstance since only checks // if external array data has been set, but would like to use a better @@ -163,12 +157,8 @@ Local New(const char* data, size_t length) { assert(length <= kMaxLength); - Handle argv[2]; - // this is safe b/c Undefined and length fits in an SMI, so there's no risk - // of GC reclaiming the values prematurely. - argv[0] = Undefined(node_isolate); - argv[1] = Uint32::New(length, node_isolate); - Local obj = NewInstance(p_buffer_fn, ARRAY_SIZE(argv), argv); + Local arg = Uint32::NewFromUnsigned(length, node_isolate); + Local obj = NewInstance(p_buffer_fn, 1, &arg); // TODO(trevnorris): done like this to handle HasInstance since only checks // if external array data has been set, but would like to use a better @@ -197,12 +187,8 @@ Local New(char* data, assert(length <= kMaxLength); - Handle argv[2]; - // this is safe b/c Undefined and length fits in an SMI, so there's no risk - // of GC reclaiming the values prematurely. - argv[0] = Undefined(node_isolate); - argv[1] = Uint32::New(length, node_isolate); - Local obj = NewInstance(p_buffer_fn, ARRAY_SIZE(argv), argv); + Local arg = Uint32::NewFromUnsigned(length, node_isolate); + Local obj = NewInstance(p_buffer_fn, 1, &arg); smalloc::Alloc(obj, data, length, callback, hint); @@ -215,12 +201,8 @@ Local Use(char* data, uint32_t length) { assert(length <= kMaxLength); - Handle argv[2]; - // this is safe b/c Undefined and length fits in an SMI, so there's no risk - // of GC reclaiming the values prematurely. - argv[0] = Undefined(node_isolate); - argv[1] = Uint32::New(length, node_isolate); - Local obj = NewInstance(p_buffer_fn, ARRAY_SIZE(argv), argv); + Local arg = Uint32::NewFromUnsigned(length, node_isolate); + Local obj = NewInstance(p_buffer_fn, 1, &arg); smalloc::Alloc(obj, data, length);