Skip to content

Commit fb8ced4

Browse files
boingoingjasongin
authored andcommitted
Convert all locals and parameter names to snake_case (#193)
* Convert all locals and parameter names to snake_case Standardize some parameter names across the N-API surface such as ```napi_env env``` and ```size_t length```. Make all the parameter names in the header match the cc file and expand some parameter names from single characters into meaningful words. Also addresses a couple of minor PR feedback items: - Rename ```napi_reference_addref``` and ```napi_reference_release``` to ```napi_reference_ref``` and ```napi_reference_unref``` (respectively) - Rename ```Reference::AddRef``` and ```Reference::Release``` to ```Reference::Ref``` and ```Reference::Unref``` (respectively) - Remove ```napi_create_boolean```, ```napi_get_true```, and ```napi_get_false``` and replace them with ```napi_get_boolean``` - Rename ```napi_get_type_of_value``` to ```napi_typeof``` - Add a ```result_data``` out parameter to ```napi_create_buffer_copy``` which returns the data pointer of the new buffer - Change ```napi_get_value_string_utf8``` and ```napi_get_value_string_utf16``` to accept a null output buffer and return the length of the source string in that case via the ```result``` parameter - Remove ```napi_get_value_string_utf8_length``` and ```napi_get_value_string_utf16_length``` due to the above - Change ```Reference``` ctor and dtor to be protected and added public ```static Reference* Reference::New``` and ```static void Reference::Delete``` methods to make it more clear how the ```Reference``` objects are allocated and cleaned-up - Add a type check to ``napi_get_array_length``` which returns an error if the argument is not an array object - Change ```napi_create_symbol``` to take a ```napi_value``` instead of a ```const char*``` for the symbol description string. The API now throws if description is not a string but NULL is allowed. * Remove UTF-8 BOM characters and other non-ANSI whitespace * Change parameter names for create string APIs and address other feedback * Fixes for unit tests * Fix lint errors found via CI
1 parent 9e3ab83 commit fb8ced4

File tree

18 files changed

+566
-588
lines changed

18 files changed

+566
-588
lines changed

src/node_api.cc

+497-498
Large diffs are not rendered by default.

src/node_api.h

+28-40
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,10 @@ NAPI_EXTERN const napi_extended_error_info* napi_get_last_error_info();
105105
// Getters for defined singletons
106106
NAPI_EXTERN napi_status napi_get_undefined(napi_env env, napi_value* result);
107107
NAPI_EXTERN napi_status napi_get_null(napi_env env, napi_value* result);
108-
NAPI_EXTERN napi_status napi_get_false(napi_env env, napi_value* result);
109-
NAPI_EXTERN napi_status napi_get_true(napi_env env, napi_value* result);
110108
NAPI_EXTERN napi_status napi_get_global(napi_env env, napi_value* result);
109+
NAPI_EXTERN napi_status napi_get_boolean(napi_env env,
110+
bool value,
111+
napi_value* result);
111112

112113
// Methods to create Primitive types/Objects
113114
NAPI_EXTERN napi_status napi_create_object(napi_env env, napi_value* result);
@@ -116,21 +117,18 @@ NAPI_EXTERN napi_status napi_create_array_with_length(napi_env env,
116117
size_t length,
117118
napi_value* result);
118119
NAPI_EXTERN napi_status napi_create_number(napi_env env,
119-
double val,
120+
double value,
120121
napi_value* result);
121122
NAPI_EXTERN napi_status napi_create_string_utf8(napi_env env,
122-
const char* s,
123+
const char* str,
123124
size_t length,
124125
napi_value* result);
125126
NAPI_EXTERN napi_status napi_create_string_utf16(napi_env env,
126-
const char16_t* s,
127+
const char16_t* str,
127128
size_t length,
128129
napi_value* result);
129-
NAPI_EXTERN napi_status napi_create_boolean(napi_env env,
130-
bool b,
131-
napi_value* result);
132130
NAPI_EXTERN napi_status napi_create_symbol(napi_env env,
133-
const char* s,
131+
napi_value description,
134132
napi_value* result);
135133
NAPI_EXTERN napi_status napi_create_function(napi_env env,
136134
const char* utf8name,
@@ -148,9 +146,9 @@ NAPI_EXTERN napi_status napi_create_range_error(napi_env env,
148146
napi_value* result);
149147

150148
// Methods to get the the native napi_value from Primitive type
151-
NAPI_EXTERN napi_status napi_get_type_of_value(napi_env env,
152-
napi_value value,
153-
napi_valuetype* result);
149+
NAPI_EXTERN napi_status napi_typeof(napi_env env,
150+
napi_value value,
151+
napi_valuetype* result);
154152
NAPI_EXTERN napi_status napi_get_value_double(napi_env env,
155153
napi_value value,
156154
double* result);
@@ -172,24 +170,13 @@ NAPI_EXTERN napi_status napi_get_value_string_length(napi_env env,
172170
napi_value value,
173171
size_t* result);
174172

175-
// Gets the number of BYTES in the UTF-8 encoded representation of the string.
176-
NAPI_EXTERN napi_status napi_get_value_string_utf8_length(napi_env env,
177-
napi_value value,
178-
size_t* result);
179-
180173
// Copies UTF-8 encoded bytes from a string into a buffer.
181174
NAPI_EXTERN napi_status napi_get_value_string_utf8(napi_env env,
182175
napi_value value,
183176
char* buf,
184177
size_t bufsize,
185178
size_t* result);
186179

187-
// Gets the number of 2-byte code units in the UTF-16 encoded
188-
// representation of the string.
189-
NAPI_EXTERN napi_status napi_get_value_string_utf16_length(napi_env env,
190-
napi_value value,
191-
size_t* result);
192-
193180
// Copies UTF-16 encoded bytes from a string into a buffer.
194181
NAPI_EXTERN napi_status napi_get_value_string_utf16(napi_env env,
195182
napi_value value,
@@ -317,8 +304,8 @@ NAPI_EXTERN napi_status napi_get_cb_args_length(napi_env env,
317304
size_t* result);
318305
NAPI_EXTERN napi_status napi_get_cb_args(napi_env env,
319306
napi_callback_info cbinfo,
320-
napi_value* buffer,
321-
size_t bufferlength);
307+
napi_value* buf,
308+
size_t bufsize);
322309
NAPI_EXTERN napi_status napi_get_cb_this(napi_env env,
323310
napi_callback_info cbinfo,
324311
napi_value* result);
@@ -343,13 +330,13 @@ napi_define_class(napi_env env,
343330

344331
// Methods to work with external data objects
345332
NAPI_EXTERN napi_status napi_wrap(napi_env env,
346-
napi_value jsObject,
347-
void* nativeObj,
333+
napi_value js_object,
334+
void* native_object,
348335
napi_finalize finalize_cb,
349336
void* finalize_hint,
350337
napi_ref* result);
351338
NAPI_EXTERN napi_status napi_unwrap(napi_env env,
352-
napi_value jsObject,
339+
napi_value js_object,
353340
void** result);
354341
NAPI_EXTERN napi_status napi_create_external(napi_env env,
355342
void* data,
@@ -375,23 +362,23 @@ NAPI_EXTERN napi_status napi_delete_reference(napi_env env, napi_ref ref);
375362
// Increments the reference count, optionally returning the resulting count.
376363
// After this call the reference will be a strong reference because its
377364
// refcount is >0, and the referenced object is effectively "pinned".
378-
// Calling this when the refcount is 0 and the object isunavailable
365+
// Calling this when the refcount is 0 and the object is unavailable
379366
// results in an error.
380-
NAPI_EXTERN napi_status napi_reference_addref(napi_env env,
381-
napi_ref ref,
382-
int* result);
367+
NAPI_EXTERN napi_status napi_reference_ref(napi_env env,
368+
napi_ref ref,
369+
int* result);
383370

384371
// Decrements the reference count, optionally returning the resulting count.
385372
// If the result is 0 the reference is now weak and the object may be GC'd
386373
// at any time if there are no other references. Calling this when the
387-
// refcount is already 0 results in an error.
388-
NAPI_EXTERN napi_status napi_reference_release(napi_env env,
389-
napi_ref ref,
390-
int* result);
374+
// refcount is already 0 results in an error.
375+
NAPI_EXTERN napi_status napi_reference_unref(napi_env env,
376+
napi_ref ref,
377+
int* result);
391378

392379
// Attempts to get a referenced value. If the reference is weak,
393380
// the value might no longer be available, in that case the call
394-
// is still successful but the result is NULL.
381+
// is still successful but the result is NULL.
395382
NAPI_EXTERN napi_status napi_get_reference_value(napi_env env,
396383
napi_ref ref,
397384
napi_value* result);
@@ -428,18 +415,19 @@ NAPI_EXTERN napi_status napi_get_and_clear_last_exception(napi_env env,
428415

429416
// Methods to provide node::Buffer functionality with napi types
430417
NAPI_EXTERN napi_status napi_create_buffer(napi_env env,
431-
size_t size,
418+
size_t length,
432419
void** data,
433420
napi_value* result);
434421
NAPI_EXTERN napi_status napi_create_external_buffer(napi_env env,
435-
size_t size,
422+
size_t length,
436423
void* data,
437424
napi_finalize finalize_cb,
438425
void* finalize_hint,
439426
napi_value* result);
440427
NAPI_EXTERN napi_status napi_create_buffer_copy(napi_env env,
428+
size_t length,
441429
const void* data,
442-
size_t size,
430+
void** result_data,
443431
napi_value* result);
444432
NAPI_EXTERN napi_status napi_is_buffer(napi_env env,
445433
napi_value value,

src/node_api_types.h

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ typedef enum {
7878
napi_function_expected,
7979
napi_number_expected,
8080
napi_boolean_expected,
81+
napi_array_expected,
8182
napi_generic_failure,
8283
napi_pending_exception,
8384
napi_status_last

test/addons-napi/2_function_arguments/binding.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ void Add(napi_env env, napi_callback_info info) {
1717
if (status != napi_ok) return;
1818

1919
napi_valuetype valuetype0;
20-
status = napi_get_type_of_value(env, args[0], &valuetype0);
20+
status = napi_typeof(env, args[0], &valuetype0);
2121
if (status != napi_ok) return;
2222

2323
napi_valuetype valuetype1;
24-
status = napi_get_type_of_value(env, args[1], &valuetype1);
24+
status = napi_typeof(env, args[1], &valuetype1);
2525
if (status != napi_ok) return;
2626

2727
if (valuetype0 != napi_number || valuetype1 != napi_number) {

test/addons-napi/6_object_wrap/myobject.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void MyObject::New(napi_env env, napi_callback_info info) {
5050
double value = 0;
5151

5252
napi_valuetype valuetype;
53-
status = napi_get_type_of_value(env, args[0], &valuetype);
53+
status = napi_typeof(env, args[0], &valuetype);
5454
if (status != napi_ok) return;
5555

5656
if (valuetype != napi_undefined) {
@@ -164,7 +164,7 @@ void MyObject::Multiply(napi_env env, napi_callback_info info) {
164164
if (status != napi_ok) return;
165165

166166
napi_valuetype valuetype;
167-
status = napi_get_type_of_value(env, args[0], &valuetype);
167+
status = napi_typeof(env, args[0], &valuetype);
168168
if (status != napi_ok) return;
169169

170170
double multiple = 1;

test/addons-napi/7_factory_wrap/myobject.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void MyObject::New(napi_env env, napi_callback_info info) {
3838
if (status != napi_ok) return;
3939

4040
napi_valuetype valuetype;
41-
status = napi_get_type_of_value(env, args[0], &valuetype);
41+
status = napi_typeof(env, args[0], &valuetype);
4242
if (status != napi_ok) return;
4343

4444
MyObject* obj = new MyObject();

test/addons-napi/8_passing_wrapped/myobject.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void MyObject::New(napi_env env, napi_callback_info info) {
3333
MyObject* obj = new MyObject();
3434

3535
napi_valuetype valuetype;
36-
status = napi_get_type_of_value(env, args[0], &valuetype);
36+
status = napi_typeof(env, args[0], &valuetype);
3737
if (status != napi_ok) return;
3838

3939
if (valuetype == napi_undefined) {

test/addons-napi/test_array/test_array.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void Test(napi_env env, napi_callback_info info) {
1818
if (status != napi_ok) return;
1919

2020
napi_valuetype valuetype0;
21-
status = napi_get_type_of_value(env, args[0], &valuetype0);
21+
status = napi_typeof(env, args[0], &valuetype0);
2222
if (status != napi_ok) return;
2323

2424
if (valuetype0 != napi_object) {
@@ -28,7 +28,7 @@ void Test(napi_env env, napi_callback_info info) {
2828
}
2929

3030
napi_valuetype valuetype1;
31-
status = napi_get_type_of_value(env, args[1], &valuetype1);
31+
status = napi_typeof(env, args[1], &valuetype1);
3232
if (status != napi_ok) return;
3333

3434
if (valuetype1 != napi_number) {
@@ -88,7 +88,7 @@ void New(napi_env env, napi_callback_info info) {
8888
if (status != napi_ok) return;
8989

9090
napi_valuetype valuetype;
91-
status = napi_get_type_of_value(env, args[0], &valuetype);
91+
status = napi_typeof(env, args[0], &valuetype);
9292
if (status != napi_ok) return;
9393

9494
if (valuetype != napi_object) {

test/addons-napi/test_buffer/test_buffer.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ void getDeleterCallCount(napi_env env, napi_callback_info info) {
7272

7373
void copyBuffer(napi_env env, napi_callback_info info) {
7474
napi_value theBuffer;
75-
NAPI_CALL(env,
76-
napi_create_buffer_copy(env, theText, sizeof(theText), &theBuffer));
75+
NAPI_CALL(env, napi_create_buffer_copy(
76+
env, sizeof(theText), theText, NULL, &theBuffer));
7777
NAPI_CALL(env, napi_set_return_value(env, info, theBuffer));
7878
}
7979

@@ -85,14 +85,14 @@ void bufferHasInstance(napi_env env, napi_callback_info info) {
8585
NAPI_CALL(env, napi_get_cb_args(env, info, &theBuffer, 1));
8686
bool hasInstance;
8787
napi_valuetype theType;
88-
NAPI_CALL(env, napi_get_type_of_value(env, theBuffer, &theType));
88+
NAPI_CALL(env, napi_typeof(env, theBuffer, &theType));
8989
JS_ASSERT(env,
9090
theType == napi_object,
9191
"bufferHasInstance: instance is not an object");
9292
NAPI_CALL(env, napi_is_buffer(env, theBuffer, &hasInstance));
9393
JS_ASSERT(env, hasInstance, "bufferHasInstance: instance is not a buffer");
9494
napi_value returnValue;
95-
NAPI_CALL(env, napi_create_boolean(env, hasInstance, &returnValue));
95+
NAPI_CALL(env, napi_get_boolean(env, hasInstance, &returnValue));
9696
NAPI_CALL(env, napi_set_return_value(env, info, returnValue));
9797
}
9898

@@ -110,7 +110,7 @@ void bufferInfo(napi_env env, napi_callback_info info) {
110110
theBuffer,
111111
(void **)(&bufferData),
112112
&bufferLength));
113-
NAPI_CALL(env, napi_create_boolean(env,
113+
NAPI_CALL(env, napi_get_boolean(env,
114114
!strcmp(bufferData, theText) && bufferLength == sizeof(theText),
115115
&returnValue));
116116
NAPI_CALL(env, napi_set_return_value(env, info, returnValue));
@@ -122,7 +122,7 @@ void staticBuffer(napi_env env, napi_callback_info info) {
122122
env,
123123
napi_create_external_buffer(env,
124124
sizeof(theText),
125-
(const char *)(theText),
125+
theText,
126126
noopDeleter,
127127
NULL, // finalize_hint
128128
&theBuffer));

test/addons-napi/test_error/test_error.cc

+1-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ void checkError(napi_env e, napi_callback_info info) {
1212
if (status != napi_ok) return;
1313

1414
napi_value result;
15-
if (r) {
16-
status = napi_get_true(e, &result);
17-
} else {
18-
status = napi_get_false(e, &result);
19-
}
15+
status = napi_get_boolean(e, r, &result);
2016
if (status != napi_ok) return;
2117

2218
status = napi_set_return_value(e, info, result);

test/addons-napi/test_exception/test_exception.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void wasPending(napi_env env, napi_callback_info info) {
4848
napi_status status;
4949

5050
napi_value result;
51-
status = napi_create_boolean(env, exceptionWasPending, &result);
51+
status = napi_get_boolean(env, exceptionWasPending, &result);
5252
if (status != napi_ok) return;
5353

5454
status = napi_set_return_value(env, info, result);

test/addons-napi/test_function/test_function.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void Test(napi_env env, napi_callback_info info) {
1717
if (status != napi_ok) return;
1818

1919
napi_valuetype valuetype;
20-
status = napi_get_type_of_value(env, args[0], &valuetype);
20+
status = napi_typeof(env, args[0], &valuetype);
2121
if (status != napi_ok) return;
2222

2323
if (valuetype != napi_function) {

test/addons-napi/test_instanceof/test_instanceof.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void doInstanceOf(napi_env env, napi_callback_info info) {
1414
if (status != napi_ok) return;
1515

1616
napi_value result;
17-
status = napi_create_boolean(env, instanceof, &result);
17+
status = napi_get_boolean(env, instanceof, &result);
1818
if (status != napi_ok) return;
1919

2020
status = napi_set_return_value(env, info, result);

test/addons-napi/test_number/test_number.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void Test(napi_env env, napi_callback_info info) {
1717
if (status != napi_ok) return;
1818

1919
napi_valuetype valuetype;
20-
status = napi_get_type_of_value(env, args[0], &valuetype);
20+
status = napi_typeof(env, args[0], &valuetype);
2121
if (status != napi_ok) return;
2222

2323
if (valuetype != napi_number) {

0 commit comments

Comments
 (0)