#include #include #include #include #include #include #include #include // This dispatch detects only the public overload. The unavailable branch reports // missing behavior; it never implements object construction for the candidate. template struct HasOverload : std::false_type {}; template struct HasOverload(), std::declval(), std::declval&>(), std::declval&>()))>> : std::true_type {}; template struct HasPointerOverload : std::false_type {}; template struct HasPointerOverload(), std::declval(), std::declval(), std::declval(), std::declval()))>> : std::true_type {}; template Napi::Object Wrapped(napi_env env, napi_value proto, std::vector& names, std::vector& values) { if constexpr (HasPointerOverload::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::value) return T::New(env, proto, names, values); else throw Napi::Error::New(env, "public object construction overload unavailable"); } template 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) 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& values) { if (values.empty()) return; size_t page = static_cast(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(memory);} }; std::vector Handles(Napi::Array a) { std::vector result; for(uint32_t i=0;i().Utf8Value(); napi_value proto=info[1].IsUndefined()?nullptr:static_cast(info[1]); auto names=Handles(info[2].As()), values=Handles(info[3].As()); const auto beforeNames=names,beforeValues=values; napi_value output=nullptr; napi_status status=napi_ok; try { if(mode=="wrapper") output=Wrapped(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::value||HasPointerOverload::value))); return exports; } NODE_API_MODULE(object_contract,Init)