File: README.md

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 (91 lines) | stat: -rw-r--r-- 2,725 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
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
# Writing Tests

There are multiple flavors of node-addon-api test builds that cover different
build flags defined in `napi.h`:

1. c++ exceptions enabled,
2. c++ exceptions disabled,
3. c++ exceptions disabled, and `NODE_ADDON_API_ENABLE_MAYBE` defined.

Functions in node-addon-api that call into JavaScript can have different
declared return types to reflect build flavor settings. For example,
`Napi::Object::Set` returns `bool` when `NODE_ADDON_API_ENABLE_MAYBE`
is not defined, and `Napi::Maybe<bool>` when `NODE_ADDON_API_ENABLE_MAYBE`
is defined. In source code, return type variants are defined as
`Napi::MaybeOrValue<>` to prevent the duplication of most of the code base.

To properly test these build flavors, all values returned by a function defined
to return `Napi::MaybeOrValue<>` should be tested by using one of the following
test helpers to handle possible JavaScript exceptions.

There are three test helper functions to conveniently convert
`Napi::MaybeOrValue<>` values to raw values.

## MaybeUnwrap

```cpp
template <typename T>
T MaybeUnwrap(MaybeOrValue<T> maybe);
```

Converts `MaybeOrValue<T>` to `T` by checking that `MaybeOrValue` is NOT an
empty `Maybe`.

Returns the original value if `NODE_ADDON_API_ENABLE_MAYBE` is not defined.

Example:

```cpp
Object obj = info[0].As<Object>();
// we are sure the parameters should not throw
Value value = MaybeUnwrap(obj->Get("foobar"));
```

## MaybeUnwrapOr

```cpp
template <typename T>
T MaybeUnwrapOr(MaybeOrValue<T> maybe, const T& default_value = T());
```

Converts `MaybeOrValue<T>` to `T` by getting the value that wrapped by the
`Maybe` or return the `default_value` if the `Maybe` is empty.

Returns the original value if `NODE_ADDON_API_ENABLE_MAYBE` is not defined.

Example:

```cpp
Value CallWithArgs(const CallbackInfo& info) {
  Function func = info[0].As<Function>();
  // We don't care if the operation is throwing or not, just return it back to node-addon-api
  return MaybeUnwrapOr(
      func.Call(std::initializer_list<napi_value>{info[1], info[2], info[3]}));
}
```

## MaybeUnwrapTo

```cpp
template <typename T>
bool MaybeUnwrapTo(MaybeOrValue<T> maybe, T* out);
```

Converts `MaybeOrValue<T>` to `T` by getting the value that wrapped by the
e`Maybe` or return `false` if the Maybe is empty

Copies the `value` to `out` when `NODE_ADDON_API_ENABLE_MAYBE` is not defined

Example:

```cpp
Object opts = info[0].As<Object>();
bool hasProperty = false;
// The check may throw, but we are going to suppress that.
if (MaybeUnwrapTo(opts.Has("blocking"), &hasProperty)) {
  isBlocking = hasProperty &&
                MaybeUnwrap(MaybeUnwrap(opts.Get("blocking")).ToBoolean());
} else {
  env.GetAndClearPendingException();
}
```