• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hanazuki / node-jsonnet / 4346020569

pending completion
4346020569

push

github

GitHub
Merge pull request #194 from hanazuki/dependabot/npm_and_yarn/definitelytyped/dtslint-0.0.151

449 of 492 relevant lines covered (91.26%)

333.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.87
/src/JsonnetNativeCallback.cpp
1
// SPDX-License-Identifier: MIT
2
#include "JsonnetNativeCallback.hpp"
3

4
namespace nodejsonnet {
5

6
  JsonnetNativeCallback::JsonnetNativeCallback(Napi::Env env, Napi::Function fun)
603✔
7
    : tsfn{ThreadSafeFunction::New(env, fun, "Jsonnet Native Callback", 0, 1)} {
603✔
8
  }
603✔
9

10
  JsonnetNativeCallback::~JsonnetNativeCallback() {
603✔
11
    this->tsfn.Release();
402✔
12
  }
603✔
13

14
  JsonnetJsonValue *JsonnetNativeCallback::call(
132✔
15
    std::shared_ptr<JsonnetVm> vm, std::vector<JsonnetJsonValue const *> args) {
16
    // This functions runs in a worker thread and cannot access Node VM.
17

18
    Payload payload(std::move(vm), std::move(args));
132✔
19
    tsfn.BlockingCall(&payload);
132✔
20
    return payload.getFuture().get();
132✔
21
  }
141✔
22

23
  void JsonnetNativeCallback::callback(
132✔
24
    Napi::Env env, Napi::Function fun, std::nullptr_t *, Payload *payload) {
25
    // This functions runs in the Node main thread.
26

27
    JsonValueConverter const conv{payload->getVm()};
132✔
28

29
    std::vector<napi_value> args;
132✔
30
    args.reserve(payload->getArgs().size());
132✔
31
    for(auto const arg: payload->getArgs()) {
240✔
32
      args.push_back(conv.toNapiValue(env, arg));
108✔
33
    }
54✔
34

35
    auto const result = fun.Call(args);
132✔
36
    if(!result.IsPromise()) {
132✔
37
      payload->setResult(conv.toJsonnetJson(result));
×
38
      return;
×
39
    }
40

41
    auto const on_success = Napi::Function::New(
198✔
42
      env,
132✔
43
      [](Napi::CallbackInfo const &info) {
114✔
44
        auto const payload = static_cast<Payload *>(info.Data());
114✔
45
        JsonValueConverter const conv{payload->getVm()};
114✔
46
        payload->setResult(conv.toJsonnetJson(info[0]));
114✔
47
      },
114✔
48
      "onSuccess", payload);
132✔
49

50
    auto const on_failure = Napi::Function::New(
198✔
51
      env,
132✔
52
      [](Napi::CallbackInfo const &info) {
18✔
53
        auto const payload = static_cast<Payload *>(info.Data());
18✔
54
        auto const error = info[0].ToString();
18✔
55
        payload->setError(std::make_exception_ptr(std::runtime_error(error)));
18✔
56
      },
18✔
57
      "onFailure", payload);
66✔
58

59
    auto const promise = result.As<Napi::Object>();
132✔
60
    promise.Get("then").As<Napi::Function>().Call(promise, {on_success, on_failure});
132✔
61
  }
132✔
62

63
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc