Skip to content

Commit 468042f

Browse files
committed
Add C++ Buffer migration tips
1 parent fe6f363 commit 468042f

1 file changed

Lines changed: 47 additions & 9 deletions

File tree

src/node_buffer.h

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,56 @@ namespace node {
1717
* buffer.asciiSlice(0, 3)
1818
*/
1919

20+
/*
21+
The C++ API for Buffer changed radically between v0.2 and v0.3, in fact
22+
it was the reason for bumping the version. In v0.2 JavaScript Buffers and
23+
C++ Buffers were in one-to-one correspondence via ObjectWrap. We found
24+
that it was faster to expose the C++ Buffers to JavaScript as a
25+
"SlowBuffer" which is used as a private backend to pure JavaScript
26+
"Buffer" objects - a 'Buffer' in v0.3 might look like this:
27+
28+
{ _parent: s,
29+
_offset: 520,
30+
length: 5 }
31+
32+
Migrating code C++ Buffer code from v0.2 to v0.3 is difficult. Here are
33+
some tips:
34+
- buffer->data() calls should become Buffer::Data(buffer) calls.
35+
- buffer->length() calls should become Buffer::Length(buffer) calls.
36+
- There should not be any ObjectWrap::Unwrap<Buffer>() calls. You should
37+
not be storing pointers to Buffer objects at all - as they are
38+
now considered internal structures. Instead consider making a
39+
JavaScript reference to the buffer.
40+
41+
See the source code node-png as an example of a module which successfully
42+
compiles on both v0.2 and v0.3 while making heavy use of the C++ Buffer
43+
API.
44+
45+
*/
46+
2047

2148
class Buffer : public ObjectWrap {
2249
public:
50+
51+
static bool HasInstance(v8::Handle<v8::Value> val);
52+
53+
static inline char* Data(v8::Handle<v8::Object> obj) {
54+
return (char*)obj->GetIndexedPropertiesExternalArrayData();
55+
}
56+
57+
static inline char* Data(Buffer *b) {
58+
return Buffer::Data(b->handle_);
59+
}
60+
61+
static inline size_t Length(v8::Handle<v8::Object> obj) {
62+
return (size_t)obj->GetIndexedPropertiesExternalArrayDataLength();
63+
}
64+
65+
static inline size_t Length(Buffer *b) {
66+
return Buffer::Length(b);
67+
}
68+
69+
2370
~Buffer();
2471

2572
typedef void (*free_callback)(char *data, void *hint);
@@ -32,15 +79,6 @@ class Buffer : public ObjectWrap {
3279
static Buffer* New(char *data, size_t len); // public constructor
3380
static Buffer* New(char *data, size_t length,
3481
free_callback callback, void *hint); // public constructor
35-
static bool HasInstance(v8::Handle<v8::Value> val);
36-
37-
static inline char* Data(v8::Handle<v8::Object> obj) {
38-
return (char*)obj->GetIndexedPropertiesExternalArrayData();
39-
}
40-
41-
static inline size_t Length(v8::Handle<v8::Object> obj) {
42-
return (size_t)obj->GetIndexedPropertiesExternalArrayDataLength();
43-
}
4482

4583
private:
4684
static v8::Persistent<v8::FunctionTemplate> constructor_template;

0 commit comments

Comments
 (0)