summaryrefslogtreecommitdiff
path: root/src/node_buffer.h
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2010-01-27 15:40:09 -0800
committerRyan Dahl <ry@tinyclouds.org>2010-01-27 15:40:09 -0800
commitbf803f478bfdee522adf42b5c1207deb75cdb7dc (patch)
tree5b84e18b0e449d8db87806aab12d2233e997d7b8 /src/node_buffer.h
parenta668d07484671c832b696c93f4895a8dfa23ea8a (diff)
downloadandroid-node-v8-bf803f478bfdee522adf42b5c1207deb75cdb7dc.tar.gz
android-node-v8-bf803f478bfdee522adf42b5c1207deb75cdb7dc.tar.bz2
android-node-v8-bf803f478bfdee522adf42b5c1207deb75cdb7dc.zip
Reimplment Buffers
Diffstat (limited to 'src/node_buffer.h')
-rw-r--r--src/node_buffer.h68
1 files changed, 35 insertions, 33 deletions
diff --git a/src/node_buffer.h b/src/node_buffer.h
index 122d589d85..d6b83fe00b 100644
--- a/src/node_buffer.h
+++ b/src/node_buffer.h
@@ -1,19 +1,19 @@
-#ifndef NODE_BUFFER
-#define NODE_BUFFER
+#ifndef NODE_BUFFER_H_
+#define NODE_BUFFER_H_
+#include <node.h>
+#include <node_object_wrap.h>
#include <v8.h>
namespace node {
-#define MIN(a,b) ((a) < (b) ? (a) : (b))
-
/* A buffer is a chunk of memory stored outside the V8 heap, mirrored by an
* object in javascript. The object is not totally opaque, one can access
* individual bytes with [] and slice it into substrings or sub-buffers
* without copying memory.
*
* // return an ascii encoded string - no memory iscopied
- * buffer.asciiSlide(0, 3)
+ * buffer.asciiSlide(0, 3)
*
* // returns another buffer - no memory is copied
* buffer.slice(0, 3)
@@ -25,40 +25,42 @@ namespace node {
* are GCed.
*/
-struct buffer {
- v8::Persistent<v8::Object> handle; // both
- bool weak; // both
- struct buffer *root; // both (NULL for root)
- size_t offset; // both (0 for root)
- size_t length; // both
- unsigned int refs; // root only
- char bytes[1]; // root only
-};
-void InitBuffer(v8::Handle<v8::Object> target);
+struct Blob_;
+
+class Buffer : public ObjectWrap {
+ public:
+ static void Initialize(v8::Handle<v8::Object> target);
+ static bool HasInstance(v8::Handle<v8::Value> val);
-struct buffer* BufferUnwrap(v8::Handle<v8::Value> val);
-bool IsBuffer(v8::Handle<v8::Value> val);
+ const char* data() const { return data_; }
+ size_t length() const { return length_; }
+ struct Blob_* blob() const { return blob_; }
-static inline struct buffer * buffer_root(struct buffer *buffer) {
- return buffer->root ? buffer->root : buffer;
-}
+ protected:
+ static v8::Persistent<v8::FunctionTemplate> constructor_template;
+ static v8::Handle<v8::Value> New(const v8::Arguments &args);
+ static v8::Handle<v8::Value> Slice(const v8::Arguments &args);
+ static v8::Handle<v8::Value> AsciiSlice(const v8::Arguments &args);
+ static v8::Handle<v8::Value> Utf8Slice(const v8::Arguments &args);
+ static v8::Handle<v8::Value> AsciiWrite(const v8::Arguments &args);
+ static v8::Handle<v8::Value> Utf8Write(const v8::Arguments &args);
+ static v8::Handle<v8::Value> Utf8Length(const v8::Arguments &args);
-static inline char * buffer_p(struct buffer *buffer, size_t off) {
- struct buffer *root = buffer_root(buffer);
- if (buffer->offset + off >= root->length) return NULL;
- return reinterpret_cast<char*>(&(root->bytes) + buffer->offset + off);
-}
+ int AsciiWrite(char *string, int offset, int length);
+ int Utf8Write(char *string, int offset, int length);
-static inline size_t buffer_remaining(struct buffer *buffer, size_t off) {
- struct buffer *root = buffer_root(buffer);
- char *end = reinterpret_cast<char*>(&(root->bytes) + root->length);
- return end - buffer_p(buffer, off);
-}
+ private:
+ Buffer(size_t length);
+ Buffer(Buffer *parent, size_t start, size_t end);
+ ~Buffer();
+
+ const char *data_;
+ size_t length_;
+ struct Blob_ *blob_;
+};
-void buffer_ref(struct buffer *buffer);
-void buffer_unref(struct buffer *buffer);
} // namespace node buffer
-#endif // NODE_BUFFER
+#endif // NODE_BUFFER_H_