Add clang-format (#1086)

Co-authored-by: mwilsnd <mwilsnd@gmail.com>
This commit is contained in:
Bart Louwers
2023-05-01 16:57:37 +02:00
committed by GitHub
parent 403a12bd1b
commit 71f6da2722
1018 changed files with 27876 additions and 25862 deletions

View File

@@ -41,7 +41,7 @@ struct NodeMap::RenderOptions {
double pitch = 0;
double latitude = 0;
double longitude = 0;
mbgl::Size size = { 512, 512 };
mbgl::Size size = {512, 512};
bool axonometric = false;
double xSkew = 0;
double ySkew = 1;
@@ -77,7 +77,9 @@ class ParseError extends Error {
}
}
}
ParseError)JS").ToLocalChecked()).ToLocalChecked();
ParseError)JS")
.ToLocalChecked())
.ToLocalChecked();
parseError.Reset(Nan::To<v8::Object>(Nan::RunScript(script).ToLocalChecked()).ToLocalChecked());
Nan::Set(target, Nan::New("ParseError").ToLocalChecked(), Nan::New(parseError));
@@ -177,35 +179,36 @@ void NodeMap::New(const Nan::FunctionCallbackInfo<v8::Value>& info) {
v8::Local<v8::Object> options;
if(info.Length() > 0) {
if (info.Length() > 0) {
options = Nan::To<v8::Object>(info[0]).ToLocalChecked();
} else {
options = Nan::New<v8::Object>();
}
// Check that if 'request' is set it must be a function, if 'cancel' is set it must be a
// function and if 'ratio' is set it must be a number.
if (Nan::Has(options, Nan::New("request").ToLocalChecked()).FromJust()
&& !Nan::Get(options, Nan::New("request").ToLocalChecked()).ToLocalChecked()->IsFunction()) {
// Check that if 'request' is set it must be a function, if 'cancel' is set
// it must be a function and if 'ratio' is set it must be a number.
if (Nan::Has(options, Nan::New("request").ToLocalChecked()).FromJust() &&
!Nan::Get(options, Nan::New("request").ToLocalChecked()).ToLocalChecked()->IsFunction()) {
return Nan::ThrowError("Options object 'request' property must be a function");
}
if (Nan::Has(options, Nan::New("cancel").ToLocalChecked()).FromJust()
&& !Nan::Get(options, Nan::New("cancel").ToLocalChecked()).ToLocalChecked()->IsFunction()) {
if (Nan::Has(options, Nan::New("cancel").ToLocalChecked()).FromJust() &&
!Nan::Get(options, Nan::New("cancel").ToLocalChecked()).ToLocalChecked()->IsFunction()) {
return Nan::ThrowError("Options object 'cancel' property must be a function");
}
if (Nan::Has(options, Nan::New("ratio").ToLocalChecked()).FromJust()
&& !Nan::Get(options, Nan::New("ratio").ToLocalChecked()).ToLocalChecked()->IsNumber()) {
if (Nan::Has(options, Nan::New("ratio").ToLocalChecked()).FromJust() &&
!Nan::Get(options, Nan::New("ratio").ToLocalChecked()).ToLocalChecked()->IsNumber()) {
return Nan::ThrowError("Options object 'ratio' property must be a number");
}
info.This()->SetInternalField(1, options);
if(Nan::Has(options, Nan::New("request").ToLocalChecked()).FromJust()
&& Nan::Get(options, Nan::New("request").ToLocalChecked()).ToLocalChecked()->IsFunction()) {
if (Nan::Has(options, Nan::New("request").ToLocalChecked()).FromJust() &&
Nan::Get(options, Nan::New("request").ToLocalChecked()).ToLocalChecked()->IsFunction()) {
mbgl::FileSourceManager::get()->registerFileSourceFactory(
mbgl::FileSourceType::ResourceLoader, [](const mbgl::ResourceOptions& resourceOptions, const mbgl::ClientOptions&) {
mbgl::FileSourceType::ResourceLoader,
[](const mbgl::ResourceOptions& resourceOptions, const mbgl::ClientOptions&) {
return std::make_unique<node_mbgl::NodeFileSource>(
reinterpret_cast<node_mbgl::NodeMap*>(resourceOptions.platformContext()));
});
@@ -214,7 +217,7 @@ void NodeMap::New(const Nan::FunctionCallbackInfo<v8::Value>& info) {
try {
auto nodeMap = new NodeMap(options);
nodeMap->Wrap(info.This());
} catch(std::exception &ex) {
} catch (std::exception& ex) {
return Nan::ThrowError(ex.what());
}
@@ -267,7 +270,7 @@ void NodeMap::Load(const Nan::FunctionCallbackInfo<v8::Value>& info) {
nodeMap->map->getStyle().loadJSON(style);
} catch (const mbgl::util::StyleParseException& ex) {
return Nan::ThrowError(ParseError(ex.what()));
} catch (const std::exception &ex) {
} catch (const std::exception& ex) {
return Nan::ThrowError(ex.what());
}
@@ -278,9 +281,12 @@ void NodeMap::Load(const Nan::FunctionCallbackInfo<v8::Value>& info) {
auto options = Nan::To<v8::Object>(info[1]).ToLocalChecked();
if (Nan::Has(options, Nan::New("defaultStyleCamera").ToLocalChecked()).FromJust()) {
if (!Nan::Get(options, Nan::New("defaultStyleCamera").ToLocalChecked()).ToLocalChecked()->IsBoolean()) {
return Nan::ThrowError("Options object 'defaultStyleCamera' property must be a boolean");
return Nan::ThrowError(
"Options object 'defaultStyleCamera' property must be a "
"boolean");
}
if (Nan::To<bool>(Nan::Get(options, Nan::New("cameraMutated").ToLocalChecked()).ToLocalChecked()).ToChecked()) {
if (Nan::To<bool>(Nan::Get(options, Nan::New("cameraMutated").ToLocalChecked()).ToLocalChecked())
.ToChecked()) {
nodeMap->map->jumpTo(nodeMap->map->getStyle().getDefaultCamera());
}
}
@@ -299,7 +305,7 @@ void NodeMap::Loaded(const Nan::FunctionCallbackInfo<v8::Value>& info) {
try {
loaded = nodeMap->map->isFullyLoaded();
} catch (const std::exception &ex) {
} catch (const std::exception& ex) {
return Nan::ThrowError(ex.what());
}
@@ -316,7 +322,8 @@ NodeMap::RenderOptions NodeMap::ParseOptions(v8::Local<v8::Object> obj) {
}
if (Nan::Has(obj, Nan::New("bearing").ToLocalChecked()).FromJust()) {
options.bearing = Nan::To<double>(Nan::Get(obj, Nan::New("bearing").ToLocalChecked()).ToLocalChecked()).ToChecked();
options.bearing =
Nan::To<double>(Nan::Get(obj, Nan::New("bearing").ToLocalChecked()).ToLocalChecked()).ToChecked();
}
if (Nan::Has(obj, Nan::New("pitch").ToLocalChecked()).FromJust()) {
@@ -334,15 +341,20 @@ NodeMap::RenderOptions NodeMap::ParseOptions(v8::Local<v8::Object> obj) {
}
if (Nan::Has(obj, Nan::New("axonometric").ToLocalChecked()).FromJust()) {
options.axonometric = Nan::To<bool>(Nan::Get(obj, Nan::New("axonometric").ToLocalChecked()).ToLocalChecked()).ToChecked();
options.axonometric =
Nan::To<bool>(Nan::Get(obj, Nan::New("axonometric").ToLocalChecked()).ToLocalChecked()).ToChecked();
}
if (Nan::Has(obj, Nan::New("skew").ToLocalChecked()).FromJust()) {
auto skewObj = Nan::Get(obj, Nan::New("skew").ToLocalChecked()).ToLocalChecked();
if (skewObj->IsArray()) {
auto skew = skewObj.As<v8::Array>();
if (skew->Length() > 0) { options.xSkew = Nan::To<double>(Nan::Get(skew, 0).ToLocalChecked()).ToChecked(); }
if (skew->Length() > 1) { options.ySkew = Nan::To<double>(Nan::Get(skew, 1).ToLocalChecked()).ToChecked(); }
if (skew->Length() > 0) {
options.xSkew = Nan::To<double>(Nan::Get(skew, 0).ToLocalChecked()).ToChecked();
}
if (skew->Length() > 1) {
options.ySkew = Nan::To<double>(Nan::Get(skew, 1).ToLocalChecked()).ToChecked();
}
}
}
@@ -350,30 +362,40 @@ NodeMap::RenderOptions NodeMap::ParseOptions(v8::Local<v8::Object> obj) {
auto centerObj = Nan::Get(obj, Nan::New("center").ToLocalChecked()).ToLocalChecked();
if (centerObj->IsArray()) {
auto center = centerObj.As<v8::Array>();
if (center->Length() > 0) { options.longitude = Nan::To<double>(Nan::Get(center, 0).ToLocalChecked()).ToChecked(); }
if (center->Length() > 1) { options.latitude = Nan::To<double>(Nan::Get(center, 1).ToLocalChecked()).ToChecked(); }
if (center->Length() > 0) {
options.longitude = Nan::To<double>(Nan::Get(center, 0).ToLocalChecked()).ToChecked();
}
if (center->Length() > 1) {
options.latitude = Nan::To<double>(Nan::Get(center, 1).ToLocalChecked()).ToChecked();
}
}
}
if (Nan::Has(obj, Nan::New("width").ToLocalChecked()).FromJust()) {
options.size.width = static_cast<uint32_t>(Nan::To<int64_t>(Nan::Get(obj, Nan::New("width").ToLocalChecked()).ToLocalChecked()).ToChecked());
options.size.width = static_cast<uint32_t>(
Nan::To<int64_t>(Nan::Get(obj, Nan::New("width").ToLocalChecked()).ToLocalChecked()).ToChecked());
}
if (Nan::Has(obj, Nan::New("height").ToLocalChecked()).FromJust()) {
options.size.height = static_cast<uint32_t>(Nan::To<int64_t>(Nan::Get(obj, Nan::New("height").ToLocalChecked()).ToLocalChecked()).ToChecked());
options.size.height = static_cast<uint32_t>(
Nan::To<int64_t>(Nan::Get(obj, Nan::New("height").ToLocalChecked()).ToLocalChecked()).ToChecked());
}
if (Nan::Has(obj, Nan::New("classes").ToLocalChecked()).FromJust()) {
auto classes = Nan::To<v8::Object>(Nan::Get(obj, Nan::New("classes").ToLocalChecked()).ToLocalChecked()).ToLocalChecked().As<v8::Array>();
auto classes = Nan::To<v8::Object>(Nan::Get(obj, Nan::New("classes").ToLocalChecked()).ToLocalChecked())
.ToLocalChecked()
.As<v8::Array>();
const int length = classes->Length();
options.classes.reserve(length);
for (int i = 0; i < length; i++) {
options.classes.emplace_back(std::string { *Nan::Utf8String(Nan::To<v8::String>(Nan::Get(classes, i).ToLocalChecked()).ToLocalChecked()) });
options.classes.emplace_back(std::string{
*Nan::Utf8String(Nan::To<v8::String>(Nan::Get(classes, i).ToLocalChecked()).ToLocalChecked())});
}
}
if (Nan::Has(obj, Nan::New("debug").ToLocalChecked()).FromJust()) {
auto debug = Nan::To<v8::Object>(Nan::Get(obj, Nan::New("debug").ToLocalChecked()).ToLocalChecked()).ToLocalChecked();
auto debug =
Nan::To<v8::Object>(Nan::Get(obj, Nan::New("debug").ToLocalChecked()).ToLocalChecked()).ToLocalChecked();
if (Nan::Has(debug, Nan::New("tileBorders").ToLocalChecked()).FromJust()) {
if (Nan::To<bool>(Nan::Get(debug, Nan::New("tileBorders").ToLocalChecked()).ToLocalChecked()).ToChecked()) {
options.debugOptions = options.debugOptions | mbgl::MapDebugOptions::TileBorders;
@@ -406,12 +428,11 @@ NodeMap::RenderOptions NodeMap::ParseOptions(v8::Local<v8::Object> obj) {
class RenderRequest : public Nan::AsyncResource {
public:
explicit RenderRequest(v8::Local<v8::Function> callback_) : AsyncResource("mbgl:RenderRequest") {
explicit RenderRequest(v8::Local<v8::Function> callback_)
: AsyncResource("mbgl:RenderRequest") {
callback.Reset(callback_);
}
~RenderRequest() {
callback.Reset();
}
~RenderRequest() { callback.Reset(); }
Nan::Persistent<v8::Function> callback;
};
@@ -471,7 +492,7 @@ void NodeMap::Render(const Nan::FunctionCallbackInfo<v8::Value>& info) {
return Nan::ThrowTypeError(err.message.c_str());
} catch (const mbgl::util::StyleParseException& ex) {
return Nan::ThrowError(ParseError(ex.what()));
} catch (const mbgl::util::Exception &ex) {
} catch (const mbgl::util::Exception& ex) {
return Nan::ThrowError(ex.what());
}
@@ -490,12 +511,12 @@ void NodeMap::startRender() {
}
});
// Retain this object, otherwise it might get destructed before we are finished rendering the
// still image.
// Retain this object, otherwise it might get destructed before we are
// finished rendering the still image.
Ref();
// Similarly, we're now waiting for the async to be called, so we need to make sure that it
// keeps the loop alive.
// Similarly, we're now waiting for the async to be called, so we need to
// make sure that it keeps the loop alive.
uv_ref(reinterpret_cast<uv_handle_t*>(async));
}
@@ -504,15 +525,13 @@ void NodeMap::startRender(const NodeMap::RenderOptions& options) {
map->setSize(options.size);
mbgl::CameraOptions camera;
camera.center = mbgl::LatLng { options.latitude, options.longitude };
camera.center = mbgl::LatLng{options.latitude, options.longitude};
camera.zoom = options.zoom;
camera.bearing = options.bearing;
camera.pitch = options.pitch;
auto projectionOptions = mbgl::ProjectionMode()
.withAxonometric(options.axonometric)
.withXSkew(options.xSkew)
.withYSkew(options.ySkew);
auto projectionOptions =
mbgl::ProjectionMode().withAxonometric(options.axonometric).withXSkew(options.xSkew).withYSkew(options.ySkew);
map->setProjectionMode(projectionOptions);
@@ -527,17 +546,17 @@ void NodeMap::startRender(const NodeMap::RenderOptions& options) {
}
});
// Retain this object, otherwise it might get destructed before we are finished rendering the
// still image.
// Retain this object, otherwise it might get destructed before we are
// finished rendering the still image.
Ref();
// Similarly, we're now waiting for the async to be called, so we need to make sure that it
// keeps the loop alive.
uv_ref(reinterpret_cast<uv_handle_t *>(async));
// Similarly, we're now waiting for the async to be called, so we need to
// make sure that it keeps the loop alive.
uv_ref(reinterpret_cast<uv_handle_t*>(async));
}
v8::Local<v8::Value> NodeMap::ParseError(const char* msg) {
v8::Local<v8::Value> argv[] = { Nan::New(msg).ToLocalChecked() };
v8::Local<v8::Value> argv[] = {Nan::New(msg).ToLocalChecked()};
return Nan::CallAsConstructor(Nan::New(parseError), 1, argv).ToLocalChecked();
}
@@ -547,7 +566,7 @@ void NodeMap::renderFinished() {
Nan::HandleScope scope;
// We're done with this render call, so we're unrefing so that the loop could close.
uv_unref(reinterpret_cast<uv_handle_t *>(async));
uv_unref(reinterpret_cast<uv_handle_t*>(async));
// Move the callback and image out of the way so that the callback can start a new render call.
auto request = std::move(req);
@@ -573,9 +592,7 @@ void NodeMap::renderFinished() {
err = Nan::Error(ex.what());
}
v8::Local<v8::Value> argv[] = {
err
};
v8::Local<v8::Value> argv[] = {err};
// This must be empty to be prepared for the next render call.
error = nullptr;
@@ -584,31 +601,25 @@ void NodeMap::renderFinished() {
request->runInAsyncScope(target, callback, 1, argv);
} else if (img.data) {
v8::Local<v8::Object> pixels = Nan::NewBuffer(
reinterpret_cast<char *>(img.data.get()), img.bytes(),
// Retain the data until the buffer is deleted.
[](char *, void * hint) {
delete [] reinterpret_cast<uint8_t*>(hint);
},
img.data.get()
).ToLocalChecked();
reinterpret_cast<char*>(img.data.get()),
img.bytes(),
// Retain the data until the buffer is deleted.
[](char*, void* hint) { delete[] reinterpret_cast<uint8_t*>(hint); },
img.data.get())
.ToLocalChecked();
if (!pixels.IsEmpty()) {
img.data.release();
}
v8::Local<v8::Value> argv[] = {
Nan::Null(),
pixels
};
v8::Local<v8::Value> argv[] = {Nan::Null(), pixels};
request->runInAsyncScope(target, callback, 2, argv);
} else {
v8::Local<v8::Value> argv[] = {
Nan::Error("Didn't get an image")
};
v8::Local<v8::Value> argv[] = {Nan::Error("Didn't get an image")};
request->runInAsyncScope(target, callback, 1, argv);
}
// There is no render pending anymore, we the GC could now delete this object if it went out
// of scope.
// There is no render pending anymore, we the GC could now delete this
// object if it went out of scope.
Unref();
}
@@ -623,7 +634,7 @@ void NodeMap::Release(const Nan::FunctionCallbackInfo<v8::Value>& info) {
try {
nodeMap->release();
} catch (const std::exception &ex) {
} catch (const std::exception& ex) {
return Nan::ThrowError(ex.what());
}
@@ -633,9 +644,7 @@ void NodeMap::Release(const Nan::FunctionCallbackInfo<v8::Value>& info) {
void NodeMap::release() {
if (!map) throw mbgl::util::Exception(releasedMessage());
uv_close(reinterpret_cast<uv_handle_t *>(async), [] (uv_handle_t *h) {
delete reinterpret_cast<uv_async_t *>(h);
});
uv_close(reinterpret_cast<uv_handle_t*>(async), [](uv_handle_t* h) { delete reinterpret_cast<uv_async_t*>(h); });
map.reset();
frontend.reset();
@@ -655,7 +664,7 @@ void NodeMap::Cancel(const Nan::FunctionCallbackInfo<v8::Value>& info) {
try {
nodeMap->cancel();
} catch (const std::exception &ex) {
} catch (const std::exception& ex) {
return Nan::ThrowError(ex.what());
}
@@ -669,22 +678,21 @@ void NodeMap::cancel() {
map.reset();
// Remove the existing async handle to flush any scheduled calls to renderFinished.
uv_unref(reinterpret_cast<uv_handle_t *>(async));
uv_close(reinterpret_cast<uv_handle_t *>(async), [] (uv_handle_t *h) {
delete reinterpret_cast<uv_async_t *>(h);
});
uv_unref(reinterpret_cast<uv_handle_t*>(async));
uv_close(reinterpret_cast<uv_handle_t*>(async), [](uv_handle_t* h) { delete reinterpret_cast<uv_async_t*>(h); });
async = new uv_async_t;
async->data = this;
uv_async_init(uv_default_loop(), async, [](uv_async_t* h) {
reinterpret_cast<NodeMap *>(h->data)->renderFinished();
});
uv_async_init(
uv_default_loop(), async, [](uv_async_t* h) { reinterpret_cast<NodeMap*>(h->data)->renderFinished(); });
frontend = std::make_unique<mbgl::HeadlessFrontend>(mbgl::Size{ 512, 512 }, pixelRatio);
map = std::make_unique<mbgl::Map>(*frontend, mapObserver,
mbgl::MapOptions().withSize(frontend->getSize())
.withPixelRatio(pixelRatio)
.withMapMode(mode)
.withCrossSourceCollisions(crossSourceCollisions),
frontend = std::make_unique<mbgl::HeadlessFrontend>(mbgl::Size{512, 512}, pixelRatio);
map = std::make_unique<mbgl::Map>(*frontend,
mapObserver,
mbgl::MapOptions()
.withSize(frontend->getSize())
.withPixelRatio(pixelRatio)
.withMapMode(mode)
.withCrossSourceCollisions(crossSourceCollisions),
mbgl::ResourceOptions().withPlatformContext(reinterpret_cast<void*>(this)),
mbgl::ClientOptions());
@@ -717,7 +725,8 @@ void NodeMap::AddSource(const Nan::FunctionCallbackInfo<v8::Value>& info) {
}
Error error;
std::optional<std::unique_ptr<Source>> source = convert<std::unique_ptr<Source>>(info[1], error, *Nan::Utf8String(info[0]));
std::optional<std::unique_ptr<Source>> source = convert<std::unique_ptr<Source>>(
info[1], error, *Nan::Utf8String(info[0]));
if (!source) {
Nan::ThrowTypeError(error.message.c_str());
return;
@@ -822,8 +831,10 @@ void NodeMap::AddImage(const Nan::FunctionCallbackInfo<v8::Value>& info) {
return Nan::ThrowTypeError("pixelRatio parameter required");
}
uint32_t imageHeight = Nan::To<uint32_t>(Nan::Get(optionObject, Nan::New("height").ToLocalChecked()).ToLocalChecked()).ToChecked();
uint32_t imageWidth = Nan::To<uint32_t>(Nan::Get(optionObject, Nan::New("width").ToLocalChecked()).ToLocalChecked()).ToChecked();
uint32_t imageHeight =
Nan::To<uint32_t>(Nan::Get(optionObject, Nan::New("height").ToLocalChecked()).ToLocalChecked()).ToChecked();
uint32_t imageWidth =
Nan::To<uint32_t>(Nan::Get(optionObject, Nan::New("width").ToLocalChecked()).ToLocalChecked()).ToChecked();
if (imageWidth > 1024 || imageHeight > 1024) {
return Nan::ThrowTypeError("Max height and width is 1024");
@@ -834,10 +845,12 @@ void NodeMap::AddImage(const Nan::FunctionCallbackInfo<v8::Value>& info) {
sdf = Nan::To<bool>(Nan::Get(optionObject, Nan::New("sdf").ToLocalChecked()).ToLocalChecked()).ToChecked();
}
float pixelRatio = (float)Nan::To<double>(Nan::Get(optionObject, Nan::New("pixelRatio").ToLocalChecked()).ToLocalChecked()).ToChecked();
float pixelRatio = (float)Nan::To<double>(
Nan::Get(optionObject, Nan::New("pixelRatio").ToLocalChecked()).ToLocalChecked())
.ToChecked();
auto imageBuffer = Nan::To<v8::Object>(info[1]).ToLocalChecked()->ToObject(context).ToLocalChecked();
char * imageDataBuffer = node::Buffer::Data(imageBuffer);
char* imageDataBuffer = node::Buffer::Data(imageBuffer);
size_t imageLength = node::Buffer::Length(imageBuffer);
if (imageLength != imageHeight * imageWidth * 4) {
@@ -847,9 +860,10 @@ void NodeMap::AddImage(const Nan::FunctionCallbackInfo<v8::Value>& info) {
std::unique_ptr<uint8_t[]> data = std::make_unique<uint8_t[]>(imageLength);
std::copy(imageDataBuffer, imageDataBuffer + imageLength, data.get());
mbgl::UnassociatedImage cImage({ imageWidth, imageHeight}, std::move(data));
mbgl::UnassociatedImage cImage({imageWidth, imageHeight}, std::move(data));
mbgl::PremultipliedImage cPremultipliedImage = mbgl::util::premultiply(std::move(cImage));
nodeMap->map->getStyle().addImage(std::make_unique<mbgl::style::Image>(*Nan::Utf8String(info[0]), std::move(cPremultipliedImage), pixelRatio, sdf));
nodeMap->map->getStyle().addImage(std::make_unique<mbgl::style::Image>(
*Nan::Utf8String(info[0]), std::move(cPremultipliedImage), pixelRatio, sdf));
}
void NodeMap::RemoveImage(const Nan::FunctionCallbackInfo<v8::Value>& info) {
@@ -973,15 +987,19 @@ void NodeMap::SetSize(const Nan::FunctionCallbackInfo<v8::Value>& info) {
}
auto size = info[0].As<v8::Array>();
uint32_t width= 0;
uint32_t width = 0;
uint32_t height = 0;
if (size->Length() > 0) { width = Nan::To<uint32_t>(Nan::Get(size, 0).ToLocalChecked()).ToChecked(); }
if (size->Length() > 1) { height = Nan::To<uint32_t>(Nan::Get(size, 1).ToLocalChecked()).ToChecked(); }
if (size->Length() > 0) {
width = Nan::To<uint32_t>(Nan::Get(size, 0).ToLocalChecked()).ToChecked();
}
if (size->Length() > 1) {
height = Nan::To<uint32_t>(Nan::Get(size, 1).ToLocalChecked()).ToChecked();
}
try {
nodeMap->frontend->setSize({ width, height });
nodeMap->map->setSize({ width, height });
} catch (const std::exception &ex) {
nodeMap->frontend->setSize({width, height});
nodeMap->map->setSize({width, height});
} catch (const std::exception& ex) {
return Nan::ThrowError(ex.what());
}
@@ -999,12 +1017,16 @@ void NodeMap::SetCenter(const Nan::FunctionCallbackInfo<v8::Value>& info) {
auto center = info[0].As<v8::Array>();
double latitude = 0;
double longitude = 0;
if (center->Length() > 0) { longitude = Nan::To<double>(Nan::Get(center, 0).ToLocalChecked()).ToChecked(); }
if (center->Length() > 1) { latitude = Nan::To<double>(Nan::Get(center, 1).ToLocalChecked()).ToChecked(); }
if (center->Length() > 0) {
longitude = Nan::To<double>(Nan::Get(center, 0).ToLocalChecked()).ToChecked();
}
if (center->Length() > 1) {
latitude = Nan::To<double>(Nan::Get(center, 1).ToLocalChecked()).ToChecked();
}
try {
nodeMap->map->jumpTo(mbgl::CameraOptions().withCenter(mbgl::LatLng { latitude, longitude }));
} catch (const std::exception &ex) {
nodeMap->map->jumpTo(mbgl::CameraOptions().withCenter(mbgl::LatLng{latitude, longitude}));
} catch (const std::exception& ex) {
return Nan::ThrowError(ex.what());
}
@@ -1021,7 +1043,7 @@ void NodeMap::SetZoom(const Nan::FunctionCallbackInfo<v8::Value>& info) {
try {
nodeMap->map->jumpTo(mbgl::CameraOptions().withZoom(Nan::To<double>(info[0]).ToChecked()));
} catch (const std::exception &ex) {
} catch (const std::exception& ex) {
return Nan::ThrowError(ex.what());
}
@@ -1038,7 +1060,7 @@ void NodeMap::SetBearing(const Nan::FunctionCallbackInfo<v8::Value>& info) {
try {
nodeMap->map->jumpTo(mbgl::CameraOptions().withBearing(Nan::To<double>(info[0]).ToChecked()));
} catch (const std::exception &ex) {
} catch (const std::exception& ex) {
return Nan::ThrowError(ex.what());
}
@@ -1055,7 +1077,7 @@ void NodeMap::SetPitch(const Nan::FunctionCallbackInfo<v8::Value>& info) {
try {
nodeMap->map->jumpTo(mbgl::CameraOptions().withPitch(Nan::To<double>(info[0]).ToChecked()));
} catch (const std::exception &ex) {
} catch (const std::exception& ex) {
return Nan::ThrowError(ex.what());
}
@@ -1080,7 +1102,7 @@ void NodeMap::SetLight(const Nan::FunctionCallbackInfo<v8::Value>& info) {
} else {
return Nan::ThrowTypeError(conversionError.message.c_str());
}
} catch (const std::exception &ex) {
} catch (const std::exception& ex) {
return Nan::ThrowError(ex.what());
}
@@ -1096,9 +1118,8 @@ void NodeMap::SetAxonometric(const Nan::FunctionCallbackInfo<v8::Value>& info) {
}
try {
nodeMap->map->setProjectionMode(mbgl::ProjectionMode()
.withAxonometric(Nan::To<bool>(info[0]).ToChecked()));
} catch (const std::exception &ex) {
nodeMap->map->setProjectionMode(mbgl::ProjectionMode().withAxonometric(Nan::To<bool>(info[0]).ToChecked()));
} catch (const std::exception& ex) {
return Nan::ThrowError(ex.what());
}
@@ -1114,9 +1135,8 @@ void NodeMap::SetXSkew(const Nan::FunctionCallbackInfo<v8::Value>& info) {
}
try {
nodeMap->map->setProjectionMode(mbgl::ProjectionMode()
.withXSkew(Nan::To<double>(info[0]).ToChecked()));
} catch (const std::exception &ex) {
nodeMap->map->setProjectionMode(mbgl::ProjectionMode().withXSkew(Nan::To<double>(info[0]).ToChecked()));
} catch (const std::exception& ex) {
return Nan::ThrowError(ex.what());
}
@@ -1132,9 +1152,8 @@ void NodeMap::SetYSkew(const Nan::FunctionCallbackInfo<v8::Value>& info) {
}
try {
nodeMap->map->setProjectionMode(mbgl::ProjectionMode()
.withYSkew(Nan::To<double>(info[0]).ToChecked()));
} catch (const std::exception &ex) {
nodeMap->map->setProjectionMode(mbgl::ProjectionMode().withYSkew(Nan::To<double>(info[0]).ToChecked()));
} catch (const std::exception& ex) {
return Nan::ThrowError(ex.what());
}
@@ -1173,7 +1192,9 @@ void NodeMap::SetFeatureState(const Nan::FunctionCallbackInfo<v8::Value>& info)
if (Nan::Has(feature, Nan::New("sourceLayer").ToLocalChecked()).FromJust()) {
auto sourceLayerOption = Nan::Get(feature, Nan::New("sourceLayer").ToLocalChecked()).ToLocalChecked();
if (!sourceLayerOption->IsString()) {
return Nan::ThrowTypeError("SetFeatureState: Requires feature.sourceLayer property to be a string");
return Nan::ThrowTypeError(
"SetFeatureState: Requires feature.sourceLayer property to be "
"a string");
}
sourceLayerID = {*Nan::Utf8String(sourceLayerOption)};
}
@@ -1274,7 +1295,9 @@ void NodeMap::GetFeatureState(const Nan::FunctionCallbackInfo<v8::Value>& info)
if (Nan::Has(feature, Nan::New("sourceLayer").ToLocalChecked()).FromJust()) {
auto sourceLayerOption = Nan::Get(feature, Nan::New("sourceLayer").ToLocalChecked()).ToLocalChecked();
if (!sourceLayerOption->IsString()) {
return Nan::ThrowTypeError("GetFeatureState: Requires feature.sourceLayer property to be a string");
return Nan::ThrowTypeError(
"GetFeatureState: Requires feature.sourceLayer property to be "
"a string");
}
sourceLayerID = {*Nan::Utf8String(sourceLayerOption)};
}
@@ -1333,7 +1356,9 @@ void NodeMap::RemoveFeatureState(const Nan::FunctionCallbackInfo<v8::Value>& inf
if (Nan::Has(feature, Nan::New("sourceLayer").ToLocalChecked()).FromJust()) {
auto sourceLayerOption = Nan::Get(feature, Nan::New("sourceLayer").ToLocalChecked()).ToLocalChecked();
if (!sourceLayerOption->IsString()) {
return Nan::ThrowTypeError("RemoveFeatureState: Requires feature.sourceLayer property to be a string");
return Nan::ThrowTypeError(
"RemoveFeatureState: Requires feature.sourceLayer property to "
"be a string");
}
sourceLayerID = {*Nan::Utf8String(sourceLayerOption)};
}
@@ -1349,7 +1374,9 @@ void NodeMap::RemoveFeatureState(const Nan::FunctionCallbackInfo<v8::Value>& inf
if (info.Length() == 2) {
auto keyParam = Nan::To<v8::String>(info[1]).ToLocalChecked();
if (!keyParam->IsString()) {
return Nan::ThrowTypeError("RemoveFeatureState: Requires feature key property to be a string");
return Nan::ThrowTypeError(
"RemoveFeatureState: Requires feature key property to be a "
"string");
}
stateKey = {*Nan::Utf8String(keyParam)};
}
@@ -1397,7 +1424,7 @@ void NodeMap::QueryRenderedFeatures(const Nan::FunctionCallbackInfo<v8::Value>&
auto options = Nan::To<v8::Object>(info[1]).ToLocalChecked();
//Check if layers is set. If provided, it must be an array of strings
// Check if layers is set. If provided, it must be an array of strings
if (Nan::Has(options, Nan::New("layers").ToLocalChecked()).FromJust()) {
auto layersOption = Nan::Get(options, Nan::New("layers").ToLocalChecked()).ToLocalChecked();
if (!layersOption->IsArray()) {
@@ -1405,13 +1432,13 @@ void NodeMap::QueryRenderedFeatures(const Nan::FunctionCallbackInfo<v8::Value>&
}
auto layers = layersOption.As<v8::Array>();
std::vector<std::string> layersVec;
for (uint32_t i=0; i < layers->Length(); i++) {
layersVec.emplace_back(*Nan::Utf8String(Nan::Get(layers,i).ToLocalChecked()));
for (uint32_t i = 0; i < layers->Length(); i++) {
layersVec.emplace_back(*Nan::Utf8String(Nan::Get(layers, i).ToLocalChecked()));
}
queryOptions.layerIDs = layersVec;
}
//Check if filter is provided. If set it must be a valid Filter object
// Check if filter is provided. If set it must be a valid Filter object
if (Nan::Has(options, Nan::New("filter").ToLocalChecked()).FromJust()) {
auto filterOption = Nan::Get(options, Nan::New("filter").ToLocalChecked()).ToLocalChecked();
Error error;
@@ -1427,25 +1454,21 @@ void NodeMap::QueryRenderedFeatures(const Nan::FunctionCallbackInfo<v8::Value>&
std::vector<mbgl::Feature> optional;
if (Nan::Get(posOrBox, 0).ToLocalChecked()->IsArray()) {
auto pos0 = Nan::Get(posOrBox, 0).ToLocalChecked().As<v8::Array>();
auto pos1 = Nan::Get(posOrBox, 1).ToLocalChecked().As<v8::Array>();
optional = nodeMap->frontend->getRenderer()->queryRenderedFeatures(mbgl::ScreenBox {
{
Nan::To<double>(Nan::Get(pos0, 0).ToLocalChecked()).ToChecked(),
Nan::To<double>(Nan::Get(pos0, 1).ToLocalChecked()).ToChecked()
}, {
Nan::To<double>(Nan::Get(pos1, 0).ToLocalChecked()).ToChecked(),
Nan::To<double>(Nan::Get(pos1, 1).ToLocalChecked()).ToChecked()
}
}, queryOptions);
optional = nodeMap->frontend->getRenderer()->queryRenderedFeatures(
mbgl::ScreenBox{{Nan::To<double>(Nan::Get(pos0, 0).ToLocalChecked()).ToChecked(),
Nan::To<double>(Nan::Get(pos0, 1).ToLocalChecked()).ToChecked()},
{Nan::To<double>(Nan::Get(pos1, 0).ToLocalChecked()).ToChecked(),
Nan::To<double>(Nan::Get(pos1, 1).ToLocalChecked()).ToChecked()}},
queryOptions);
} else {
optional = nodeMap->frontend->getRenderer()->queryRenderedFeatures(mbgl::ScreenCoordinate {
Nan::To<double>(Nan::Get(posOrBox, 0).ToLocalChecked()).ToChecked(),
Nan::To<double>(Nan::Get(posOrBox, 1).ToLocalChecked()).ToChecked()
}, queryOptions);
optional = nodeMap->frontend->getRenderer()->queryRenderedFeatures(
mbgl::ScreenCoordinate{Nan::To<double>(Nan::Get(posOrBox, 0).ToLocalChecked()).ToChecked(),
Nan::To<double>(Nan::Get(posOrBox, 1).ToLocalChecked()).ToChecked()},
queryOptions);
}
auto array = Nan::New<v8::Array>();
@@ -1453,7 +1476,7 @@ void NodeMap::QueryRenderedFeatures(const Nan::FunctionCallbackInfo<v8::Value>&
Nan::Set(array, static_cast<uint32_t>(i), toJS(optional[i]));
}
info.GetReturnValue().Set(array);
} catch (const std::exception &ex) {
} catch (const std::exception& ex) {
return Nan::ThrowError(ex.what());
}
}
@@ -1462,42 +1485,47 @@ NodeMap::NodeMap(v8::Local<v8::Object> options)
: pixelRatio([&] {
Nan::HandleScope scope;
return Nan::Has(options, Nan::New("ratio").ToLocalChecked()).FromJust()
? static_cast<float>(Nan::To<double>(Nan::Get(options, Nan::New("ratio").ToLocalChecked())
.ToLocalChecked()).ToChecked())
? static_cast<float>(
Nan::To<double>(Nan::Get(options, Nan::New("ratio").ToLocalChecked()).ToLocalChecked())
.ToChecked())
: 1.0f;
}())
, mode([&] {
Nan::HandleScope scope;
if (std::string(*v8::String::Utf8Value(v8::Isolate::GetCurrent(), Nan::Get(options, Nan::New("mode").ToLocalChecked()).ToLocalChecked())) == "tile") {
return mbgl::MapMode::Tile;
} else {
return mbgl::MapMode::Static;
}
}())
, crossSourceCollisions([&] {
Nan::HandleScope scope;
return Nan::Has(options, Nan::New("crossSourceCollisions").ToLocalChecked()).FromJust()
? Nan::To<bool>(Nan::Get(options, Nan::New("crossSourceCollisions").ToLocalChecked())
.ToLocalChecked()).ToChecked()
: true;
}())
, mapObserver(NodeMapObserver())
, frontend(std::make_unique<mbgl::HeadlessFrontend>(mbgl::Size { 512, 512 }, pixelRatio))
, map(std::make_unique<mbgl::Map>(*frontend, mapObserver,
mbgl::MapOptions().withSize(frontend->getSize())
.withPixelRatio(pixelRatio)
.withMapMode(mode)
.withCrossSourceCollisions(crossSourceCollisions),
}()),
mode([&] {
Nan::HandleScope scope;
if (std::string(*v8::String::Utf8Value(
v8::Isolate::GetCurrent(), Nan::Get(options, Nan::New("mode").ToLocalChecked()).ToLocalChecked())) ==
"tile") {
return mbgl::MapMode::Tile;
} else {
return mbgl::MapMode::Static;
}
}()),
crossSourceCollisions([&] {
Nan::HandleScope scope;
return Nan::Has(options, Nan::New("crossSourceCollisions").ToLocalChecked()).FromJust()
? Nan::To<bool>(
Nan::Get(options, Nan::New("crossSourceCollisions").ToLocalChecked()).ToLocalChecked())
.ToChecked()
: true;
}()),
mapObserver(NodeMapObserver()),
frontend(std::make_unique<mbgl::HeadlessFrontend>(mbgl::Size{512, 512}, pixelRatio)),
map(std::make_unique<mbgl::Map>(*frontend,
mapObserver,
mbgl::MapOptions()
.withSize(frontend->getSize())
.withPixelRatio(pixelRatio)
.withMapMode(mode)
.withCrossSourceCollisions(crossSourceCollisions),
mbgl::ResourceOptions().withPlatformContext(reinterpret_cast<void*>(this)),
mbgl::ClientOptions()))
, async(new uv_async_t) {
mbgl::ClientOptions())),
async(new uv_async_t) {
async->data = this;
uv_async_init(uv_default_loop(), async, [](uv_async_t* h) {
reinterpret_cast<NodeMap *>(h->data)->renderFinished();
});
uv_async_init(
uv_default_loop(), async, [](uv_async_t* h) { reinterpret_cast<NodeMap*>(h->data)->renderFinished(); });
// Make sure the async handle doesn't keep the loop alive.
uv_unref(reinterpret_cast<uv_handle_t *>(async));
uv_unref(reinterpret_cast<uv_handle_t*>(async));
}
NodeMap::~NodeMap() {
@@ -1508,24 +1536,24 @@ NodeMap::~NodeMap() {
}
}
std::unique_ptr<mbgl::AsyncRequest> NodeFileSource::request(const mbgl::Resource& resource, mbgl::FileSource::Callback callback_) {
std::unique_ptr<mbgl::AsyncRequest> NodeFileSource::request(const mbgl::Resource& resource,
mbgl::FileSource::Callback callback_) {
assert(nodeMap);
Nan::HandleScope scope;
// Because this method may be called while this NodeMap is already eligible for garbage collection,
// we need to explicitly hold onto our own handle here so that GC during a v8 call doesn't destroy
// *this while we're still executing code.
// Because this method may be called while this NodeMap is already eligible
// for garbage collection, we need to explicitly hold onto our own handle
// here so that GC during a v8 call doesn't destroy *this while we're still
// executing code.
nodeMap->handle();
auto asyncRequest = std::make_unique<node_mbgl::NodeAsyncRequest>();
v8::Local<v8::Value> argv[] = {
Nan::New<v8::External>(nodeMap),
Nan::New<v8::External>(&callback_),
Nan::New<v8::External>(asyncRequest.get()),
Nan::New(resource.url).ToLocalChecked(),
Nan::New<v8::Integer>(resource.kind)
};
v8::Local<v8::Value> argv[] = {Nan::New<v8::External>(nodeMap),
Nan::New<v8::External>(&callback_),
Nan::New<v8::External>(asyncRequest.get()),
Nan::New(resource.url).ToLocalChecked(),
Nan::New<v8::Integer>(resource.kind)};
Nan::NewInstance(Nan::New(node_mbgl::NodeRequest::constructor), 5, argv).ToLocalChecked();
@@ -1537,7 +1565,7 @@ bool NodeFileSource::canRequest(const mbgl::Resource&) const {
}
void NodeFileSource::setResourceOptions(mbgl::ResourceOptions options) {
this->_resourceOptions = std::move(options);
this->_resourceOptions = std::move(options);
}
mbgl::ResourceOptions NodeFileSource::getResourceOptions() {