-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAge.cpp
More file actions
55 lines (43 loc) · 1.47 KB
/
Age.cpp
File metadata and controls
55 lines (43 loc) · 1.47 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
#include "Age.hpp"
#include "AgeUnitDefinitions.hpp"
#include "Arguments.hpp"
#include "imebra.hpp"
#include <imebra/definitions.h>
using namespace bindings;
Nan::Persistent<v8::Function> Age::constructor;
Age::Age(std::uint32_t initialAge, imebra::ageUnit_t initialUnits): value(initialAge,initialUnits) {
}
void Age::Init(v8::Local<v8::Object>exports){
auto tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
tpl->SetClassName(Nan::New("Age").ToLocalChecked());
Nan::SetPrototypeMethod(tpl,"getAgeValue",GetAgeValue);
constructor.Reset(Nan::GetFunction(tpl).ToLocalChecked());
Nan::Set(exports,Nan::New("Age").ToLocalChecked(),Nan::GetFunction(tpl).ToLocalChecked());
}
NAN_METHOD(Age::GetAgeValue){
UNWRAP(info.This(),Age,a);
info.GetReturnValue().Set(Nan::New(a->value.getAgeValue()));
}
NAN_METHOD(Age::New){
UNWRAP(info.This(),Age,a);
uint_fast32_t initialAge;
if(!Arguments::GetUint32(info[0],initialAge)){
Nan::ThrowError("Second argument must be a valid string");
return;
}
imebra::ageUnit_t ageUnit;
if(!AgeUnitDefinitions::Decode(info[1],ageUnit)){
Nan::ThrowError("Second argument must be a valid string");
return;
}
Age* age;
try {
age = new Age(initialAge,ageUnit);
} catch(std::exception& e) {
Nan::ThrowError(e.what());
return;
}
age->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}