#pragma once #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-parameter" #pragma GCC diagnostic ignored "-Wshadow" #include #pragma GCC diagnostic pop #include #include #include #include namespace mbgl { namespace style { namespace conversion { template <> class ConversionTraits> { public: static bool isUndefined(const v8::Local& value) { Nan::HandleScope scope; return value->IsUndefined() || value->IsNull(); } static bool isArray(const v8::Local& value) { Nan::HandleScope scope; return value->IsArray(); } static std::size_t arrayLength(const v8::Local& value) { Nan::HandleScope scope; // const_cast because v8::Local::As is not marked const until node v8.0 v8::Local array = const_cast&>(value).As(); return array->Length(); } static v8::Local arrayMember(const v8::Local& value, std::size_t i) { Nan::EscapableHandleScope scope; // const_cast because v8::Local::As is not marked const until node v8.0 v8::Local array = const_cast&>(value).As(); return scope.Escape(Nan::Get(array, i).ToLocalChecked()); } static bool isObject(const v8::Local& value) { Nan::HandleScope scope; return value->IsObject() && !value->IsArray(); } static optional> objectMember(const v8::Local& value, const char * name) { Nan::EscapableHandleScope scope; if (!Nan::Has(Nan::To(value).ToLocalChecked(), Nan::New(name).ToLocalChecked()).FromJust()) { return {}; } Nan::MaybeLocal result = Nan::Get(Nan::To(value).ToLocalChecked(), Nan::New(name).ToLocalChecked()); if (result.IsEmpty()) { return {}; } return {scope.Escape(result.ToLocalChecked())}; } template static optional eachMember(const v8::Local& value, Fn&& fn) { Nan::HandleScope scope; v8::Local names = Nan::GetOwnPropertyNames(Nan::To(value).ToLocalChecked()).ToLocalChecked(); for (uint32_t i = 0; i < names->Length(); ++i) { v8::Local k = Nan::Get(names, i).ToLocalChecked(); v8::Local v = Nan::Get(Nan::To(value).ToLocalChecked(), k).ToLocalChecked(); optional result = fn(*Nan::Utf8String(k), std::move(v)); if (result) { return result; } } return {}; } static optional toBool(const v8::Local& value) { Nan::HandleScope scope; if (!value->IsBoolean()) { return {}; } return value->BooleanValue(); } static optional toNumber(const v8::Local& value) { Nan::HandleScope scope; if (!value->IsNumber()) { return {}; } return value->NumberValue(); } static optional toDouble(const v8::Local& value) { Nan::HandleScope scope; if (!value->IsNumber()) { return {}; } return value->NumberValue(); } static optional toString(const v8::Local& value) { Nan::HandleScope scope; if (!value->IsString()) { return {}; } return std::string(*Nan::Utf8String(value)); } static optional toValue(const v8::Local& value) { if (value->IsFalse()) { return { false }; } else if (value->IsTrue()) { return { true }; } else if (value->IsString()) { return { std::string(*Nan::Utf8String(value)) }; } else if (value->IsUint32()) { return { std::uint64_t(value->Uint32Value()) }; } else if (value->IsInt32()) { return { std::int64_t(value->Int32Value()) }; } else if (value->IsNumber()) { return { value->NumberValue() }; } else { return {}; } } static optional toGeoJSON(const v8::Local& value, Error& error) { try { Nan::JSON JSON; std::string string = *Nan::Utf8String(JSON.Stringify(value->ToObject()).ToLocalChecked()); return parseGeoJSON(string, error); } catch (const std::exception& ex) { error = { ex.what() }; return {}; } } }; template optional convert(const v8::Local& value, Error& error, Args&&...args) { return convert(Convertible(value), error, std::forward(args)...); } } // namespace conversion } // namespace style } // namespace mbgl