Skip to content

Commit

Permalink
chore: add test using node_api in c
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Jan 18, 2024
1 parent 66fc7b5 commit 6cb6c36
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 3 deletions.
4 changes: 4 additions & 0 deletions tests/es6/buildSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ describe('BuildSystem', function () {
await testCases.buildPrototypeNapi()
})

it('should build prototpye with nodeapi in c', async function () {
await testCases.buildPrototypeNapiC()
})

it('should run with old GNU compilers', async function () {
await testCases.shouldConfigurePreC11Properly()
})
Expand Down
22 changes: 22 additions & 0 deletions tests/es6/prototype-napi-c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.15)
cmake_policy(SET CMP0091 NEW)
cmake_policy(SET CMP0042 NEW)

project (addon_napi_c LANGUAGES C)

if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU"))
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
endif()

include_directories(${CMAKE_JS_INC})

add_library(${PROJECT_NAME} SHARED src/addon.c ${CMAKE_JS_SRC})

set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")

target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB})

if(MSVC AND CMAKE_JS_NODELIB_DEF AND CMAKE_JS_NODELIB_TARGET)
# Generate node.lib
execute_process(COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS})
endif()
26 changes: 26 additions & 0 deletions tests/es6/prototype-napi-c/src/addon.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <node_api.h>

static napi_value Method(napi_env env, napi_callback_info info)
{
napi_status status;
napi_value world;
status = napi_create_string_utf8(env, "world", 5, &world);
assert(status == napi_ok);
return world;
}

#define DECLARE_NAPI_METHOD(name, func) \
{ \
name, 0, func, 0, 0, 0, napi_default, 0 \
}

static napi_value Init(napi_env env, napi_value exports)
{
napi_status status;
napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Method);
status = napi_define_properties(env, exports, 1, &desc);
assert(status == napi_ok);
return exports;
}

NODE_API_MODULE(addon_napi, Init);
2 changes: 1 addition & 1 deletion tests/es6/prototype-napi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ endif()

include_directories(${CMAKE_JS_INC})

add_library(${PROJECT_NAME} SHARED src/addon.cpp)
add_library(${PROJECT_NAME} SHARED src/addon.cpp ${CMAKE_JS_SRC})

set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")

Expand Down
2 changes: 1 addition & 1 deletion tests/es6/prototype/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ endif()

include_directories(${CMAKE_JS_INC})

add_library(addon SHARED src/addon.cpp)
add_library(addon SHARED src/addon.cpp ${CMAKE_JS_SRC})

set_target_properties(addon PROPERTIES PREFIX "" SUFFIX ".node")

Expand Down
2 changes: 1 addition & 1 deletion tests/es6/prototype2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ endif()

include_directories(${CMAKE_JS_INC})

add_library(${PROJECT_NAME} SHARED src/addon.cpp)
add_library(${PROJECT_NAME} SHARED src/addon.cpp ${CMAKE_JS_SRC})

set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")

Expand Down
11 changes: 11 additions & 0 deletions tests/es6/testCases.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ const testCases = {
process.chdir(cwd)
}
},
buildPrototypeNapiC: async function (options) {
const cwd = process.cwd()
process.chdir(path.resolve(path.join(__dirname, './prototype-napi-c')))
const buildSystem = new BuildSystem(options)
try {
await buildSystem.rebuild()
assert.ok((await fs.stat(path.join(__dirname, 'prototype-napi-c/build/Release/addon_napi_c.node'))).isFile())
} finally {
process.chdir(cwd)
}
},
shouldConfigurePreC11Properly: async function (options) {
options = {
directory: path.resolve(path.join(__dirname, './prototype')),
Expand Down

0 comments on commit 6cb6c36

Please sign in to comment.