|
@ -20,244 +20,188 @@ using namespace v8; |
|
|
if (start < 0 || end < 0) { \ |
|
|
if (start < 0 || end < 0) { \ |
|
|
return ThrowException(Exception::TypeError( \ |
|
|
return ThrowException(Exception::TypeError( \ |
|
|
String::New("Bad argument."))); \ |
|
|
String::New("Bad argument."))); \ |
|
|
|
|
|
} \ |
|
|
|
|
|
if (!(start <= end)) { \ |
|
|
|
|
|
return ThrowException(Exception::Error( \ |
|
|
|
|
|
String::New("Must have start <= end"))); \ |
|
|
|
|
|
} \ |
|
|
|
|
|
if ((size_t)end > parent->length_) { \ |
|
|
|
|
|
return ThrowException(Exception::Error( \ |
|
|
|
|
|
String::New("end cannot be longer than parent.length"))); \ |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static Persistent<String> length_symbol; |
|
|
|
|
|
static Persistent<FunctionTemplate> constructor_template; |
|
|
|
|
|
|
|
|
|
|
|
bool IsBuffer(v8::Handle<v8::Value> val) { |
|
|
static Persistent<String> length_symbol; |
|
|
if (!val->IsObject()) return false; |
|
|
Persistent<FunctionTemplate> Buffer::constructor_template; |
|
|
Local<Object> obj = val->ToObject(); |
|
|
|
|
|
return constructor_template->HasInstance(obj); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Determines the absolute position for a relative offset */ |
|
|
// Each javascript Buffer object is backed by a Blob object.
|
|
|
size_t buffer_abs_off(buffer *buffer, size_t off) { |
|
|
// the Blob is just a C-level chunk of bytes.
|
|
|
struct buffer *root = buffer_root(buffer); |
|
|
// It has a reference count.
|
|
|
off += buffer->offset; |
|
|
struct Blob_ { |
|
|
return MIN(root->length, off); |
|
|
unsigned int refs; |
|
|
} |
|
|
size_t length; |
|
|
|
|
|
char data[1]; |
|
|
|
|
|
}; |
|
|
|
|
|
typedef struct Blob_ Blob; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void buffer_ref(struct buffer *buffer) { |
|
|
static inline Blob * blob_new(size_t length) { |
|
|
buffer_root(buffer)->refs++; |
|
|
size_t s = sizeof(Blob) - 1 + length; |
|
|
|
|
|
Blob * blob = (Blob*) malloc(s); |
|
|
|
|
|
if (!blob) return NULL; |
|
|
|
|
|
V8::AdjustAmountOfExternalAllocatedMemory(s); |
|
|
|
|
|
blob->length = length; |
|
|
|
|
|
blob->refs = 0; |
|
|
|
|
|
//fprintf(stderr, "alloc %d bytes\n", length);
|
|
|
|
|
|
return blob; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct buffer* BufferUnwrap(v8::Handle<v8::Value> val) { |
|
|
static inline void blob_ref(Blob *blob) { |
|
|
assert(val->IsObject()); |
|
|
blob->refs++; |
|
|
HandleScope scope; |
|
|
|
|
|
Local<Object> obj = val->ToObject(); |
|
|
|
|
|
assert(obj->InternalFieldCount() == 1); |
|
|
|
|
|
Local<External> ext = Local<External>::Cast(obj->GetInternalField(0)); |
|
|
|
|
|
return static_cast<struct buffer*>(ext->Value()); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void RootWeakCallback(Persistent<Value> value, void *data) |
|
|
static inline void blob_unref(Blob *blob) { |
|
|
{ |
|
|
assert(blob->refs > 0); |
|
|
struct buffer *buffer = static_cast<struct buffer*>(data); |
|
|
if (--blob->refs == 0) { |
|
|
assert(buffer->root == NULL); // this is the root
|
|
|
//fprintf(stderr, "free %d bytes\n", blob->length);
|
|
|
assert(value == buffer->handle); |
|
|
size_t s = sizeof(Blob) - 1 + blob->length; |
|
|
value.ClearWeak(); |
|
|
V8::AdjustAmountOfExternalAllocatedMemory(-s); |
|
|
if (buffer->refs) { |
|
|
free(blob); |
|
|
buffer->weak = true; |
|
|
|
|
|
} else { |
|
|
|
|
|
buffer->handle.Dispose(); |
|
|
|
|
|
free(buffer); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void buffer_unref(struct buffer *buffer) { |
|
|
// When someone calls buffer.asciiSlice, data is not copied. Instead V8
|
|
|
struct buffer * root = buffer_root(buffer); |
|
|
// references in the underlying Blob with this ExternalAsciiStringResource.
|
|
|
assert(root->refs > 0); |
|
|
class AsciiSliceExt: public String::ExternalAsciiStringResource { |
|
|
root->refs--; |
|
|
friend class Buffer; |
|
|
if (root->refs == 0 && root->weak) { |
|
|
public: |
|
|
root->handle.MakeWeak(root, RootWeakCallback); |
|
|
AsciiSliceExt(Buffer *parent, size_t start, size_t end) { |
|
|
} |
|
|
blob_ = parent->blob(); |
|
|
} |
|
|
blob_ref(blob_); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void SliceWeakCallback(Persistent<Value> value, void *data) |
|
|
assert(start <= end); |
|
|
{ |
|
|
length_ = end - start; |
|
|
struct buffer *buffer = static_cast<struct buffer*>(data); |
|
|
assert(length_ <= parent->length()); |
|
|
assert(buffer->root != NULL); // this is a slice
|
|
|
data_ = parent->data() + start; |
|
|
assert(value == buffer->handle); |
|
|
|
|
|
buffer->handle.Dispose(); |
|
|
|
|
|
buffer_unref(buffer->root); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static Handle<Value> Constructor(const Arguments &args) { |
|
|
~AsciiSliceExt() { |
|
|
HandleScope scope; |
|
|
//fprintf(stderr, "free ascii slice (%d refs left)\n", blob_->refs);
|
|
|
|
|
|
blob_unref(blob_); |
|
|
size_t length; |
|
|
|
|
|
struct buffer *buffer; |
|
|
|
|
|
|
|
|
|
|
|
if (constructor_template->HasInstance(args[0])) { |
|
|
|
|
|
// slice slice
|
|
|
|
|
|
SLICE_ARGS(args[1], args[2]) |
|
|
|
|
|
|
|
|
|
|
|
struct buffer *parent = BufferUnwrap(args[0]); |
|
|
|
|
|
|
|
|
|
|
|
size_t start_abs = buffer_abs_off(parent, start); |
|
|
|
|
|
size_t end_abs = buffer_abs_off(parent, end); |
|
|
|
|
|
assert(start_abs <= end_abs); |
|
|
|
|
|
length = end_abs - start_abs; |
|
|
|
|
|
|
|
|
|
|
|
void *d = malloc(sizeof(struct buffer)); |
|
|
|
|
|
|
|
|
|
|
|
if (!d) { |
|
|
|
|
|
V8::LowMemoryNotification(); |
|
|
|
|
|
return ThrowException(Exception::Error( |
|
|
|
|
|
String::New("Could not allocate enough memory"))); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
buffer = static_cast<struct buffer*>(d); |
|
|
|
|
|
|
|
|
|
|
|
buffer->length = length; |
|
|
const char* data() const { return data_; } |
|
|
buffer->offset = start_abs; |
|
|
size_t length() const { return length_; } |
|
|
buffer->weak = false; |
|
|
|
|
|
buffer->refs = 0; |
|
|
|
|
|
buffer->root = buffer_root(parent); |
|
|
|
|
|
buffer->handle = Persistent<Object>::New(args.This()); |
|
|
|
|
|
buffer->handle.MakeWeak(buffer, SliceWeakCallback); |
|
|
|
|
|
|
|
|
|
|
|
buffer_ref(buffer->root); |
|
|
private: |
|
|
} else { |
|
|
const char *data_; |
|
|
// Root slice
|
|
|
size_t length_; |
|
|
|
|
|
Blob *blob_; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
length = args[0]->Uint32Value(); |
|
|
|
|
|
|
|
|
|
|
|
if (length < 1) { |
|
|
Handle<Value> Buffer::New(const Arguments &args) { |
|
|
return ThrowException(Exception::TypeError( |
|
|
HandleScope scope; |
|
|
String::New("Bad argument. Length must be positive"))); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// TODO alignment. modify the length?
|
|
|
Buffer *buffer; |
|
|
void *d = malloc(sizeof(struct buffer) + length - 1); |
|
|
if (args[0]->IsInt32()) { |
|
|
|
|
|
// var buffer = new Buffer(1024);
|
|
|
|
|
|
size_t length = args[0]->Uint32Value(); |
|
|
|
|
|
buffer = new Buffer(length); |
|
|
|
|
|
|
|
|
if (!d) { |
|
|
} else if (Buffer::HasInstance(args[0]) && args.Length() > 2) { |
|
|
V8::LowMemoryNotification(); |
|
|
// var slice = new Buffer(buffer, 123, 130);
|
|
|
return ThrowException(Exception::Error( |
|
|
// args: parent, start, end
|
|
|
String::New("Could not allocate enough memory"))); |
|
|
Buffer *parent = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject()); |
|
|
|
|
|
SLICE_ARGS(args[1], args[2]) |
|
|
|
|
|
buffer = new Buffer(parent, start, end); |
|
|
|
|
|
} else { |
|
|
|
|
|
return ThrowException(Exception::TypeError(String::New("Bad argument"))); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
buffer = static_cast<struct buffer*>(d); |
|
|
buffer->Wrap(args.This()); |
|
|
|
|
|
args.This()->SetIndexedPropertiesToExternalArrayData((void*)buffer->data_, |
|
|
buffer->offset = 0; |
|
|
kExternalUnsignedByteArray, |
|
|
buffer->length = length; |
|
|
buffer->length_); |
|
|
buffer->weak = false; |
|
|
args.This()->Set(length_symbol, Integer::New(buffer->length_)); |
|
|
buffer->refs = 0; |
|
|
return args.This(); |
|
|
buffer->root = NULL; |
|
|
|
|
|
buffer->handle = Persistent<Object>::New(args.This()); |
|
|
|
|
|
buffer->handle.MakeWeak(buffer, RootWeakCallback); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
args.This()->SetInternalField(0, v8::External::New(buffer)); |
|
|
|
|
|
|
|
|
|
|
|
struct buffer *root = buffer_root(buffer); |
|
|
|
|
|
|
|
|
|
|
|
args.This()-> |
|
|
Buffer::Buffer(size_t length) : ObjectWrap() { |
|
|
SetIndexedPropertiesToExternalArrayData(&root->bytes + buffer->offset, |
|
|
blob_ = blob_new(length); |
|
|
kExternalUnsignedByteArray, |
|
|
length_ = length; |
|
|
length); |
|
|
data_ = blob_->data; |
|
|
|
|
|
blob_ref(blob_); |
|
|
|
|
|
|
|
|
args.This()->Set(length_symbol, Integer::New(length)); |
|
|
V8::AdjustAmountOfExternalAllocatedMemory(sizeof(Buffer)); |
|
|
|
|
|
|
|
|
return args.This(); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AsciiSliceExt: public String::ExternalAsciiStringResource { |
|
|
Buffer::Buffer(Buffer *parent, size_t start, size_t end) : ObjectWrap() { |
|
|
public: |
|
|
blob_ = parent->blob_; |
|
|
|
|
|
assert(blob_->refs > 0); |
|
|
|
|
|
blob_ref(blob_); |
|
|
|
|
|
|
|
|
AsciiSliceExt(struct buffer *root, size_t start, size_t end) |
|
|
assert(start <= end); |
|
|
{ |
|
|
length_ = end - start; |
|
|
data_ = root->bytes + start; |
|
|
assert(length_ <= parent->length_); |
|
|
len_ = end - start; |
|
|
data_ = parent->data_ + start; |
|
|
root_ = root; |
|
|
|
|
|
buffer_ref(root_); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
~AsciiSliceExt() { |
|
|
V8::AdjustAmountOfExternalAllocatedMemory(sizeof(Buffer)); |
|
|
buffer_unref(root_); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const char* data() const { |
|
|
|
|
|
return data_; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
size_t length() const { |
|
|
Buffer::~Buffer() { |
|
|
return len_; |
|
|
assert(blob_->refs > 0); |
|
|
|
|
|
//fprintf(stderr, "free buffer (%d refs left)\n", blob_->refs);
|
|
|
|
|
|
blob_unref(blob_); |
|
|
|
|
|
V8::AdjustAmountOfExternalAllocatedMemory(-sizeof(Buffer)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private: |
|
|
|
|
|
const char *data_; |
|
|
|
|
|
size_t len_; |
|
|
|
|
|
struct buffer *root_; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
static Handle<Value> AsciiSlice(const Arguments &args) { |
|
|
Handle<Value> Buffer::AsciiSlice(const Arguments &args) { |
|
|
HandleScope scope; |
|
|
HandleScope scope; |
|
|
|
|
|
Buffer *parent = ObjectWrap::Unwrap<Buffer>(args.This()); |
|
|
SLICE_ARGS(args[0], args[1]) |
|
|
SLICE_ARGS(args[0], args[1]) |
|
|
|
|
|
AsciiSliceExt *ext = new AsciiSliceExt(parent, start, end); |
|
|
assert(args.This()->InternalFieldCount() == 1); |
|
|
Local<String> string = String::NewExternal(ext); |
|
|
struct buffer *parent = BufferUnwrap(args.This()); |
|
|
// There should be at least two references to the blob now - the parent
|
|
|
|
|
|
// and the slice.
|
|
|
size_t start_abs = buffer_abs_off(parent, start); |
|
|
assert(parent->blob_->refs >= 2); |
|
|
size_t end_abs = buffer_abs_off(parent, end); |
|
|
|
|
|
|
|
|
|
|
|
assert(start_abs <= end_abs); |
|
|
|
|
|
|
|
|
|
|
|
AsciiSliceExt *s = new AsciiSliceExt(buffer_root(parent), start_abs, end_abs); |
|
|
|
|
|
Local<String> string = String::NewExternal(s); |
|
|
|
|
|
|
|
|
|
|
|
struct buffer *root = buffer_root(parent); |
|
|
|
|
|
assert(root->refs > 0); |
|
|
|
|
|
|
|
|
|
|
|
return scope.Close(string); |
|
|
return scope.Close(string); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static Handle<Value> Utf8Slice(const Arguments &args) { |
|
|
|
|
|
HandleScope scope; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> Buffer::Utf8Slice(const Arguments &args) { |
|
|
|
|
|
HandleScope scope; |
|
|
|
|
|
Buffer *parent = ObjectWrap::Unwrap<Buffer>(args.This()); |
|
|
SLICE_ARGS(args[0], args[1]) |
|
|
SLICE_ARGS(args[0], args[1]) |
|
|
|
|
|
const char *data = reinterpret_cast<const char*>(parent->data_ + start); |
|
|
struct buffer *parent = BufferUnwrap(args.This()); |
|
|
Local<String> string = String::New(data, end - start); |
|
|
size_t start_abs = buffer_abs_off(parent, start); |
|
|
|
|
|
size_t end_abs = buffer_abs_off(parent, end); |
|
|
|
|
|
assert(start_abs <= end_abs); |
|
|
|
|
|
|
|
|
|
|
|
struct buffer *root = buffer_root(parent); |
|
|
|
|
|
|
|
|
|
|
|
Local<String> string = |
|
|
|
|
|
String::New(reinterpret_cast<const char*>(&root->bytes + start_abs), |
|
|
|
|
|
end_abs - start_abs); |
|
|
|
|
|
return scope.Close(string); |
|
|
return scope.Close(string); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static Handle<Value> Slice(const Arguments &args) { |
|
|
|
|
|
HandleScope scope; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Handle<Value> Buffer::Slice(const Arguments &args) { |
|
|
|
|
|
HandleScope scope; |
|
|
Local<Value> argv[3] = { args.This(), args[0], args[1] }; |
|
|
Local<Value> argv[3] = { args.This(), args[0], args[1] }; |
|
|
|
|
|
|
|
|
Local<Object> slice = |
|
|
Local<Object> slice = |
|
|
constructor_template->GetFunction()->NewInstance(3, argv); |
|
|
constructor_template->GetFunction()->NewInstance(3, argv); |
|
|
|
|
|
|
|
|
return scope.Close(slice); |
|
|
return scope.Close(slice); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// var charsWritten = buffer.utf8Write(string, offset, length);
|
|
|
// var charsWritten = buffer.utf8Write(string, offset);
|
|
|
static Handle<Value> Utf8Write(const Arguments &args) { |
|
|
Handle<Value> Buffer::Utf8Write(const Arguments &args) { |
|
|
HandleScope scope; |
|
|
HandleScope scope; |
|
|
|
|
|
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This()); |
|
|
struct buffer *buffer = BufferUnwrap(args.This()); |
|
|
|
|
|
|
|
|
|
|
|
if (!args[0]->IsString()) { |
|
|
if (!args[0]->IsString()) { |
|
|
return ThrowException(Exception::TypeError(String::New( |
|
|
return ThrowException(Exception::TypeError(String::New( |
|
@ -268,30 +212,28 @@ static Handle<Value> Utf8Write(const Arguments &args) { |
|
|
|
|
|
|
|
|
size_t offset = args[1]->Int32Value(); |
|
|
size_t offset = args[1]->Int32Value(); |
|
|
|
|
|
|
|
|
char *p = buffer_p(buffer, offset); |
|
|
if (offset >= buffer->length_) { |
|
|
if (buffer_p(buffer, offset) == NULL) { |
|
|
|
|
|
return ThrowException(Exception::TypeError(String::New( |
|
|
return ThrowException(Exception::TypeError(String::New( |
|
|
"Offset is out of bounds"))); |
|
|
"Offset is out of bounds"))); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
size_t toWrite = args[2]->Int32Value(); |
|
|
const char *p = buffer->data_ + offset; |
|
|
|
|
|
|
|
|
if (buffer_remaining(buffer, offset) < toWrite) { |
|
|
if (s->Length() + offset > buffer->length_) { |
|
|
return ThrowException(Exception::TypeError(String::New( |
|
|
return ThrowException(Exception::TypeError(String::New( |
|
|
"Length is out of bounds"))); |
|
|
"Not enough space in Buffer for string"))); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int written = s->WriteUtf8(p, toWrite); |
|
|
int written = s->WriteUtf8((char*)p); |
|
|
|
|
|
|
|
|
return scope.Close(Integer::New(written)); |
|
|
return scope.Close(Integer::New(written)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// var charsWritten = buffer.asciiWrite(string, offset, length);
|
|
|
// var charsWritten = buffer.asciiWrite(string, offset);
|
|
|
static Handle<Value> AsciiWrite(const Arguments &args) { |
|
|
Handle<Value> Buffer::AsciiWrite(const Arguments &args) { |
|
|
HandleScope scope; |
|
|
HandleScope scope; |
|
|
|
|
|
|
|
|
struct buffer *buffer = BufferUnwrap(args.This()); |
|
|
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This()); |
|
|
|
|
|
|
|
|
if (!args[0]->IsString()) { |
|
|
if (!args[0]->IsString()) { |
|
|
return ThrowException(Exception::TypeError(String::New( |
|
|
return ThrowException(Exception::TypeError(String::New( |
|
@ -302,30 +244,25 @@ static Handle<Value> AsciiWrite(const Arguments &args) { |
|
|
|
|
|
|
|
|
size_t offset = args[1]->Int32Value(); |
|
|
size_t offset = args[1]->Int32Value(); |
|
|
|
|
|
|
|
|
char *p = buffer_p(buffer, offset); |
|
|
if (offset >= buffer->length_) { |
|
|
if (buffer_p(buffer, offset) == NULL) { |
|
|
|
|
|
return ThrowException(Exception::TypeError(String::New( |
|
|
return ThrowException(Exception::TypeError(String::New( |
|
|
"Offset is out of bounds"))); |
|
|
"Offset is out of bounds"))); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
size_t toWrite = args[2]->Int32Value(); |
|
|
const char *p = buffer->data_ + offset; |
|
|
|
|
|
|
|
|
if (buffer_remaining(buffer, offset) < toWrite) { |
|
|
if (s->Length() + offset > buffer->length_) { |
|
|
return ThrowException(Exception::TypeError(String::New( |
|
|
return ThrowException(Exception::TypeError(String::New( |
|
|
"Length is out of bounds"))); |
|
|
"Not enough space in Buffer for string"))); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// TODO Expose the second argument of WriteAscii?
|
|
|
int written = s->WriteAscii((char*)p); |
|
|
// Could avoid doing slices when the string doesn't fit in a buffer. V8
|
|
|
|
|
|
// slice() does copy the string, so exposing that argument would help.
|
|
|
|
|
|
|
|
|
|
|
|
int written = s->WriteAscii(p, 0, toWrite); |
|
|
|
|
|
|
|
|
|
|
|
return scope.Close(Integer::New(written)); |
|
|
return scope.Close(Integer::New(written)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static Handle<Value> Utf8Length(const Arguments &args) { |
|
|
// var nbytes = Buffer.utf8Length("string")
|
|
|
|
|
|
Handle<Value> Buffer::Utf8Length(const Arguments &args) { |
|
|
HandleScope scope; |
|
|
HandleScope scope; |
|
|
if (!args[0]->IsString()) { |
|
|
if (!args[0]->IsString()) { |
|
|
return ThrowException(Exception::TypeError(String::New( |
|
|
return ThrowException(Exception::TypeError(String::New( |
|
@ -336,29 +273,37 @@ static Handle<Value> Utf8Length(const Arguments &args) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void InitBuffer(Handle<Object> target) { |
|
|
bool Buffer::HasInstance(Handle<Value> val) { |
|
|
|
|
|
if (!val->IsObject()) return false; |
|
|
|
|
|
Local<Object> obj = val->ToObject(); |
|
|
|
|
|
return constructor_template->HasInstance(obj); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Buffer::Initialize(Handle<Object> target) { |
|
|
HandleScope scope; |
|
|
HandleScope scope; |
|
|
|
|
|
|
|
|
length_symbol = Persistent<String>::New(String::NewSymbol("length")); |
|
|
length_symbol = Persistent<String>::New(String::NewSymbol("length")); |
|
|
|
|
|
|
|
|
Local<FunctionTemplate> t = FunctionTemplate::New(Constructor); |
|
|
Local<FunctionTemplate> t = FunctionTemplate::New(Buffer::New); |
|
|
constructor_template = Persistent<FunctionTemplate>::New(t); |
|
|
constructor_template = Persistent<FunctionTemplate>::New(t); |
|
|
constructor_template->InstanceTemplate()->SetInternalFieldCount(1); |
|
|
constructor_template->InstanceTemplate()->SetInternalFieldCount(1); |
|
|
constructor_template->SetClassName(String::NewSymbol("Buffer")); |
|
|
constructor_template->SetClassName(String::NewSymbol("Buffer")); |
|
|
|
|
|
|
|
|
// copy free
|
|
|
// copy free
|
|
|
NODE_SET_PROTOTYPE_METHOD(constructor_template, "asciiSlice", AsciiSlice); |
|
|
NODE_SET_PROTOTYPE_METHOD(constructor_template, "asciiSlice", Buffer::AsciiSlice); |
|
|
NODE_SET_PROTOTYPE_METHOD(constructor_template, "slice", Slice); |
|
|
NODE_SET_PROTOTYPE_METHOD(constructor_template, "slice", Buffer::Slice); |
|
|
// TODO NODE_SET_PROTOTYPE_METHOD(t, "utf16Slice", Utf16Slice);
|
|
|
// TODO NODE_SET_PROTOTYPE_METHOD(t, "utf16Slice", Utf16Slice);
|
|
|
// copy
|
|
|
// copy
|
|
|
NODE_SET_PROTOTYPE_METHOD(constructor_template, "utf8Slice", Utf8Slice); |
|
|
NODE_SET_PROTOTYPE_METHOD(constructor_template, "utf8Slice", Buffer::Utf8Slice); |
|
|
|
|
|
|
|
|
NODE_SET_PROTOTYPE_METHOD(constructor_template, "utf8Write", Utf8Write); |
|
|
NODE_SET_PROTOTYPE_METHOD(constructor_template, "utf8Write", Buffer::Utf8Write); |
|
|
NODE_SET_PROTOTYPE_METHOD(constructor_template, "asciiWrite", AsciiWrite); |
|
|
NODE_SET_PROTOTYPE_METHOD(constructor_template, "asciiWrite", Buffer::AsciiWrite); |
|
|
|
|
|
|
|
|
NODE_SET_METHOD(constructor_template->GetFunction(), "utf8Length", Utf8Length); |
|
|
NODE_SET_METHOD(constructor_template->GetFunction(), "utf8Length", Buffer::Utf8Length); |
|
|
|
|
|
|
|
|
target->Set(String::NewSymbol("Buffer"), constructor_template->GetFunction()); |
|
|
target->Set(String::NewSymbol("Buffer"), constructor_template->GetFunction()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace node
|
|
|
} // namespace node
|
|
|