-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbook_wrap.cpp
More file actions
268 lines (228 loc) · 9.22 KB
/
book_wrap.cpp
File metadata and controls
268 lines (228 loc) · 9.22 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
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
#include <node.h>
#include <iostream>
#include "book_wrap.hpp"
#include "person_wrap.hpp"
#include "book.hpp"
using namespace v8;
Persistent<Function> BookWrap::Constructor;
BookWrap::BookWrap() : m_book() {}
BookWrap::~BookWrap() {}
// Add wrapper class to runtime environment
void BookWrap::Init(Handle<Object> exports) {
Isolate* isolate = exports->GetIsolate();
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, BookWrap::New);
tpl->SetClassName(String::NewFromUtf8(isolate, "Book"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(tpl, "add", Add);
NODE_SET_PROTOTYPE_METHOD(tpl, "length", Length);
NODE_SET_PROTOTYPE_METHOD(tpl, "lookup", Lookup);
NODE_SET_PROTOTYPE_METHOD(tpl, "each", Each);
NODE_SET_PROTOTYPE_METHOD(tpl, "apply", Apply);
tpl->InstanceTemplate()->SetIndexedPropertyHandler(Getter, Setter, 0, Deleter, Enumerator);
Constructor.Reset(isolate, tpl->GetFunction());
exports->Set(String::NewFromUtf8(isolate, "Book"), tpl->GetFunction());
}
// Create a new instance of the BookWrap class
void BookWrap::New(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
// constructor take no arguments
if (args.IsConstructCall()) {
// constructor take no arguments
if (args.Length() == 0) {
BookWrap* bw = new BookWrap();
Book *p = new Book();
bw->m_book = p;
bw->Wrap(args.This());
args.GetReturnValue().Set(args.This());
}
else {
isolate->ThrowException(Exception::SyntaxError(String::NewFromUtf8(isolate, "No arguments ecpected")));
args.GetReturnValue().SetUndefined();
}
}
else {
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Called as plain function is not permitted")));
args.GetReturnValue().SetUndefined();
}
}
void BookWrap::Getter(uint32_t index, const PropertyCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
HandleScope scope(isolate);
BookWrap* bw = ObjectWrap::Unwrap<BookWrap>(info.This());
Book* b = bw->m_book;
if (index >= b->size()) {
isolate->ThrowException(Exception::RangeError(String::NewFromUtf8(isolate, "invalid row index")));
info.GetReturnValue().SetUndefined();
}
else {
Handle<Object> result = PersonWrap::New(isolate, b, index);
info.GetReturnValue().Set(result);
}
}
void BookWrap::Setter(uint32_t index, Local<Value> value, const PropertyCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
HandleScope scope(isolate);
BookWrap* bw = ObjectWrap::Unwrap<BookWrap>(info.This());
Book* b = bw->m_book;
if (value->IsArray()) {
if (index < b->size()) {
Local<v8::Array> arr = Local<v8::Array>::Cast(value);
if (arr->Length() == 3) {
const String::Utf8Value firstname(arr->Get(0)->ToString());
const String::Utf8Value lastname(arr->Get(1)->ToString());
const time_t birthday = time_t(0.001*(*arr->Get(2))->NumberValue());
Person *p = (*b)[index];
p->firstname(*firstname);
p->lastname(*lastname);
p->birthday(birthday);
}
else {
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Three elements expected")));
info.GetReturnValue().SetUndefined();
return;
}
}
if (index == b->size()) {
Local<v8::Array> arr = Local<v8::Array>::Cast(value);
if (arr->Length() == 3) {
const String::Utf8Value firstname(arr->Get(0)->ToString());
const String::Utf8Value lastname(arr->Get(1)->ToString());
const time_t birthday = time_t(0.001*(*arr->Get(2))->NumberValue());
Person *p = new Person();
p->firstname(*firstname);
p->lastname(*lastname);
p->birthday(birthday);
b->add(p);
}
else {
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Three elements expected")));
info.GetReturnValue().SetUndefined();
return;
}
}
else {
isolate->ThrowException(Exception::RangeError(String::NewFromUtf8(isolate, "Invalid index")));
info.GetReturnValue().SetUndefined();
return;
}
}
else {
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Object expected")));
info.GetReturnValue().SetUndefined();
return;
}
info.GetReturnValue().SetUndefined();
}
void BookWrap::Deleter(uint32_t index, const PropertyCallbackInfo<Boolean>& info) {
Isolate* isolate = info.GetIsolate();
HandleScope scope(isolate);
Book* b = ObjectWrap::Unwrap<BookWrap>(info.This())->m_book;
try {
b->remove(index);
info.GetReturnValue().Set(Boolean::New(isolate, true));
}
catch (Exception e) {
info.GetReturnValue().Set(Boolean::New(isolate, false));
}
}
void BookWrap::Enumerator(const PropertyCallbackInfo<Array>& info) {
Isolate* isolate = info.GetIsolate();
HandleScope scope(isolate);
Book* b = ObjectWrap::Unwrap<BookWrap>(info.This())->m_book;
Handle<v8::Array> result = v8::Array::New(isolate, b->size());
for(size_t i=0; i<b->size(); ++i) {
result->Set(i, Integer::New(isolate, i));
}
info.GetReturnValue().Set(result);
}
void BookWrap::Length(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
BookWrap* bw = ObjectWrap::Unwrap<BookWrap>(args.This());
const int count = bw->m_book->size();
Local<Integer> result = Integer::New(isolate, count);
args.GetReturnValue().Set(result);
}
void BookWrap::Each(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
Book* book = ObjectWrap::Unwrap<BookWrap>(args.This())->m_book;
if (args.Length() == 1) {
if (args[0]->IsFunction()) {
Local<Function> fun = Local<Function>::Cast(args[0]);
for(uint32_t i = 0; i < book->size(); ++i) {
Local<Object> pw = PersonWrap::New(isolate, book, i);
Local<Value> argv[1] = { pw };
fun->Call(Null(isolate), 1, argv);
}
args.GetReturnValue().SetUndefined();
return;
}
else {
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Function expected")));
args.GetReturnValue().SetUndefined();
return;
}
}
else {
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "One argument expected")));
args.GetReturnValue().SetUndefined();
return;
}
}
void BookWrap::Apply(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
Local<Function> fun = Local<Function>::Cast(args[0]);
Handle<Value> argv[] = { args.This() };
TryCatch trycatch;
Handle<Value> v = fun->Call(Null(isolate), 1, argv);
if (trycatch.HasCaught()) {
trycatch.ReThrow();
}
args.GetReturnValue().Set(v);
}
void BookWrap::Add(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
if (args.Length() == 1) {
BookWrap* bw = ObjectWrap::Unwrap<BookWrap>(args.This());
Book* b = bw->m_book;
PersonWrap* pw = ObjectWrap::Unwrap<PersonWrap>(args[0]->ToObject());
b->add(pw->m_person);
}
else {
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Object expected")));
}
args.GetReturnValue().SetUndefined();
}
void BookWrap::Lookup(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
if (args.Length() == 1) {
if (args[0]->IsString()) {
const String::Utf8Value s(args[0]->ToString());
Book* b = ObjectWrap::Unwrap<BookWrap>(args.This())->m_book;
try {
Person* p = b->lookup(*s);
Local<Object> obj = PersonWrap::NewInstance();
PersonWrap* pw = ObjectWrap::Unwrap<PersonWrap>(obj);
pw->m_person = p;
args.GetReturnValue().Set(obj);
}
catch (...) {
isolate->ThrowException(Exception::RangeError(String::NewFromUtf8(isolate, "Not found")));
args.GetReturnValue().SetUndefined();
}
}
else {
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "String expected")));
args.GetReturnValue().SetUndefined();
}
}
else {
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "One argument expected")));
args.GetReturnValue().SetUndefined();
}
}