-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculator.cpp
111 lines (85 loc) · 3.26 KB
/
calculator.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
#include "calculator.h"
// static 변수 초기화
Napi::FunctionReference* Calculator::functionRef =
new Napi::FunctionReference();
// Javasciprt 클래스 생성 및 초기화
Napi::Function Calculator::Init(const Napi::Env env) {
// 클래스 생성
Napi::Function func =
DefineClass(env, "Calculator",
{InstanceMethod("add", &Calculator::Add),
InstanceMethod("subtract", &Calculator::Subtract),
InstanceMethod("multiply", &Calculator::Multiply),
InstanceMethod("divide", &Calculator::Divide),
InstanceMethod("getValue", &Calculator::GetValue)});
// 클래스 생성 후 functionRef에 저장 (Persistent 함수는 GC 대상 제외시킴)
*Calculator::functionRef = Napi::Persistent(func);
return func;
}
// 팩토리 함수
Napi::Object Calculator::From(Napi::Env env, Napi::Value arg) {
Napi::Object obj = Calculator::functionRef->New({arg});
return obj;
}
// 생성자
Calculator::Calculator(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<Calculator>(info) {
Napi::Env env = info.Env();
// 인자가 없거나 숫자가 아닌 경우 에러 처리
int length = info.Length();
if (length <= 0 || !info[0].IsNumber()) {
Napi::TypeError::New(env, "Number expected").ThrowAsJavaScriptException();
return;
}
Napi::Number value = info[0].As<Napi::Number>();
this->value_ = value.DoubleValue();
}
Napi::Value Calculator::GetValue(const Napi::CallbackInfo& info) {
double num = this->value_;
return Napi::Number::New(info.Env(), num);
}
Napi::Value Calculator::Add(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
int length = info.Length();
if (length <= 0 || !info[0].IsNumber()) {
Napi::TypeError::New(env, "Number expected").ThrowAsJavaScriptException();
return env.Null();
}
const Napi::Number value = info[0].As<Napi::Number>();
this->value_ = this->value_ + value.DoubleValue();
return Calculator::GetValue(info);
}
Napi::Value Calculator::Subtract(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
int length = info.Length();
if (length <= 0 || !info[0].IsNumber()) {
Napi::TypeError::New(env, "Number expected").ThrowAsJavaScriptException();
return env.Null();
}
const Napi::Number value = info[0].As<Napi::Number>();
this->value_ = this->value_ - value.DoubleValue();
return Calculator::GetValue(info);
}
Napi::Value Calculator::Multiply(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Number multiple;
int length = info.Length();
if (length <= 0 || !info[0].IsNumber()) {
Napi::TypeError::New(env, "Number expected").ThrowAsJavaScriptException();
return env.Null();
}
const Napi::Number value = info[0].As<Napi::Number>();
this->value_ = this->value_ * value.DoubleValue();
return Calculator::GetValue(info);
}
Napi::Value Calculator::Divide(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
int length = info.Length();
if (length <= 0 || !info[0].IsNumber()) {
Napi::TypeError::New(env, "Number expected").ThrowAsJavaScriptException();
return env.Null();
}
const Napi::Number value = info[0].As<Napi::Number>();
this->value_ = this->value_ / value.DoubleValue();
return Calculator::GetValue(info);
}