summaryrefslogtreecommitdiff
path: root/platform/node/src/node_mapbox_gl_native.cpp
blob: a51e047b927d3f3dcb7c18da56499fe7f7717576 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wshadow"
#include <node.h>
#include <nan.h>
#pragma GCC diagnostic pop

#include <mbgl/util/run_loop.hpp>

#include "node_map.hpp"
#include "node_logging.hpp"
#include "node_request.hpp"

void RegisterModule(v8::Local<v8::Object> target, v8::Local<v8::Object> module) {
    // This has the effect of:
    //   a) Ensuring that the static local variable is initialized before any thread contention.
    //   b) unreffing an async handle, which otherwise would keep the default loop running.
    static mbgl::util::RunLoop nodeRunLoop;
    nodeRunLoop.stop();

    node_mbgl::NodeMap::Init(target);
    node_mbgl::NodeRequest::Init();

    // Exports ResourceKind constants.
    v8::Local<v8::Object> resource = Nan::New<v8::Object>();

    Nan::Set(resource,
        Nan::New("Unknown").ToLocalChecked(),
        Nan::New(static_cast<int32_t>(mbgl::ResourceKind::Unknown)));

    Nan::Set(resource,
        Nan::New("Style").ToLocalChecked(),
        Nan::New(static_cast<int32_t>(mbgl::ResourceKind::Style)));

    Nan::Set(resource,
        Nan::New("Source").ToLocalChecked(),
        Nan::New(static_cast<int32_t>(mbgl::ResourceKind::Source)));

    Nan::Set(resource,
        Nan::New("Tile").ToLocalChecked(),
        Nan::New(static_cast<int32_t>(mbgl::ResourceKind::Tile)));

    Nan::Set(resource,
        Nan::New("Glyphs").ToLocalChecked(),
        Nan::New(static_cast<int32_t>(mbgl::ResourceKind::Glyphs)));

    Nan::Set(resource,
        Nan::New("SpriteImage").ToLocalChecked(),
        Nan::New(static_cast<int32_t>(mbgl::ResourceKind::SpriteImage)));

    Nan::Set(resource,
        Nan::New("SpriteJSON").ToLocalChecked(),
        Nan::New(static_cast<int32_t>(mbgl::ResourceKind::SpriteJSON)));

    Nan::Set(target,
        Nan::New("Resource").ToLocalChecked(),
        resource);

     // Exports ResourceStatus constants.
    v8::Local<v8::Object> errorCode = Nan::New<v8::Object>();

    Nan::Set(errorCode,
        Nan::New("NotFound").ToLocalChecked(),
        Nan::New(static_cast<int32_t>(mbgl::ResourceStatus::NotFoundError)));

    Nan::Set(errorCode,
        Nan::New("Server").ToLocalChecked(),
        Nan::New(static_cast<int32_t>(mbgl::ResourceStatus::ServerError)));

    Nan::Set(errorCode,
        Nan::New("Connection").ToLocalChecked(),
        Nan::New(static_cast<int32_t>(mbgl::ResourceStatus::ConnectionError)));

    Nan::Set(errorCode,
        Nan::New("RateLimit").ToLocalChecked(),
        Nan::New(static_cast<int32_t>(mbgl::ResourceStatus::RateLimitError)));

    Nan::Set(errorCode,
        Nan::New("Other").ToLocalChecked(),
        Nan::New(static_cast<int32_t>(mbgl::ResourceStatus::OtherError)));

    Nan::Set(target,
        Nan::New("ErrorCode").ToLocalChecked(),
        errorCode);

    // Make the exported object inherit from EventEmitter
    v8::Local<v8::Function> require = Nan::Get(module,
        Nan::New("require").ToLocalChecked()).ToLocalChecked().As<v8::Function>();

    v8::Local<v8::Value> eventsString = Nan::New("events").ToLocalChecked();
    v8::Local<v8::Object> events = Nan::To<v8::Object>(Nan::Call(require, module, 1, &eventsString).ToLocalChecked()).ToLocalChecked();

    v8::Local<v8::Object> EventEmitter = Nan::To<v8::Object>(
        Nan::Get(
            events,
            Nan::New("EventEmitter").ToLocalChecked()
        ).ToLocalChecked()
    ).ToLocalChecked();

    Nan::SetPrototype(target,
        Nan::Get(EventEmitter, Nan::New("prototype").ToLocalChecked()).ToLocalChecked());
    Nan::CallAsFunction(EventEmitter, target, 0, nullptr);

    mbgl::Log::setObserver(std::make_unique<node_mbgl::NodeLogObserver>(target));
}

NODE_MODULE(mapbox_gl_native, RegisterModule)