Use node-api instead of NAN (#6452)

This commit is contained in:
Siarhei Fedartsou
2022-11-16 15:44:36 +01:00
committed by GitHub
parent a1b3efe260
commit 18dcd27a9b
13 changed files with 785 additions and 833 deletions
+22 -22
View File
@@ -2,11 +2,7 @@
#define OSRM_BINDINGS_NODE_JSON_V8_RENDERER_HPP
#include "osrm/json_container.hpp"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include <nan.h>
#pragma GCC diagnostic pop
#include <napi.h>
#include <functional>
@@ -15,52 +11,56 @@ namespace node_osrm
struct V8Renderer
{
explicit V8Renderer(v8::Local<v8::Value> &_out) : out(_out) {}
explicit V8Renderer(const Napi::Env &env, Napi::Value &out) : env(env), out(out) {}
void operator()(const osrm::json::String &string) const
{
out = Nan::New(std::cref(string.value)).ToLocalChecked();
out = Napi::String::New(env, string.value);
}
void operator()(const osrm::json::Number &number) const { out = Nan::New(number.value); }
void operator()(const osrm::json::Number &number) const
{
out = Napi::Number::New(env, number.value);
}
void operator()(const osrm::json::Object &object) const
{
v8::Local<v8::Object> obj = Nan::New<v8::Object>();
Napi::Object obj = Napi::Object::New(env);
for (const auto &keyValue : object.values)
{
v8::Local<v8::Value> child;
mapbox::util::apply_visitor(V8Renderer(child), keyValue.second);
Nan::Set(obj, Nan::New(keyValue.first).ToLocalChecked(), child);
Napi::Value child;
mapbox::util::apply_visitor(V8Renderer(env, child), keyValue.second);
obj.Set(keyValue.first, child);
}
out = obj;
}
void operator()(const osrm::json::Array &array) const
{
v8::Local<v8::Array> a = Nan::New<v8::Array>(array.values.size());
Napi::Array a = Napi::Array::New(env, array.values.size());
for (auto i = 0u; i < array.values.size(); ++i)
{
v8::Local<v8::Value> child;
mapbox::util::apply_visitor(V8Renderer(child), array.values[i]);
Nan::Set(a, i, child);
Napi::Value child;
mapbox::util::apply_visitor(V8Renderer(env, child), array.values[i]);
a.Set(i, child);
}
out = a;
}
void operator()(const osrm::json::True &) const { out = Nan::New(true); }
void operator()(const osrm::json::True &) const { out = Napi::Boolean::New(env, true); }
void operator()(const osrm::json::False &) const { out = Nan::New(false); }
void operator()(const osrm::json::False &) const { out = Napi::Boolean::New(env, false); }
void operator()(const osrm::json::Null &) const { out = Nan::Null(); }
void operator()(const osrm::json::Null &) const { out = env.Null(); }
private:
v8::Local<v8::Value> &out;
const Napi::Env &env;
Napi::Value &out;
};
inline void renderToV8(v8::Local<v8::Value> &out, const osrm::json::Object &object)
inline void renderToV8(const Napi::Env &env, Napi::Value &out, const osrm::json::Object &object)
{
V8Renderer renderer(out);
V8Renderer renderer(env, out);
renderer(object);
}
} // namespace node_osrm
+13 -28
View File
@@ -3,45 +3,30 @@
#include "osrm/osrm_fwd.hpp"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include <nan.h>
#pragma GCC diagnostic pop
#include <napi.h>
#include <memory>
namespace node_osrm
{
struct Engine final : public Nan::ObjectWrap
class Engine final : public Napi::ObjectWrap<Engine>
{
using Base = Nan::ObjectWrap;
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports);
Engine(const Napi::CallbackInfo &info);
static NAN_MODULE_INIT(Init);
static NAN_METHOD(New);
static NAN_METHOD(route);
static NAN_METHOD(nearest);
static NAN_METHOD(table);
static NAN_METHOD(tile);
static NAN_METHOD(match);
static NAN_METHOD(trip);
Engine(osrm::EngineConfig &config);
// Thread-safe singleton accessor
static Nan::Persistent<v8::Function> &constructor();
// Ref-counted OSRM alive even after shutdown until last callback is done
std::shared_ptr<osrm::OSRM> this_;
private:
Napi::Value route(const Napi::CallbackInfo &info);
Napi::Value nearest(const Napi::CallbackInfo &info);
Napi::Value table(const Napi::CallbackInfo &info);
Napi::Value tile(const Napi::CallbackInfo &info);
Napi::Value match(const Napi::CallbackInfo &info);
Napi::Value trip(const Napi::CallbackInfo &info);
};
} // namespace node_osrm
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
NAN_MODULE_WORKER_ENABLED(osrm, node_osrm::Engine::Init)
#pragma GCC diagnostic pop
#endif
File diff suppressed because it is too large Load Diff