-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHelloBuffer.cc
87 lines (68 loc) · 1.7 KB
/
HelloBuffer.cc
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
/*
* get point from buffer
* writed by jifeng([email protected])
*
*/
#include <string.h>
#include <v8.h>
#include <node.h>
#include <node_buffer.h>
using namespace node;
using namespace v8;
using namespace std;
class HelloBuffer: ObjectWrap
{
private:
int m_count;
public:
static Persistent<FunctionTemplate> s_ct;
static void Init(Handle<Object> target)
{
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(New);
s_ct = Persistent<FunctionTemplate>::New(t);
s_ct->InstanceTemplate()->SetInternalFieldCount(1);
s_ct->SetClassName(String::NewSymbol("HelloBuffer"));
NODE_SET_PROTOTYPE_METHOD(s_ct , "manageBuffer" , manageBuffer);
target->Set(String::NewSymbol("HelloBuffer"),
s_ct->GetFunction());
}
HelloBuffer() :
m_count(0)
{
}
~HelloBuffer()
{
}
static Handle<Value> New(const Arguments& args)
{
HandleScope scope;
HelloBuffer* hw = new HelloBuffer();
hw->Wrap(args.This());
return args.This();
}
static Handle<Value> manageBuffer(const Arguments& args)
{
HandleScope scope;
HelloBuffer* hw = ObjectWrap::Unwrap<HelloBuffer>(args.This());
hw->m_count++;
Local<Value> arg1 = args[0];
if (Buffer::HasInstance(arg1)) {
size_t size = Buffer::Length(arg1->ToObject());
char* bufferdata = Buffer::Data(arg1->ToObject());
//to do with size and bufferdata
return scope.Close(Number::New(size));
}
else{
return scope.Close(Number::New(-1));
}
}
};
Persistent<FunctionTemplate> HelloBuffer::s_ct;
extern "C" {
static void init (Handle<Object> target)
{
HelloBuffer::Init(target);
}
NODE_MODULE(HelloBuffer, init);
}