File: value_type_cast.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 (60 lines) | stat: -rw-r--r-- 2,242 bytes parent folder | download
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
#include "common/test_helper.h"
#include "napi.h"

using namespace Napi;

#define TYPE_CAST_TYPES(V)                                                     \
  V(Boolean)                                                                   \
  V(Number)                                                                    \
  V(BigInt)                                                                    \
  V(Date)                                                                      \
  V(String)                                                                    \
  V(Symbol)                                                                    \
  V(Object)                                                                    \
  V(Array)                                                                     \
  V(ArrayBuffer)                                                               \
  V(TypedArray)                                                                \
  V(DataView)                                                                  \
  V(Function)                                                                  \
  V(Promise)

// The following types are tested individually.
// External
// TypedArrayOf
// Buffer

namespace {
#define V(Type)                                                                \
  void TypeCast##Type(const CallbackInfo& info) { USE(info[0].As<Type>()); }
TYPE_CAST_TYPES(V)
#undef V

void TypeCastBuffer(const CallbackInfo& info) {
  USE(info[0].As<Buffer<uint8_t>>());
}

void TypeCastExternal(const CallbackInfo& info) {
  USE(info[0].As<External<void>>());
}

void TypeCastTypeArrayOfUint8(const CallbackInfo& info) {
  USE(info[0].As<TypedArrayOf<uint8_t>>());
}
}  // namespace

Object InitValueTypeCast(Env env, Object exports) {
  exports["external"] = External<void>::New(env, nullptr);

#define V(Type) exports["typeCast" #Type] = Function::New(env, TypeCast##Type);
  TYPE_CAST_TYPES(V)
#undef V

  exports["typeCastBuffer"] = Function::New(env, TypeCastBuffer);
  exports["typeCastExternal"] = Function::New(env, TypeCastExternal);
  exports["typeCastTypeArrayOfUint8"] =
      Function::New(env, TypeCastTypeArrayOfUint8);

  return exports;
}

NODE_API_MODULE(addon, InitValueTypeCast)