-
Notifications
You must be signed in to change notification settings - Fork 137
/
addon.cpp
43 lines (32 loc) · 1.19 KB
/
addon.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
/**
* @file addon.cpp
* @brief A quick 'hello world' Napi Addon in C++
*/
// Required header and C++ flag
#if __has_include(<napi.h>) && BUILDING_NODE_EXTENSION
#include <napi.h>
Napi::Value Hello(const Napi::CallbackInfo& info) {
return Napi::String::New(info.Env(), "addon.node is online!");
}
Napi::Value Version(const Napi::CallbackInfo& info) {
return Napi::Number::New(info.Env(), NAPI_VERSION);
}
Napi::Object Init(Napi::Env env, Napi::Object exports) {
// Export a chosen C++ function under a given Javascript key
exports.Set(
Napi::String::New(env, "hello"), // Name of function on Javascript side...
Napi::Function::New(env, Hello) // Name of function on C++ side...
);
exports.Set(
Napi::String::New(env, "version"),
Napi::Function::New(env, Version)
);
// The above will expose the C++ function 'Hello' as a javascript function
// named 'hello', etc...
return exports;
}
// Register a new addon with the intializer function defined above
NODE_API_MODULE(addon, Init) // (name to use, initializer to use)
#else // !__has_include(<napi.h>) || !BUILDING_NODE_EXTENSION
#warning "Warning: Cannot find '<napi.h>' - try running 'npm -g install cmake-js'..."
#endif