summaryrefslogtreecommitdiff
path: root/platform/node/src/node_conversion.hpp
blob: 03c13b5846d04bea58d87a712565eae6e7f81443 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#pragma once

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wshadow"
#include <nan.h>
#pragma GCC diagnostic pop

#include <mbgl/util/optional.hpp>
#include <mbgl/util/feature.hpp>

namespace mbgl {
namespace style {
namespace conversion {

inline bool isArray(v8::Local<v8::Value> value) {
    Nan::HandleScope scope;
    return value->IsArray();
}

inline std::size_t arrayLength(v8::Local<v8::Value> value) {
    Nan::HandleScope scope;
    return value.As<v8::Array>()->Length();
}

inline v8::Local<v8::Value> arrayMember(v8::Local<v8::Value> value, std::size_t i) {
    Nan::EscapableHandleScope scope;
    return scope.Escape(Nan::Get(value.As<v8::Array>(), i).ToLocalChecked());
}

inline bool isObject(v8::Local<v8::Value> value) {
    Nan::HandleScope scope;
    return value->IsObject();
}

inline optional<v8::Local<v8::Value>> objectMember(v8::Local<v8::Value> value, const char * name) {
    Nan::EscapableHandleScope scope;
    if (!Nan::Has(value.As<v8::Array>(), Nan::New(name).ToLocalChecked()).FromJust()) {
        return {};
    }
    Nan::MaybeLocal<v8::Value> result = Nan::Get(value.As<v8::Array>(), Nan::New(name).ToLocalChecked());
    if (result.IsEmpty()) {
        return {};
    }
    return scope.Escape(result.ToLocalChecked());
}

inline optional<bool> toBool(v8::Local<v8::Value> value) {
    Nan::HandleScope scope;
    if (!value->IsBoolean()) {
        return {};
    }
    return value->BooleanValue();
}

inline optional<float> toNumber(v8::Local<v8::Value> value) {
    Nan::HandleScope scope;
    if (!value->IsNumber()) {
        return {};
    }
    return value->NumberValue();
}

inline optional<std::string> toString(v8::Local<v8::Value> value) {
    Nan::HandleScope scope;
    if (!value->IsString()) {
        return {};
    }
    return std::string(*Nan::Utf8String(value));
}

inline optional<Value> toValue(v8::Local<v8::Value> 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 { value->Uint32Value() };
    } else if (value->IsInt32()) {
        return { value->Int32Value() };
    } else if (value->IsNumber()) {
        return { value->NumberValue() };
    } else {
        return {};
    }
}

} // namespace conversion
} // namespace style
} // namespace mbgl