-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathRuntimeNodeApi.cpp
More file actions
112 lines (95 loc) · 2.92 KB
/
RuntimeNodeApi.cpp
File metadata and controls
112 lines (95 loc) · 2.92 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
#include "RuntimeNodeApi.hpp"
#include <string>
auto ArrayType = napi_uint8_array;
napi_status NAPI_CDECL callstack::nodeapihost::napi_create_buffer(
napi_env env, size_t length, void** data, napi_value* result) {
napi_value buffer;
const auto status = napi_create_arraybuffer(env, length, data, &buffer);
if (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_CDECL callstack::nodeapihost::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 callstack::nodeapihost::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 callstack::nodeapihost::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 callstack::nodeapihost::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) {
return napi_create_external_arraybuffer(
env, data, length, basic_finalize_cb, finalize_hint, result);
}