Browse Source

buffer: fix Buffer::HasInstance() check

It was returning true for typed arrays. Check that the object was
instantiated with the Buffer constructor.
v0.9.10-release
Ben Noordhuis 12 years ago
parent
commit
234551a22a
  1. 1
      src/node.js
  2. 18
      src/node_buffer.cc
  3. 38
      test/simple/test-typed-arrays.js

1
src/node.js

@ -165,6 +165,7 @@
global.GLOBAL = global;
global.root = global;
global.Buffer = NativeModule.require('buffer').Buffer;
process.binding('buffer').setFastBufferConstructor(global.Buffer);
};
startup.globalTimeouts = function() {

18
src/node_buffer.cc

@ -61,6 +61,7 @@ using namespace v8;
static Persistent<String> length_symbol;
static Persistent<String> chars_written_sym;
static Persistent<String> write_sym;
static Persistent<Function> fast_buffer_constructor;
Persistent<FunctionTemplate> Buffer::constructor_template;
@ -913,14 +914,23 @@ bool Buffer::HasInstance(Handle<Value> val) {
if (!val->IsObject()) return false;
Local<Object> obj = val->ToObject();
if (obj->GetIndexedPropertiesExternalArrayDataType() == kExternalUnsignedByteArray)
return true;
ExternalArrayType type = obj->GetIndexedPropertiesExternalArrayDataType();
if (type != kExternalUnsignedByteArray)
return false;
// Also check for SlowBuffers that are empty.
if (constructor_template->HasInstance(obj))
return true;
return false;
assert(!fast_buffer_constructor.IsEmpty());
return obj->GetConstructor()->StrictEquals(fast_buffer_constructor);
}
Handle<Value> SetFastBufferConstructor(const Arguments& args) {
assert(args[0]->IsFunction());
fast_buffer_constructor = Persistent<Function>::New(args[0].As<Function>());
return Undefined();
}
@ -1036,6 +1046,8 @@ void Buffer::Initialize(Handle<Object> target) {
Buffer::MakeFastBuffer);
target->Set(String::NewSymbol("SlowBuffer"), constructor_template->GetFunction());
target->Set(String::NewSymbol("setFastBufferConstructor"),
FunctionTemplate::New(SetFastBufferConstructor)->GetFunction());
HeapProfiler::DefineWrapperClass(BUFFER_CLASS_ID, WrapperInfo);
}

38
test/simple/test-typed-arrays.js

@ -201,3 +201,41 @@ assert.throws(function() {
view.setUint16(0, 1);
assert.equal(view.getUint16(0), 1);
})();
(function() {
// Backing store should not be shared.
var a = new Uint8Array(1);
var b = new Uint8Array(a);
a[0] = 0;
b[0] = 1;
assert.equal(a[0], 0);
assert.equal(b[0], 1);
assert.notEqual(a, b.buffer);
assert.notEqual(a.buffer, b.buffer);
})();
(function() {
// Backing store should not be shared.
var a = new Uint8Array(2);
var b = new Uint16Array(a);
a[0] = 0;
a[1] = 0;
b[0] = 257;
assert.equal(a[0], 0);
assert.equal(a[1], 0);
assert.equal(b[0], 257);
assert.notEqual(a, b.buffer);
assert.notEqual(a.buffer, b.buffer);
})();
(function() {
// Backing store should be shared.
var abuf = new ArrayBuffer(32);
var a = new Uint8Array(abuf);
var b = new Uint8Array(abuf);
a[0] = 0;
b[0] = 1;
assert.equal(a[0], 1);
assert.equal(b[0], 1);
assert.equal(a.buffer, b.buffer);
})();

Loading…
Cancel
Save