@@ -7,17 +7,23 @@ static const char theText[] =
77 "Lorem ipsum dolor sit amet, consectetur adipiscing elit." ;
88
99static int deleterCallCount = 0 ;
10- static void deleteTheText (napi_env env , void * data , void * finalize_hint ) {
11- NODE_API_ASSERT_RETURN_VOID (
12- env , data != NULL && strcmp (data , theText ) == 0 , "invalid data" );
10+
11+ static void deleteTheText (node_api_basic_env env ,
12+ void * data ,
13+ void * finalize_hint ) {
14+ NODE_API_BASIC_ASSERT_RETURN_VOID (data != NULL && strcmp (data , theText ) == 0 ,
15+ "invalid data" );
16+
1317 (void )finalize_hint ;
1418 free (data );
1519 deleterCallCount ++ ;
1620}
1721
18- static void noopDeleter (napi_env env , void * data , void * finalize_hint ) {
19- NODE_API_ASSERT_RETURN_VOID (
20- env , data != NULL && strcmp (data , theText ) == 0 , "invalid data" );
22+ static void noopDeleter (node_api_basic_env env ,
23+ void * data ,
24+ void * finalize_hint ) {
25+ NODE_API_BASIC_ASSERT_RETURN_VOID (data != NULL && strcmp (data , theText ) == 0 ,
26+ "invalid data" );
2127 (void )finalize_hint ;
2228 deleterCallCount ++ ;
2329}
@@ -42,9 +48,12 @@ static napi_value newExternalBuffer(napi_env env, napi_callback_info info) {
4248 NODE_API_ASSERT (
4349 env , theCopy , "Failed to copy static text for newExternalBuffer" );
4450 NODE_API_CALL (env ,
45- napi_create_external_buffer (
46- env , sizeof (theText ), theCopy , deleteTheText ,
47- NULL /* finalize_hint */ , & theBuffer ));
51+ napi_create_external_buffer (env ,
52+ sizeof (theText ),
53+ theCopy ,
54+ deleteTheText ,
55+ NULL /* finalize_hint */ ,
56+ & theBuffer ));
4857
4958 return theBuffer ;
5059}
@@ -101,9 +110,12 @@ static napi_value bufferInfo(napi_env env, napi_callback_info info) {
101110static napi_value staticBuffer (napi_env env , napi_callback_info info ) {
102111 napi_value theBuffer ;
103112 NODE_API_CALL (env ,
104- napi_create_external_buffer (
105- env , sizeof (theText ), (void * )theText , noopDeleter ,
106- NULL /* finalize_hint */ , & theBuffer ));
113+ napi_create_external_buffer (env ,
114+ sizeof (theText ),
115+ (void * )theText ,
116+ noopDeleter ,
117+ NULL /* finalize_hint */ ,
118+ & theBuffer ));
107119 return theBuffer ;
108120}
109121
@@ -123,6 +135,31 @@ static napi_value invalidObjectAsBuffer(napi_env env, napi_callback_info info) {
123135 return notTheBuffer ;
124136}
125137
138+ static napi_value bufferFromArrayBuffer (napi_env env , napi_callback_info info ) {
139+ napi_status status ;
140+ napi_value arraybuffer ;
141+ napi_value buffer ;
142+ size_t byte_length = 1024 ;
143+ void * data = NULL ;
144+ size_t buffer_length = 0 ;
145+ void * buffer_data = NULL ;
146+
147+ status = napi_create_arraybuffer (env , byte_length , & data , & arraybuffer );
148+ NODE_API_ASSERT (env , status == napi_ok , "Failed to create arraybuffer" );
149+
150+ status = node_api_create_buffer_from_arraybuffer (
151+ env , arraybuffer , 0 , byte_length , & buffer );
152+ NODE_API_ASSERT (
153+ env , status == napi_ok , "Failed to create buffer from arraybuffer" );
154+
155+ status = napi_get_buffer_info (env , buffer , & buffer_data , & buffer_length );
156+ NODE_API_ASSERT (env , status == napi_ok , "Failed to get buffer info" );
157+
158+ NODE_API_ASSERT (env , buffer_length == byte_length , "Buffer length mismatch" );
159+
160+ return buffer ;
161+ }
162+
126163static napi_value Init (napi_env env , napi_value exports ) {
127164 napi_value theValue ;
128165
@@ -140,6 +177,7 @@ static napi_value Init(napi_env env, napi_value exports) {
140177 DECLARE_NODE_API_PROPERTY ("bufferInfo" , bufferInfo ),
141178 DECLARE_NODE_API_PROPERTY ("staticBuffer" , staticBuffer ),
142179 DECLARE_NODE_API_PROPERTY ("invalidObjectAsBuffer" , invalidObjectAsBuffer ),
180+ DECLARE_NODE_API_PROPERTY ("bufferFromArrayBuffer" , bufferFromArrayBuffer ),
143181 };
144182
145183 NODE_API_CALL (env , napi_define_properties (
0 commit comments