-
Notifications
You must be signed in to change notification settings - Fork 7
/
native.cc
46 lines (36 loc) · 1.21 KB
/
native.cc
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
#include <node.h>
const int maxValue = 10;
int numberOfCalls = 0;
void WhoAmI(const v8::FunctionCallbackInfo<v8::Value> &args)
{
v8::Isolate *isolate = args.GetIsolate();
auto message = v8::String::NewFromUtf8(isolate, "I'm a Node Hero!");
args.GetReturnValue().Set(message);
}
void Increment(const v8::FunctionCallbackInfo<v8::Value> &args)
{
v8::Isolate *isolate = args.GetIsolate();
if (!args[0]->IsNumber())
{
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Argument must be a number")));
return;
}
double argsValue = args[0]->NumberValue();
if (numberOfCalls + argsValue > maxValue)
{
isolate->ThrowException(v8::Exception::Error(
v8::String::NewFromUtf8(isolate, "Counter went through the roof!")));
return;
}
numberOfCalls += argsValue;
auto currentNumberOfCalls =
v8::Number::New(isolate, static_cast<double>(numberOfCalls));
args.GetReturnValue().Set(currentNumberOfCalls);
}
void Initialize(v8::Local<v8::Object> exports)
{
NODE_SET_METHOD(exports, "whoami", WhoAmI);
NODE_SET_METHOD(exports, "increment", Increment);
}
NODE_MODULE(module_name, Initialize)