Commit
- Loading branch information
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
cmake_minimum_required(VERSION 3.15) | ||
|
||
project(hello_c) | ||
|
||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/node_modules/cmake-js/share/cmake") | ||
include(CMakeJS) | ||
|
||
# cmakejs_setup_node_api_c_library() # This can be used, but is called by cmakejs_create_node_api_addon | ||
|
||
cmakejs_create_node_api_addon( | ||
addon | ||
"src/hello/addon.c" | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// This small codeblock in your root-level index.js allows others to consume | ||
// your addon as any other NodeJS module | ||
|
||
const addon = require(`./build/lib/addon.node`); | ||
module.exports = addon; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @file addon.cpp | ||
* @brief A quick 'hello world' Napi Addon in C++ | ||
*/ | ||
|
||
// Required header and flag | ||
#if __has_include(<node_api.h>) && BUILDING_NODE_EXTENSION | ||
|
||
#include <node_api.h> | ||
|
||
napi_value vendor_addon_hello(napi_env env, napi_callback_info args) | ||
{ | ||
napi_value greeting; | ||
napi_status status; | ||
|
||
status = napi_create_string_utf8(env, "addon.node is online!", NAPI_AUTO_LENGTH, &greeting); | ||
if (status != napi_ok) return NULL; | ||
return greeting; | ||
} | ||
|
||
napi_value vendor_addon_init(napi_env env, napi_value exports) | ||
{ | ||
napi_status status; | ||
napi_value fn; | ||
|
||
// Export a chosen C function under a given Javascript key | ||
|
||
status = napi_create_function(env, NULL, 0, vendor_addon_hello, NULL, &fn); // Name of function on Javascript side... | ||
if (status != napi_ok) return NULL; | ||
|
||
status = napi_set_named_property(env, exports, "hello", fn); // Name of function on C side... | ||
if (status != napi_ok) return NULL; | ||
|
||
// The above expose the C function 'addon_hello' as a javascript function named '<name>.hello', etc... | ||
return exports; | ||
} | ||
|
||
// Register a new addon with the intializer function defined above | ||
NAPI_MODULE(addon, vendor_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 |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.