2010-01-28 00:40:09 +01:00
|
|
|
#ifndef NODE_BUFFER_H_
|
|
|
|
#define NODE_BUFFER_H_
|
2009-12-13 08:42:45 +01:00
|
|
|
|
2010-01-28 00:40:09 +01:00
|
|
|
#include <node.h>
|
|
|
|
#include <node_object_wrap.h>
|
2009-12-13 08:42:45 +01:00
|
|
|
#include <v8.h>
|
2010-10-09 21:15:14 +02:00
|
|
|
#include <assert.h>
|
2009-12-13 08:42:45 +01:00
|
|
|
|
|
|
|
namespace node {
|
|
|
|
|
2009-12-15 09:22:36 +01:00
|
|
|
/* 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.
|
|
|
|
*
|
2010-10-21 11:37:08 +02:00
|
|
|
* // return an ascii encoded string - no memory is copied
|
|
|
|
* buffer.asciiSlice(0, 3)
|
2009-12-15 09:22:36 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-01-28 00:40:09 +01:00
|
|
|
class Buffer : public ObjectWrap {
|
|
|
|
public:
|
2010-05-20 18:42:47 +02:00
|
|
|
~Buffer();
|
|
|
|
|
2010-10-22 12:08:55 +02:00
|
|
|
typedef void (*free_callback)(char *data, void *hint);
|
|
|
|
|
2010-10-26 22:43:58 +02:00
|
|
|
// C++ API for constructing fast buffer
|
2010-11-21 05:49:44 +01:00
|
|
|
static v8::Handle<v8::Object> New(v8::Handle<v8::String> string);
|
2010-10-26 22:43:58 +02:00
|
|
|
|
2010-01-28 00:40:09 +01:00
|
|
|
static void Initialize(v8::Handle<v8::Object> target);
|
2010-05-24 21:59:22 +02:00
|
|
|
static Buffer* New(size_t length); // public constructor
|
2010-10-11 04:08:51 +02:00
|
|
|
static Buffer* New(char *data, size_t len); // public constructor
|
2010-10-22 12:08:55 +02:00
|
|
|
static Buffer* New(char *data, size_t length,
|
|
|
|
free_callback callback, void *hint); // public constructor
|
2010-09-05 05:59:24 +02:00
|
|
|
static bool HasInstance(v8::Handle<v8::Value> val);
|
|
|
|
|
2010-11-20 09:04:22 +01:00
|
|
|
static inline char* Data(v8::Handle<v8::Object> obj) {
|
|
|
|
return (char*)obj->GetIndexedPropertiesExternalArrayData();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t Length(v8::Handle<v8::Object> obj) {
|
|
|
|
return (size_t)obj->GetIndexedPropertiesExternalArrayDataLength();
|
|
|
|
}
|
2009-12-13 08:42:45 +01:00
|
|
|
|
2010-11-01 22:59:30 +01:00
|
|
|
private:
|
2010-05-24 21:59:22 +02:00
|
|
|
static v8::Persistent<v8::FunctionTemplate> constructor_template;
|
|
|
|
|
2010-01-28 00:40:09 +01:00
|
|
|
static v8::Handle<v8::Value> New(const v8::Arguments &args);
|
2010-03-20 04:33:09 +01:00
|
|
|
static v8::Handle<v8::Value> BinarySlice(const v8::Arguments &args);
|
2010-01-28 00:40:09 +01:00
|
|
|
static v8::Handle<v8::Value> AsciiSlice(const v8::Arguments &args);
|
2010-07-23 22:52:44 +02:00
|
|
|
static v8::Handle<v8::Value> Base64Slice(const v8::Arguments &args);
|
2010-01-28 00:40:09 +01:00
|
|
|
static v8::Handle<v8::Value> Utf8Slice(const v8::Arguments &args);
|
2010-03-20 04:33:09 +01:00
|
|
|
static v8::Handle<v8::Value> BinaryWrite(const v8::Arguments &args);
|
2010-07-24 01:36:47 +02:00
|
|
|
static v8::Handle<v8::Value> Base64Write(const v8::Arguments &args);
|
2010-01-28 00:40:09 +01:00
|
|
|
static v8::Handle<v8::Value> AsciiWrite(const v8::Arguments &args);
|
|
|
|
static v8::Handle<v8::Value> Utf8Write(const v8::Arguments &args);
|
2010-03-19 20:02:59 +01:00
|
|
|
static v8::Handle<v8::Value> ByteLength(const v8::Arguments &args);
|
2010-08-21 10:19:39 +02:00
|
|
|
static v8::Handle<v8::Value> MakeFastBuffer(const v8::Arguments &args);
|
2010-04-05 03:58:55 +02:00
|
|
|
static v8::Handle<v8::Value> Copy(const v8::Arguments &args);
|
2010-01-24 23:06:07 +01:00
|
|
|
|
2010-10-22 12:08:55 +02:00
|
|
|
Buffer(v8::Handle<v8::Object> wrapper, size_t length);
|
|
|
|
void Replace(char *data, size_t length, free_callback callback, void *hint);
|
2009-12-15 09:22:36 +01:00
|
|
|
|
2010-09-08 08:48:42 +02:00
|
|
|
size_t length_;
|
|
|
|
char* data_;
|
2010-10-22 12:08:55 +02:00
|
|
|
free_callback callback_;
|
|
|
|
void* callback_hint_;
|
2010-01-28 00:40:09 +01:00
|
|
|
};
|
2009-12-15 09:22:36 +01:00
|
|
|
|
2010-01-26 02:55:02 +01:00
|
|
|
|
|
|
|
} // namespace node buffer
|
2009-12-13 08:42:45 +01:00
|
|
|
|
2010-01-28 00:40:09 +01:00
|
|
|
#endif // NODE_BUFFER_H_
|