-
Notifications
You must be signed in to change notification settings - Fork 15k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
32 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters