1 #include <stdlib.h>
2 #include <string.h>
3 #include <node_api.h>
4 #include "../../js-native-api/common.h"
5
6 static const char theText[] =
7 "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
8
9 static int deleterCallCount = 0;
deleteTheText(napi_env env,void * data,void * finalize_hint)10 static void deleteTheText(napi_env env, void* data, void* finalize_hint) {
11 NAPI_ASSERT_RETURN_VOID(env, data != NULL && strcmp(data, theText) == 0, "invalid data");
12 (void)finalize_hint;
13 free(data);
14 deleterCallCount++;
15 }
16
noopDeleter(napi_env env,void * data,void * finalize_hint)17 static void noopDeleter(napi_env env, void* data, void* finalize_hint) {
18 NAPI_ASSERT_RETURN_VOID(env, data != NULL && strcmp(data, theText) == 0, "invalid data");
19 (void)finalize_hint;
20 deleterCallCount++;
21 }
22
newBuffer(napi_env env,napi_callback_info info)23 static napi_value newBuffer(napi_env env, napi_callback_info info) {
24 napi_value theBuffer;
25 char* theCopy;
26 const unsigned int kBufferSize = sizeof(theText);
27
28 NAPI_CALL(env,
29 napi_create_buffer(
30 env,
31 sizeof(theText),
32 (void**)(&theCopy),
33 &theBuffer));
34 NAPI_ASSERT(env, theCopy, "Failed to copy static text for newBuffer");
35 memcpy(theCopy, theText, kBufferSize);
36
37 return theBuffer;
38 }
39
newExternalBuffer(napi_env env,napi_callback_info info)40 static napi_value newExternalBuffer(napi_env env, napi_callback_info info) {
41 napi_value theBuffer;
42 char* theCopy = strdup(theText);
43 NAPI_ASSERT(env, theCopy, "Failed to copy static text for newExternalBuffer");
44 NAPI_CALL(env,
45 napi_create_external_buffer(
46 env,
47 sizeof(theText),
48 theCopy,
49 deleteTheText,
50 NULL, // finalize_hint
51 &theBuffer));
52
53 return theBuffer;
54 }
55
getDeleterCallCount(napi_env env,napi_callback_info info)56 static napi_value getDeleterCallCount(napi_env env, napi_callback_info info) {
57 napi_value callCount;
58 NAPI_CALL(env, napi_create_int32(env, deleterCallCount, &callCount));
59 return callCount;
60 }
61
copyBuffer(napi_env env,napi_callback_info info)62 static napi_value copyBuffer(napi_env env, napi_callback_info info) {
63 napi_value theBuffer;
64 NAPI_CALL(env, napi_create_buffer_copy(
65 env, sizeof(theText), theText, NULL, &theBuffer));
66 return theBuffer;
67 }
68
bufferHasInstance(napi_env env,napi_callback_info info)69 static napi_value bufferHasInstance(napi_env env, napi_callback_info info) {
70 size_t argc = 1;
71 napi_value args[1];
72 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
73 NAPI_ASSERT(env, argc == 1, "Wrong number of arguments");
74 napi_value theBuffer = args[0];
75 bool hasInstance;
76 napi_valuetype theType;
77 NAPI_CALL(env, napi_typeof(env, theBuffer, &theType));
78 NAPI_ASSERT(env,
79 theType == napi_object,
80 "bufferHasInstance: instance is not an object");
81 NAPI_CALL(env, napi_is_buffer(env, theBuffer, &hasInstance));
82 NAPI_ASSERT(env, hasInstance, "bufferHasInstance: instance is not a buffer");
83 napi_value returnValue;
84 NAPI_CALL(env, napi_get_boolean(env, hasInstance, &returnValue));
85 return returnValue;
86 }
87
bufferInfo(napi_env env,napi_callback_info info)88 static napi_value bufferInfo(napi_env env, napi_callback_info info) {
89 size_t argc = 1;
90 napi_value args[1];
91 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
92 NAPI_ASSERT(env, argc == 1, "Wrong number of arguments");
93 napi_value theBuffer = args[0];
94 char *bufferData;
95 napi_value returnValue;
96 size_t bufferLength;
97 NAPI_CALL(env,
98 napi_get_buffer_info(
99 env,
100 theBuffer,
101 (void**)(&bufferData),
102 &bufferLength));
103 NAPI_CALL(env, napi_get_boolean(env,
104 !strcmp(bufferData, theText) && bufferLength == sizeof(theText),
105 &returnValue));
106 return returnValue;
107 }
108
staticBuffer(napi_env env,napi_callback_info info)109 static napi_value staticBuffer(napi_env env, napi_callback_info info) {
110 napi_value theBuffer;
111 NAPI_CALL(
112 env,
113 napi_create_external_buffer(env,
114 sizeof(theText),
115 (void*)theText,
116 noopDeleter,
117 NULL, // finalize_hint
118 &theBuffer));
119 return theBuffer;
120 }
121
Init(napi_env env,napi_value exports)122 static napi_value Init(napi_env env, napi_value exports) {
123 napi_value theValue;
124
125 NAPI_CALL(env,
126 napi_create_string_utf8(env, theText, sizeof(theText), &theValue));
127 NAPI_CALL(env, napi_set_named_property(env, exports, "theText", theValue));
128
129 napi_property_descriptor methods[] = {
130 DECLARE_NAPI_PROPERTY("newBuffer", newBuffer),
131 DECLARE_NAPI_PROPERTY("newExternalBuffer", newExternalBuffer),
132 DECLARE_NAPI_PROPERTY("getDeleterCallCount", getDeleterCallCount),
133 DECLARE_NAPI_PROPERTY("copyBuffer", copyBuffer),
134 DECLARE_NAPI_PROPERTY("bufferHasInstance", bufferHasInstance),
135 DECLARE_NAPI_PROPERTY("bufferInfo", bufferInfo),
136 DECLARE_NAPI_PROPERTY("staticBuffer", staticBuffer),
137 };
138
139 NAPI_CALL(env, napi_define_properties(
140 env, exports, sizeof(methods) / sizeof(methods[0]), methods));
141
142 return exports;
143 }
144
145 NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
146