-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cc
172 lines (134 loc) · 4.14 KB
/
main.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
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
// hello.cc
#include <node.h>
#include <node_buffer.h>
#include <uv.h>
#include <string.h>
#include <iostream>
#include "open_vcdiff/src/google/vcencoder.h"
#include "open_vcdiff/src/google/vcdecoder.h"
struct PersistentBuffer
{
v8::Persistent<v8::Value> buffer;
char* data;
size_t len;
void Reset(v8::Isolate* isolate, v8::Local<v8::Value> val)
{
buffer.Reset(isolate, val);
data = node::Buffer::Data(val);
len = node::Buffer::Length(val);
}
void Reset()
{
data = NULL;
len = 0;
buffer.Reset();
}
};
struct InfoEncode
{
v8::Isolate* isolate;
uv_work_t request;
v8::Persistent<v8::Function> callback;
PersistentBuffer bufferSrc;
PersistentBuffer bufferIn;
std::string out;
bool success;
InfoEncode() { Reset(); }
void Reset()
{
callback.Reset();
bufferSrc.Reset();
bufferIn.Reset();
success = false;
}
};
template<class T>
inline v8::Local<T> ToLocal(v8::Persistent<T>* p_) {
return *reinterpret_cast<v8::Local<T>*>(p_);
}
void ThrowTypeError(v8::Isolate* isolate, const char* message)
{
isolate->ThrowException(v8::Exception::TypeError(v8::String::NewFromUtf8(isolate, message)));
}
void ThrowBadNumberArguments(v8::Isolate* isolate)
{
return ThrowTypeError(isolate, "Wrong number of arguments");
}
bool CheckArgs(v8::Isolate* isolate, const v8::FunctionCallbackInfo<v8::Value>& args)
{
if (args.Length() != 3)
return ThrowBadNumberArguments(isolate), false;
if (!node::Buffer::HasInstance(args[0]))
return ThrowTypeError(isolate, "args[0] should be a buffer."), false;
if (!node::Buffer::HasInstance(args[1]))
return ThrowTypeError(isolate, "args[1] should be a buffer."), false;
if (!args[2]->IsFunction())
return ThrowTypeError(isolate, "args[2] should be a function."), false;
return true ;
}
void DoDecode(uv_work_t* request)
{
InfoEncode* info = (InfoEncode*)request->data;
open_vcdiff::VCDiffDecoder decoder;
std::string delta(info->bufferIn.data, info->bufferIn.len);
info->success = decoder.Decode<std::string>(info->bufferSrc.data, info->bufferSrc.len, delta, &info->out);
}
void DoEncode(uv_work_t* request)
{
InfoEncode* info = (InfoEncode*)request->data;
open_vcdiff::VCDiffEncoder encoder(info->bufferSrc.data, info->bufferSrc.len);
//encoder.SetFormatFlags(open_vcdiff::VCD_FORMAT_INTERLEAVED); // no need
info->success = encoder.Encode<std::string>(info->bufferIn.data, info->bufferIn.len, &info->out);
}
void CleanInfo(uv_work_t* request, int status)
{
InfoEncode* info = (InfoEncode*)request->data;
v8::HandleScope scope(info->isolate);
v8::Local<v8::Object> buffer;
if (info->success)
{
buffer = node::Buffer::New(info->out.size());
memcpy(node::Buffer::Data(buffer), info->out.c_str(), info->out.size());
}
else
buffer = node::Buffer::New(0);
const unsigned argc = 1;
v8::Local<v8::Value> argv[argc] = { buffer };
ToLocal<v8::Function>(&info->callback)->Call(info->isolate->GetCurrentContext()->Global(), argc, argv);
info->Reset();
delete info;
}
void Encode(const v8::FunctionCallbackInfo<v8::Value>& args)
{
v8::Isolate* isolate = args.GetIsolate();
v8::HandleScope scope(isolate);
if (!CheckArgs(isolate, args))
return ;
InfoEncode* info = new InfoEncode();
info->isolate = isolate;
info->request.data = info;
info->bufferSrc.Reset(isolate, args[0]);
info->bufferIn.Reset(isolate, args[1]);
info->callback.Reset(isolate, v8::Local<v8::Function>::Cast(args[2]));
uv_queue_work(uv_default_loop(), &info->request, DoEncode, CleanInfo);
}
void Decode(const v8::FunctionCallbackInfo<v8::Value>& args)
{
v8::Isolate* isolate = args.GetIsolate();
v8::HandleScope scope(isolate);
if (!CheckArgs(isolate, args))
return ;
InfoEncode* info = new InfoEncode();
info->isolate = isolate;
info->request.data = info;
info->bufferSrc.Reset(isolate, args[0]);
info->bufferIn.Reset(isolate, args[1]);
info->callback.Reset(isolate, v8::Local<v8::Function>::Cast(args[2]));
uv_queue_work(uv_default_loop(), &info->request, DoDecode, CleanInfo);
}
void init(v8::Handle<v8::Object> exports)
{
NODE_SET_METHOD(exports, "Encode", Encode);
NODE_SET_METHOD(exports, "Decode", Decode);
}
NODE_MODULE(simple_vcdiff, init)