summaryrefslogtreecommitdiffstats
path: root/mock-ril/src/cpp/node_buffer.cpp
blob: 4c08a2676a31aa7776eb6c4f1a5fd870729cc78a (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/**
 * Copied from node_buffer.cc
 * see http://www.nodejs.org/
 *
 * Node's license follows:
 *
 * Copyright 2009, 2010 Ryan Lienhart Dahl. All rights reserved.
 * 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.
 */

#include "node_buffer.h"

#include <assert.h>
#include <stdlib.h> // malloc, free
#include <v8.h>

#include <string.h> // memcpy

#include <arpa/inet.h>  // htons, htonl

#include "logging.h"
#include "node_util.h"
#include "util.h"

//#define BUFFER_DEBUG
#ifdef  BUFFER_DEBUG

#define DBG(...) ALOGD(__VA_ARGS__)

#else

#define DBG(...)

#endif

#define MIN(a,b) ((a) < (b) ? (a) : (b))

using namespace v8;

#define SLICE_ARGS(start_arg, end_arg)                                   \
  if (!start_arg->IsInt32() || !end_arg->IsInt32()) {                    \
    return ThrowException(Exception::TypeError(                          \
          v8::String::New("Bad argument.")));                            \
  }                                                                      \
  int32_t start = start_arg->Int32Value();                               \
  int32_t end = end_arg->Int32Value();                                   \
  if (start < 0 || end < 0) {                                            \
    return ThrowException(Exception::TypeError(                          \
          v8::String::New("Bad argument.")));                            \
  }                                                                      \
  if (!(start <= end)) {                                                 \
    return ThrowException(Exception::Error(                              \
          v8::String::New("Must have start <= end")));                   \
  }                                                                      \
  if ((size_t)end > parent->length_) {                                   \
    return ThrowException(Exception::Error(                              \
          v8::String::New("end cannot be longer than parent.length")));  \
  }

static Persistent<String> length_symbol;
static Persistent<String> chars_written_sym;
static Persistent<String> write_sym;
Persistent<FunctionTemplate> Buffer::constructor_template;


// Each javascript Buffer object is backed by a Blob object.
// the Blob is just a C-level chunk of bytes.
// It has a reference count.
struct Blob_ {
  unsigned int refs;
  size_t length;
  char *data;
};
typedef struct Blob_ Blob;


static inline Blob * blob_new(size_t length) {
  DBG("blob_new E");
  Blob * blob  = (Blob*) malloc(sizeof(Blob));
  if (!blob) return NULL;

  blob->data = (char*) malloc(length);
  if (!blob->data) {
    DBG("blob_new X no memory for data");
    free(blob);
    return NULL;
  }

  V8::AdjustAmountOfExternalAllocatedMemory(sizeof(Blob) + length);
  blob->length = length;
  blob->refs = 0;
  DBG("blob_new X");
  return blob;
}


static inline void blob_ref(Blob *blob) {
  blob->refs++;
}


static inline void blob_unref(Blob *blob) {
  assert(blob->refs > 0);
  if (--blob->refs == 0) {
    DBG("blob_unref == 0");
    //fprintf(stderr, "free %d bytes\n", blob->length);
    V8::AdjustAmountOfExternalAllocatedMemory(-(sizeof(Blob) + blob->length));
    free(blob->data);
    free(blob);
    DBG("blob_unref blob and its data freed");
  }
}

#if 0
// When someone calls buffer.asciiSlice, data is not copied. Instead V8
// references in the underlying Blob with this ExternalAsciiStringResource.
class AsciiSliceExt: public String::ExternalAsciiStringResource {
 friend class Buffer;
 public:
  AsciiSliceExt(Buffer *parent, size_t start, size_t end) {
    blob_ = parent->blob();
    blob_ref(blob_);

    assert(start <= end);
    length_ = end - start;
    assert(start + length_ <= parent->length());
    data_ = parent->data() + start;
  }


  ~AsciiSliceExt() {
    //fprintf(stderr, "free ascii slice (%d refs left)\n", blob_->refs);
    blob_unref(blob_);
  }


  const char* data() const { return data_; }
  size_t length() const { return length_; }

 private:
  const char *data_;
  size_t length_;
  Blob *blob_;
};
#endif

Buffer* Buffer::New(size_t size) {
  DBG("Buffer::New(size) E");
  HandleScope scope;

  Local<Value> arg = Integer::NewFromUnsigned(size);
  Local<Object> b = constructor_template->GetFunction()->NewInstance(1, &arg);

  DBG("Buffer::New(size) X");
  return ObjectWrap::Unwrap<Buffer>(b);
}


Handle<Value> Buffer::New(const Arguments &args) {
  DBG("Buffer::New(args) E");
  HandleScope scope;

  Buffer *buffer;
  if ((args.Length() == 0) || args[0]->IsInt32()) {
    size_t length = 0;
    if (args[0]->IsInt32()) {
      length = args[0]->Uint32Value();
    }
    buffer = new Buffer(length);
  } else if (args[0]->IsArray()) {
    Local<Array> a = Local<Array>::Cast(args[0]);
    buffer = new Buffer(a->Length());
    char *p = buffer->data();
    for (unsigned int i = 0; i < a->Length(); i++) {
      p[i] = a->Get(i)->Uint32Value();
    }
  } else if (args[0]->IsString()) {
    Local<String> s = args[0]->ToString();
    enum encoding e = ParseEncoding(args[1], UTF8);
    int length = e == UTF8 ? s->Utf8Length() : s->Length();
    buffer = new Buffer(length);
  } else if (Buffer::HasInstance(args[0]) && args.Length() > 2) {
    // var slice = new Buffer(buffer, 123, 130);
    // args: parent, start, end
    Buffer *parent = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
    SLICE_ARGS(args[1], args[2])
    buffer = new Buffer(parent, start, end);
  } else {
    DBG("Buffer::New(args) X Bad argument");
    return ThrowException(Exception::TypeError(String::New("Bad argument")));
  }

  buffer->Wrap(args.This());
  args.This()->SetIndexedPropertiesToExternalArrayData(buffer->data(),
                                                       kExternalUnsignedByteArray,
                                                       buffer->length());
  args.This()->Set(length_symbol, Integer::New(buffer->length_));

  if (args[0]->IsString()) {
    if (write_sym.IsEmpty()) {
      write_sym = Persistent<String>::New(String::NewSymbol("write"));
    }

    Local<Value> write_v = args.This()->Get(write_sym);
    assert(write_v->IsFunction());
    Local<Function> write = Local<Function>::Cast(write_v);

    Local<Value> argv[2] = { args[0], args[1] };

    TryCatch try_catch;

    write->Call(args.This(), 2, argv);

    if (try_catch.HasCaught()) {
      ReportException(&try_catch);
    }
  }

  DBG("Buffer::New(args) X");
  return args.This();
}


Buffer::Buffer(size_t length) : ObjectWrap() {
  DBG("Buffer::Buffer(length) E");
  blob_ = blob_new(length);
  off_ = 0;
  length_ = length;

  blob_ref(blob_);

  V8::AdjustAmountOfExternalAllocatedMemory(sizeof(Buffer));
  DBG("Buffer::Buffer(length) X");
}


Buffer::Buffer(Buffer *parent, size_t start, size_t end) : ObjectWrap() {
  DBG("Buffer::Buffer(parent, start, end) E");
  blob_ = parent->blob_;
  assert(blob_->refs > 0);
  blob_ref(blob_);

  assert(start <= end);
  off_ = parent->off_ + start;
  length_ = end - start;
  assert(length_ <= parent->length_);

  V8::AdjustAmountOfExternalAllocatedMemory(sizeof(Buffer));
  DBG("Buffer::Buffer(parent, start, end) X");
}


Buffer::~Buffer() {
  DBG("Buffer::~Buffer() E");
  assert(blob_->refs > 0);
  //fprintf(stderr, "free buffer (%d refs left)\n", blob_->refs);
  blob_unref(blob_);
  V8::AdjustAmountOfExternalAllocatedMemory(-static_cast<long int>(sizeof(Buffer)));
  DBG("Buffer::~Buffer() X");
}


char* Buffer::data() {
  char *p = blob_->data + off_;
  DBG("Buffer::data() EX p=%p", p);
  return p;
}

void Buffer::NewBlob(size_t length) {
  DBG("Buffer::NewBlob(length) E");
  blob_unref(blob_);
  blob_ = blob_new(length);
  off_ = 0;
  length_ = length;

  blob_ref(blob_);

  V8::AdjustAmountOfExternalAllocatedMemory(sizeof(Buffer));
  DBG("Buffer::NewBlob(length) X");
}


Handle<Value> Buffer::BinarySlice(const Arguments &args) {
  DBG("Buffer::BinarySlice(args) E");
  HandleScope scope;
  Buffer *parent = ObjectWrap::Unwrap<Buffer>(args.This());
  SLICE_ARGS(args[0], args[1])

  const char *data = const_cast<char*>(parent->data() + start);
  //Local<String> string = String::New(data, end - start);

  Local<Value> b =  Encode(data, end - start, BINARY);

  DBG("Buffer::BinarySlice(args) X");
  return scope.Close(b);
}


Handle<Value> Buffer::AsciiSlice(const Arguments &args) {
  DBG("Buffer::AsciiSlice(args) E");
  HandleScope scope;
  Buffer *parent = ObjectWrap::Unwrap<Buffer>(args.This());
  SLICE_ARGS(args[0], args[1])

#if 0
  AsciiSliceExt *ext = new AsciiSliceExt(parent, start, end);
  Local<String> string = String::NewExternal(ext);
  // There should be at least two references to the blob now - the parent
  // and the slice.
  assert(parent->blob_->refs >= 2);
#endif

  const char *data = const_cast<char*>(parent->data() + start);
  Local<String> string = String::New(data, end - start);


  DBG("Buffer::AsciiSlice(args) X");
  return scope.Close(string);
}


Handle<Value> Buffer::Utf8Slice(const Arguments &args) {
  DBG("Buffer::Utf8Slice(args) E");
  HandleScope scope;
  Buffer *parent = ObjectWrap::Unwrap<Buffer>(args.This());
  SLICE_ARGS(args[0], args[1])
  const char *data = const_cast<char*>(parent->data() + start);
  Local<String> string = String::New(data, end - start);
  DBG("Buffer::Utf8Slice(args) X");
  return scope.Close(string);
}


Handle<Value> Buffer::Slice(const Arguments &args) {
  DBG("Buffer::Slice(args) E");
  HandleScope scope;
  Local<Value> argv[3] = { args.This(), args[0], args[1] };
  Local<Object> slice =
    constructor_template->GetFunction()->NewInstance(3, argv);
  DBG("Buffer::Slice(args) X");
  return scope.Close(slice);
}


// var bytesCopied = buffer.copy(target, targetStart, sourceStart, sourceEnd);
Handle<Value> Buffer::Copy(const Arguments &args) {
  DBG("Buffer::Copy(args) E");
  HandleScope scope;

  Buffer *source = ObjectWrap::Unwrap<Buffer>(args.This());

  if (!Buffer::HasInstance(args[0])) {
    DBG("Buffer::Copy(args) X arg[0] not buffer");
    return ThrowException(Exception::TypeError(String::New(
            "First arg should be a Buffer")));
  }

  Buffer *target = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());

  ssize_t target_start = args[1]->Int32Value();
  ssize_t source_start = args[2]->Int32Value();
  ssize_t source_end = args[3]->IsInt32() ? args[3]->Int32Value()
                                          : source->length();

  if (source_end < source_start) {
    DBG("Buffer::Copy(args) X end < start");
    return ThrowException(Exception::Error(String::New(
            "sourceEnd < sourceStart")));
  }

  if (target_start < 0 || ((size_t)target_start) > target->length()) {
    DBG("Buffer::Copy(args) X targetStart bad");
    return ThrowException(Exception::Error(String::New(
            "targetStart out of bounds")));
  }

  if (source_start < 0 || ((size_t)source_start) > source->length()) {
    DBG("Buffer::Copy(args) X base source start");
    return ThrowException(Exception::Error(String::New(
            "sourceStart out of bounds")));
  }

  if (source_end < 0 || ((size_t)source_end) > source->length()) {
    DBG("Buffer::Copy(args) X bad source");
    return ThrowException(Exception::Error(String::New(
            "sourceEnd out of bounds")));
  }

  ssize_t to_copy = MIN(source_end - source_start,
                        target->length() - target_start);

  memcpy((void*)(target->data() + target_start),
         (const void*)(source->data() + source_start),
         to_copy);

  DBG("Buffer::Copy(args) X");
  return scope.Close(Integer::New(to_copy));
}


// var charsWritten = buffer.utf8Write(string, offset);
Handle<Value> Buffer::Utf8Write(const Arguments &args) {
  DBG("Buffer::Utf8Write(args) X");
  HandleScope scope;
  Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This());

  if (!args[0]->IsString()) {
  DBG("Buffer::Utf8Write(args) X arg[0] not string");
    return ThrowException(Exception::TypeError(String::New(
            "Argument must be a string")));
  }

  Local<String> s = args[0]->ToString();

  size_t offset = args[1]->Int32Value();

  if (offset >= buffer->length_) {
    DBG("Buffer::Utf8Write(args) X offset bad");
    return ThrowException(Exception::TypeError(String::New(
            "Offset is out of bounds")));
  }

  const char *p = buffer->data() + offset;

  int char_written;

  int written = s->WriteUtf8((char*)p,
                             buffer->length_ - offset,
                             &char_written,
                             String::HINT_MANY_WRITES_EXPECTED);

  constructor_template->GetFunction()->Set(chars_written_sym,
                                           Integer::New(char_written));

  if (written > 0 && p[written-1] == '\0') written--;

  DBG("Buffer::Utf8Write(args) X");
  return scope.Close(Integer::New(written));
}


// var charsWritten = buffer.asciiWrite(string, offset);
Handle<Value> Buffer::AsciiWrite(const Arguments &args) {
  DBG("Buffer::AsciiWrite(args) E");
  HandleScope scope;

  Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This());

  if (!args[0]->IsString()) {
    DBG("Buffer::AsciiWrite(args) X arg[0] not string");
    return ThrowException(Exception::TypeError(String::New(
            "Argument must be a string")));
  }

  Local<String> s = args[0]->ToString();

  size_t offset = args[1]->Int32Value();

  if (offset >= buffer->length_) {
    DBG("Buffer::AsciiWrite(args) X bad offset");
    return ThrowException(Exception::TypeError(String::New(
            "Offset is out of bounds")));
  }

  const char *p = buffer->data() + offset;

  size_t towrite = MIN((unsigned long) s->Length(), buffer->length_ - offset);

  int written = s->WriteAscii((char*)p, 0, towrite, String::HINT_MANY_WRITES_EXPECTED);
  DBG("Buffer::AsciiWrite(args) X");
  return scope.Close(Integer::New(written));
}


Handle<Value> Buffer::BinaryWrite(const Arguments &args) {
  DBG("Buffer::BinaryWrite(args) E");
  HandleScope scope;

  Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This());

  if (!args[0]->IsString()) {
    DBG("Buffer::BinaryWrite(args) X arg[0] not string");
    return ThrowException(Exception::TypeError(String::New(
            "Argument must be a string")));
  }

  Local<String> s = args[0]->ToString();

  size_t offset = args[1]->Int32Value();

  if (offset >= buffer->length_) {
    DBG("Buffer::BinaryWrite(args) X offset bad");
    return ThrowException(Exception::TypeError(String::New(
            "Offset is out of bounds")));
  }

  char *p = (char*)buffer->data() + offset;

  size_t towrite = MIN((unsigned long) s->Length(), buffer->length_ - offset);

  int written = DecodeWrite(p, towrite, s, BINARY);
  DBG("Buffer::BinaryWrite(args) X");
  return scope.Close(Integer::New(written));
}


// buffer.unpack(format, index);
// Starting at 'index', unpacks binary from the buffer into an array.
// 'format' is a string
//
//  FORMAT  RETURNS
//    N     uint32_t   a 32bit unsigned integer in network byte order
//    n     uint16_t   a 16bit unsigned integer in network byte order
//    o     uint8_t    a 8bit unsigned integer
Handle<Value> Buffer::Unpack(const Arguments &args) {
  DBG("Buffer::Unpack(args) E");
  HandleScope scope;
  Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args.This());

  if (!args[0]->IsString()) {
    DBG("Buffer::Unpack(args) X arg[0] not string");
    return ThrowException(Exception::TypeError(String::New(
            "Argument must be a string")));
  }

  String::AsciiValue format(args[0]->ToString());
  uint32_t index = args[1]->Uint32Value();

#define OUT_OF_BOUNDS ThrowException(Exception::Error(String::New("Out of bounds")))

  Local<Array> array = Array::New(format.length());

  uint8_t  uint8;
  uint16_t uint16;
  uint32_t uint32;

  for (int i = 0; i < format.length(); i++) {
    switch ((*format)[i]) {
      // 32bit unsigned integer in network byte order
      case 'N':
        if (index + 3 >= buffer->length_) return OUT_OF_BOUNDS;
        uint32 = htonl(*(uint32_t*)(buffer->data() + index));
        array->Set(Integer::New(i), Integer::NewFromUnsigned(uint32));
        index += 4;
        break;

      // 16bit unsigned integer in network byte order
      case 'n':
        if (index + 1 >= buffer->length_) return OUT_OF_BOUNDS;
        uint16 = htons(*(uint16_t*)(buffer->data() + index));
        array->Set(Integer::New(i), Integer::NewFromUnsigned(uint16));
        index += 2;
        break;

      // a single octet, unsigned.
      case 'o':
        if (index >= buffer->length_) return OUT_OF_BOUNDS;
        uint8 = (uint8_t)buffer->data()[index];
        array->Set(Integer::New(i), Integer::NewFromUnsigned(uint8));
        index += 1;
        break;

      default:
        DBG("Buffer::Unpack(args) X unknown format character");
        return ThrowException(Exception::Error(
              String::New("Unknown format character")));
    }
  }

  DBG("Buffer::Unpack(args) X");
  return scope.Close(array);
}


// var nbytes = Buffer.byteLength("string", "utf8")
Handle<Value> Buffer::ByteLength(const Arguments &args) {
  DBG("Buffer::ByteLength(args) E");
  HandleScope scope;

  if (!args[0]->IsString()) {
    DBG("Buffer::ByteLength(args) X arg[0] not a string");
    return ThrowException(Exception::TypeError(String::New(
            "Argument must be a string")));
  }

  Local<String> s = args[0]->ToString();
  enum encoding e = ParseEncoding(args[1], UTF8);

  Local<Integer> length =
    Integer::New(e == UTF8 ? s->Utf8Length() : s->Length());

  DBG("Buffer::ByteLength(args) X");
  return scope.Close(length);
}

void Buffer::InitializeObjectTemplate(Handle<ObjectTemplate> target) {
  DBG("InitializeObjectTemplate(target) E:");
  HandleScope scope;

  length_symbol = Persistent<String>::New(String::NewSymbol("length"));
  chars_written_sym = Persistent<String>::New(String::NewSymbol("_charsWritten"));

  Local<FunctionTemplate> t = FunctionTemplate::New(Buffer::New);
  constructor_template = Persistent<FunctionTemplate>::New(t);
  constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
  constructor_template->SetClassName(String::NewSymbol("Buffer"));

  // copy free
  SET_PROTOTYPE_METHOD(constructor_template, "binarySlice", Buffer::BinarySlice);
  SET_PROTOTYPE_METHOD(constructor_template, "asciiSlice", Buffer::AsciiSlice);
  SET_PROTOTYPE_METHOD(constructor_template, "slice", Buffer::Slice);
  // TODO SET_PROTOTYPE_METHOD(t, "utf16Slice", Utf16Slice);
  // copy
  SET_PROTOTYPE_METHOD(constructor_template, "utf8Slice", Buffer::Utf8Slice);

  SET_PROTOTYPE_METHOD(constructor_template, "utf8Write", Buffer::Utf8Write);
  SET_PROTOTYPE_METHOD(constructor_template, "asciiWrite", Buffer::AsciiWrite);
  SET_PROTOTYPE_METHOD(constructor_template, "binaryWrite", Buffer::BinaryWrite);
  SET_PROTOTYPE_METHOD(constructor_template, "unpack", Buffer::Unpack);
  SET_PROTOTYPE_METHOD(constructor_template, "copy", Buffer::Copy);

  SET_PROTOTYPE_METHOD(constructor_template, "byteLength", Buffer::ByteLength);

  target->Set(String::NewSymbol("Buffer"), constructor_template);
  DBG("InitializeObjectTemplate(target) X:");
}