File: objectwrap_removewrap.cc

package info (click to toggle)
node-addon-api 8.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,248 kB
  • sloc: cpp: 15,431; javascript: 5,631; ansic: 157; makefile: 7
file content (45 lines) | stat: -rw-r--r-- 1,021 bytes parent folder | download | duplicates (3)
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
#include <assert.h>
#include <napi.h>

namespace {

static int dtor_called = 0;

class DtorCounter {
 public:
  ~DtorCounter() {
    assert(dtor_called == 0);
    dtor_called++;
  }
};

Napi::Value GetDtorCalled(const Napi::CallbackInfo& info) {
  return Napi::Number::New(info.Env(), dtor_called);
}

class Test : public Napi::ObjectWrap<Test> {
 public:
  Test(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Test>(info) {
#ifdef NAPI_CPP_EXCEPTIONS
    throw Napi::Error::New(Env(), "Some error");
#else
    Napi::Error::New(Env(), "Some error").ThrowAsJavaScriptException();
#endif
  }

  static void Initialize(Napi::Env env, Napi::Object exports) {
    exports.Set("Test", DefineClass(env, "Test", {}));
    exports.Set("getDtorCalled", Napi::Function::New(env, GetDtorCalled));
  }

 private:
  DtorCounter dtor_counter_;
};

}  // anonymous namespace

Napi::Object InitObjectWrapRemoveWrap(Napi::Env env) {
  Napi::Object exports = Napi::Object::New(env);
  Test::Initialize(env, exports);
  return exports;
}