Skip to content

Commit

Permalink
refactor: use napi c api directly
Browse files Browse the repository at this point in the history
  • Loading branch information
koonpeng authored and zcbenz committed Aug 10, 2020
1 parent 2c6bfbe commit c03eacb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
2 changes: 0 additions & 2 deletions spec-main/fixtures/native-addon/uv-dlopen/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
{
"target_name": "test_module",
"sources": [ "main.cpp" ],
"include_dirs": ["<!@(node -p \"require('node-addon-api').include\")"],
"defines": [ "NAPI_DISABLE_CPP_EXCEPTIONS" ]
},
{
"target_name": "libfoo",
Expand Down
40 changes: 29 additions & 11 deletions spec-main/fixtures/native-addon/uv-dlopen/main.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
#include <napi.h>
#include <node_api.h>
#include <uv.h>

namespace test_module {

Napi::Value TestLoadLibrary(const Napi::CallbackInfo& info) {
auto lib_path = info[0].ToString().Utf8Value();
napi_value TestLoadLibrary(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value argv;
napi_status status;
status = napi_get_cb_info(env, info, &argc, &argv, NULL, NULL);
if (status != napi_ok) napi_fatal_error(NULL, 0, NULL, 0);

char lib_path[256];
status = napi_get_value_string_utf8(env, argv, lib_path, 256, NULL);
if (status != napi_ok) napi_fatal_error(NULL, 0, NULL, 0);

uv_lib_t lib;
auto result = uv_dlopen(lib_path.c_str(), &lib);
if (result == 0) {
return Napi::Value::From(info.Env(), true);
auto uv_status = uv_dlopen(lib_path, &lib);
if (uv_status == 0) {
napi_value result;
status = napi_get_boolean(env, true, &result);
if (status != napi_ok) napi_fatal_error(NULL, 0, NULL, 0);
return result;
} else {
Napi::Error::New(info.Env(), uv_dlerror(&lib)).ThrowAsJavaScriptException();
return Napi::Value();
status = napi_throw_error(env, NULL, uv_dlerror(&lib));
if (status != napi_ok) napi_fatal_error(NULL, 0, NULL, 0);
}
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
return Napi::Function::New(env, TestLoadLibrary);
napi_value Init(napi_env env, napi_value exports) {
napi_value method;
napi_status status;
status = napi_create_function(env, "testLoadLibrary", NAPI_AUTO_LENGTH,
TestLoadLibrary, NULL, &method);
if (status != napi_ok)
return NULL;
return method;
}

NODE_API_MODULE(TestLoadLibrary, Init);
NAPI_MODULE(TestLoadLibrary, Init);

} // namespace test_module
6 changes: 1 addition & 5 deletions spec-main/fixtures/native-addon/uv-dlopen/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
{
"name": "uv-dlopen",
"version": "0.0.1",
"description": "",
"main": "index.js",
"dependencies": {
"node-addon-api": "^3.0.0"
}
"main": "index.js"
}
2 changes: 1 addition & 1 deletion spec-main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"echo": "file:fixtures/native-addon/echo",
"q": "^1.5.1",
"sinon": "^9.0.1",
"uv-dlopen": "file:fixtures/native-addon/uv-dlopen",
"uv-dlopen": "./fixtures/native-addon/uv-dlopen/",
"ws": "^7.2.1"
},
"dependencies": {
Expand Down
9 changes: 1 addition & 8 deletions spec-main/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,6 @@ nise@^4.0.1:
just-extend "^4.0.2"
path-to-regexp "^1.7.0"

node-addon-api@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-3.0.0.tgz#812446a1001a54f71663bed188314bba07e09247"
integrity sha512-sSHCgWfJ+Lui/u+0msF3oyCgvdkhxDbkCS6Q8uiJquzOimkJBvX6hl5aSSA7DR1XbMpdM8r7phjcF63sF4rkKg==

node-ensure@^0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/node-ensure/-/node-ensure-0.0.0.tgz#ecae764150de99861ec5c810fd5d096b183932a7"
Expand Down Expand Up @@ -273,10 +268,8 @@ uri-js@^4.2.2:
dependencies:
punycode "^2.1.0"

"uv-dlopen@file:fixtures/native-addon/uv-dlopen":
uv-dlopen@./fixtures/native-addon/uv-dlopen/:
version "0.0.1"
dependencies:
node-addon-api "^3.0.0"

worker-loader@^2.0.0:
version "2.0.0"
Expand Down

0 comments on commit c03eacb

Please sign in to comment.