-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDate.cpp
More file actions
161 lines (141 loc) · 4.5 KB
/
Date.cpp
File metadata and controls
161 lines (141 loc) · 4.5 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
#include "Date.hpp"
#include "Arguments.hpp"
#include "imebra.hpp"
#include <cstdint>
#include <vector>
using namespace bindings;
Nan::Persistent<v8::Function> Date::constructor;
Date::Date(
uint32_t initialYear,
uint32_t initialMonth,
uint32_t initialDay,
uint32_t initialHour,
uint32_t initialMinutes,
uint32_t initialSeconds,
uint32_t initialNanoseconds,
int32_t initialOffsetHours,
int32_t initialOffsetMinutes
):
value(initialYear,
initialMonth,
initialDay,
initialHour,
initialMinutes,
initialSeconds,
initialNanoseconds,
initialOffsetHours,
initialOffsetMinutes
)
{
}
void Date::Init(v8::Local<v8::Object> exports){
auto tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New("Date").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(tpl,"getYear",GetYear);
Nan::SetPrototypeMethod(tpl,"getMinutes",GetMinutes);
Nan::SetPrototypeMethod(tpl,"getDay",GetDay);
Nan::SetPrototypeMethod(tpl,"getMonth",GetMonth);
Nan::SetPrototypeMethod(tpl,"getSeconds",GetSeconds);
Nan::SetPrototypeMethod(tpl,"getNanoseconds",GetNanoseconds);
Nan::SetPrototypeMethod(tpl,"getOffsetHours",GetOffsetHours);
Nan::SetPrototypeMethod(tpl,"getOffsetMinutes",GetOffsetMinutes);
Nan::SetPrototypeMethod(tpl,"getHour",GetHour);
constructor.Reset(Nan::GetFunction(tpl).ToLocalChecked());
Nan::Set(exports,Nan::New("Date").ToLocalChecked(),Nan::GetFunction(tpl).ToLocalChecked());
}
NAN_METHOD(Date::New){
uint_fast32_t initialYear = 0;
uint_fast32_t initialMonth = 0;
uint_fast32_t initialDay = 0;
uint_fast32_t initialHour = 0;
uint_fast32_t initialMinutes = 0;
uint_fast32_t initialSeconds = 0;
uint_fast32_t initialNanoseconds = 0;
int_fast32_t initialOffsetHours = 0;
int_fast32_t initialOffsetMinutes = 0;
std::vector<uint_fast32_t*> unsignedIntegers {
&initialYear,
&initialMonth,
&initialDay,
&initialHour,
&initialMinutes,
&initialSeconds,
&initialNanoseconds
};
std::vector<int_fast32_t*> signedIntegers {
&initialOffsetHours,
&initialOffsetMinutes
};
uint32_t i,lastIndex;
for(i = 0; i < unsignedIntegers.size(); i++){
auto& out = *unsignedIntegers[i];
if(!Arguments::GetUint32(info[i],out)){
Nan::ThrowError(std::string("Argument " + std::to_string(i + 1) + " must be a valid 32-bit integer").c_str());
return;
}
}
lastIndex = i;
for(i = 0; i < signedIntegers.size(); i++){
auto& out = *signedIntegers[i];
if(!Arguments::GetInt32(info[lastIndex + i],out)){
Nan::ThrowError(std::string("Argument " + std::to_string(lastIndex + i + 1) + " must be a valid 32-bit integer").c_str());
return;
}
}
Date* date;
try {
date = new Date(
initialYear,
initialMonth,
initialDay,
initialHour,
initialMinutes,
initialSeconds,
initialNanoseconds,
initialOffsetHours,
initialOffsetMinutes
);
} catch(std::exception&e){
Nan::ThrowError(e.what());
return;
}
date->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
NAN_METHOD(Date::GetYear) {
UNWRAP(info.This(),Date,d);
info.GetReturnValue().Set(Nan::New(d->value.getYear()));
}
NAN_METHOD(Date::GetMonth) {
UNWRAP(info.This(),Date,d);
info.GetReturnValue().Set(Nan::New(d->value.getMonth()));
}
NAN_METHOD(Date::GetDay) {
UNWRAP(info.This(),Date,d);
info.GetReturnValue().Set(Nan::New(d->value.getDay()));
}
NAN_METHOD(Date::GetMinutes) {
UNWRAP(info.This(),Date,d);
info.GetReturnValue().Set(Nan::New(d->value.getMinutes()));
}
NAN_METHOD(Date::GetSeconds) {
UNWRAP(info.This(),Date,d);
info.GetReturnValue().Set(Nan::New(d->value.getSeconds()));
}
NAN_METHOD(Date::GetHour) {
UNWRAP(info.This(),Date,d);
info.GetReturnValue().Set(Nan::New(d->value.getHour()));
}
NAN_METHOD(Date::GetNanoseconds) {
UNWRAP(info.This(),Date,d);
info.GetReturnValue().Set(Nan::New(d->value.getNanoseconds()));
}
NAN_METHOD(Date::GetOffsetHours) {
UNWRAP(info.This(),Date,d);
info.GetReturnValue().Set(Nan::New(d->value.getOffsetHours()));
}
NAN_METHOD(Date::GetOffsetMinutes) {
UNWRAP(info.This(),Date,d);
info.GetReturnValue().Set(Nan::New(d->value.getOffsetMinutes()));
}