forked from ibmruntimes/vsam.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVsamFile.cpp
732 lines (598 loc) · 20.7 KB
/
VsamFile.cpp
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
/*
* Licensed Materials - Property of IBM
* (C) Copyright IBM Corp. 2017. All Rights Reserved.
* US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*/
#include "VsamFile.h"
#include <node_buffer.h>
#include <unistd.h>
#include <dynit.h>
#include <sstream>
#include <numeric>
using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::Persistent;
using v8::String;
using v8::Value;
using v8::Exception;
using v8::HandleScope;
using v8::Handle;
using v8::Array;
using v8::Integer;
using v8::Boolean;
using v8::MaybeLocal;
Persistent<Function> VsamFile::OpenSyncConstructor;
Persistent<Function> VsamFile::AllocSyncConstructor;
static void print_amrc() {
__amrc_type currErr = *__amrc; /* copy contents of __amrc */
/* structure so that values */
#pragma convert("IBM-1047")
printf("R15 value = %d\n", currErr.__code.__feedback.__rc);
printf("Reason code = %d\n", currErr.__code.__feedback.__fdbk);
printf("RBA = %d\n", currErr.__RBA);
printf("Last op = %d\n", currErr.__last_op);
#pragma convert(pop)
}
void VsamFile::DeleteCallback(uv_work_t* req, int status) {
VsamFile* obj = (VsamFile*)(req->data);
delete req;
//TODO: what if the write failed (buf != NULL)
if (status == UV_ECANCELED)
return;
const unsigned argc = 1;
HandleScope scope(obj->isolate_);
Local<Value> argv[argc];
if (obj->lastrc_ != 0) {
argv[0] = Exception::TypeError(
String::NewFromUtf8(obj->isolate_, "Failed to delete"));
obj->lastrc_ = 0;
}
else
argv[0] = v8::Null(obj->isolate_);
auto fn = Local<Function>::New(obj->isolate_, obj->cb_);
fn->Call(Null(obj->isolate_), argc, argv);
}
void VsamFile::WriteCallback(uv_work_t* req, int status) {
VsamFile* obj = (VsamFile*)(req->data);
delete req;
if (status == UV_ECANCELED)
return;
const unsigned argc = 1;
HandleScope scope(obj->isolate_);
Local<Value> argv[argc];
if (obj->lastrc_ != obj->reclen_)
argv[0] = Exception::TypeError(
String::NewFromUtf8(obj->isolate_, "Failed to write"));
else
argv[0] = v8::Null(obj->isolate_);
auto fn = Local<Function>::New(obj->isolate_, obj->cb_);
fn->Call(Null(obj->isolate_), argc, argv);
}
void VsamFile::UpdateCallback(uv_work_t* req, int status) {
VsamFile* obj = (VsamFile*)(req->data);
delete req;
//TODO: what if the update failed (buf != NULL)
if (status == UV_ECANCELED)
return;
const unsigned argc = 1;
HandleScope scope(obj->isolate_);
Local<Value> argv[argc] = { v8::Null(obj->isolate_) };
auto fn = Local<Function>::New(obj->isolate_, obj->cb_);
fn->Call(Null(obj->isolate_), argc, argv);
}
void VsamFile::ReadCallback(uv_work_t* req, int status) {
VsamFile* obj = (VsamFile*)(req->data);
delete req;
if (status == UV_ECANCELED)
return;
char* buf = (char*)(obj->buf_);
if (buf != NULL) {
const unsigned argc = 2;
HandleScope scope(obj->isolate_);
Local<Object> record = Object::New(obj->isolate_);
for(auto i = obj->layout_.begin(); i != obj->layout_.end(); ++i) {
if (i->type == LayoutItem::STRING) {
std::string str;
transform(buf, buf + i->maxLength, back_inserter(str), [](char c) -> char {
__e2a_l(&c, 1);
return c;
});
record->Set(String::NewFromUtf8(obj->isolate_, &(i->name[0])),
//node::Buffer::Copy(obj->isolate_, key.data(), key.size()).ToLocalChecked());
String::NewFromUtf8(obj->isolate_, str.c_str()));
}
buf += i->maxLength;
}
Local<Value> argv[argc] = { record, v8::Null(obj->isolate_) };
auto fn = Local<Function>::New(obj->isolate_, obj->cb_);
fn->Call(Null(obj->isolate_), argc, argv);
}
else {
const unsigned argc = 2;
HandleScope scope(obj->isolate_);
Local<Value> argv[argc] = { v8::Null(obj->isolate_),
v8::Null(obj->isolate_) };
auto fn = Local<Function>::New(obj->isolate_, obj->cb_);
fn->Call(Null(obj->isolate_), argc, argv);
}
}
void VsamFile::Find(uv_work_t* req) {
VsamFile* obj = (VsamFile*)(req->data);
if (flocate(obj->stream_, obj->key_.c_str(), obj->keylen_, __KEY_EQ)) {
obj->buf_ = NULL;
return;
}
char buf[obj->reclen_];
int ret = fread(buf, obj->reclen_, 1, obj->stream_);
//TODO: if read fails
if (ret == 1) {
obj->buf_ = malloc(obj->reclen_);
//TODO: if malloc fails
memcpy(obj->buf_, buf, obj->reclen_);
}
else {
obj->buf_ = NULL;
}
}
void VsamFile::Read(uv_work_t* req) {
VsamFile* obj = (VsamFile*)(req->data);
char buf[obj->reclen_];
int ret = fread(buf, obj->reclen_, 1, obj->stream_);
//TODO: if read fails
if (ret == 1) {
obj->buf_ = malloc(obj->reclen_);
//TODO: if malloc fails
memcpy(obj->buf_, buf, obj->reclen_);
}
else {
obj->buf_ = NULL;
}
}
void VsamFile::Delete(uv_work_t* req) {
VsamFile* obj = (VsamFile*)(req->data);
obj->lastrc_ = fdelrec(obj->stream_);
}
void VsamFile::Write(uv_work_t* req) {
VsamFile* obj = (VsamFile*)(req->data);
obj->lastrc_ = fwrite(obj->buf_, 1, obj->reclen_, obj->stream_);
free(obj->buf_);
obj->buf_ = NULL;
}
void VsamFile::Update(uv_work_t* req) {
VsamFile* obj = (VsamFile*)(req->data);
int ret = fupdate(obj->buf_, obj->reclen_, obj->stream_);
if (ret == 0) {
//TODO: error
}
free(obj->buf_);
obj->buf_ = NULL;
}
void VsamFile::Dealloc(uv_work_t* req) {
VsamFile* obj = (VsamFile*)(req->data);
#pragma convert("IBM-1047")
std::ostringstream dataset;
dataset << "//'" << obj->path_.c_str() << "'";
#pragma convert(pop)
obj->lastrc_ = remove(dataset.str().c_str());
}
void VsamFile::DeallocCallback(uv_work_t* req, int status) {
VsamFile* obj = (VsamFile*)(req->data);
delete req;
HandleScope scope(obj->isolate_);
if (status == UV_ECANCELED) {
return;
}
else if (obj->lastrc_ != 0) {
const unsigned argc = 1;
Local<Value> argv[argc] = {
Exception::TypeError(String::NewFromUtf8(obj->isolate_,
"Couldn't deallocate dataset"))
};
auto fn = Local<Function>::New(obj->isolate_, obj->cb_);
fn->Call(Null(obj->isolate_), argc, argv);
}
else {
const unsigned argc = 1;
Local<Value> argv[argc] = {
Null(obj->isolate_)
};
auto fn = Local<Function>::New(obj->isolate_, obj->cb_);
fn->Call(Null(obj->isolate_), argc, argv);
}
}
VsamFile::VsamFile(std::string& path, std::vector<LayoutItem>& layout,
Isolate* isolate, bool alloc) :
path_(path),
stream_(NULL),
keylen_(-1),
lastrc_(0),
layout_(layout),
isolate_(isolate),
buf_(NULL) {
#pragma convert("IBM-1047")
std::ostringstream dataset;
dataset << "//'" << path.c_str() << "'";
stream_ = fopen(dataset.str().c_str(), "rb+,type=record");
int err = __errno2();
#pragma convert(pop)
if (!alloc) {
if (stream_ == NULL) {
errmsg_ = err == 0xC00B0641 ? "Dataset does not exist" : "Invalid dataset name";
lastrc_ = -1;
return;
}
#pragma convert("IBM-1047")
stream_ = freopen(dataset.str().c_str(), "ab+,type=record", stream_);
#pragma convert(pop)
if (stream_ == NULL) {
errmsg_ = "Failed to open dataset";
lastrc_ = -1;
return;
}
} else {
if (stream_ != NULL) {
errmsg_ = "Dataset already exists";
fclose(stream_);
lastrc_ = -1;
return;
}
if (err != 0xC00B0641) {
errmsg_ = "Invalid dataset format";
lastrc_ = -1;
return;
}
std::ostringstream ddname;
#pragma convert("IBM-1047")
ddname << "NAMEDD";
#pragma convert(pop)
__dyn_t dyn;
dyninit(&dyn);
dyn.__dsname = &(path_[0]);
dyn.__ddname = &(ddname.str()[0]);
dyn.__normdisp = __DISP_CATLG;
dyn.__lrecl = std::accumulate(layout_.begin(), layout_.end(), 0,
[](int n, LayoutItem& l) -> int { return n + l.maxLength; });
dyn.__keylength = layout_[0].maxLength;
dyn.__recorg = __KS;
if (dynalloc(&dyn) != 0) {
errmsg_ = "Failed to allocate dataset";
lastrc_ = -1;
return;
}
#pragma convert("IBM-1047")
stream_ = fopen(dataset.str().c_str(), "ab+,type=record");
#pragma convert(pop)
if (stream_ == NULL) {
errmsg_ = "Failed to open new dataset";
lastrc_ = -1;
return;
}
}
fldata_t info;
fldata(stream_, NULL, &info);
keylen_ = info.__vsamkeylen;
reclen_ = info.__maxreclen;
if (keylen_ != layout_[0].maxLength) {
errmsg_ = "Incorrect key length";
fclose(stream_);
lastrc_ = -1;
return;
}
}
VsamFile::~VsamFile() {
if (stream_ != NULL)
fclose(stream_);
}
void VsamFile::SetPrototypeMethods(Local<FunctionTemplate>& tpl) {
// Prototype
NODE_SET_PROTOTYPE_METHOD(tpl, "read", Read);
NODE_SET_PROTOTYPE_METHOD(tpl, "find", Find);
NODE_SET_PROTOTYPE_METHOD(tpl, "update", Update);
NODE_SET_PROTOTYPE_METHOD(tpl, "write", Write);
NODE_SET_PROTOTYPE_METHOD(tpl, "delete", Delete);
NODE_SET_PROTOTYPE_METHOD(tpl, "close", Close);
NODE_SET_PROTOTYPE_METHOD(tpl, "dealloc", Dealloc);
}
void VsamFile::Init(Isolate* isolate) {
// Prepare constructor template
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, VsamFile::OpenSync);
tpl->SetClassName(String::NewFromUtf8(isolate, "VsamFile"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
SetPrototypeMethods(tpl);
OpenSyncConstructor.Reset(isolate, tpl->GetFunction());
tpl = FunctionTemplate::New(isolate, VsamFile::AllocSync);
tpl->SetClassName(String::NewFromUtf8(isolate, "VsamFile"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
SetPrototypeMethods(tpl);
AllocSyncConstructor.Reset(isolate, tpl->GetFunction());
}
void VsamFile::Construct(const FunctionCallbackInfo<Value>& args, bool alloc) {
Isolate* isolate = args.GetIsolate();
// Check the number of arguments passed.
if (args.Length() != 2) {
// Throw an Error that is passed back to JavaScript
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}
if (!args[0]->IsString()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong arguments")));
return;
}
std::string path (*v8::String::Utf8Value(args[0]->ToString()));
transform(path.begin(), path.end(), path.begin(), [](char c) -> char {
__a2e_l(&c, 1);
return c;
});
Local<Object> schema = args[1]->ToObject();
Local<Array> properties = schema->GetPropertyNames();
std::vector<LayoutItem> layout;
for (int i = 0; i < properties->Length(); ++i) {
String::Utf8Value name(properties->Get(i)->ToString());
Local<Object> item = Local<Object>::Cast(schema->Get(properties->Get(i)));
if (item.IsEmpty()) {
isolate->ThrowException(Exception::TypeError( String::NewFromUtf8(isolate, "Json is incorrect")));
return;
}
Local<Value> length = Local<Value>::Cast(item->Get( Local<String>(String::NewFromUtf8(isolate, "maxLength"))));
if (length.IsEmpty() || !length->IsNumber()) {
isolate->ThrowException(Exception::TypeError( String::NewFromUtf8(isolate, "Json is incorrect")));
return;
}
layout.push_back(LayoutItem(name, length->ToInteger()->Value(), LayoutItem::STRING));
}
VsamFile *obj = new VsamFile(path, layout, isolate, alloc);
if (obj->lastrc_) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, obj->errmsg_.c_str())));
delete obj;
return;
}
obj->Wrap(args.This());
args.GetReturnValue().Set(args.This());
}
void VsamFile::AllocSync(const FunctionCallbackInfo<Value>& args) {
if (args.IsConstructCall()) {
Construct(args, true);
} else {
// Invoked as plain function `MyObject(...)`, turn into construct call.
Isolate* isolate = args.GetIsolate();
const int argc = 2;
Local<Value> argv[argc] = { args[0], args[1] };
Local<Function> cons = Local<Function>::New(isolate, AllocSyncConstructor);
Local<Context> context = isolate->GetCurrentContext();
MaybeLocal<Object> instance =
cons->NewInstance(context, argc, argv);
if(!instance.IsEmpty())
args.GetReturnValue().Set(instance.ToLocalChecked());
}
}
void VsamFile::OpenSync(const FunctionCallbackInfo<Value>& args) {
if (args.IsConstructCall()) {
Construct(args, false);
} else {
// Invoked as plain function `MyObject(...)`, turn into construct call.
Isolate* isolate = args.GetIsolate();
const int argc = 2;
Local<Value> argv[argc] = { args[0], args[1] };
Local<Function> cons = Local<Function>::New(isolate, OpenSyncConstructor);
Local<Context> context = isolate->GetCurrentContext();
MaybeLocal<Object> instance =
cons->NewInstance(context, argc, argv);
if(!instance.IsEmpty())
args.GetReturnValue().Set(instance.ToLocalChecked());
}
}
void VsamFile::Exist(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
if (args.Length() != 1) {
// Throw an Error that is passed back to JavaScript
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}
if (!args[0]->IsString()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong arguments")));
return;
}
std::string path (*v8::String::Utf8Value(args[0]->ToString()));
transform(path.begin(), path.end(), path.begin(), [](char c) -> char {
__a2e_l(&c, 1);
return c;
});
#pragma convert("IBM-1047")
std::ostringstream dataset;
dataset << "//'" << path.c_str() << "'";
FILE *stream = fopen(dataset.str().c_str(), "rb+,type=record");
#pragma convert(pop)
if (stream == NULL) {
args.GetReturnValue().Set(Boolean::New(isolate, false));
return;
}
fclose(stream);
args.GetReturnValue().Set(Boolean::New(isolate, true));
}
void VsamFile::Close(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
VsamFile* obj = ObjectWrap::Unwrap<VsamFile>(args.Holder());
if (obj->stream_ == NULL) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "VSAM file is not open")));
return;
}
if (fclose(obj->stream_)) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Error closing file")));
return;
}
obj->stream_ = NULL;
}
void VsamFile::Delete(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
if (args.Length() < 1) {
// Throw an Error that is passed back to JavaScript
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}
if (!args[0]->IsFunction()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong arguments")));
return;
}
VsamFile* obj = ObjectWrap::Unwrap<VsamFile>(args.Holder());
uv_work_t* request = new uv_work_t;
request->data = obj;
obj->cb_ = Persistent<Function>(isolate, Handle<Function>::Cast(args[0]));
obj->isolate_ = isolate;
uv_queue_work(uv_default_loop(), request, Delete, DeleteCallback);
}
void VsamFile::Write(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
if (args.Length() < 2) {
// Throw an Error that is passed back to JavaScript
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}
if (!args[1]->IsFunction()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong arguments")));
return;
}
Local<Object> record = args[0]->ToObject();
VsamFile* obj = ObjectWrap::Unwrap<VsamFile>(args.Holder());
obj->buf_ = malloc(obj->reclen_); //TODO: error
char* buf = (char*)obj->buf_;
for(auto i = obj->layout_.begin(); i != obj->layout_.end(); ++i) {
if (i->type == LayoutItem::STRING) {
Local<String> field = Local<String>::Cast(record->Get(String::NewFromUtf8(obj->isolate_, &(i->name[0])))); //TODO: error if type is not string
std::string key (*v8::String::Utf8Value(field));
transform(key.begin(), key.end(), key.begin(), [](char c) -> char {
__a2e_l(&c, 1);
return c;
});
memcpy(buf, key.c_str(), key.length() + 1);
buf += i->maxLength; //TODO: error if key length > maxLength
}
}
uv_work_t* request = new uv_work_t;
request->data = obj;
obj->isolate_ = isolate;
uv_queue_work(uv_default_loop(), request, Write, WriteCallback);
obj->cb_ = Persistent<Function>(isolate, Handle<Function>::Cast(args[1]));
}
void VsamFile::Update(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
if (args.Length() < 2) {
// Throw an Error that is passed back to JavaScript
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}
if (!args[1]->IsFunction()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong arguments")));
return;
}
Local<Object> record = args[0]->ToObject();
VsamFile* obj = ObjectWrap::Unwrap<VsamFile>(args.Holder());
obj->buf_ = malloc(obj->reclen_); //TODO: error
char* buf = (char*)obj->buf_;
for(auto i = obj->layout_.begin(); i != obj->layout_.end(); ++i) {
if (i->type == LayoutItem::STRING) {
Local<String> field = Local<String>::Cast(record->Get(String::NewFromUtf8(obj->isolate_, &(i->name[0])))); //TODO: error if type is not string
std::string key (*v8::String::Utf8Value(field));
transform(key.begin(), key.end(), key.begin(), [](char c) -> char {
__a2e_l(&c, 1);
return c;
});
memcpy(buf, key.c_str(), key.length() + 1);
buf += i->maxLength; //TODO: error if key length > maxLength
}
}
uv_work_t* request = new uv_work_t;
request->data = obj;
obj->cb_ = Persistent<Function>(isolate, Handle<Function>::Cast(args[1]));
obj->isolate_ = isolate;
uv_queue_work(uv_default_loop(), request, Update, UpdateCallback);
}
void VsamFile::Find(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
if (args.Length() < 2) {
// Throw an Error that is passed back to JavaScript
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}
if (!args[0]->IsString() || !args[1]->IsFunction()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong arguments")));
return;
}
std::string key (*v8::String::Utf8Value(args[0]->ToString()));
transform(key.begin(), key.end(), key.begin(), [](char c) -> char {
__a2e_l(&c, 1);
return c;
});
VsamFile* obj = ObjectWrap::Unwrap<VsamFile>(args.Holder());
uv_work_t* request = new uv_work_t;
request->data = obj;
obj->cb_ = Persistent<Function>(isolate, Handle<Function>::Cast(args[1]));
obj->isolate_ = isolate;
obj->key_ = key;
uv_queue_work(uv_default_loop(), request, Find, ReadCallback);
}
void VsamFile::Read(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
if (args.Length() < 1) {
// Throw an Error that is passed back to JavaScript
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}
if (!args[0]->IsFunction()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong arguments")));
return;
}
VsamFile* obj = ObjectWrap::Unwrap<VsamFile>(args.Holder());
uv_work_t* request = new uv_work_t;
request->data = obj;
obj->cb_ = Persistent<Function>(isolate, Handle<Function>::Cast(args[0]));
obj->isolate_ = isolate;
uv_queue_work(uv_default_loop(), request, Read, ReadCallback);
}
void VsamFile::Dealloc(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
if (args.Length() < 1) {
// Throw an Error that is passed back to JavaScript
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}
if (!args[0]->IsFunction()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong arguments")));
return;
}
VsamFile* obj = ObjectWrap::Unwrap<VsamFile>(args.Holder());
if (obj->stream_ != NULL) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Cannot dealloc an open VSAM file")));
return;
}
uv_work_t* request = new uv_work_t;
request->data = obj;
obj->cb_ = Persistent<Function>(isolate, Handle<Function>::Cast(args[0]));
obj->isolate_ = isolate;
uv_queue_work(uv_default_loop(), request, Dealloc, DeallocCallback);
}