-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathRuntimeNodeApi.cpp
More file actions
162 lines (137 loc) · 4.05 KB
/
RuntimeNodeApi.cpp
File metadata and controls
162 lines (137 loc) · 4.05 KB
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
156
157
158
159
160
161
162
#include "RuntimeNodeApi.hpp"
#include <string>
#include "Logger.hpp"
#include "Versions.hpp"
auto ArrayType = napi_uint8_array;
namespace callstack::nodeapihost {
napi_status napi_create_buffer(
napi_env env, size_t length, void** data, napi_value* result) {
napi_value buffer;
if (const auto status = napi_create_arraybuffer(env, length, data, &buffer);
status != napi_ok) {
return status;
}
// Warning: The returned data structure does not fully align with the
// characteristics of a Buffer.
// @see
// https://github.com/callstackincubator/react-native-node-api/issues/171
return napi_create_typedarray(env, ArrayType, length, buffer, 0, result);
}
napi_status napi_create_buffer_copy(napi_env env,
size_t length,
const void* data,
void** result_data,
napi_value* result) {
if (!length || !data || !result) {
return napi_invalid_arg;
}
void* buffer = nullptr;
if (const auto status = ::napi_create_buffer(env, length, &buffer, result);
status != napi_ok) {
return status;
}
std::memcpy(buffer, data, length);
return napi_ok;
}
napi_status napi_is_buffer(napi_env env, napi_value value, bool* result) {
if (!result) {
return napi_invalid_arg;
}
if (!value) {
*result = false;
return napi_ok;
}
napi_valuetype type{};
if (const auto status = napi_typeof(env, value, &type); status != napi_ok) {
return status;
}
if (type != napi_object && type != napi_external) {
*result = false;
return napi_ok;
}
auto isArrayBuffer{false};
if (const auto status = napi_is_arraybuffer(env, value, &isArrayBuffer);
status != napi_ok) {
return status;
}
auto isTypedArray{false};
if (const auto status = napi_is_typedarray(env, value, &isTypedArray);
status != napi_ok) {
return status;
}
*result = isArrayBuffer || isTypedArray;
return napi_ok;
}
napi_status napi_get_buffer_info(
napi_env env, napi_value value, void** data, size_t* length) {
if (!data || !length) {
return napi_invalid_arg;
}
*data = nullptr;
*length = 0;
if (!value) {
return napi_ok;
}
auto isArrayBuffer{false};
if (const auto status = napi_is_arraybuffer(env, value, &isArrayBuffer);
status == napi_ok && isArrayBuffer) {
return napi_get_arraybuffer_info(env, value, data, length);
}
auto isTypedArray{false};
if (const auto status = napi_is_typedarray(env, value, &isTypedArray);
status == napi_ok && isTypedArray) {
return napi_get_typedarray_info(
env, value, &ArrayType, length, data, nullptr, nullptr);
}
return napi_ok;
}
napi_status napi_create_external_buffer(napi_env env,
size_t length,
void* data,
node_api_basic_finalize basic_finalize_cb,
void* finalize_hint,
napi_value* result) {
napi_value buffer;
if (const auto status = napi_create_external_arraybuffer(
env, data, length, basic_finalize_cb, finalize_hint, &buffer);
status != napi_ok) {
return status;
}
// Warning: The returned data structure does not fully align with the
// characteristics of a Buffer.
// @see
// https://github.com/callstackincubator/react-native-node-api/issues/171
return napi_create_typedarray(env, ArrayType, length, buffer, 0, result);
}
void napi_fatal_error(const char* location,
size_t location_len,
const char* message,
size_t message_len) {
if (location && location_len) {
log_error("Fatal Node-API error: %.*s %.*s",
static_cast<int>(location_len),
location,
static_cast<int>(message_len),
message);
} else {
log_error(
"Fatal Node-API error: %.*s", static_cast<int>(message_len), message);
}
abort();
}
napi_status napi_get_node_version(
node_api_basic_env env, const napi_node_version** result) {
if (!result) {
return napi_invalid_arg;
}
*result = nullptr;
return napi_generic_failure;
}
napi_status napi_get_version(node_api_basic_env env, uint32_t* result) {
if (!result) {
return napi_invalid_arg;
}
*result = NAPI_VERSION;
return napi_ok;
}
} // namespace callstack::nodeapihost