@auto_js/napi 0.0.9 → 0.0.10

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/_module.cc CHANGED
@@ -7,14 +7,15 @@ export import :class_template_storage;
7
7
  export import :environment;
8
8
  export import :error_scope;
9
9
  export import :function_definitions;
10
+ export import :handle.local_of;
11
+ export import :handle.types;
12
+ export import :handle.value_of;
10
13
  export import :initialize;
11
14
  export import :promise;
12
15
  export import :reference;
13
16
  export import :remote;
14
17
  export import :string_table;
18
+ export import :support.host;
15
19
  export import :utility;
16
- export import :handle.local_of;
17
- export import :handle.types;
18
- export import :handle.value_of;
19
20
  export import :value;
20
21
  export import :visit;
package/api/finalizer.cc CHANGED
@@ -43,7 +43,7 @@ auto make_finalizer(std::type_identity<Env> /*type*/, std::shared_ptr<Type> shar
43
43
  template <class Pointer, class Apply>
44
44
  auto apply_finalizer(Pointer smart_ptr, Apply apply) -> decltype(auto) {
45
45
  using element_type = Pointer::element_type;
46
- constexpr auto napi_env_type = []() consteval -> auto {
46
+ constexpr auto napi_env_type = [] consteval -> auto {
47
47
  if constexpr (std::invocable<Apply, element_type*, node_api_basic_finalize, void*>) {
48
48
  return type<node_api_basic_env>;
49
49
  } else {
@@ -48,7 +48,7 @@ class threadsafe_function_of : util::non_copyable {
48
48
 
49
49
  template <class Context, class Message>
50
50
  threadsafe_function_of<Context, Message>::threadsafe_function_of(auto& env, napi_value value, auto&&... args) :
51
- tsfn_{[ & ]() -> auto {
51
+ tsfn_{[ & ] -> auto {
52
52
  constexpr auto name = util::make_consteval_string_view(util::cw<"threadsafe_function_of">);
53
53
  auto resource_name = napi::invoke(napi_create_string_latin1, env, name.data(), name.size());
54
54
  auto context = std::make_unique<storage>(std::forward<decltype(args)>(args)...);
@@ -50,7 +50,7 @@ auto uv_scheduler::operator()(auto task, auto&&... args) const -> void {
50
50
  if (shared->is_open) {
51
51
  shared->tasks.emplace_back(
52
52
  [ task = std::move(task),
53
- ... args = std::forward<decltype(args)>(args) ]() mutable -> void {
53
+ ... args = std::forward<decltype(args)>(args) ] mutable -> void {
54
54
  task(std::move(args)...);
55
55
  }
56
56
  );
package/handle/local.cc CHANGED
@@ -13,7 +13,7 @@ struct local_specialization;
13
13
 
14
14
  // Tagged napi_value. Each `local_of<T>` inherits from the tag before it which makes overloading
15
15
  // acceptor functions naturally hierarchical.
16
- export template <class Tag = value_tag>
16
+ export template <class Tag>
17
17
  class local_of : public local_of<typename Tag::tag_type> {
18
18
  protected:
19
19
  using local_type = local_specialization<Tag>::type;
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@auto_js/napi",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
4
  "author": "https://github.com/laverdet/",
5
5
  "description": "Automatic JavaScript to N-API bindings",
6
6
  "homepage": "https://github.com/laverdet/isolated-vm/tree/experimental/packages/auto_js/napi",
7
7
  "license": "ISC",
8
8
  "dependencies": {
9
- "@auto_js/v8_exports": "^0.0.7",
10
- "@auto_js/js": "^0.0.9",
11
- "@auto_js/napi_exports": "^0.0.9"
9
+ "@auto_js/js": "^0.0.10",
10
+ "@auto_js/v8_exports": "^0.0.8",
11
+ "@auto_js/napi_exports": "^0.0.10"
12
12
  },
13
13
  "repository": {
14
14
  "type": "git",
@@ -40,8 +40,8 @@ constexpr auto make_free_function(auto function) {
40
40
  using callback_type = decltype(callback);
41
41
  return util::bind{
42
42
  [](callback_type& callback, Environment& env, const callback_info& info) noexcept(Nx) -> napi_value {
43
- return invoke_internal_error_scope(env, [ & ]() -> napi_value {
44
- auto run = util::regular_return{[ & ]() -> decltype(auto) {
43
+ return invoke_internal_error_scope(env, [ & ] -> napi_value {
44
+ auto run = util::regular_return{[ & ] -> decltype(auto) {
45
45
  return std::apply(
46
46
  callback,
47
47
  std::tuple_cat(
@@ -65,7 +65,7 @@ constexpr auto make_free_function(auto function) {
65
65
  using callback_type = decltype(callback);
66
66
  return util::bind{
67
67
  [](callback_type& callback, Environment& env, const callback_info& /*info*/) noexcept -> napi_value {
68
- auto run = util::regular_return{[ & ]() -> decltype(auto) {
68
+ auto run = util::regular_return{[ & ] -> decltype(auto) {
69
69
  return callback(env);
70
70
  }};
71
71
  return js::transfer_in_strict<napi_value>(run().value_or(std::monostate{}), env);
@@ -112,7 +112,7 @@ constexpr auto make_constructor_function(auto constructor) {
112
112
  using callback_type = decltype(callback);
113
113
  return util::bind{
114
114
  [](callback_type& callback, wrap_type& wrap, Environment& env, const callback_info& info) noexcept(Nx) -> napi_value {
115
- return invoke_internal_error_scope(env, [ & ]() -> napi_value {
115
+ return invoke_internal_error_scope(env, [ & ] -> napi_value {
116
116
  auto instance = std::apply(
117
117
  callback,
118
118
  std::tuple_cat(
@@ -159,7 +159,7 @@ constexpr auto make_constructor_function(auto constructor) {
159
159
  [](runtime_constructor_type& constructor, Environment& env, const callback_info& info) -> napi_value {
160
160
  auto arguments = info.arguments();
161
161
  if (!arguments.empty()) {
162
- auto maybe_constructor = [ & ]() -> std::optional<internal_constructor*> {
162
+ auto maybe_constructor = [ & ] -> std::optional<internal_constructor*> {
163
163
  try {
164
164
  // Check truthiness since null or undefined causes `napi_coerce_to_object` to throw
165
165
  auto* arg0 = util::at(arguments, 0);
@@ -181,7 +181,7 @@ constexpr auto make_constructor_function(auto constructor) {
181
181
  }
182
182
  if (*maybe_constructor != nullptr) {
183
183
  const auto& constructor = **maybe_constructor;
184
- return invoke_internal_error_scope(env, [ & ]() -> napi_value {
184
+ return invoke_internal_error_scope(env, [ & ] -> napi_value {
185
185
  return constructor(info.this_arg());
186
186
  });
187
187
  }
@@ -208,8 +208,8 @@ constexpr auto make_member_function(Method method) {
208
208
  if (!maybe_that) {
209
209
  return nullptr;
210
210
  }
211
- return invoke_internal_error_scope(env, [ & ]() -> napi_value {
212
- auto run = util::regular_return{[ & ]() -> decltype(auto) {
211
+ return invoke_internal_error_scope(env, [ & ] -> napi_value {
212
+ auto run = util::regular_return{[ & ] -> decltype(auto) {
213
213
  return std::apply(
214
214
  callback,
215
215
  std::tuple_cat(
@@ -237,7 +237,7 @@ constexpr auto make_member_function(Method method) {
237
237
  if (!maybe_that) {
238
238
  return nullptr;
239
239
  }
240
- auto run = util::regular_return{[ & ]() -> decltype(auto) {
240
+ auto run = util::regular_return{[ & ] -> decltype(auto) {
241
241
  return callback(**maybe_that, env);
242
242
  }};
243
243
  return js::transfer_in_strict<napi_value>(run().value_or(std::monostate{}), env);
@@ -10,7 +10,7 @@ namespace js::napi {
10
10
  // Given a pack of strings it returns a static reference to a `util::sealed_map` of napi object
11
11
  // references.
12
12
  template <const auto& Strings>
13
- constexpr auto class_template_references_of = []() -> auto {
13
+ constexpr auto class_template_references_of = [] consteval -> auto {
14
14
  const auto [... strings ] = Strings;
15
15
  return util::sealed_map{std::type_identity<napi::reference<class_tag>>{}, strings...};
16
16
  }();
package/support/host.cc CHANGED
@@ -123,7 +123,7 @@ constexpr auto unknown_maybe_is = [](auto...) -> std::optional<bool> { return st
123
123
 
124
124
  // 'SharedArrayBuffer' functions
125
125
  auto v8_make_shared_array_buffer(js::shared_array_buffer::shared_pointer_type data, std::size_t byte_length) -> local_of<shared_array_buffer_tag> {
126
- auto backing_store = [ & ]() -> auto {
126
+ auto backing_store = [ & ] -> auto {
127
127
  // v8 does not call the deleter `byte_length` is zero. So the heap-allocated shared_ptr trick
128
128
  // does not work in that case.
129
129
  if (byte_length == 0) {
@@ -233,7 +233,7 @@ auto initialize_host_environment(napi_env env) -> void {
233
233
  }
234
234
 
235
235
  // Get `globalThis.process`
236
- auto* process_global = [ & ]() -> napi_value {
236
+ auto* process_global = [ & ] -> napi_value {
237
237
  auto* global = napi::invoke(napi_get_global, env);
238
238
  constexpr auto process_cw = util::make_consteval_string_view(util::cw<"process">);
239
239
  auto* process_str = napi::invoke(node_api_create_property_key_latin1, env, process_cw.data(), process_cw.length());
@@ -246,7 +246,7 @@ auto initialize_host_environment(napi_env env) -> void {
246
246
  }();
247
247
 
248
248
  // Detect bun via `process.isBun`
249
- auto is_bun = process_global == nullptr ? false : [ & ]() -> bool {
249
+ auto is_bun = process_global == nullptr ? false : [ & ] -> bool {
250
250
  constexpr auto is_bun_cw = util::make_consteval_string_view(util::cw<"isBun">);
251
251
  auto* is_bun_str = napi::invoke(node_api_create_property_key_latin1, env, is_bun_cw.data(), is_bun_cw.length());
252
252
  auto* is_bun = napi::invoke(napi_get_property, env, process_global, is_bun_str);
@@ -255,7 +255,7 @@ auto initialize_host_environment(napi_env env) -> void {
255
255
  }();
256
256
 
257
257
  // Detect deno via `process.versions.deno`
258
- auto is_deno = process_global == nullptr ? false : [ & ]() -> bool {
258
+ auto is_deno = process_global == nullptr ? false : [ & ] -> bool {
259
259
  constexpr auto versions_cw = util::make_consteval_string_view(util::cw<"versions">);
260
260
  auto* versions_str = napi::invoke(node_api_create_property_key_latin1, env, versions_cw.data(), versions_cw.length());
261
261
  auto* versions = napi::invoke(napi_get_property, env, process_global, versions_str);
@@ -31,7 +31,7 @@ class napi_js_module final : private detail::initialize_require {
31
31
  private:
32
32
  auto operator()(napi_env env, napi_value exports) -> void override {
33
33
  // construct environment, set napi instance data w/ finalizer
34
- const auto [... indices ] = util::sequence<sizeof...(Args)>;
34
+ constexpr auto [... indices ] = util::sequence<sizeof...(Args)>;
35
35
  auto& client_environment = environment::make_and_set_environment<Environment>(env, std::get<indices>(std::move(args_))...);
36
36
  // assign export descriptor to napi-constructor namespace object
37
37
  auto exports_local = local_of<dictionary_tag>::from(exports);
@@ -10,7 +10,7 @@ namespace js::napi {
10
10
  // Given a pack of strings it returns a static reference to a `util::sealed_map` of napi string
11
11
  // references.
12
12
  template <const auto& Strings>
13
- constexpr auto string_table_of = []() -> auto {
13
+ constexpr auto string_table_of = [] consteval -> auto {
14
14
  const auto [... strings ] = Strings;
15
15
  return util::sealed_map{std::type_identity<napi::reference<string_tag>>{}, strings...};
16
16
  }();
@@ -51,6 +51,12 @@ struct accept_basic_napi_value {
51
51
  return (*this)(number_tag_of<double>{}, visit, double{std::forward<decltype(subject)>(subject)});
52
52
  }
53
53
 
54
+ template <class Type>
55
+ auto operator()(number_tag_of<Type> tag, visit_holder visit, Type subject) const -> local_of<number_tag_of<Type>> {
56
+ auto value = (*this)(tag, visit, std::int32_t{std::forward<decltype(subject)>(subject)});
57
+ return local_of<number_tag_of<Type>>::from(value);
58
+ }
59
+
54
60
  template <class Type>
55
61
  auto operator()(number_tag_of<Type> tag, visit_holder visit, auto&& subject) const -> local_of<number_tag_of<Type>> {
56
62
  return (*this)(tag, visit, Type{std::forward<decltype(subject)>(subject)});
@@ -190,8 +196,8 @@ struct accept_napi_value : accept_basic_napi_value {
190
196
  local_of<vector_tag>::from(napi::invoke(napi_create_array_with_length, napi_env{self}, Size)),
191
197
  std::forward_as_tuple(self, visit, std::forward<decltype(subject)>(subject)),
192
198
  [](local_of<vector_tag> array, auto& self, auto& visit, auto /*&&*/ tuple) -> void {
193
- const auto [... indices ] = util::sequence<Size>;
194
- (..., [ & ]() -> void {
199
+ constexpr auto [... indices ] = util::sequence<Size>;
200
+ (..., [ & ] -> void {
195
201
  // nb: This is forwarded to *each* visitor. The visitor should be aware and only lvalue
196
202
  // reference members one at a time.
197
203
  auto* element = napi_value{visit(indices, std::forward<decltype(tuple)>(tuple), self)};
@@ -267,8 +273,8 @@ struct accept_napi_value : accept_basic_napi_value {
267
273
  auto accept_entry_pair_struct(this const auto& self, auto& visit, local_of<object_tag> target, auto&& subject) -> void {
268
274
  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
269
275
  std::array<napi_property_descriptor, Size> properties;
270
- const auto [... indices ] = util::sequence<Size>;
271
- (..., [ & ]() -> void {
276
+ constexpr auto [... indices ] = util::sequence<Size>;
277
+ (..., [ & ] -> void {
272
278
  // nb: This is forwarded to *each* visitor. The visitor should be aware and only lvalue
273
279
  // reference members one at a time.
274
280
  auto accept_entry = accept_entry_pair<decltype(self), decltype(self)>{self};
package/transfer/visit.cc CHANGED
@@ -22,7 +22,7 @@ struct visit_property_name {
22
22
 
23
23
  template <class Accept>
24
24
  auto operator()(napi_value subject, const Accept& accept) -> accept_target_t<Accept> {
25
- return visit_.get().lookup_or_visit(subject, [ & ]() -> accept_target_t<Accept> {
25
+ return visit_.get().lookup_or_visit(subject, [ & ] -> accept_target_t<Accept> {
26
26
  auto accept_as = [ & ]<class Tag>(Tag tag) -> auto {
27
27
  auto value = value_of{napi_env{visit_.get()}, local_of<Tag>::from(subject)};
28
28
  return accept(tag, *this, value);
@@ -111,7 +111,7 @@ struct visit_value : reference_map_t<Reference, reference_map_type> {
111
111
  template <class Tag, class Accept>
112
112
  auto operator()(local_of<Tag> subject, const Accept& accept) -> accept_target_t<Accept>
113
113
  requires requires { immediate(subject, accept); } {
114
- return lookup_or_visit(subject, [ & ]() -> accept_target_t<Accept> {
114
+ return lookup_or_visit(subject, [ & ] -> accept_target_t<Accept> {
115
115
  return immediate(subject, accept);
116
116
  });
117
117
  }
@@ -151,10 +151,10 @@ struct visit_value : reference_map_t<Reference, reference_map_type> {
151
151
  auto operator()(napi_value subject, const Accept& accept) -> accept_target_t<Accept> {
152
152
 
153
153
  // Check the reference map, and lookup type via napi
154
- return lookup_or_visit(subject, [ & ]() -> accept_target_t<Accept> {
154
+ return lookup_or_visit(subject, [ & ] -> accept_target_t<Accept> {
155
155
  // This is the pure-napi implementation
156
156
  auto slow_path = util::overloaded{
157
- [ & ]() -> accept_target_t<Accept> {
157
+ [ & ] -> accept_target_t<Accept> {
158
158
  auto type_of = napi::invoke(napi_typeof, napi_env{*this}, subject);
159
159
  switch (type_of) {
160
160
  case napi_undefined:
@@ -247,7 +247,7 @@ struct visit_value : reference_map_t<Reference, reference_map_type> {
247
247
 
248
248
  template <class Accept>
249
249
  auto operator()(local_of<data_block_tag> subject, const Accept& accept) -> accept_target_t<Accept> {
250
- return lookup_or_visit(subject, [ & ]() -> accept_target_t<Accept> {
250
+ return lookup_or_visit(subject, [ & ] -> accept_target_t<Accept> {
251
251
  return util::template_traverse(
252
252
  accept_tags_of_v<Accept>,
253
253
  util::overloaded{
@@ -257,7 +257,7 @@ struct visit_value : reference_map_t<Reference, reference_map_type> {
257
257
  [ & ](shared_array_buffer_tag /*tag*/, auto next) -> accept_target_t<Accept> { return next(); },
258
258
 
259
259
  // Slow path
260
- [ & ]() -> accept_target_t<Accept> {
260
+ [ & ] -> accept_target_t<Accept> {
261
261
  return immediate(subject, accept);
262
262
  }
263
263
  }
package/value/class.cc CHANGED
@@ -27,7 +27,7 @@ auto local_for_class_of<Type>::runtime_construct(
27
27
  std::tuple<RuntimeArgs...> runtime_args
28
28
  ) const -> local_of<object_tag>
29
29
  requires std::constructible_from<Type, HostArgs...> {
30
- const auto [... indices ] = util::sequence<sizeof...(HostArgs)>;
30
+ constexpr auto [... indices ] = util::sequence<sizeof...(HostArgs)>;
31
31
  auto instance = std::make_unique<Type>(std::get<indices>(std::move(host_args))...);
32
32
  return transfer_construct(env, std::move(instance), std::move(runtime_args));
33
33
  }