File: threadsafe_function_ctx.cc

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

#if (NAPI_VERSION > 3)

using namespace Napi;

namespace {

class TSFNWrap : public ObjectWrap<TSFNWrap> {
 public:
  static Function Init(Napi::Env env);
  TSFNWrap(const CallbackInfo& info);

  Napi::Value GetContext(const CallbackInfo& /*info*/) {
    Reference<Napi::Value>* ctx = _tsfn.GetContext();
    return ctx->Value();
  };

  Napi::Value Release(const CallbackInfo& info) {
    Napi::Env env = info.Env();
    _deferred = std::unique_ptr<Promise::Deferred>(new Promise::Deferred(env));
    _tsfn.Release();
    return _deferred->Promise();
  };

 private:
  ThreadSafeFunction _tsfn;
  std::unique_ptr<Promise::Deferred> _deferred;
};

Function TSFNWrap::Init(Napi::Env env) {
  Function func =
      DefineClass(env,
                  "TSFNWrap",
                  {InstanceMethod("getContext", &TSFNWrap::GetContext),
                   InstanceMethod("release", &TSFNWrap::Release)});
  return func;
}

TSFNWrap::TSFNWrap(const CallbackInfo& info) : ObjectWrap<TSFNWrap>(info) {
  Napi::Env env = info.Env();

  Reference<Napi::Value>* _ctx = new Reference<Napi::Value>;
  *_ctx = Persistent(info[0]);

  _tsfn = ThreadSafeFunction::New(
      info.Env(),
      Function::New(env, [](const CallbackInfo& /*info*/) {}),
      Object::New(env),
      "Test",
      1,
      1,
      _ctx,
      [this](Napi::Env env, Reference<Napi::Value>* ctx) {
        _deferred->Resolve(env.Undefined());
        ctx->Reset();
        delete ctx;
      });
}
struct SimpleTestContext {
  SimpleTestContext(int val) : _val(val) {}
  int _val = -1;
};

void AssertGetContextFromVariousTSOverloads(const CallbackInfo& info) {
  Env env = info.Env();
  Function emptyFunc;

  SimpleTestContext* ctx = new SimpleTestContext(42);
  ThreadSafeFunction fn =
      ThreadSafeFunction::New(env, emptyFunc, "testResource", 1, 1, ctx);

  assert(fn.GetContext() == ctx);
  delete ctx;
  fn.Release();

  fn = ThreadSafeFunction::New(env, emptyFunc, "testRes", 1, 1, [](Env) {});
  fn.Release();

  ctx = new SimpleTestContext(42);
  fn = ThreadSafeFunction::New(env,
                               emptyFunc,
                               Object::New(env),
                               "resStrObj",
                               1,
                               1,
                               ctx,
                               [](Env, SimpleTestContext*) {});
  assert(fn.GetContext() == ctx);
  delete ctx;
  fn.Release();

  fn = ThreadSafeFunction::New(
      env, emptyFunc, Object::New(env), "resStrObj", 1, 1);
  fn.Release();

  ctx = new SimpleTestContext(42);
  fn = ThreadSafeFunction::New(
      env, emptyFunc, Object::New(env), "resStrObj", 1, 1, ctx);
  assert(fn.GetContext() == ctx);
  delete ctx;
  fn.Release();

  using FinalizerDataType = int;
  FinalizerDataType* finalizerData = new int(42);
  fn = ThreadSafeFunction::New(
      env,
      emptyFunc,
      Object::New(env),
      "resObject",
      1,
      1,
      [](Env, FinalizerDataType* data) {
        assert(*data == 42);
        delete data;
      },
      finalizerData);
  fn.Release();

  ctx = new SimpleTestContext(42);
  FinalizerDataType* finalizerDataB = new int(42);

  fn = ThreadSafeFunction::New(
      env,
      emptyFunc,
      Object::New(env),
      "resObject",
      1,
      1,
      ctx,
      [](Env, FinalizerDataType* _data, SimpleTestContext* _ctx) {
        assert(*_data == 42);
        assert(_ctx->_val == 42);
        delete _data;
        delete _ctx;
      },
      finalizerDataB);
  assert(fn.GetContext() == ctx);
  fn.Release();
}

}  // namespace

Object InitThreadSafeFunctionCtx(Env env) {
  Object exports = Object::New(env);
  Function tsfnWrap = TSFNWrap::Init(env);
  exports.Set("TSFNWrap", tsfnWrap);
  exports.Set("AssertFnReturnCorrectCxt",
              Function::New(env, AssertGetContextFromVariousTSOverloads));

  return exports;
}

#endif