summaryrefslogtreecommitdiff
path: root/test/addons-napi/6_object_wrap/myobject.cc
blob: 67521ae3132fc684d64bf34cb92296637a86fb60 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "myobject.h"

napi_ref MyObject::constructor;

MyObject::MyObject(double value)
    : value_(value), env_(nullptr), wrapper_(nullptr) {}

MyObject::~MyObject() { napi_delete_reference(env_, wrapper_); }

void MyObject::Destructor(
  napi_env env, void* nativeObject, void* /*finalize_hint*/) {
  MyObject* obj = static_cast<MyObject*>(nativeObject);
  delete obj;
}

#define DECLARE_NAPI_METHOD(name, func)                          \
  { name, func, 0, 0, 0, napi_default, 0 }

void MyObject::Init(napi_env env, napi_value exports) {
  napi_status status;
  napi_property_descriptor properties[] = {
      { "value", nullptr, GetValue, SetValue, 0, napi_default, 0 },
      DECLARE_NAPI_METHOD("plusOne", PlusOne),
      DECLARE_NAPI_METHOD("multiply", Multiply),
  };

  napi_value cons;
  status =
      napi_define_class(env, "MyObject", New, nullptr, 3, properties, &cons);
  if (status != napi_ok) return;

  status = napi_create_reference(env, cons, 1, &constructor);
  if (status != napi_ok) return;

  status = napi_set_named_property(env, exports, "MyObject", cons);
  if (status != napi_ok) return;
}

void MyObject::New(napi_env env, napi_callback_info info) {
  napi_status status;

  bool is_constructor;
  status = napi_is_construct_call(env, info, &is_constructor);
  if (status != napi_ok) return;

  if (is_constructor) {
    // Invoked as constructor: `new MyObject(...)`
    napi_value args[1];
    status = napi_get_cb_args(env, info, args, 1);
    if (status != napi_ok) return;

    double value = 0;

    napi_valuetype valuetype;
    status = napi_typeof(env, args[0], &valuetype);
    if (status != napi_ok) return;

    if (valuetype != napi_undefined) {
      status = napi_get_value_double(env, args[0], &value);
      if (status != napi_ok) return;
    }

    MyObject* obj = new MyObject(value);

    napi_value jsthis;
    status = napi_get_cb_this(env, info, &jsthis);
    if (status != napi_ok) return;

    obj->env_ = env;
    status = napi_wrap(env,
                       jsthis,
                       obj,
                       MyObject::Destructor,
                       nullptr,  // finalize_hint
                       &obj->wrapper_);
    if (status != napi_ok) return;

    status = napi_set_return_value(env, info, jsthis);
    if (status != napi_ok) return;
  } else {
    // Invoked as plain function `MyObject(...)`, turn into construct call.
    napi_value args[1];
    status = napi_get_cb_args(env, info, args, 1);
    if (status != napi_ok) return;

    const int argc = 1;
    napi_value argv[argc] = {args[0]};

    napi_value cons;
    status = napi_get_reference_value(env, constructor, &cons);
    if (status != napi_ok) return;

    napi_value instance;
    status = napi_new_instance(env, cons, argc, argv, &instance);
    if (status != napi_ok) return;

    status = napi_set_return_value(env, info, instance);
    if (status != napi_ok) return;
  }
}

void MyObject::GetValue(napi_env env, napi_callback_info info) {
  napi_status status;

  napi_value jsthis;
  status = napi_get_cb_this(env, info, &jsthis);
  if (status != napi_ok) return;

  MyObject* obj;
  status = napi_unwrap(env, jsthis, reinterpret_cast<void**>(&obj));
  if (status != napi_ok) return;

  napi_value num;
  status = napi_create_number(env, obj->value_, &num);
  if (status != napi_ok) return;

  status = napi_set_return_value(env, info, num);
  if (status != napi_ok) return;
}

void MyObject::SetValue(napi_env env, napi_callback_info info) {
  napi_status status;

  napi_value value;
  status = napi_get_cb_args(env, info, &value, 1);
  if (status != napi_ok) return;

  napi_value jsthis;
  status = napi_get_cb_this(env, info, &jsthis);
  if (status != napi_ok) return;

  MyObject* obj;
  status = napi_unwrap(env, jsthis, reinterpret_cast<void**>(&obj));
  if (status != napi_ok) return;

  status = napi_get_value_double(env, value, &obj->value_);
  if (status != napi_ok) return;
}

void MyObject::PlusOne(napi_env env, napi_callback_info info) {
  napi_status status;

  napi_value jsthis;
  status = napi_get_cb_this(env, info, &jsthis);
  if (status != napi_ok) return;

  MyObject* obj;
  status = napi_unwrap(env, jsthis, reinterpret_cast<void**>(&obj));
  if (status != napi_ok) return;

  obj->value_ += 1;

  napi_value num;
  status = napi_create_number(env, obj->value_, &num);
  if (status != napi_ok) return;

  status = napi_set_return_value(env, info, num);
  if (status != napi_ok) return;
}

void MyObject::Multiply(napi_env env, napi_callback_info info) {
  napi_status status;

  napi_value args[1];
  status = napi_get_cb_args(env, info, args, 1);
  if (status != napi_ok) return;

  napi_valuetype valuetype;
  status = napi_typeof(env, args[0], &valuetype);
  if (status != napi_ok) return;

  double multiple = 1;
  if (valuetype != napi_undefined) {
    status = napi_get_value_double(env, args[0], &multiple);
    if (status != napi_ok) return;
  }

  napi_value jsthis;
  status = napi_get_cb_this(env, info, &jsthis);
  if (status != napi_ok) return;

  MyObject* obj;
  status = napi_unwrap(env, jsthis, reinterpret_cast<void**>(&obj));
  if (status != napi_ok) return;

  napi_value cons;
  status = napi_get_reference_value(env, constructor, &cons);
  if (status != napi_ok) return;

  const int kArgCount = 1;
  napi_value argv[kArgCount];
  status = napi_create_number(env, obj->value_ * multiple, argv);
  if (status != napi_ok) return;

  napi_value instance;
  status = napi_new_instance(env, cons, kArgCount, argv, &instance);
  if (status != napi_ok) return;

  status = napi_set_return_value(env, info, instance);
  if (status != napi_ok) return;
}