Skip to content
This repository
branch: v0.6
Fetching contributors…

Cannot retrieve contributors at this time

file 131 lines (104 sloc) 5.112 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

#ifndef NODE_BUFFER_H_
#define NODE_BUFFER_H_

#include "node.h"
#include "node_object_wrap.h"
#include "v8.h"
#include <assert.h>

namespace node {

/* 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.
*/

/*
The C++ API for Buffer changed radically between v0.2 and v0.3, in fact
it was the reason for bumping the version. In v0.2 JavaScript Buffers and
C++ Buffers were in one-to-one correspondence via ObjectWrap. We found
that it was faster to expose the C++ Buffers to JavaScript as a
"SlowBuffer" which is used as a private backend to pure JavaScript
"Buffer" objects - a 'Buffer' in v0.3 might look like this:

{ _parent: s,
_offset: 520,
length: 5 }

Migrating code C++ Buffer code from v0.2 to v0.3 is difficult. Here are
some tips:
- buffer->data() calls should become Buffer::Data(buffer) calls.
- buffer->length() calls should become Buffer::Length(buffer) calls.
- There should not be any ObjectWrap::Unwrap<Buffer>() calls. You should
not be storing pointers to Buffer objects at all - as they are
now considered internal structures. Instead consider making a
JavaScript reference to the buffer.

See the source code node-png as an example of a module which successfully
compiles on both v0.2 and v0.3 while making heavy use of the C++ Buffer
API.

*/


class NODE_EXTERN Buffer: public ObjectWrap {
 public:
  static v8::Persistent<v8::FunctionTemplate> constructor_template;

  static bool HasInstance(v8::Handle<v8::Value> val);

  static inline char* Data(v8::Handle<v8::Object> obj) {
    return (char*)obj->GetIndexedPropertiesExternalArrayData();
  }

  static inline char* Data(Buffer *b) {
    return Buffer::Data(b->handle_);
  }

  static inline size_t Length(v8::Handle<v8::Object> obj) {
    return (size_t)obj->GetIndexedPropertiesExternalArrayDataLength();
  }

  static inline size_t Length(Buffer *b) {
    return Buffer::Length(b->handle_);
  }


  ~Buffer();

  typedef void (*free_callback)(char *data, void *hint);

  // C++ API for constructing fast buffer
  static v8::Handle<v8::Object> New(v8::Handle<v8::String> string);

  static void Initialize(v8::Handle<v8::Object> target);
  static Buffer* New(size_t length); // public constructor
  static Buffer* New(char *data, size_t len); // public constructor
  static Buffer* New(char *data, size_t length,
                     free_callback callback, void *hint); // public constructor

  private:
  static v8::Handle<v8::Value> New(const v8::Arguments &args);
  static v8::Handle<v8::Value> BinarySlice(const v8::Arguments &args);
  static v8::Handle<v8::Value> AsciiSlice(const v8::Arguments &args);
  static v8::Handle<v8::Value> Base64Slice(const v8::Arguments &args);
  static v8::Handle<v8::Value> Utf8Slice(const v8::Arguments &args);
  static v8::Handle<v8::Value> Ucs2Slice(const v8::Arguments &args);
  static v8::Handle<v8::Value> BinaryWrite(const v8::Arguments &args);
  static v8::Handle<v8::Value> Base64Write(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> Ucs2Write(const v8::Arguments &args);
  static v8::Handle<v8::Value> ByteLength(const v8::Arguments &args);
  static v8::Handle<v8::Value> MakeFastBuffer(const v8::Arguments &args);
  static v8::Handle<v8::Value> Fill(const v8::Arguments &args);
  static v8::Handle<v8::Value> Copy(const v8::Arguments &args);

  Buffer(v8::Handle<v8::Object> wrapper, size_t length);
  void Replace(char *data, size_t length, free_callback callback, void *hint);

  size_t length_;
  char* data_;
  free_callback callback_;
  void* callback_hint_;
};


} // namespace node buffer

#endif // NODE_BUFFER_H_
Something went wrong with that request. Please try again.