Feature - Bring back node support (#217)

This PR brings back support for node.js later versions, as well as actually making it work. Maps now render, properly, using node v10, v12, and v16.
This commit is contained in:
Justas Brazauskas
2022-02-08 21:40:19 +02:00
committed by GitHub
parent b7a42e9974
commit cfcbc62fad
25 changed files with 3357 additions and 131 deletions

View File

@@ -17,6 +17,7 @@ namespace node_mbgl {
Nan::Persistent<v8::Function> NodeExpression::constructor;
void NodeExpression::Init(v8::Local<v8::Object> target) {
v8::Local<v8::Context> context = target->CreationContext();
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New("Expression").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1); // what is this doing?
@@ -30,8 +31,8 @@ void NodeExpression::Init(v8::Local<v8::Object> target) {
Nan::SetMethod(tpl, "parse", Parse);
constructor.Reset(tpl->GetFunction()); // what is this doing?
Nan::Set(target, Nan::New("Expression").ToLocalChecked(), tpl->GetFunction());
constructor.Reset(tpl->GetFunction(context).ToLocalChecked()); // what is this doing?
Nan::Set(target, Nan::New("Expression").ToLocalChecked(), tpl->GetFunction(context).ToLocalChecked());
}
type::Type parseType(v8::Local<v8::Object> type) {
@@ -46,10 +47,11 @@ type::Type parseType(v8::Local<v8::Object> type) {
{"resolvedImage", type::Image}};
v8::Local<v8::Value> v8kind = Nan::Get(type, Nan::New("kind").ToLocalChecked()).ToLocalChecked();
std::string kind(*v8::String::Utf8Value(v8kind));
std::string kind(*v8::String::Utf8Value(v8::Isolate::GetCurrent(), v8kind));
if (kind == "array") {
type::Type itemType = parseType(Nan::Get(type, Nan::New("itemType").ToLocalChecked()).ToLocalChecked()->ToObject());
v8::Local<v8::Context> context = type->CreationContext();
type::Type itemType = parseType(Nan::Get(type, Nan::New("itemType").ToLocalChecked()).ToLocalChecked()->ToObject(context).ToLocalChecked());
mbgl::optional<std::size_t> N;
v8::Local<v8::String> Nkey = Nan::New("N").ToLocalChecked();
@@ -64,6 +66,7 @@ type::Type parseType(v8::Local<v8::Object> type) {
void NodeExpression::Parse(const Nan::FunctionCallbackInfo<v8::Value>& info) {
v8::Local<v8::Function> cons = Nan::New(constructor);
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
if (info.Length() < 1 || info[0]->IsUndefined()) {
return Nan::ThrowTypeError("Requires a JSON style expression argument.");
@@ -71,7 +74,7 @@ void NodeExpression::Parse(const Nan::FunctionCallbackInfo<v8::Value>& info) {
mbgl::optional<type::Type> expected;
if (info.Length() > 1 && info[1]->IsObject()) {
expected = parseType(info[1]->ToObject());
expected = parseType(info[1]->ToObject(context).ToLocalChecked());
}
auto success = [&cons, &info](std::unique_ptr<Expression> result) {
@@ -157,8 +160,8 @@ struct ToValue {
v8::Local<v8::Value> operator()(const std::vector<Value>& array) {
Nan::EscapableHandleScope scope;
v8::Local<v8::Array> result = Nan::New<v8::Array>();
for (std::size_t i = 0; i < array.size(); i++) {
result->Set(i, toJS(array[i]));
for (std::uint32_t i = 0; i < array.size(); i++) {
Nan::Set(result, i, toJS(array[i]));
}
return scope.Escape(result);
}
@@ -170,7 +173,7 @@ struct ToValue {
Nan::EscapableHandleScope scope;
return scope.Escape(Nan::Null());
}
v8::Local<v8::Value> operator()(const Formatted& formatted) {
// This mimics the internal structure of the Formatted class in formatted.js
// A better approach might be to use the explicit serialized form
@@ -230,6 +233,7 @@ v8::Local<v8::Value> toJS(const Value& value) {
}
void NodeExpression::Evaluate(const Nan::FunctionCallbackInfo<v8::Value>& info) {
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
auto* nodeExpr = ObjectWrap::Unwrap<NodeExpression>(info.Holder());
const std::unique_ptr<Expression>& expression = nodeExpr->expression;
@@ -238,12 +242,12 @@ void NodeExpression::Evaluate(const Nan::FunctionCallbackInfo<v8::Value>& info)
}
mbgl::optional<float> zoom;
v8::Local<v8::Value> v8zoom = Nan::Get(info[0]->ToObject(), Nan::New("zoom").ToLocalChecked()).ToLocalChecked();
if (v8zoom->IsNumber()) zoom = v8zoom->NumberValue();
v8::Local<v8::Value> v8zoom = Nan::Get(info[0]->ToObject(context).ToLocalChecked(), Nan::New("zoom").ToLocalChecked()).ToLocalChecked();
if (v8zoom->IsNumber()) zoom = Nan::To<double>(v8zoom).FromJust();
mbgl::optional<double> heatmapDensity;
v8::Local<v8::Value> v8heatmapDensity = Nan::Get(info[0]->ToObject(), Nan::New("heatmapDensity").ToLocalChecked()).ToLocalChecked();
if (v8heatmapDensity->IsNumber()) heatmapDensity = v8heatmapDensity->NumberValue();
v8::Local<v8::Value> v8heatmapDensity = Nan::Get(info[0]->ToObject(context).ToLocalChecked(), Nan::New("heatmapDensity").ToLocalChecked()).ToLocalChecked();
if (v8heatmapDensity->IsNumber()) heatmapDensity = Nan::To<double>(v8heatmapDensity).FromJust();
Nan::JSON NanJSON;
conversion::Error conversionError;