1+ #include < node_buffer.h>
2+
13#include < assert.h>
24#include < stdlib.h> // malloc, free
35#include < v8.h>
6+
47#include < node.h>
58
69namespace node {
710
811using namespace v8 ;
912
10- #define MIN (a,b ) ((a) < (b) ? (a) : (b))
11-
1213#define SLICE_ARGS (start_arg, end_arg ) \
1314 if (!start_arg->IsInt32 () || !end_arg->IsInt32()) { \
1415 return ThrowException (Exception::TypeError ( \
@@ -24,33 +25,11 @@ using namespace v8;
2425static Persistent<String> length_symbol;
2526static Persistent<FunctionTemplate> constructor_template;
2627
27- /* A buffer is a chunk of memory stored outside the V8 heap, mirrored by an
28- * object in javascript. The object is not totally opaque, one can access
29- * individual bytes with [] and slice it into substrings or sub-buffers
30- * without copying memory.
31- *
32- * // return an ascii encoded string - no memory iscopied
33- * buffer.asciiSlide(0, 3)
34- *
35- * // returns another buffer - no memory is copied
36- * buffer.slice(0, 3)
37- *
38- * Interally, each javascript buffer object is backed by a "struct buffer"
39- * object. These "struct buffer" objects are either a root buffer (in the
40- * case that buffer->root == NULL) or slice objects (in which case
41- * buffer->root != NULL). A root buffer is only GCed once all its slices
42- * are GCed.
43- */
44-
45- struct buffer {
46- Persistent<Object> handle; // both
47- bool weak; // both
48- struct buffer *root; // both (NULL for root)
49- size_t offset; // both (0 for root)
50- size_t length; // both
51- unsigned int refs; // root only
52- char bytes[1 ]; // root only
53- };
28+ bool IsBuffer (v8::Handle<v8::Value> val) {
29+ if (!val->IsObject ()) return false ;
30+ Local<Object> obj = val->ToObject ();
31+ return constructor_template->HasInstance (obj);
32+ }
5433
5534
5635static inline struct buffer * buffer_root (buffer *buffer) {
@@ -79,7 +58,7 @@ static inline void buffer_unref(struct buffer *buffer) {
7958}
8059
8160
82- static inline struct buffer * Unwrap ( Handle<Value> val) {
61+ struct buffer * BufferUnwrap (v8:: Handle<v8:: Value> val) {
8362 assert (val->IsObject ());
8463 HandleScope scope;
8564 Local<Object> obj = val->ToObject ();
@@ -123,7 +102,7 @@ static Handle<Value> Constructor(const Arguments &args) {
123102 // slice slice
124103 SLICE_ARGS (args[1 ], args[2 ])
125104
126- struct buffer *parent = Unwrap (args[0 ]);
105+ struct buffer *parent = BufferUnwrap (args[0 ]);
127106
128107 size_t start_abs = buffer_abs_off (parent, start);
129108 size_t end_abs = buffer_abs_off (parent, end);
@@ -230,7 +209,7 @@ static Handle<Value> AsciiSlice(const Arguments &args) {
230209 SLICE_ARGS (args[0 ], args[1 ])
231210
232211 assert (args.This ()->InternalFieldCount () == 1 );
233- struct buffer *parent = Unwrap (args.This ());
212+ struct buffer *parent = BufferUnwrap (args.This ());
234213
235214 size_t start_abs = buffer_abs_off (parent, start);
236215 size_t end_abs = buffer_abs_off (parent, end);
@@ -251,7 +230,7 @@ static Handle<Value> Utf8Slice(const Arguments &args) {
251230
252231 SLICE_ARGS (args[0 ], args[1 ])
253232
254- struct buffer *parent = Unwrap (args.This ());
233+ struct buffer *parent = BufferUnwrap (args.This ());
255234 size_t start_abs = buffer_abs_off (parent, start);
256235 size_t end_abs = buffer_abs_off (parent, end);
257236 assert (start_abs <= end_abs);
0 commit comments