File size: 4,824 Bytes
0e239c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <napi.h>
#include <vector>
#include <type_traits>
#include <utility>
#include <sys/mman.h>
#include <unistd.h>
#include <cstring>
#include <stdexcept>

// This dispatch detects only the public overload. The unavailable branch reports
// missing behavior; it never implements object construction for the candidate.
template<class T, class = void> struct HasOverload : std::false_type {};
template<class T> struct HasOverload<T, std::void_t<decltype(T::New(
    std::declval<napi_env>(), std::declval<napi_value>(),
    std::declval<std::vector<napi_value>&>(),
    std::declval<std::vector<napi_value>&>()))>> : std::true_type {};
template<class T, class = void> struct HasPointerOverload : std::false_type {};
template<class T> struct HasPointerOverload<T, std::void_t<decltype(T::New(
    std::declval<napi_env>(), std::declval<napi_value>(),
    std::declval<const napi_value*>(), std::declval<const napi_value*>(),
    std::declval<size_t>()))>> : std::true_type {};
template<class T> Napi::Object Wrapped(napi_env env, napi_value proto,
    std::vector<napi_value>& names, std::vector<napi_value>& values) {
  if constexpr (HasPointerOverload<T>::value) {
    const napi_value* n=names.data(); const napi_value* v=values.data();
    return T::New(env, proto, n, v, names.size());
  } else if constexpr (HasOverload<T>::value) return T::New(env, proto, names, values);
  else throw Napi::Error::New(env, "public object construction overload unavailable");
}
template<class F> napi_status ReadOnly(F fn, napi_env env, napi_value proto,
    const napi_value* names, const napi_value* values, size_t n, napi_value* out) {
  if constexpr (std::is_invocable_r_v<napi_status,F,napi_env,napi_value,
      const napi_value*,const napi_value*,size_t,napi_value*>)
    return fn(env, proto, names, values, n, out);
  else throw Napi::Error::New(env, "public C API rejects read-only arrays");
}
struct ReadOnlyStorage {
  void* memory = nullptr; size_t bytes = 0;
  explicit ReadOnlyStorage(const std::vector<napi_value>& values) {
    if (values.empty()) return;
    size_t page = static_cast<size_t>(sysconf(_SC_PAGESIZE));
    bytes = ((values.size()*sizeof(napi_value)+page-1)/page)*page;
    memory = mmap(nullptr, bytes, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,-1,0);
    if (memory == MAP_FAILED) throw std::runtime_error("mmap failed");
    std::memcpy(memory, values.data(), values.size()*sizeof(napi_value));
    if (mprotect(memory,bytes,PROT_READ)!=0) throw std::runtime_error("mprotect failed");
  }
  ~ReadOnlyStorage() {if(memory && memory!=MAP_FAILED) munmap(memory,bytes);}
  const napi_value* data() const {return static_cast<const napi_value*>(memory);}
};
std::vector<napi_value> Handles(Napi::Array a) {
  std::vector<napi_value> result;
  for(uint32_t i=0;i<a.Length();i++) result.push_back(a.Get(i));
  return result;
}
Napi::Value Construct(const Napi::CallbackInfo& info) {
  auto env=info.Env();
  auto mode=info[0].As<Napi::String>().Utf8Value();
  napi_value proto=info[1].IsUndefined()?nullptr:static_cast<napi_value>(info[1]);
  auto names=Handles(info[2].As<Napi::Array>()), values=Handles(info[3].As<Napi::Array>());
  const auto beforeNames=names,beforeValues=values;
  napi_value output=nullptr; napi_status status=napi_ok;
  try {
    if(mode=="wrapper") output=Wrapped<Napi::Object>(env,proto,names,values);
    else {
      if(names.size()!=values.size()) throw Napi::Error::New(env,"fixture raw length mismatch");
      if(mode=="readonly") {
        ReadOnlyStorage n(names),v(values);
        status=ReadOnly(&node_api_create_object_with_properties,env,proto,n.data(),v.data(),names.size(),&output);
      } else if(mode=="mutable") {
        status=node_api_create_object_with_properties(env,proto,names.data(),values.data(),names.size(),&output);
      } else throw Napi::Error::New(env,"unknown fixture mode");
    }
  } catch (...) {
    if(names!=beforeNames||values!=beforeValues)
      throw Napi::Error::New(env,"caller handle arrays mutated on error");
    throw;
  }
  if(names!=beforeNames||values!=beforeValues)
    throw Napi::Error::New(env,"caller handle arrays mutated");
  if(status!=napi_ok) throw Napi::Error::New(env,"native construction rejected input");
  return Napi::Value(env,output);
}
Napi::Value Ordinary(const Napi::CallbackInfo& info) {
  auto env=info.Env();auto a=Napi::Object::New(env);
  a.Set("value",info[0]);a.Set("answer",Napi::Number::New(env,42));
  return a;
}
Napi::Object Init(Napi::Env env,Napi::Object exports) {
  exports.Set("construct",Napi::Function::New(env,Construct));
  exports.Set("ordinary",Napi::Function::New(env,Ordinary));
  exports.Set("wrapperAvailable",Napi::Boolean::New(env,(HasOverload<Napi::Object>::value||HasPointerOverload<Napi::Object>::value)));
  return exports;
}
NODE_API_MODULE(object_contract,Init)