GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_api.cc Lines: 543 588 92.3 %
Date: 2021-05-31 04:11:52 Branches: 235 388 60.6 %

Line Branch Exec Source
1
#include "async_wrap-inl.h"
2
#include "env-inl.h"
3
#define NAPI_EXPERIMENTAL
4
#include "js_native_api_v8.h"
5
#include "memory_tracker-inl.h"
6
#include "node_api.h"
7
#include "node_binding.h"
8
#include "node_buffer.h"
9
#include "node_errors.h"
10
#include "node_internals.h"
11
#include "threadpoolwork-inl.h"
12
#include "tracing/traced_value.h"
13
#include "util-inl.h"
14
15
#include <atomic>
16
#include <memory>
17
18
166
struct node_napi_env__ : public napi_env__ {
19
85
  explicit node_napi_env__(v8::Local<v8::Context> context,
20
85
                           const std::string& module_filename):
21
85
      napi_env__(context), filename(module_filename) {
22
85
    CHECK_NOT_NULL(node_env());
23
85
  }
24
25
121055
  inline node::Environment* node_env() const {
26
121055
    return node::Environment::GetCurrent(context());
27
  }
28
29
118695
  bool can_call_into_js() const override {
30
118695
    return node_env()->can_call_into_js();
31
  }
32
33
  v8::Maybe<bool> mark_arraybuffer_as_untransferable(
34
      v8::Local<v8::ArrayBuffer> ab) const override {
35
    return ab->SetPrivate(
36
        context(),
37
        node_env()->untransferable_object_private_symbol(),
38
        v8::True(isolate));
39
  }
40
41
1013
  void CallFinalizer(napi_finalize cb, void* data, void* hint) override {
42
    // we need to keep the env live until the finalizer has been run
43
    // EnvRefHolder provides an exception safe wrapper to Ref and then
44
    // Unref once the lamba is freed
45
2026
    EnvRefHolder liveEnv(static_cast<napi_env>(this));
46
5065
    node_env()->SetImmediate([=, liveEnv = std::move(liveEnv)]
47
1013
        (node::Environment* node_env) {
48
1013
      napi_env env = liveEnv.env();
49
2026
      v8::HandleScope handle_scope(env->isolate);
50
1013
      v8::Context::Scope context_scope(env->context());
51
3039
      env->CallIntoModule([&](napi_env env) {
52
1013
        cb(env, data, hint);
53
2026
      });
54
2026
    });
55
1013
  }
56
57
1
  const char* GetFilename() const { return filename.c_str(); }
58
59
  std::string filename;
60
};
61
62
typedef node_napi_env__* node_napi_env;
63
64
namespace v8impl {
65
66
namespace {
67
68
class BufferFinalizer : private Finalizer {
69
 public:
70
  // node::Buffer::FreeCallback
71
13
  static void FinalizeBufferCallback(char* data, void* hint) {
72
    std::unique_ptr<BufferFinalizer, Deleter> finalizer{
73
26
        static_cast<BufferFinalizer*>(hint)};
74
13
    finalizer->_finalize_data = data;
75
76
    node::Environment* node_env =
77
13
        static_cast<node_napi_env>(finalizer->_env)->node_env();
78
26
    node_env->SetImmediate(
79
65
        [finalizer = std::move(finalizer)](node::Environment* env) {
80
13
      if (finalizer->_finalize_callback == nullptr) return;
81
82
24
      v8::HandleScope handle_scope(finalizer->_env->isolate);
83
12
      v8::Context::Scope context_scope(finalizer->_env->context());
84
85
36
      finalizer->_env->CallIntoModule([&](napi_env env) {
86
36
        finalizer->_finalize_callback(
87
            env,
88
12
            finalizer->_finalize_data,
89
24
            finalizer->_finalize_hint);
90
24
      });
91
13
    });
92
13
  }
93
94
  struct Deleter {
95
13
    void operator()(BufferFinalizer* finalizer) {
96
13
      Finalizer::Delete(finalizer);
97
13
    }
98
  };
99
};
100
101
static inline napi_env
102
85
NewEnv(v8::Local<v8::Context> context, const std::string& module_filename) {
103
  node_napi_env result;
104
105
85
  result = new node_napi_env__(context, module_filename);
106
  // TODO(addaleax): There was previously code that tried to delete the
107
  // napi_env when its v8::Context was garbage collected;
108
  // However, as long as N-API addons using this napi_env are in place,
109
  // the Context needs to be accessible and alive.
110
  // Ideally, we'd want an on-addon-unload hook that takes care of this
111
  // once all N-API addons using this napi_env are unloaded.
112
  // For now, a per-Environment cleanup hook is the best we can do.
113
170
  result->node_env()->AddCleanupHook(
114
251
      [](void* arg) {
115
83
        static_cast<napi_env>(arg)->Unref();
116
251
      },
117
85
      static_cast<void*>(result));
118
119
85
  return result;
120
}
121
122
3
static inline void trigger_fatal_exception(
123
    napi_env env, v8::Local<v8::Value> local_err) {
124
  v8::Local<v8::Message> local_msg =
125
3
    v8::Exception::CreateMessage(env->isolate, local_err);
126
3
  node::errors::TriggerUncaughtException(env->isolate, local_err, local_msg);
127
2
}
128
129
class ThreadSafeFunction : public node::AsyncResource {
130
 public:
131
18
  ThreadSafeFunction(v8::Local<v8::Function> func,
132
                     v8::Local<v8::Object> resource,
133
                     v8::Local<v8::String> name,
134
                     size_t thread_count_,
135
                     void* context_,
136
                     size_t max_queue_size_,
137
                     node_napi_env env_,
138
                     void* finalize_data_,
139
                     napi_finalize finalize_cb_,
140
18
                     napi_threadsafe_function_call_js call_js_cb_):
141
18
                     AsyncResource(env_->isolate,
142
                                   resource,
143
36
                                   *v8::String::Utf8Value(env_->isolate, name)),
144
      thread_count(thread_count_),
145
      is_closing(false),
146
      dispatch_state(kDispatchIdle),
147
      context(context_),
148
      max_queue_size(max_queue_size_),
149
      env(env_),
150
      finalize_data(finalize_data_),
151
      finalize_cb(finalize_cb_),
152
18
      call_js_cb(call_js_cb_ == nullptr ? CallJs : call_js_cb_),
153
72
      handles_closing(false) {
154
18
    ref.Reset(env->isolate, func);
155
18
    node::AddEnvironmentCleanupHook(env->isolate, Cleanup, this);
156
18
    env->Ref();
157
18
  }
158
159
72
  ~ThreadSafeFunction() override {
160
18
    node::RemoveEnvironmentCleanupHook(env->isolate, Cleanup, this);
161
18
    env->Unref();
162
36
  }
163
164
  // These methods can be called from any thread.
165
166
13297126
  napi_status Push(void* data, napi_threadsafe_function_call_mode mode) {
167
26594252
    node::Mutex::ScopedLock lock(this->mutex);
168
169

39886023
    while (queue.size() >= max_queue_size &&
170

26523593
        max_queue_size > 0 &&
171
13204817
        !is_closing) {
172
13204814
      if (mode == napi_tsfn_nonblocking) {
173
13183164
        return napi_queue_full;
174
      }
175
21650
      cond->Wait(lock);
176
    }
177
178
113962
    if (is_closing) {
179
5
      if (thread_count == 0) {
180
        return napi_invalid_arg;
181
      } else {
182
5
        thread_count--;
183
5
        return napi_closing;
184
      }
185
    } else {
186
113957
      queue.push(data);
187
113957
      Send();
188
113957
      return napi_ok;
189
    }
190
  }
191
192
4
  napi_status Acquire() {
193
8
    node::Mutex::ScopedLock lock(this->mutex);
194
195
4
    if (is_closing) {
196
      return napi_closing;
197
    }
198
199
4
    thread_count++;
200
201
4
    return napi_ok;
202
  }
203
204
33
  napi_status Release(napi_threadsafe_function_release_mode mode) {
205
66
    node::Mutex::ScopedLock lock(this->mutex);
206
207
33
    if (thread_count == 0) {
208
      return napi_invalid_arg;
209
    }
210
211
33
    thread_count--;
212
213

33
    if (thread_count == 0 || mode == napi_tsfn_abort) {
214
16
      if (!is_closing) {
215
16
        is_closing = (mode == napi_tsfn_abort);
216

16
        if (is_closing && max_queue_size > 0) {
217
2
          cond->Signal(lock);
218
        }
219
16
        Send();
220
      }
221
    }
222
223
33
    return napi_ok;
224
  }
225
226
25
  void EmptyQueueAndDelete() {
227
32
    for (; !queue.empty() ; queue.pop()) {
228
7
      call_js_cb(nullptr, nullptr, context, queue.front());
229
    }
230
18
    delete this;
231
18
  }
232
233
  // These methods must only be called from the loop thread.
234
235
18
  napi_status Init() {
236
18
    ThreadSafeFunction* ts_fn = this;
237
18
    uv_loop_t* loop = env->node_env()->event_loop();
238
239
18
    if (uv_async_init(loop, &async, AsyncCb) == 0) {
240
18
      if (max_queue_size > 0) {
241
12
        cond = std::make_unique<node::ConditionVariable>();
242
      }
243

18
      if (max_queue_size == 0 || cond) {
244
18
        return napi_ok;
245
      }
246
247
      env->node_env()->CloseHandle(
248
          reinterpret_cast<uv_handle_t*>(&async),
249
          [](uv_handle_t* handle) -> void {
250
            ThreadSafeFunction* ts_fn =
251
                node::ContainerOf(&ThreadSafeFunction::async,
252
                                  reinterpret_cast<uv_async_t*>(handle));
253
            delete ts_fn;
254
          });
255
256
      // Prevent the thread-safe function from being deleted here, because
257
      // the callback above will delete it.
258
      ts_fn = nullptr;
259
    }
260
261
    delete ts_fn;
262
263
    return napi_generic_failure;
264
  }
265
266
2
  napi_status Unref() {
267
2
    uv_unref(reinterpret_cast<uv_handle_t*>(&async));
268
269
2
    return napi_ok;
270
  }
271
272
  napi_status Ref() {
273
    uv_ref(reinterpret_cast<uv_handle_t*>(&async));
274
275
    return napi_ok;
276
  }
277
278
16
  inline void* Context() {
279
16
    return context;
280
  }
281
282
 protected:
283
10255
  void Dispatch() {
284
10255
    bool has_more = true;
285
286
    // Limit maximum synchronous iteration count to prevent event loop
287
    // starvation. See `src/node_messaging.cc` for an inspiration.
288
10255
    unsigned int iterations_left = kMaxIterationCount;
289

238177
    while (has_more && --iterations_left != 0) {
290
113961
      dispatch_state = kDispatchRunning;
291
113961
      has_more = DispatchOne();
292
293
      // Send() was called while we were executing the JS function
294
227922
      if (dispatch_state.exchange(kDispatchIdle) != kDispatchRunning) {
295
49948
        has_more = true;
296
      }
297
    }
298
299
10255
    if (has_more) {
300
42
      Send();
301
    }
302
10255
  }
303
304
113961
  bool DispatchOne() {
305
113961
    void* data = nullptr;
306
113961
    bool popped_value = false;
307
113961
    bool has_more = false;
308
309
    {
310
227922
      node::Mutex::ScopedLock lock(this->mutex);
311
113961
      if (is_closing) {
312
3
        CloseHandlesAndMaybeDelete();
313
      } else {
314
113958
        size_t size = queue.size();
315
113958
        if (size > 0) {
316
113950
          data = queue.front();
317
113950
          queue.pop();
318
113950
          popped_value = true;
319

113950
          if (size == max_queue_size && max_queue_size > 0) {
320
51535
            cond->Signal(lock);
321
          }
322
113950
          size--;
323
        }
324
325
113958
        if (size == 0) {
326
32024
          if (thread_count == 0) {
327
13
            is_closing = true;
328
13
            if (max_queue_size > 0) {
329
9
              cond->Signal(lock);
330
            }
331
13
            CloseHandlesAndMaybeDelete();
332
          }
333
        } else {
334
81934
          has_more = true;
335
        }
336
      }
337
    }
338
339
113961
    if (popped_value) {
340
227900
      v8::HandleScope scope(env->isolate);
341
227900
      CallbackScope cb_scope(this);
342
113950
      napi_value js_callback = nullptr;
343
227900
      if (!ref.IsEmpty()) {
344
        v8::Local<v8::Function> js_cb =
345
207896
          v8::Local<v8::Function>::New(env->isolate, ref);
346
103948
        js_callback = v8impl::JsValueFromV8LocalValue(js_cb);
347
      }
348
341850
      env->CallIntoModule([&](napi_env env) {
349
113950
        call_js_cb(env, js_callback, context, data);
350
227900
      });
351
    }
352
353
113961
    return has_more;
354
  }
355
356
18
  void Finalize() {
357
36
    v8::HandleScope scope(env->isolate);
358
18
    if (finalize_cb) {
359
36
      CallbackScope cb_scope(this);
360
54
      env->CallIntoModule([&](napi_env env) {
361
18
        finalize_cb(env, finalize_data, context);
362
36
      });
363
    }
364
18
    EmptyQueueAndDelete();
365
18
  }
366
367
18
  void CloseHandlesAndMaybeDelete(bool set_closing = false) {
368
36
    v8::HandleScope scope(env->isolate);
369
18
    if (set_closing) {
370
4
      node::Mutex::ScopedLock lock(this->mutex);
371
2
      is_closing = true;
372
2
      if (max_queue_size > 0) {
373
1
        cond->Signal(lock);
374
      }
375
    }
376
18
    if (handles_closing) {
377
      return;
378
    }
379
18
    handles_closing = true;
380
36
    env->node_env()->CloseHandle(
381
18
        reinterpret_cast<uv_handle_t*>(&async),
382
18
        [](uv_handle_t* handle) -> void {
383
          ThreadSafeFunction* ts_fn =
384
36
              node::ContainerOf(&ThreadSafeFunction::async,
385
18
                                reinterpret_cast<uv_async_t*>(handle));
386
18
          ts_fn->Finalize();
387
36
        });
388
  }
389
390
114015
  void Send() {
391
    // Ask currently running Dispatch() to make one more iteration
392
228030
    unsigned char current_state = dispatch_state.fetch_or(kDispatchPending);
393
114015
    if ((current_state & kDispatchRunning) == kDispatchRunning) {
394
80331
      return;
395
    }
396
397
33684
    CHECK_EQ(0, uv_async_send(&async));
398
  }
399
400
  // Default way of calling into JavaScript. Used when ThreadSafeFunction is
401
  //  without a call_js_cb_.
402
10000
  static void CallJs(napi_env env, napi_value cb, void* context, void* data) {
403

10000
    if (!(env == nullptr || cb == nullptr)) {
404
      napi_value recv;
405
      napi_status status;
406
407
10000
      status = napi_get_undefined(env, &recv);
408
10000
      if (status != napi_ok) {
409
        napi_throw_error(env, "ERR_NAPI_TSFN_GET_UNDEFINED",
410
            "Failed to retrieve undefined value");
411
        return;
412
      }
413
414
10000
      status = napi_call_function(env, recv, cb, 0, nullptr, nullptr);
415

10000
      if (status != napi_ok && status != napi_pending_exception) {
416
        napi_throw_error(env, "ERR_NAPI_TSFN_CALL_JS",
417
            "Failed to call JS callback");
418
        return;
419
      }
420
    }
421
  }
422
423
10255
  static void AsyncCb(uv_async_t* async) {
424
    ThreadSafeFunction* ts_fn =
425
10255
        node::ContainerOf(&ThreadSafeFunction::async, async);
426
10255
    ts_fn->Dispatch();
427
10255
  }
428
429
2
  static void Cleanup(void* data) {
430
    reinterpret_cast<ThreadSafeFunction*>(data)
431
2
        ->CloseHandlesAndMaybeDelete(true);
432
2
  }
433
434
 private:
435
  static const unsigned char kDispatchIdle = 0;
436
  static const unsigned char kDispatchRunning = 1 << 0;
437
  static const unsigned char kDispatchPending = 1 << 1;
438
439
  static const unsigned int kMaxIterationCount = 1000;
440
441
  // These are variables protected by the mutex.
442
  node::Mutex mutex;
443
  std::unique_ptr<node::ConditionVariable> cond;
444
  std::queue<void*> queue;
445
  uv_async_t async;
446
  size_t thread_count;
447
  bool is_closing;
448
  std::atomic_uchar dispatch_state;
449
450
  // These are variables set once, upon creation, and then never again, which
451
  // means we don't need the mutex to read them.
452
  void* context;
453
  size_t max_queue_size;
454
455
  // These are variables accessed only from the loop thread.
456
  v8impl::Persistent<v8::Function> ref;
457
  node_napi_env env;
458
  void* finalize_data;
459
  napi_finalize finalize_cb;
460
  napi_threadsafe_function_call_js call_js_cb;
461
  bool handles_closing;
462
};
463
464
/**
465
 * Compared to node::AsyncResource, the resource object in AsyncContext is
466
 * gc-able. AsyncContext holds a weak reference to the resource object.
467
 * AsyncContext::MakeCallback doesn't implicitly set the receiver of the
468
 * callback to the resource object.
469
 */
470
class AsyncContext {
471
 public:
472
13
  AsyncContext(node_napi_env env,
473
               v8::Local<v8::Object> resource_object,
474
               const v8::Local<v8::String> resource_name,
475
               bool externally_managed_resource)
476
13
      : env_(env) {
477
13
    async_id_ = node_env()->new_async_id();
478
13
    trigger_async_id_ = node_env()->get_default_trigger_async_id();
479
13
    resource_.Reset(node_env()->isolate(), resource_object);
480
13
    lost_reference_ = false;
481
13
    if (externally_managed_resource) {
482
11
      resource_.SetWeak(
483
          this, AsyncContext::WeakCallback, v8::WeakCallbackType::kParameter);
484
    }
485
486
13
    node::AsyncWrap::EmitAsyncInit(node_env(),
487
                                   resource_object,
488
                                   resource_name,
489
                                   async_id_,
490
13
                                   trigger_async_id_);
491
13
  }
492
493
39
  ~AsyncContext() {
494
13
    resource_.Reset();
495
13
    lost_reference_ = true;
496
13
    node::AsyncWrap::EmitDestroy(node_env(), async_id_);
497
13
  }
498
499
8
  inline v8::MaybeLocal<v8::Value> MakeCallback(
500
      v8::Local<v8::Object> recv,
501
      const v8::Local<v8::Function> callback,
502
      int argc,
503
      v8::Local<v8::Value> argv[]) {
504
8
    EnsureReference();
505
    return node::InternalMakeCallback(node_env(),
506
                                      resource(),
507
                                      recv,
508
                                      callback,
509
                                      argc,
510
                                      argv,
511
8
                                      {async_id_, trigger_async_id_});
512
  }
513
514
4
  inline napi_callback_scope OpenCallbackScope() {
515
4
    EnsureReference();
516
    napi_callback_scope it =
517
4
        reinterpret_cast<napi_callback_scope>(new CallbackScope(this));
518
4
    env_->open_callback_scopes++;
519
4
    return it;
520
  }
521
522
12
  inline void EnsureReference() {
523
12
    if (lost_reference_) {
524
2
      const v8::HandleScope handle_scope(node_env()->isolate());
525
1
      resource_.Reset(node_env()->isolate(),
526
2
                      v8::Object::New(node_env()->isolate()));
527
1
      lost_reference_ = false;
528
    }
529
12
  }
530
531
92
  inline node::Environment* node_env() { return env_->node_env(); }
532
8
  inline v8::Local<v8::Object> resource() {
533
16
    return resource_.Get(node_env()->isolate());
534
  }
535
4
  inline node::async_context async_context() {
536
4
    return {async_id_, trigger_async_id_};
537
  }
538
539
4
  static inline void CloseCallbackScope(node_napi_env env,
540
                                        napi_callback_scope s) {
541
4
    CallbackScope* callback_scope = reinterpret_cast<CallbackScope*>(s);
542
4
    delete callback_scope;
543
4
    env->open_callback_scopes--;
544
4
  }
545
546
2
  static void WeakCallback(const v8::WeakCallbackInfo<AsyncContext>& data) {
547
2
    AsyncContext* async_context = data.GetParameter();
548
2
    async_context->resource_.Reset();
549
2
    async_context->lost_reference_ = true;
550
2
  }
551
552
 private:
553
4
  class CallbackScope : public node::CallbackScope {
554
   public:
555
4
    explicit CallbackScope(AsyncContext* async_context)
556
4
        : node::CallbackScope(async_context->node_env()->isolate(),
557
                              async_context->resource_.Get(
558
                                  async_context->node_env()->isolate()),
559
8
                              async_context->async_context()) {}
560
  };
561
562
  node_napi_env env_;
563
  double async_id_;
564
  double trigger_async_id_;
565
  v8::Global<v8::Object> resource_;
566
  bool lost_reference_;
567
};
568
569
}  // end of anonymous namespace
570
571
}  // end of namespace v8impl
572
573
// Intercepts the Node-V8 module registration callback. Converts parameters
574
// to NAPI equivalents and then calls the registration callback specified
575
// by the NAPI module.
576
83
static void napi_module_register_cb(v8::Local<v8::Object> exports,
577
                                    v8::Local<v8::Value> module,
578
                                    v8::Local<v8::Context> context,
579
                                    void* priv) {
580
  napi_module_register_by_symbol(exports, module, context,
581
83
      static_cast<const napi_module*>(priv)->nm_register_func);
582
83
}
583
584
86
void napi_module_register_by_symbol(v8::Local<v8::Object> exports,
585
                                    v8::Local<v8::Value> module,
586
                                    v8::Local<v8::Context> context,
587
                                    napi_addon_register_func init) {
588
86
  node::Environment* node_env = node::Environment::GetCurrent(context);
589
171
  std::string module_filename = "";
590
86
  if (init == nullptr) {
591
1
    CHECK_NOT_NULL(node_env);
592
1
    node_env->ThrowError(
593
1
        "Module has no declared entry point.");
594
1
    return;
595
  }
596
597
  // We set `env->filename` from `module.filename` here, but we could just as
598
  // easily add a private property to `exports` in `process.dlopen`, which
599
  // receives the file name from JS, and retrieve *that* here. Thus, we are not
600
  // endorsing commonjs here by making use of `module.filename`.
601
  v8::Local<v8::Value> filename_js;
602
  v8::Local<v8::Object> modobj;
603

425
  if (module->ToObject(context).ToLocal(&modobj) &&
604

425
      modobj->Get(context, node_env->filename_string()).ToLocal(&filename_js) &&
605
170
      filename_js->IsString()) {
606
166
    node::Utf8Value filename(node_env->isolate(), filename_js);  // Cast
607
608
    // Turn the absolute path into a URL. Currently the absolute path is always
609
    // a file system path.
610
    // TODO(gabrielschulhof): Pass the `filename` through unchanged if/when we
611
    // receive it as a URL already.
612
83
    module_filename = std::string("file://") + (*filename);
613
  }
614
615
  // Create a new napi_env for this specific module.
616
85
  napi_env env = v8impl::NewEnv(context, module_filename);
617
618
  napi_value _exports;
619
255
  env->CallIntoModule([&](napi_env env) {
620
170
    _exports = init(env, v8impl::JsValueFromV8LocalValue(exports));
621
170
  });
622
623
  // If register function returned a non-null exports object different from
624
  // the exports object we passed it, set that as the "exports" property of
625
  // the module.
626

250
  if (_exports != nullptr &&
627
165
      _exports != v8impl::JsValueFromV8LocalValue(exports)) {
628
4
    napi_value _module = v8impl::JsValueFromV8LocalValue(module);
629
4
    napi_set_named_property(env, _module, "exports", _exports);
630
  }
631
}
632
633
namespace node {
634
82
node_module napi_module_to_node_module(const napi_module* mod) {
635
  return {
636
    -1,
637
82
    mod->nm_flags | NM_F_DELETEME,
638
    nullptr,
639
82
    mod->nm_filename,
640
    nullptr,
641
    napi_module_register_cb,
642
82
    mod->nm_modname,
643
    const_cast<napi_module*>(mod),  // priv
644
    nullptr,
645
328
  };
646
}
647
}  // namespace node
648
649
// Registers a NAPI module.
650
80
void napi_module_register(napi_module* mod) {
651
  node::node_module* nm = new node::node_module(
652
80
      node::napi_module_to_node_module(mod));
653
80
  node::node_module_register(nm);
654
80
}
655
656
2
napi_status napi_add_env_cleanup_hook(napi_env env,
657
                                      void (*fun)(void* arg),
658
                                      void* arg) {
659
2
  CHECK_ENV(env);
660
2
  CHECK_ARG(env, fun);
661
662
2
  node::AddEnvironmentCleanupHook(env->isolate, fun, arg);
663
664
2
  return napi_ok;
665
}
666
667
1
napi_status napi_remove_env_cleanup_hook(napi_env env,
668
                                         void (*fun)(void* arg),
669
                                         void* arg) {
670
1
  CHECK_ENV(env);
671
1
  CHECK_ARG(env, fun);
672
673
1
  node::RemoveEnvironmentCleanupHook(env->isolate, fun, arg);
674
675
1
  return napi_ok;
676
}
677
678
struct napi_async_cleanup_hook_handle__ {
679
6
  napi_async_cleanup_hook_handle__(napi_env env,
680
                                   napi_async_cleanup_hook user_hook,
681
6
                                   void* user_data):
682
      env_(env),
683
      user_hook_(user_hook),
684
6
      user_data_(user_data) {
685
6
    handle_ = node::AddEnvironmentCleanupHook(env->isolate, Hook, this);
686
6
    env->Ref();
687
6
  }
688
689
12
  ~napi_async_cleanup_hook_handle__() {
690
6
    node::RemoveEnvironmentCleanupHook(std::move(handle_));
691
6
    if (done_cb_ != nullptr)
692
4
      done_cb_(done_data_);
693
694
    // Release the `env` handle asynchronously since it would be surprising if
695
    // a call to a N-API function would destroy `env` synchronously.
696
6
    static_cast<node_napi_env>(env_)->node_env()
697
18
        ->SetImmediate([env = env_](node::Environment*) { env->Unref(); });
698
6
  }
699
700
4
  static void Hook(void* data, void (*done_cb)(void*), void* done_data) {
701
4
    auto handle = static_cast<napi_async_cleanup_hook_handle__*>(data);
702
4
    handle->done_cb_ = done_cb;
703
4
    handle->done_data_ = done_data;
704
4
    handle->user_hook_(handle, handle->user_data_);
705
4
  }
706
707
  node::AsyncCleanupHookHandle handle_;
708
  napi_env env_ = nullptr;
709
  napi_async_cleanup_hook user_hook_ = nullptr;
710
  void* user_data_ = nullptr;
711
  void (*done_cb_)(void*) = nullptr;
712
  void* done_data_ = nullptr;
713
};
714
715
6
napi_status napi_add_async_cleanup_hook(
716
    napi_env env,
717
    napi_async_cleanup_hook hook,
718
    void* arg,
719
    napi_async_cleanup_hook_handle* remove_handle) {
720
6
  CHECK_ENV(env);
721
6
  CHECK_ARG(env, hook);
722
723
  napi_async_cleanup_hook_handle__* handle =
724
6
    new napi_async_cleanup_hook_handle__(env, hook, arg);
725
726
6
  if (remove_handle != nullptr)
727
4
    *remove_handle = handle;
728
729
6
  return napi_clear_last_error(env);
730
}
731
732
6
napi_status napi_remove_async_cleanup_hook(
733
    napi_async_cleanup_hook_handle remove_handle) {
734
735
6
  if (remove_handle == nullptr)
736
    return napi_invalid_arg;
737
738
6
  delete remove_handle;
739
740
6
  return napi_ok;
741
}
742
743
1
napi_status napi_fatal_exception(napi_env env, napi_value err) {
744


3
  NAPI_PREAMBLE(env);
745
1
  CHECK_ARG(env, err);
746
747
1
  v8::Local<v8::Value> local_err = v8impl::V8LocalValueFromJsValue(err);
748
1
  v8impl::trigger_fatal_exception(env, local_err);
749
750
1
  return napi_clear_last_error(env);
751
}
752
753
NAPI_NO_RETURN void napi_fatal_error(const char* location,
754
                                     size_t location_len,
755
                                     const char* message,
756
                                     size_t message_len) {
757
  std::string location_string;
758
  std::string message_string;
759
760
  if (location_len != NAPI_AUTO_LENGTH) {
761
    location_string.assign(
762
        const_cast<char*>(location), location_len);
763
  } else {
764
    location_string.assign(
765
        const_cast<char*>(location), strlen(location));
766
  }
767
768
  if (message_len != NAPI_AUTO_LENGTH) {
769
    message_string.assign(
770
        const_cast<char*>(message), message_len);
771
  } else {
772
    message_string.assign(
773
        const_cast<char*>(message), strlen(message));
774
  }
775
776
  node::FatalError(location_string.c_str(), message_string.c_str());
777
}
778
779
4
napi_status napi_open_callback_scope(napi_env env,
780
                                     napi_value /** ignored */,
781
                                     napi_async_context async_context_handle,
782
                                     napi_callback_scope* result) {
783
  // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw
784
  // JS exceptions.
785
4
  CHECK_ENV(env);
786
4
  CHECK_ARG(env, result);
787
788
  v8impl::AsyncContext* node_async_context =
789
4
      reinterpret_cast<v8impl::AsyncContext*>(async_context_handle);
790
791
4
  *result = node_async_context->OpenCallbackScope();
792
793
4
  return napi_clear_last_error(env);
794
}
795
796
4
napi_status napi_close_callback_scope(napi_env env, napi_callback_scope scope) {
797
  // Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw
798
  // JS exceptions.
799
4
  CHECK_ENV(env);
800
4
  CHECK_ARG(env, scope);
801
4
  if (env->open_callback_scopes == 0) {
802
    return napi_callback_scope_mismatch;
803
  }
804
805
4
  v8impl::AsyncContext::CloseCallbackScope(reinterpret_cast<node_napi_env>(env),
806
8
                                           scope);
807
808
4
  return napi_clear_last_error(env);
809
}
810
811
13
napi_status napi_async_init(napi_env env,
812
                            napi_value async_resource,
813
                            napi_value async_resource_name,
814
                            napi_async_context* result) {
815
13
  CHECK_ENV(env);
816
13
  CHECK_ARG(env, async_resource_name);
817
13
  CHECK_ARG(env, result);
818
819
13
  v8::Isolate* isolate = env->isolate;
820
13
  v8::Local<v8::Context> context = env->context();
821
822
  v8::Local<v8::Object> v8_resource;
823
  bool externally_managed_resource;
824
13
  if (async_resource != nullptr) {
825

44
    CHECK_TO_OBJECT(env, context, v8_resource, async_resource);
826
11
    externally_managed_resource = true;
827
  } else {
828
2
    v8_resource = v8::Object::New(isolate);
829
2
    externally_managed_resource = false;
830
  }
831
832
  v8::Local<v8::String> v8_resource_name;
833

52
  CHECK_TO_STRING(env, context, v8_resource_name, async_resource_name);
834
835
  auto async_context =
836
      new v8impl::AsyncContext(reinterpret_cast<node_napi_env>(env),
837
                               v8_resource,
838
                               v8_resource_name,
839
13
                               externally_managed_resource);
840
841
13
  *result = reinterpret_cast<napi_async_context>(async_context);
842
843
13
  return napi_clear_last_error(env);
844
}
845
846
13
napi_status napi_async_destroy(napi_env env,
847
                               napi_async_context async_context) {
848
13
  CHECK_ENV(env);
849
13
  CHECK_ARG(env, async_context);
850
851
  v8impl::AsyncContext* node_async_context =
852
13
      reinterpret_cast<v8impl::AsyncContext*>(async_context);
853
854
13
  delete node_async_context;
855
856
13
  return napi_clear_last_error(env);
857
}
858
859
25
napi_status napi_make_callback(napi_env env,
860
                               napi_async_context async_context,
861
                               napi_value recv,
862
                               napi_value func,
863
                               size_t argc,
864
                               const napi_value* argv,
865
                               napi_value* result) {
866


75
  NAPI_PREAMBLE(env);
867
25
  CHECK_ARG(env, recv);
868
25
  if (argc > 0) {
869
4
    CHECK_ARG(env, argv);
870
  }
871
872
25
  v8::Local<v8::Context> context = env->context();
873
874
  v8::Local<v8::Object> v8recv;
875

100
  CHECK_TO_OBJECT(env, context, v8recv, recv);
876
877
  v8::Local<v8::Function> v8func;
878

75
  CHECK_TO_FUNCTION(env, v8func, func);
879
880
  v8::MaybeLocal<v8::Value> callback_result;
881
882
25
  if (async_context == nullptr) {
883
    callback_result = node::MakeCallback(
884
17
        env->isolate,
885
        v8recv,
886
        v8func,
887
        argc,
888
        reinterpret_cast<v8::Local<v8::Value>*>(const_cast<napi_value*>(argv)),
889
34
        {0, 0});
890
  } else {
891
    auto node_async_context =
892
8
        reinterpret_cast<v8impl::AsyncContext*>(async_context);
893
    callback_result = node_async_context->MakeCallback(
894
        v8recv,
895
        v8func,
896
        argc,
897
8
        reinterpret_cast<v8::Local<v8::Value>*>(const_cast<napi_value*>(argv)));
898
  }
899
900
25
  if (try_catch.HasCaught()) {
901
4
    return napi_set_last_error(env, napi_pending_exception);
902
  } else {
903
21
    CHECK_MAYBE_EMPTY(env, callback_result, napi_generic_failure);
904
21
    if (result != nullptr) {
905
8
      *result = v8impl::JsValueFromV8LocalValue(
906
          callback_result.ToLocalChecked());
907
    }
908
  }
909
910
21
  return GET_RETURN_STATUS(env);
911
}
912
913
1
napi_status napi_create_buffer(napi_env env,
914
                               size_t length,
915
                               void** data,
916
                               napi_value* result) {
917


3
  NAPI_PREAMBLE(env);
918
1
  CHECK_ARG(env, result);
919
920
1
  auto maybe = node::Buffer::New(env->isolate, length);
921
922
1
  CHECK_MAYBE_EMPTY(env, maybe, napi_generic_failure);
923
924
1
  v8::Local<v8::Object> buffer = maybe.ToLocalChecked();
925
926
2
  *result = v8impl::JsValueFromV8LocalValue(buffer);
927
928
1
  if (data != nullptr) {
929
1
    *data = node::Buffer::Data(buffer);
930
  }
931
932
1
  return GET_RETURN_STATUS(env);
933
}
934
935
13
napi_status napi_create_external_buffer(napi_env env,
936
                                        size_t length,
937
                                        void* data,
938
                                        napi_finalize finalize_cb,
939
                                        void* finalize_hint,
940
                                        napi_value* result) {
941


39
  NAPI_PREAMBLE(env);
942
13
  CHECK_ARG(env, result);
943
944
13
  v8::Isolate* isolate = env->isolate;
945
946
  // The finalizer object will delete itself after invoking the callback.
947
13
  v8impl::Finalizer* finalizer = v8impl::Finalizer::New(
948
      env, finalize_cb, nullptr, finalize_hint,
949
13
      v8impl::Finalizer::kKeepEnvReference);
950
951
  auto maybe = node::Buffer::New(isolate,
952
                                static_cast<char*>(data),
953
                                length,
954
                                v8impl::BufferFinalizer::FinalizeBufferCallback,
955
13
                                finalizer);
956
957
13
  CHECK_MAYBE_EMPTY(env, maybe, napi_generic_failure);
958
959
26
  *result = v8impl::JsValueFromV8LocalValue(maybe.ToLocalChecked());
960
13
  return GET_RETURN_STATUS(env);
961
  // Tell coverity that 'finalizer' should not be freed when we return
962
  // as it will be deleted when the buffer to which it is associated
963
  // is finalized.
964
  // coverity[leaked_storage]
965
}
966
967
1
napi_status napi_create_buffer_copy(napi_env env,
968
                                    size_t length,
969
                                    const void* data,
970
                                    void** result_data,
971
                                    napi_value* result) {
972


3
  NAPI_PREAMBLE(env);
973
1
  CHECK_ARG(env, result);
974
975
1
  auto maybe = node::Buffer::Copy(env->isolate,
976
2
    static_cast<const char*>(data), length);
977
978
1
  CHECK_MAYBE_EMPTY(env, maybe, napi_generic_failure);
979
980
1
  v8::Local<v8::Object> buffer = maybe.ToLocalChecked();
981
2
  *result = v8impl::JsValueFromV8LocalValue(buffer);
982
983
1
  if (result_data != nullptr) {
984
    *result_data = node::Buffer::Data(buffer);
985
  }
986
987
1
  return GET_RETURN_STATUS(env);
988
}
989
990
1
napi_status napi_is_buffer(napi_env env, napi_value value, bool* result) {
991
1
  CHECK_ENV(env);
992
1
  CHECK_ARG(env, value);
993
1
  CHECK_ARG(env, result);
994
995
1
  *result = node::Buffer::HasInstance(v8impl::V8LocalValueFromJsValue(value));
996
1
  return napi_clear_last_error(env);
997
}
998
999
1
napi_status napi_get_buffer_info(napi_env env,
1000
                                 napi_value value,
1001
                                 void** data,
1002
                                 size_t* length) {
1003
1
  CHECK_ENV(env);
1004
1
  CHECK_ARG(env, value);
1005
1006
1
  v8::Local<v8::Value> buffer = v8impl::V8LocalValueFromJsValue(value);
1007
1008
1
  if (data != nullptr) {
1009
1
    *data = node::Buffer::Data(buffer);
1010
  }
1011
1
  if (length != nullptr) {
1012
1
    *length = node::Buffer::Length(buffer);
1013
  }
1014
1015
1
  return napi_clear_last_error(env);
1016
}
1017
1018
1
napi_status napi_get_node_version(napi_env env,
1019
                                  const napi_node_version** result) {
1020
1
  CHECK_ENV(env);
1021
1
  CHECK_ARG(env, result);
1022
  static const napi_node_version version = {
1023
    NODE_MAJOR_VERSION,
1024
    NODE_MINOR_VERSION,
1025
    NODE_PATCH_VERSION,
1026
    NODE_RELEASE
1027
  };
1028
1
  *result = &version;
1029
1
  return napi_clear_last_error(env);
1030
}
1031
1032
namespace {
1033
namespace uvimpl {
1034
1035
513
static napi_status ConvertUVErrorCode(int code) {
1036

513
  switch (code) {
1037
    case 0:
1038
512
      return napi_ok;
1039
    case UV_EINVAL:
1040
      return napi_invalid_arg;
1041
    case UV_ECANCELED:
1042
1
      return napi_cancelled;
1043
    default:
1044
      return napi_generic_failure;
1045
  }
1046
}
1047
1048
// Wrapper around uv_work_t which calls user-provided callbacks.
1049
class Work : public node::AsyncResource, public node::ThreadPoolWork {
1050
 private:
1051
512
  explicit Work(node_napi_env env,
1052
                v8::Local<v8::Object> async_resource,
1053
                v8::Local<v8::String> async_resource_name,
1054
                napi_async_execute_callback execute,
1055
                napi_async_complete_callback complete = nullptr,
1056
                void* data = nullptr)
1057
1024
    : AsyncResource(env->isolate,
1058
                    async_resource,
1059
1024
                    *v8::String::Utf8Value(env->isolate, async_resource_name)),
1060
      ThreadPoolWork(env->node_env()),
1061
      _env(env),
1062
      _data(data),
1063
      _execute(execute),
1064
1024
      _complete(complete) {
1065
512
  }
1066
1067
1016
  ~Work() override = default;
1068
1069
 public:
1070
512
  static Work* New(node_napi_env env,
1071
                   v8::Local<v8::Object> async_resource,
1072
                   v8::Local<v8::String> async_resource_name,
1073
                   napi_async_execute_callback execute,
1074
                   napi_async_complete_callback complete,
1075
                   void* data) {
1076
    return new Work(env, async_resource, async_resource_name,
1077
512
                    execute, complete, data);
1078
  }
1079
1080
508
  static void Delete(Work* work) {
1081
508
    delete work;
1082
508
  }
1083
1084
511
  void DoThreadPoolWork() override {
1085
511
    _execute(_env, _data);
1086
511
  }
1087
1088
512
  void AfterThreadPoolWork(int status) override {
1089
512
    if (_complete == nullptr)
1090
      return;
1091
1092
    // Establish a handle scope here so that every callback doesn't have to.
1093
    // Also it is needed for the exception-handling below.
1094
1023
    v8::HandleScope scope(_env->isolate);
1095
1096
1023
    CallbackScope callback_scope(this);
1097
1098
1535
    _env->CallIntoModule([&](napi_env env) {
1099
512
      _complete(env, ConvertUVErrorCode(status), _data);
1100
514
    }, [](napi_env env, v8::Local<v8::Value> local_err) {
1101
      // If there was an unhandled exception in the complete callback,
1102
      // report it as a fatal exception. (There is no JavaScript on the
1103
      // callstack that can possibly handle it.)
1104
2
      v8impl::trigger_fatal_exception(env, local_err);
1105
513
    });
1106
1107
    // Note: Don't access `work` after this point because it was
1108
    // likely deleted by the complete callback.
1109
  }
1110
1111
 private:
1112
  node_napi_env _env;
1113
  void* _data;
1114
  napi_async_execute_callback _execute;
1115
  napi_async_complete_callback _complete;
1116
};
1117
1118
}  // end of namespace uvimpl
1119
}  // end of anonymous namespace
1120
1121
#define CALL_UV(env, condition)                                         \
1122
  do {                                                                  \
1123
    int result = (condition);                                           \
1124
    napi_status status = uvimpl::ConvertUVErrorCode(result);            \
1125
    if (status != napi_ok) {                                            \
1126
      return napi_set_last_error(env, status, result);                  \
1127
    }                                                                   \
1128
  } while (0)
1129
1130
512
napi_status napi_create_async_work(napi_env env,
1131
                                   napi_value async_resource,
1132
                                   napi_value async_resource_name,
1133
                                   napi_async_execute_callback execute,
1134
                                   napi_async_complete_callback complete,
1135
                                   void* data,
1136
                                   napi_async_work* result) {
1137
512
  CHECK_ENV(env);
1138
512
  CHECK_ARG(env, execute);
1139
512
  CHECK_ARG(env, result);
1140
1141
512
  v8::Local<v8::Context> context = env->context();
1142
1143
  v8::Local<v8::Object> resource;
1144
512
  if (async_resource != nullptr) {
1145

16
    CHECK_TO_OBJECT(env, context, resource, async_resource);
1146
  } else {
1147
508
    resource = v8::Object::New(env->isolate);
1148
  }
1149
1150
  v8::Local<v8::String> resource_name;
1151

2048
  CHECK_TO_STRING(env, context, resource_name, async_resource_name);
1152
1153
512
  uvimpl::Work* work = uvimpl::Work::New(reinterpret_cast<node_napi_env>(env),
1154
                                         resource,
1155
                                         resource_name,
1156
                                         execute,
1157
                                         complete,
1158
512
                                         data);
1159
1160
512
  *result = reinterpret_cast<napi_async_work>(work);
1161
1162
512
  return napi_clear_last_error(env);
1163
}
1164
1165
508
napi_status napi_delete_async_work(napi_env env, napi_async_work work) {
1166
508
  CHECK_ENV(env);
1167
508
  CHECK_ARG(env, work);
1168
1169
508
  uvimpl::Work::Delete(reinterpret_cast<uvimpl::Work*>(work));
1170
1171
508
  return napi_clear_last_error(env);
1172
}
1173
1174
518
napi_status napi_get_uv_event_loop(napi_env env, uv_loop_t** loop) {
1175
518
  CHECK_ENV(env);
1176
518
  CHECK_ARG(env, loop);
1177
518
  *loop = reinterpret_cast<node_napi_env>(env)->node_env()->event_loop();
1178
518
  return napi_clear_last_error(env);
1179
}
1180
1181
512
napi_status napi_queue_async_work(napi_env env, napi_async_work work) {
1182
512
  CHECK_ENV(env);
1183
512
  CHECK_ARG(env, work);
1184
1185
512
  uv_loop_t* event_loop = nullptr;
1186
512
  STATUS_CALL(napi_get_uv_event_loop(env, &event_loop));
1187
1188
512
  uvimpl::Work* w = reinterpret_cast<uvimpl::Work*>(work);
1189
1190
512
  w->ScheduleWork();
1191
1192
512
  return napi_clear_last_error(env);
1193
}
1194
1195
1
napi_status napi_cancel_async_work(napi_env env, napi_async_work work) {
1196
1
  CHECK_ENV(env);
1197
1
  CHECK_ARG(env, work);
1198
1199
1
  uvimpl::Work* w = reinterpret_cast<uvimpl::Work*>(work);
1200
1201
1
  CALL_UV(env, w->CancelWork());
1202
1203
1
  return napi_clear_last_error(env);
1204
}
1205
1206
napi_status
1207
18
napi_create_threadsafe_function(napi_env env,
1208
                                napi_value func,
1209
                                napi_value async_resource,
1210
                                napi_value async_resource_name,
1211
                                size_t max_queue_size,
1212
                                size_t initial_thread_count,
1213
                                void* thread_finalize_data,
1214
                                napi_finalize thread_finalize_cb,
1215
                                void* context,
1216
                                napi_threadsafe_function_call_js call_js_cb,
1217
                                napi_threadsafe_function* result) {
1218
18
  CHECK_ENV(env);
1219
18
  CHECK_ARG(env, async_resource_name);
1220
18
  RETURN_STATUS_IF_FALSE(env, initial_thread_count > 0, napi_invalid_arg);
1221
18
  CHECK_ARG(env, result);
1222
1223
18
  napi_status status = napi_ok;
1224
1225
  v8::Local<v8::Function> v8_func;
1226
18
  if (func == nullptr) {
1227
3
    CHECK_ARG(env, call_js_cb);
1228
  } else {
1229

45
    CHECK_TO_FUNCTION(env, v8_func, func);
1230
  }
1231
1232
18
  v8::Local<v8::Context> v8_context = env->context();
1233
1234
  v8::Local<v8::Object> v8_resource;
1235
18
  if (async_resource == nullptr) {
1236
18
    v8_resource = v8::Object::New(env->isolate);
1237
  } else {
1238
    CHECK_TO_OBJECT(env, v8_context, v8_resource, async_resource);
1239
  }
1240
1241
  v8::Local<v8::String> v8_name;
1242

72
  CHECK_TO_STRING(env, v8_context, v8_name, async_resource_name);
1243
1244
  v8impl::ThreadSafeFunction* ts_fn =
1245
      new v8impl::ThreadSafeFunction(v8_func,
1246
                                     v8_resource,
1247
                                     v8_name,
1248
                                     initial_thread_count,
1249
                                     context,
1250
                                     max_queue_size,
1251
                                     reinterpret_cast<node_napi_env>(env),
1252
                                     thread_finalize_data,
1253
                                     thread_finalize_cb,
1254
18
                                     call_js_cb);
1255
1256
18
  if (ts_fn == nullptr) {
1257
    status = napi_generic_failure;
1258
  } else {
1259
    // Init deletes ts_fn upon failure.
1260
18
    status = ts_fn->Init();
1261
18
    if (status == napi_ok) {
1262
18
      *result = reinterpret_cast<napi_threadsafe_function>(ts_fn);
1263
    }
1264
  }
1265
1266
18
  return napi_set_last_error(env, status);
1267
}
1268
1269
napi_status
1270
16
napi_get_threadsafe_function_context(napi_threadsafe_function func,
1271
                                     void** result) {
1272
16
  CHECK_NOT_NULL(func);
1273
16
  CHECK_NOT_NULL(result);
1274
1275
16
  *result = reinterpret_cast<v8impl::ThreadSafeFunction*>(func)->Context();
1276
16
  return napi_ok;
1277
}
1278
1279
napi_status
1280
13297126
napi_call_threadsafe_function(napi_threadsafe_function func,
1281
                              void* data,
1282
                              napi_threadsafe_function_call_mode is_blocking) {
1283
13297126
  CHECK_NOT_NULL(func);
1284
13297126
  return reinterpret_cast<v8impl::ThreadSafeFunction*>(func)->Push(data,
1285
13297126
                                                                   is_blocking);
1286
}
1287
1288
napi_status
1289
4
napi_acquire_threadsafe_function(napi_threadsafe_function func) {
1290
4
  CHECK_NOT_NULL(func);
1291
4
  return reinterpret_cast<v8impl::ThreadSafeFunction*>(func)->Acquire();
1292
}
1293
1294
napi_status
1295
33
napi_release_threadsafe_function(napi_threadsafe_function func,
1296
                                 napi_threadsafe_function_release_mode mode) {
1297
33
  CHECK_NOT_NULL(func);
1298
33
  return reinterpret_cast<v8impl::ThreadSafeFunction*>(func)->Release(mode);
1299
}
1300
1301
napi_status
1302
2
napi_unref_threadsafe_function(napi_env env, napi_threadsafe_function func) {
1303
2
  CHECK_NOT_NULL(func);
1304
2
  return reinterpret_cast<v8impl::ThreadSafeFunction*>(func)->Unref();
1305
}
1306
1307
napi_status
1308
napi_ref_threadsafe_function(napi_env env, napi_threadsafe_function func) {
1309
  CHECK_NOT_NULL(func);
1310
  return reinterpret_cast<v8impl::ThreadSafeFunction*>(func)->Ref();
1311
}
1312
1313
1
napi_status node_api_get_module_file_name(napi_env env, const char** result) {
1314
1
  CHECK_ENV(env);
1315
1
  CHECK_ARG(env, result);
1316
1317
1
  *result = static_cast<node_napi_env>(env)->GetFilename();
1318
1
  return napi_clear_last_error(env);
1319

14520
}