@auto_js/napi 0.0.1 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CMakeLists.txt +10 -4
- package/README.md +30 -3
- package/_module.cc +12 -10
- package/api/api.cc +8 -7
- package/api/callback_info.cc +1 -7
- package/api/finalizer.cc +1 -4
- package/api/invoke.cc +4 -9
- package/api/uv_dlib.cc +27 -0
- package/api/uv_dlib.h.cc +21 -0
- package/api/uv_handle.cc +9 -9
- package/api/uv_scheduler.cc +11 -17
- package/api/uv_scheduler.h.cc +3 -6
- package/handle/bound_value.cc +4 -7
- package/handle/reference.cc +9 -5
- package/handle/remote.cc +11 -17
- package/handle/types.cc +61 -46
- package/handle/value.cc +12 -14
- package/package.json +7 -6
- package/support/callback.cc +7 -19
- package/support/callback_storage.cc +4 -9
- package/support/class_template_storage.cc +4 -6
- package/support/container.cc +9 -10
- package/support/environment.cc +32 -20
- package/support/environment.h.cc +8 -18
- package/support/environment_fwd.cc +1 -2
- package/support/error_scope.cc +0 -2
- package/support/initialize.h.cc +2 -5
- package/support/promise.cc +3 -10
- package/support/string_table.cc +3 -5
- package/support/utility.cc +54 -28
- package/transfer/accept.cc +131 -327
- package/transfer/accept.h.cc +390 -0
- package/transfer/visit.cc +201 -132
- package/value/array.cc +1 -3
- package/value/array.h.cc +5 -11
- package/value/array_buffer.cc +109 -0
- package/value/array_buffer.h.cc +150 -0
- package/value/class.cc +12 -21
- package/value/class.h.cc +5 -10
- package/value/external.cc +6 -10
- package/value/function.cc +5 -12
- package/value/function.h.cc +2 -9
- package/value/object.cc +2 -6
- package/value/object.h.cc +7 -15
- package/value/primitive.cc +12 -81
- package/value/primitive.h.cc +6 -64
- package/value/record.cc +38 -0
- package/value/record.h.cc +39 -0
- package/value/value.cc +8 -8
- package/value/dictionary.cc +0 -33
- package/value/dictionary.h.cc +0 -56
package/CMakeLists.txt
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
find_package_from_npm(@auto_js/js)
|
|
2
2
|
find_package_from_npm(@auto_js/napi_exports)
|
|
3
|
+
find_package_from_npm(@auto_js/v8_exports)
|
|
3
4
|
|
|
4
5
|
add_library(napi_js OBJECT)
|
|
5
6
|
target_compile_features(napi_js PUBLIC cxx_std_23)
|
|
6
7
|
target_include_directories(napi_js INTERFACE include)
|
|
7
|
-
target_link_libraries(napi_js PRIVATE auto_js nodejs utility_js)
|
|
8
|
+
target_link_libraries(napi_js PRIVATE auto_js nodejs nodejs_v8 utility_js)
|
|
8
9
|
|
|
9
10
|
target_sources(napi_js
|
|
10
11
|
PRIVATE
|
|
12
|
+
api/uv_dlib.cc
|
|
11
13
|
api/uv_scheduler.cc
|
|
12
14
|
support/environment.cc
|
|
13
15
|
support/initialize.cc
|
|
16
|
+
transfer/accept.cc
|
|
17
|
+
value/array_buffer.cc
|
|
14
18
|
value/array.cc
|
|
15
|
-
value/dictionary.cc
|
|
16
19
|
value/object.cc
|
|
17
20
|
value/primitive.cc
|
|
21
|
+
value/record.cc
|
|
18
22
|
PUBLIC FILE_SET CXX_MODULES FILES
|
|
19
23
|
_module.cc
|
|
20
24
|
api/api.cc
|
|
@@ -22,6 +26,7 @@ target_sources(napi_js
|
|
|
22
26
|
api/finalizer.cc
|
|
23
27
|
api/handle_scope.cc
|
|
24
28
|
api/invoke.cc
|
|
29
|
+
api/uv_dlib.h.cc
|
|
25
30
|
api/uv_handle.cc
|
|
26
31
|
api/uv_scheduler.h.cc
|
|
27
32
|
handle/bound_value.cc
|
|
@@ -40,16 +45,17 @@ target_sources(napi_js
|
|
|
40
45
|
support/promise.cc
|
|
41
46
|
support/string_table.cc
|
|
42
47
|
support/utility.cc
|
|
43
|
-
transfer/accept.cc
|
|
48
|
+
transfer/accept.h.cc
|
|
44
49
|
transfer/visit.cc
|
|
50
|
+
value/array_buffer.h.cc
|
|
45
51
|
value/array.h.cc
|
|
46
52
|
value/class.cc
|
|
47
53
|
value/class.h.cc
|
|
48
|
-
value/dictionary.h.cc
|
|
49
54
|
value/external.cc
|
|
50
55
|
value/function.cc
|
|
51
56
|
value/function.h.cc
|
|
52
57
|
value/object.h.cc
|
|
53
58
|
value/primitive.h.cc
|
|
59
|
+
value/record.h.cc
|
|
54
60
|
value/value.cc
|
|
55
61
|
)
|
package/README.md
CHANGED
|
@@ -5,10 +5,14 @@
|
|
|
5
5
|
`@auto_js` -- Automatic C++ bindings for JavaScript
|
|
6
6
|
===================================================
|
|
7
7
|
|
|
8
|
-
<img align="right" width="33%" height="33%" src="upload/macro.jpg">
|
|
8
|
+
<img align="right" width="33%" height="33%" src="https://github.com/laverdet/isolated-vm/raw/experimental/packages/auto_js/napi/upload/macro.jpg">
|
|
9
9
|
|
|
10
10
|
I made this C++ template thing that converts JavaScript functions and values automatically.
|
|
11
11
|
|
|
12
|
+
It requires at least clang 21 to build. I think gcc 16, when it is released, would also be able to
|
|
13
|
+
compile it. C++23 modules are used which requires a modern toolchain. It doesn't use reflection but
|
|
14
|
+
it does use C++26 variadic structured bindings which is only recently getting compiler support.
|
|
15
|
+
|
|
12
16
|
```c++
|
|
13
17
|
auto log(std::string message) -> void {
|
|
14
18
|
std::print("{}", message);
|
|
@@ -25,6 +29,29 @@ js::napi::napi_js_module module_namespace{
|
|
|
25
29
|
};
|
|
26
30
|
```
|
|
27
31
|
|
|
32
|
+
## Example module
|
|
33
|
+
|
|
34
|
+
An example module is located at: [packages/auto_js/example](https://github.com/laverdet/isolated-vm/tree/experimental/packages/auto_js/example).
|
|
35
|
+
|
|
36
|
+
Instructions for building the demo on macOS:
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
# all this nonsense is just checking out the example subdirectory
|
|
40
|
+
git clone --branch=experimental --no-checkout --depth=1 https://github.com/laverdet/isolated-vm.git
|
|
41
|
+
cd isolated-vm
|
|
42
|
+
git sparse-checkout set packages/auto_js/example
|
|
43
|
+
git checkout
|
|
44
|
+
cd packages/auto_js/example
|
|
45
|
+
|
|
46
|
+
# install llvm, npm, configure, then build
|
|
47
|
+
brew install llvm
|
|
48
|
+
npm install
|
|
49
|
+
CXX=$(brew --prefix llvm)/bin/clang npm run configure
|
|
50
|
+
npm run build
|
|
51
|
+
node main.mjs
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
|
|
28
55
|
## It's better than what you're writing by hand
|
|
29
56
|
|
|
30
57
|
```c++
|
|
@@ -247,8 +274,8 @@ export class database {
|
|
|
247
274
|
using transfer_type = js::tagged_external<database>;
|
|
248
275
|
|
|
249
276
|
auto query(environment& env, std::string query) -> std::string;
|
|
250
|
-
static auto connect(environment& env, std::string uri) -> js::forward<js::napi::
|
|
251
|
-
static auto class_template(environment& env) -> js::napi::
|
|
277
|
+
static auto connect(environment& env, std::string uri) -> js::forward<js::napi::value_of<>>;
|
|
278
|
+
static auto class_template(environment& env) -> js::napi::value_of<class_tag_of<database>> {
|
|
252
279
|
// incomplete
|
|
253
280
|
}
|
|
254
281
|
};
|
package/_module.cc
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
export module napi_js;
|
|
2
|
-
|
|
3
|
-
import :
|
|
4
|
-
import :
|
|
5
|
-
|
|
6
|
-
export import :
|
|
2
|
+
export import :accept;
|
|
3
|
+
export import :api;
|
|
4
|
+
export import :bound_value;
|
|
5
|
+
export import :callback;
|
|
6
|
+
export import :class_definitions;
|
|
7
7
|
export import :class_template_storage;
|
|
8
|
+
export import :container;
|
|
8
9
|
export import :environment;
|
|
9
|
-
export import :
|
|
10
|
-
export import :
|
|
10
|
+
export import :error_scope;
|
|
11
|
+
export import :function_definitions;
|
|
12
|
+
export import :handle.types;
|
|
11
13
|
export import :initialize;
|
|
12
|
-
export import :object;
|
|
13
14
|
export import :promise;
|
|
14
15
|
export import :reference;
|
|
15
16
|
export import :remote;
|
|
16
17
|
export import :string_table;
|
|
18
|
+
export import :utility;
|
|
19
|
+
export import :value_handle;
|
|
17
20
|
export import :value;
|
|
18
|
-
import :
|
|
19
|
-
import :function_definitions;
|
|
21
|
+
export import :visit;
|
package/api/api.cc
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
export module napi_js:api;
|
|
2
2
|
// Internal nodejs API utilities which have no dependencies on anything else in `napi_js`
|
|
3
|
-
export import :api.callback_info;
|
|
4
|
-
export import :api.finalizer;
|
|
5
|
-
export import :api.handle_scope;
|
|
6
|
-
export import :api.invoke;
|
|
7
|
-
export import :api.uv_handle;
|
|
8
|
-
export import :api.uv_scheduler;
|
|
9
|
-
export import auto_js;
|
|
10
3
|
export import nodejs;
|
|
4
|
+
export import auto_js;
|
|
5
|
+
export import :api.uv_scheduler;
|
|
6
|
+
export import :api.uv_handle;
|
|
7
|
+
export import :api.uv_dlib;
|
|
8
|
+
export import :api.invoke;
|
|
9
|
+
export import :api.handle_scope;
|
|
10
|
+
export import :api.finalizer;
|
|
11
|
+
export import :api.callback_info;
|
package/api/callback_info.cc
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
module;
|
|
2
|
-
#include <array>
|
|
3
|
-
#include <span>
|
|
4
|
-
#if _LIBCPP_VERSION
|
|
5
|
-
// clang 22.1.0: Without this I get a big horrible ICE in this file
|
|
6
|
-
#include <new>
|
|
7
|
-
#endif
|
|
8
1
|
export module napi_js:api.callback_info;
|
|
9
2
|
import :api.invoke;
|
|
10
3
|
import auto_js;
|
|
4
|
+
import std;
|
|
11
5
|
import util;
|
|
12
6
|
|
|
13
7
|
namespace js::napi {
|
package/api/finalizer.cc
CHANGED
package/api/invoke.cc
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
module;
|
|
2
|
-
#include <concepts>
|
|
3
|
-
#include <exception>
|
|
4
|
-
#include <optional>
|
|
5
|
-
#include <stdexcept>
|
|
6
|
-
#include <type_traits>
|
|
7
1
|
export module napi_js:api.invoke;
|
|
8
2
|
export import nodejs;
|
|
3
|
+
import std;
|
|
9
4
|
import util;
|
|
10
5
|
|
|
11
6
|
namespace js::napi {
|
|
@@ -69,7 +64,7 @@ using napi_invoke_out_t = type_t<napi_invoke_out(type<Params>...)>;
|
|
|
69
64
|
|
|
70
65
|
// Invoke the given napi function and throw if it fails.
|
|
71
66
|
export template <class... Params>
|
|
72
|
-
auto invoke(auto(function)(Params...)->napi_status, auto&&... args)
|
|
67
|
+
auto invoke(auto(function)(Params...)->napi_status, auto&&... args) -> napi_invoke_out_t<Params...>
|
|
73
68
|
requires std::invocable<decltype(function), decltype(args)..., napi_invoke_out_t<Params...>*> {
|
|
74
69
|
napi_invoke_out_t<Params...> out_arg;
|
|
75
70
|
napi::invoke0(function, std::forward<decltype(args)>(args)..., &out_arg);
|
|
@@ -78,7 +73,7 @@ auto invoke(auto(function)(Params...)->napi_status, auto&&... args)
|
|
|
78
73
|
|
|
79
74
|
// Invoke the given napi function and return `optional<T>` indicating failure.
|
|
80
75
|
export template <class... Params>
|
|
81
|
-
auto invoke_maybe(auto(function)(Params...)->napi_status, auto&&... args)
|
|
76
|
+
auto invoke_maybe(auto(function)(Params...)->napi_status, auto&&... args) -> std::optional<napi_invoke_out_t<Params...>>
|
|
82
77
|
requires std::invocable<decltype(function), decltype(args)..., napi_invoke_out_t<Params...>*> {
|
|
83
78
|
using out_type = napi_invoke_out_t<Params...>;
|
|
84
79
|
out_type out_arg;
|
|
@@ -89,7 +84,7 @@ auto invoke_maybe(auto(function)(Params...)->napi_status, auto&&... args)
|
|
|
89
84
|
|
|
90
85
|
// Invoke the given napi function and terminate if it fails.
|
|
91
86
|
export template <class... Params>
|
|
92
|
-
auto invoke_noexcept(auto(function)(Params...)->napi_status, auto&&... args) noexcept
|
|
87
|
+
auto invoke_noexcept(auto(function)(Params...)->napi_status, auto&&... args) noexcept -> napi_invoke_out_t<Params...>
|
|
93
88
|
requires std::invocable<decltype(function), decltype(args)..., napi_invoke_out_t<Params...>*> {
|
|
94
89
|
napi_invoke_out_t<Params...> out_arg;
|
|
95
90
|
napi::invoke0_noexcept(function, std::forward<decltype(args)>(args)..., &out_arg);
|
package/api/uv_dlib.cc
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module napi_js;
|
|
2
|
+
import :api.uv_dlib;
|
|
3
|
+
import util;
|
|
4
|
+
|
|
5
|
+
namespace js::napi {
|
|
6
|
+
|
|
7
|
+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
|
|
8
|
+
uv_dlib::uv_dlib(const std::string& filename) {
|
|
9
|
+
if (uv_dlopen(filename.c_str(), &lib_) != 0) {
|
|
10
|
+
throw js::runtime_error{util::transcode_string<char16_t>(std::string_view{uv_dlerror(&lib_)})};
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
uv_dlib::uv_dlib(uv_dlib&& right) noexcept : lib_{std::exchange(right.lib_, {})} {}
|
|
15
|
+
|
|
16
|
+
auto uv_dlib::operator=(uv_dlib&& right) noexcept -> uv_dlib& {
|
|
17
|
+
lib_ = std::exchange(right.lib_, {});
|
|
18
|
+
return *this;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
uv_dlib::~uv_dlib() {
|
|
22
|
+
if (lib_.handle != nullptr) {
|
|
23
|
+
uv_dlclose(&lib_);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
} // namespace js::napi
|
package/api/uv_dlib.h.cc
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export module napi_js:api.uv_dlib;
|
|
2
|
+
import nodejs;
|
|
3
|
+
import std;
|
|
4
|
+
|
|
5
|
+
namespace js::napi {
|
|
6
|
+
|
|
7
|
+
export class uv_dlib {
|
|
8
|
+
public:
|
|
9
|
+
// Constructor must take `std::string` because it needs to be null-terminated.
|
|
10
|
+
explicit uv_dlib(const std::string& filename);
|
|
11
|
+
uv_dlib(const uv_dlib&) = delete;
|
|
12
|
+
uv_dlib(uv_dlib&&) noexcept;
|
|
13
|
+
auto operator=(const uv_dlib&) -> uv_dlib& = delete;
|
|
14
|
+
auto operator=(uv_dlib&&) noexcept -> uv_dlib&;
|
|
15
|
+
~uv_dlib();
|
|
16
|
+
|
|
17
|
+
private:
|
|
18
|
+
uv_lib_t lib_;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
} // namespace js::napi
|
package/api/uv_handle.cc
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
module;
|
|
2
|
-
#include <memory>
|
|
3
|
-
#include <stdexcept>
|
|
4
|
-
#include <type_traits>
|
|
5
|
-
#include <utility>
|
|
6
1
|
export module napi_js:api.uv_handle;
|
|
7
2
|
import nodejs;
|
|
3
|
+
import std;
|
|
8
4
|
import util;
|
|
9
5
|
|
|
10
6
|
namespace js::napi {
|
|
@@ -35,7 +31,7 @@ class uv_typed_handle {
|
|
|
35
31
|
|
|
36
32
|
// `uv_handle_t` subtype wrapper with uv-owned shared memory. An open handle must be closed before
|
|
37
33
|
// the handle is destroyed or it is UB.
|
|
38
|
-
|
|
34
|
+
template <class Handle, class Type>
|
|
39
35
|
class uv_handle_of : public util::pointer_facade {
|
|
40
36
|
private:
|
|
41
37
|
struct private_constructor {
|
|
@@ -59,7 +55,7 @@ class uv_handle_of : public util::pointer_facade {
|
|
|
59
55
|
auto operator=(const uv_handle_of&) -> uv_handle_of& = delete;
|
|
60
56
|
|
|
61
57
|
auto operator*() -> auto& { return value_; }
|
|
62
|
-
auto close() -> void;
|
|
58
|
+
auto close(util::function_ref<auto()->void> close_hook) -> void;
|
|
63
59
|
auto handle() -> auto& { return handle_; }
|
|
64
60
|
auto open(const auto& init, uv_loop_t* loop, auto&&... args) -> void;
|
|
65
61
|
|
|
@@ -68,6 +64,7 @@ class uv_handle_of : public util::pointer_facade {
|
|
|
68
64
|
|
|
69
65
|
private:
|
|
70
66
|
uv_typed_handle<Handle, shared_or_weak_ptr> handle_;
|
|
67
|
+
util::function_ref<auto()->void> close_hook_{[] -> void {}};
|
|
71
68
|
Type value_;
|
|
72
69
|
};
|
|
73
70
|
|
|
@@ -79,9 +76,12 @@ uv_handle_of<Handle, Type>::~uv_handle_of() {
|
|
|
79
76
|
}
|
|
80
77
|
|
|
81
78
|
template <class Handle, class Type>
|
|
82
|
-
auto uv_handle_of<Handle, Type>::close() -> void {
|
|
79
|
+
auto uv_handle_of<Handle, Type>::close(util::function_ref<auto()->void> close_hook) -> void {
|
|
80
|
+
close_hook_ = close_hook;
|
|
83
81
|
uv_close(handle(), [](uv_handle_t* handle) -> void {
|
|
84
|
-
|
|
82
|
+
auto self = std::unique_ptr<shared_ptr_type>(static_cast<shared_ptr_type*>(std::exchange(handle->data, nullptr)));
|
|
83
|
+
auto& handle_of = **self;
|
|
84
|
+
handle_of.close_hook_();
|
|
85
85
|
});
|
|
86
86
|
}
|
|
87
87
|
|
package/api/uv_scheduler.cc
CHANGED
|
@@ -1,26 +1,20 @@
|
|
|
1
1
|
module;
|
|
2
2
|
#include <cassert>
|
|
3
|
-
#include <functional>
|
|
4
|
-
#include <thread>
|
|
5
|
-
#include <utility>
|
|
6
3
|
module napi_js;
|
|
4
|
+
import std;
|
|
7
5
|
|
|
8
6
|
namespace js::napi {
|
|
9
7
|
|
|
10
|
-
auto uv_scheduler::close() -> void {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
std::
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
auto tasks = std::exchange(lock->tasks, {});
|
|
21
|
-
async_->close();
|
|
22
|
-
return tasks;
|
|
23
|
-
});
|
|
8
|
+
auto uv_scheduler::close(util::function_ref<auto()->void> close_hook) -> void {
|
|
9
|
+
auto& async = *async_;
|
|
10
|
+
assert(async->thread_id == std::this_thread::get_id());
|
|
11
|
+
std::ignore = [ & ] -> auto {
|
|
12
|
+
auto lock = async->shared.write();
|
|
13
|
+
lock->is_open = false;
|
|
14
|
+
auto tasks = std::exchange(lock->tasks, {});
|
|
15
|
+
async.close(close_hook);
|
|
16
|
+
return tasks;
|
|
17
|
+
}();
|
|
24
18
|
}
|
|
25
19
|
|
|
26
20
|
auto uv_scheduler::decrement_ref() -> void {
|
package/api/uv_scheduler.h.cc
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
module;
|
|
2
|
-
#include <memory>
|
|
3
|
-
#include <thread>
|
|
4
|
-
#include <vector>
|
|
5
1
|
export module napi_js:api.uv_scheduler;
|
|
6
2
|
import :api.uv_handle;
|
|
3
|
+
import std;
|
|
7
4
|
import util;
|
|
8
5
|
|
|
9
6
|
#if _LIBCPP_VERSION
|
|
@@ -11,7 +8,7 @@ import util;
|
|
|
11
8
|
// /usr/lib/llvm-22/bin/../include/c++/v1/__new/allocate.h:39:30: error: no matching function for call to 'operator new'
|
|
12
9
|
// 39 | return static_cast<_Tp*>(__builtin_operator_new(__size, static_cast<align_val_t>(__align)));
|
|
13
10
|
export {
|
|
14
|
-
|
|
11
|
+
using ::operator new;
|
|
15
12
|
}
|
|
16
13
|
#endif
|
|
17
14
|
|
|
@@ -23,7 +20,7 @@ export class uv_scheduler {
|
|
|
23
20
|
using task_type = util::maybe_move_only_function<auto()->void>;
|
|
24
21
|
|
|
25
22
|
public:
|
|
26
|
-
auto close() -> void;
|
|
23
|
+
auto close(util::function_ref<auto()->void> close_hook) -> void;
|
|
27
24
|
auto decrement_ref() -> void;
|
|
28
25
|
auto increment_ref() -> void;
|
|
29
26
|
auto open(uv_loop_t* loop) -> void;
|
package/handle/bound_value.cc
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
module;
|
|
2
|
-
#include <concepts>
|
|
3
1
|
export module napi_js:bound_value;
|
|
4
2
|
import :handle.types;
|
|
5
3
|
import nodejs;
|
|
@@ -12,17 +10,16 @@ class bound_value_next : public bound_value<typename Tag::tag_type> {
|
|
|
12
10
|
public:
|
|
13
11
|
using bound_value<typename Tag::tag_type>::bound_value;
|
|
14
12
|
bound_value_next() = default;
|
|
15
|
-
bound_value_next(napi_env env,
|
|
13
|
+
bound_value_next(napi_env env, value_of<Tag> value) :
|
|
16
14
|
bound_value<typename Tag::tag_type>{env, napi_value{value}} {}
|
|
17
15
|
|
|
18
|
-
template <std::constructible_from<Tag> To>
|
|
19
16
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
|
20
|
-
operator
|
|
17
|
+
operator value_of<Tag>() const { return value_of<Tag>::from(napi_value{*this}); }
|
|
21
18
|
};
|
|
22
19
|
|
|
23
20
|
// Member & method implementation for stateful objects. Used internally in visitors. I think it
|
|
24
21
|
// might make sense to have the environment specified by a template parameter. Then you would use
|
|
25
|
-
// `bound_value<T>` or something instead of passing the environment to each `
|
|
22
|
+
// `bound_value<T>` or something instead of passing the environment to each `value_of<T>` method.
|
|
26
23
|
template <class Tag>
|
|
27
24
|
class bound_value : public value_specialization<Tag>::bound_type {
|
|
28
25
|
public:
|
|
@@ -30,7 +27,7 @@ class bound_value : public value_specialization<Tag>::bound_type {
|
|
|
30
27
|
};
|
|
31
28
|
|
|
32
29
|
template <class Tag>
|
|
33
|
-
bound_value(auto,
|
|
30
|
+
bound_value(auto, value_of<Tag>) -> bound_value<Tag>;
|
|
34
31
|
|
|
35
32
|
template <>
|
|
36
33
|
class bound_value<void> : public value_handle {
|
package/handle/reference.cc
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
module;
|
|
2
|
-
#include <stdexcept>
|
|
3
|
-
#include <utility>
|
|
4
1
|
export module napi_js:reference;
|
|
5
2
|
import :api;
|
|
6
3
|
import :value_handle;
|
|
4
|
+
import std;
|
|
7
5
|
import util;
|
|
8
6
|
|
|
9
7
|
namespace js::napi {
|
|
@@ -36,9 +34,15 @@ class reference_handle : util::non_copyable {
|
|
|
36
34
|
}
|
|
37
35
|
|
|
38
36
|
public:
|
|
37
|
+
#if __GNUC__ > 14
|
|
38
|
+
constexpr reference_handle(const reference_handle& /*right*/) {
|
|
39
|
+
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125008
|
|
40
|
+
};
|
|
41
|
+
#else
|
|
39
42
|
consteval reference_handle(const reference_handle& /*right*/) {
|
|
40
43
|
// consteval no-op!
|
|
41
44
|
};
|
|
45
|
+
#endif
|
|
42
46
|
|
|
43
47
|
constexpr reference_handle(reference_handle&& right) noexcept :
|
|
44
48
|
env_{right.env_},
|
|
@@ -68,7 +72,7 @@ class reference_handle : util::non_copyable {
|
|
|
68
72
|
export template <class Tag>
|
|
69
73
|
class reference : public reference<typename Tag::tag_type> {
|
|
70
74
|
public:
|
|
71
|
-
using value_type =
|
|
75
|
+
using value_type = value_of<Tag>;
|
|
72
76
|
using reference<typename Tag::tag_type>::reference;
|
|
73
77
|
|
|
74
78
|
reference() = default;
|
|
@@ -82,7 +86,7 @@ class reference : public reference<typename Tag::tag_type> {
|
|
|
82
86
|
};
|
|
83
87
|
|
|
84
88
|
template <class Tag>
|
|
85
|
-
reference(auto,
|
|
89
|
+
reference(auto, value_of<Tag>) -> reference<Tag>;
|
|
86
90
|
|
|
87
91
|
// nb: Protected inheritance so that `remote<T> is not comparable to `reference<T>`.
|
|
88
92
|
template <>
|
package/handle/remote.cc
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
module;
|
|
2
|
-
#include <cassert>
|
|
3
|
-
#include <memory>
|
|
4
|
-
#include <type_traits>
|
|
5
|
-
#include <utility>
|
|
6
1
|
export module napi_js:remote;
|
|
7
|
-
import :api;
|
|
8
2
|
import :environment;
|
|
9
3
|
import :reference;
|
|
10
|
-
import
|
|
4
|
+
import std;
|
|
11
5
|
import util;
|
|
12
6
|
|
|
13
7
|
namespace js::napi {
|
|
@@ -29,14 +23,14 @@ class remote : protected reference_handle {
|
|
|
29
23
|
public:
|
|
30
24
|
using unique_remote = std::unique_ptr<remote, util::function_constant<expire>>;
|
|
31
25
|
|
|
32
|
-
remote(private_constructor /*private*/, const environment& env,
|
|
26
|
+
remote(private_constructor /*private*/, const environment& env, value_of<Tag> value, uv_scheduler scheduler) :
|
|
33
27
|
reference_handle{napi_env{env}, napi_value{value}},
|
|
34
28
|
scheduler_{std::move(scheduler)} {}
|
|
35
29
|
|
|
36
|
-
auto deref(const environment& env) const ->
|
|
30
|
+
auto deref(const environment& env) const -> value_of<Tag>;
|
|
37
31
|
|
|
38
|
-
static auto make_shared(remote_handle_environment auto& env,
|
|
39
|
-
static auto make_unique(remote_handle_environment auto& env,
|
|
32
|
+
static auto make_shared(remote_handle_environment auto& env, value_of<Tag> value) -> std::shared_ptr<remote>;
|
|
33
|
+
static auto make_unique(remote_handle_environment auto& env, value_of<Tag> value) -> unique_remote;
|
|
40
34
|
|
|
41
35
|
private:
|
|
42
36
|
uv_scheduler scheduler_;
|
|
@@ -50,12 +44,12 @@ export template <class Type>
|
|
|
50
44
|
using unique_remote = remote<Type>::unique_remote;
|
|
51
45
|
|
|
52
46
|
export template <class Tag>
|
|
53
|
-
auto make_shared_remote(remote_handle_environment auto& env,
|
|
47
|
+
auto make_shared_remote(remote_handle_environment auto& env, value_of<Tag> value) -> shared_remote<Tag> {
|
|
54
48
|
return remote<Tag>::make_shared(env, value);
|
|
55
49
|
}
|
|
56
50
|
|
|
57
51
|
export template <class Tag>
|
|
58
|
-
auto make_unique_remote(remote_handle_environment auto& env,
|
|
52
|
+
auto make_unique_remote(remote_handle_environment auto& env, value_of<Tag> value) -> unique_remote<Tag> {
|
|
59
53
|
return remote<Tag>::make_unique(env, value);
|
|
60
54
|
}
|
|
61
55
|
|
|
@@ -75,17 +69,17 @@ auto remote<Tag>::expire(remote* ptr) -> void {
|
|
|
75
69
|
}
|
|
76
70
|
|
|
77
71
|
template <class Tag>
|
|
78
|
-
auto remote<Tag>::deref(const environment& env) const ->
|
|
79
|
-
return
|
|
72
|
+
auto remote<Tag>::deref(const environment& env) const -> value_of<Tag> {
|
|
73
|
+
return value_of<Tag>::from(get_value(napi_env{env}));
|
|
80
74
|
}
|
|
81
75
|
|
|
82
76
|
template <class Tag>
|
|
83
|
-
auto remote<Tag>::make_shared(remote_handle_environment auto& env,
|
|
77
|
+
auto remote<Tag>::make_shared(remote_handle_environment auto& env, value_of<Tag> value) -> std::shared_ptr<remote> {
|
|
84
78
|
return std::shared_ptr<remote>{new remote{private_constructor{}, env, value, env.scheduler()}, expire};
|
|
85
79
|
}
|
|
86
80
|
|
|
87
81
|
template <class Tag>
|
|
88
|
-
auto remote<Tag>::make_unique(remote_handle_environment auto& env,
|
|
82
|
+
auto remote<Tag>::make_unique(remote_handle_environment auto& env, value_of<Tag> value) -> unique_remote {
|
|
89
83
|
return unique_remote{new remote{private_constructor{}, env, value, env.scheduler()}};
|
|
90
84
|
}
|
|
91
85
|
|