0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
Commit Graph

61 Commits

Author SHA1 Message Date
Jason Ginchereau
af70c3b478 n-api: optimize number API performance
- Add separate APIs for creating different kinds of numbers,
   because creating a V8 number value from an integer is faster
   than creating one from a double.
 - When getting number values, avoid getting the current context
   because the context will not actually be used and is expensive
   to obtain.
 - When creating values, don't use v8::TryCatch (NAPI_PREAMBLE),
   because these functions have no possibility of executing JS code.

Refs: https://github.com/nodejs/node/issues/14379
PR-URL: https://github.com/nodejs/node/pull/14573
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2017-08-07 17:31:17 -07:00
Aditya Anand
ad664eae7d
test: mitigate RegEx exceeding 80 chars
Format commit wrapping lines containing RegEx and exceeding 80
chars.

PR-URL: https://github.com/nodejs/node/pull/14607
Fixes: https://github.com/nodejs/node/issues/14586
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
2017-08-07 19:13:41 -04:00
Rich Trott
aa6fac68da test: adjust indentation for stricter linting
ESLint 4.x has stricter linting than previous versions. We are currently
using the legacy indentation rules in the test directory. This commit
changes the indentation of files to comply with the stricter 4.x linting
and enable stricter linting in the test directory.

PR-URL: https://github.com/nodejs/node/pull/14431
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2017-07-27 09:24:20 -07:00
Shivanth MP
77ca3cb474 n-api: add support for DataView
Basic support for Dataview is added in this commit. This is achieved
by using three functions, napi_create_dataview(), napi_is_dataview()
and napi_get_dataview_info().

PR-URL: https://github.com/nodejs/node/pull/14382
Fixes: https://github.com/nodejs/node/issues/13926
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2017-07-27 02:30:05 +02:00
Gabriel Schulhof
9926dfebc8
n-api: re-use napi_env between modules
Store the `napi_env` on the global object at a private key. This gives
us one `napi_env` per context.

Refs: https://github.com/nodejs/node/issues/14367
PR-URL: https://github.com/nodejs/node/pull/14217
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
2017-07-27 07:55:48 +08:00
Pratik Jain
7849b52810
test: changed error message validator
Replaced TypeError with RegEx to match the exact error message in file
test/addons-napi/test_properties/test.js. The RegEx will check the
validity of the error being thrown.

PR-URL: https://github.com/nodejs/node/pull/14443
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2017-07-26 20:22:47 +02:00
Sebastiaan Deckers
bb29405904
lib,src: fix consistent spacing inside braces
PR-URL: https://github.com/nodejs/node/pull/14162
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-07-21 15:13:47 -04:00
Song, Bintao Garfield
e3f7a54d0a test: replace string concat with template literal
Replace the string concat at test/addons-napi/test_reference/test.js.

PR-URL: https://github.com/nodejs/node/pull/14269
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2017-07-16 06:33:09 -07:00
Kyle Farnung
73078d6072 n-api: add napi_fatal_error API
PR-URL: https://github.com/nodejs/node/pull/13971
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2017-07-13 18:48:27 -04:00
Michael Dawson
ac41db4af0 n-api: add code parameter to error helpers
In support of the effort to add error codes to all errors
generated by Node.js, add an optional code parameter to the
helper functions used to throw/create errors in N-API.

PR-URL: https://github.com/nodejs/node/pull/13988
Fixes: https://github.com/nodejs/node/issues/13933
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2017-07-13 15:05:19 -04:00
cjihrig
598a128ff4 test: handle missing V8 tests in n-api test
The N-API test testInstanceOf.js relies on several V8 test files
which may not exist in downloadable archives. If the files are
missing, this commit causes a warning to be emitted rather than
failing the test.

Refs: https://github.com/nodejs/node/issues/14113
Fixes: https://github.com/nodejs/node/issues/13344
PR-URL: https://github.com/nodejs/node/pull/14123
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-07-13 11:51:43 -04:00
Kyle Farnung
f52c707853 n-api: wrap test macros in do/while
PR-URL: https://github.com/nodejs/node/pull/14095
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-07-12 12:26:15 -04:00
Gabriel Schulhof
d5b397c9b6 n-api: Implement stricter wrapping
Use a stronger criterion to identify objects in the prototype chain that store
pointers to native data that were added by previous calls to `napi_wrap()`.

Whereas the old criterion for identifying `napi_wrap()`-injected prototype
chain objects was to consider an object with an internal field
count of 1 to be such an object, the new criterion is to consider an object
with an internal field count of 2 such that the second field holds a
`v8::External` which itself contains a pointer to a global static string unique
to N-API to be a `napi_wrap()`-injected prototype chain object.

This greatly reduces the possibility of returning a pointer that was not
previously added with `napi_wrap()`, and it allows us to recognize that an
object has already undergone `napi_wrap()` and we can thus prevent a chain of
wrappers only the first of which is accessible from appearing in the prototype
chain, as would be the result of multiple calls to `napi_wrap()` using the same
object.

PR-URL: https://github.com/nodejs/node/pull/13872
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
2017-07-12 12:05:44 -04:00
Daniel Bevenius
8f3dab4b63 n-api: fix warning in test_general
Currently the following warning is issued when buildning:

Building addon
/work/nodejs/node/test/addons-napi/test_general/
  CC(target) Debug/obj.target/test_general/test_general.o
../test_general.c:116:14: warning: variable 'result' is used
uninitialized whenever 'if' condition is false
[-Wsometimes-uninitialized]
  } else if (argument_type == napi_null) {
             ^~~~~~~~~~~~~~~~~~~~~~~~~~
../test_general.c:119:10: note: uninitialized use occurs here
  return result;
         ^~~~~~
../test_general.c:116:10: note: remove the 'if' if its condition is
always true
  } else if (argument_type == napi_null) {
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../test_general.c:101:20: note: initialize the variable 'result' to
silence this warning
  napi_value result;
                   ^
                    = NULL

This commit simply initializes result to NULL to avoid this warning.

PR-URL: https://github.com/nodejs/node/pull/14104
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2017-07-07 07:18:28 +02:00
cjihrig
9c6804c1df n-api: add napi_has_own_property()
Refs: https://github.com/nodejs/node/issues/13925
PR-URL: https://github.com/nodejs/node/pull/14063
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
2017-07-06 15:25:30 -04:00
Michael Dawson
34cf8ad73d test: add coverage for napi_typeof
We had some, but not complete coverage indirectly through
other tests.  Add test to validate it specifically and
covers cases that were not being covered.

PR-URL: https://github.com/nodejs/node/pull/13990
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2017-07-05 18:35:04 -04:00
cjihrig
732ae419b4 n-api: add napi_delete_property()
Fixes: https://github.com/nodejs/node/issues/13924
PR-URL: https://github.com/nodejs/node/pull/13934
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
2017-07-01 11:40:14 -04:00
cjihrig
f803e77b1e n-api: add napi_delete_element()
Refs: https://github.com/nodejs/node/issues/13924
PR-URL: https://github.com/nodejs/node/pull/13949
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
2017-06-30 17:08:51 -04:00
cjihrig
0f1888f313 test: verify napi_get_property() walks prototype
Refs: https://github.com/nodejs/node/issues/13925
PR-URL: https://github.com/nodejs/node/pull/13961
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2017-06-30 15:22:37 -04:00
Michael Dawson
3e18c49657 n-api: avoid crash in napi_escape_scope()
V8 will crash if escape is called twice on the same
scope.

Add checks to avoid crashing if napi_escape_scope() is
called to try and do this.

Add test that tries to call napi_create_scope() twice.

PR-URL: https://github.com/nodejs/node/pull/13651
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
2017-06-21 16:57:39 -04:00
Roman Reiss
ecf6a46d98
src,lib,test,doc: correct misspellings
PR-URL: https://github.com/nodejs/node/pull/13719
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
2017-06-17 19:17:46 +02:00
Michael Dawson
01f4d9af5e test: add coverage for napi_property_descriptor
We did not have test coverage for using a napi_value
pointing to a string or symbol for the name when
creating a property.  Add that coverage.

PR-URL: https://github.com/nodejs/node/pull/13510
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
2017-06-09 10:08:11 -04:00
Jason Ginchereau
ddba969321 test: fix build warning in addons-napi/test_object
PR-URL: https://github.com/nodejs/node/pull/13412
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2017-06-05 16:34:13 -07:00
Michael Dawson
bd4b79051a test: consolidate n-api test addons - part2
It takes time to build each of the addons used to test n-api.
Consolidate a few of the smaller ones to save build time.

Get rid of one more small addon.

PR-URL: https://github.com/nodejs/node/pull/13380
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2017-06-05 19:21:08 -04:00
Michael Dawson
7a7ac1c6db test: consolidate n-api test addons
It takes time to build each of the addons used to test n-api.
Consolidate a few of the smaller ones to save build time.

PR-URL: https://github.com/nodejs/node/pull/13317
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2017-06-01 17:50:14 -04:00
Jason Ginchereau
8ab8c33985 n-api: enable napi_wrap() to work with any object
Previously, napi_wrap() would only work with objects created from a
constructor returned by napi_define_class(). While the N-API team
was aware of this limitation, it was not clearly documented and is
likely to cause confusion anyway. It's much simpler if addons are
allowed to use any JS object. Also, the specific behavior of the
limitation is difficult to reimplement on other VMs that work
differently from V8.

V8 requires object internal fields to be declared on the object
prototype (which napi_define_class() used to do). Since it's too
late to modify the object prototype by the time napi_wrap() is
called, napi_wrap() now inserts a new object (with the internal
field) into the supplied object's prototype chain. Then it can be
retrieved from there later by napi_unwrap().

This change also includes improvements to the documentation for
napi_create_external(), partly to explain how it is different from
napi_wrap().

PR-URL: https://github.com/nodejs/node/pull/13250
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2017-06-01 11:46:55 -07:00
Jason Ginchereau
bb91879f31 test: Make N-API weak-ref GC tests asynchronous
One of the N-API weak-reference test cases already had to be made
asynchronous to handle different behavior in a newer V8 version:
https://github.com/nodejs/node/pull/12864
When porting N-API to Node-ChakraCore, we found more of the test
cases needed similar treatment:
https://github.com/nodejs/node-chakracore/issues/246 So to make
thes tests more robust (and avoid having differences in the test code
for Node-ChakraCore), I am refactoring the tests in this file to
insert a `setImmedate()` callback before every call to `gc()` and
assertions about the effects of the GC.

PR-URL: https://github.com/nodejs/node/pull/13121
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2017-05-30 12:45:27 -07:00
Michael Dawson
effeff1843 test: improve n-api coverage for typed arrays
Add testing for all types of typed arrays.
Add testing for napi_is_arraybuffer.

PR-URL: https://github.com/nodejs/node/pull/13244
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Kunal Pathak <kunal.pathak@microsoft.com>
Reviewed-By: Hitesh Kanwathirtha <digitalinfinity@gmail.com>
2017-05-30 13:50:45 -04:00
Michael Dawson
d9ee297afb n-api: add napi_get_version
Add napi_get_version function so that addons can
query the level of N-API supported.

PR-URL: https://github.com/nodejs/node/pull/13207
Fixes: https://github.com/nodejs/abi-stable-node/issues/231
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
2017-05-26 17:05:42 -04:00
Michael Dawson
196190015d test: add coverage for napi_has_named_property
Add test to cover napi_has_named_property

PR-URL: https://github.com/nodejs/node/pull/13178
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
2017-05-25 19:35:18 -04:00
Michael Dawson
47919b3b6f
test: increase n-api constructor coverage
Add tests to validate that properties marked as static
are available through the class as opposed to instances

PR-URL: https://github.com/nodejs/node/pull/13124
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Hitesh Kanwathirtha <digitalinfinity@gmail.com>
2017-05-23 21:05:30 +02:00
Jason Ginchereau
a63b245b0a n-api: Retain last code when getting error info
Unlike most N-API functions, `napi_get_last_error_info()` should not
clear the last error code when successful, because a pointer to (not
a copy of) the error info structure is returned via an out parameter.

PR-URL: https://github.com/nodejs/node/pull/13087
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2017-05-19 17:04:02 -04:00
Michael Dawson
0dd8b9a965 test: Improve N-API test coverage
- add coverage for napi_get_prototype
- add coverage for napi_strict_equals

PR-URL: https://github.com/nodejs/node/pull/13044
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
2017-05-17 18:03:35 -04:00
Michael Dawson
47c3c58704 test: improve N-API test coverage
Add tests to cover functions that return globals

PR-URL: https://github.com/nodejs/node/pull/13006
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
2017-05-15 20:12:40 -04:00
Rich Trott
9516aa19c1 test: add common.mustCall() to NAPI exception test
Use `common.mustCall()` to confirm that function is invoked.

PR-URL: https://github.com/nodejs/node/pull/12959
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-05-13 16:49:24 -07:00
Michael Dawson
654afa2c19 test: improve n-api array func coverage
- add coverage for napi_has_element
- add coverage for napi_create_array_with_length

PR-URL: https://github.com/nodejs/node/pull/12890
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-05-11 09:16:17 -04:00
Jason Ginchereau
2e3fef7628 n-api: Handle fatal exception in async callback
- Create a handle scope before invoking the async completion
   callback, because it is basically always needed, easy for user
   code to forget, and this makes it more consistent with ordinary
   N-API function callbacks.

 - Check for an unhandled JS exception after invoking an async
   completion callback, and report it via `node::FatalException()`.

 - Add a corresponding test case for an exception in async callback.

Previously, any unhandled JS exception thrown from a
`napi_async_complete_callback` would be silently ignored. Among other
things this meant assertions in some test cases could be undetected.

PR-URL: https://github.com/nodejs/node/pull/12838
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-05-09 15:17:36 -04:00
Jason Ginchereau
2bbabb1f85 n-api: napi_get_cb_info should fill array
When the number of args requested is greater than the actual number of
args supplied to the function call, the remainder of the args array
should be filled in with `undefined` values. Because of this bug, the
remainder of the array was left uninitialized, which could cause a
crash.

Refer to the documentation for the `argv` parameter at
https://github.com/nodejs/node/blob/master/doc/api/n-api.md#napi_get_cb_info

PR-URL: https://github.com/nodejs/node/pull/12863
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2017-05-09 15:12:15 -04:00
Michaël Zasso
0a734fec88 test: fix napi test_reference for recent V8
PR-URL: https://github.com/nodejs/node/pull/12864
Ref: https://github.com/nodejs/node/pull/12551#issuecomment-297949361
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2017-05-08 01:10:51 +02:00
Hitesh Kanwathirtha
73d9c0f903 test: port test for make_callback to n-api
Improved test coverage for napi_make_callback by porting the
existing addons/make_callback test to n-api

PR-URL: https://github.com/nodejs/node/pull/12409
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
2017-05-06 12:24:43 -04:00
Michael Dawson
94a120cf65 test: add coverage for error apis
Add coverage for N-API functions related to
throwing and creating errors.  A number of these
are currently showing as not having any
coverage in the nightly code coverage reports.

PR-URL: https://github.com/nodejs/node/pull/12729
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-05-05 04:57:54 -04:00
Michael Dawson
8aca66a1f3
test: fix warning in n-api reference test
Add cast to avoid warning during build of addon.

PR-URL: https://github.com/nodejs/node/pull/12730
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2017-05-03 15:38:18 +02:00
Rich Trott
a180259e42 test,lib,doc: use function declarations
Replace function expressions with function declarations in preparation
for a lint rule requiring function declarations.

PR-URL: https://github.com/nodejs/node/pull/12711
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
2017-05-01 15:56:58 -07:00
gwer
0142276977
test: replace indexOf with includes
Start the transition to Array.prototype.includes() and
String.prototype.includes().  This commit refactors most of the
comparisons of Array.prototype.indexOf() and String.prototype.indexOf()
return values with -1 to the former methods in tests.

PR-URL: https://github.com/nodejs/node/pull/12604
Refs: https://github.com/nodejs/node/issues/12586
Reviewed-By: Alexey Orlenko <eaglexrlnk@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
2017-04-30 04:08:46 +03:00
Jason Ginchereau
427125491f n-api: Reference and external tests
- Add a test project to addons-napi that covers the
   N-API reference and external APIs
 - Fix a bug in napi_typeof that was found by the new tests

PR-URL: https://github.com/nodejs/node/pull/12551
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2017-04-27 16:33:19 -04:00
Michael Dawson
1d968030d4 test: add coverage for napi_cancel_async_work
adding test coverage for napi_cancel_async_work based
on coverage report

PR-URL: https://github.com/nodejs/node/pull/12575
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2017-04-26 21:53:03 -04:00
Michael Dawson
4241577112 test: test doc'd napi_get_value_int32 behaviour
We chose to document this in the docs as there are
different possible behaviours.  Adding this test to validate
that all vm implementations do it the same way.

PR-URL: https://github.com/nodejs/node/pull/12633
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2017-04-26 16:00:16 -04:00
Jason Ginchereau
b7a341d7e5
n-api: Enable scope and ref APIs during exception
N-API is somewhat strict about blocking calls to many APIs while there
is a pending exception. The NAPI_PREAMBLE macro at the beginning of
many API implementations checks for a pending exception. However, a
subset of the APIs (which don't call back into JavaScript) still need
to work while in a pending-exception state. This changes the reference
APIs (equivalent to v8::Persistent) and handle scope APIs so that they
can be used for cleanup up while an exception is pending.

We may decide to similarly enable a few other APIs later, (which would
be a non-breaking change) but we know at least these are needed now
to unblock some specific scenarios.

Fixes: https://github.com/nodejs/abi-stable-node/issues/122
Fixes: https://github.com/nodejs/abi-stable-node/issues/228
PR-URL: https://github.com/nodejs/node/pull/12524
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
2017-04-25 22:02:56 +02:00
Gabriel Schulhof
ba7bac5c37
n-api: tighten null-checking and clean up last error
We left out null-checks for many of the parameters passed to our APIs.
In particular, arguments of type `napi_value` were often accepted
without a null-check, even though they should never be null.

Additionally, many APIs simply returned `napi_ok` on success. This
leaves in place an error that may have occurred in a previous N-API
call. Others (those which perform `NAPI_PREAMBLE(env)` at the top)
OTOH explicitly clear the last error before proceeding. With this
modification all APIs explicitly clear the last error on success.

Fixes: https://github.com/nodejs/abi-stable-node/issues/227
PR-URL: https://github.com/nodejs/node/pull/12539
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2017-04-25 19:29:34 +02:00
Jason Ginchereau
468275ac79 n-api: remove napi_get_value_string_length()
This API doesn't serve much purpose, and is only likely to cause
confusion and bugs. The intention was that this would return the
number of characters in a string independent of encoding, but
that's not generally useful. In almost all cases, one of the
encoding-specific napi_get_value_string_* APIs is more correct.
(Pass a null buffer if only the encoded length is desired.)

Anyway the current implementation of napi_get_value_string_length()
is technically wrong: it returns the number of 2-byte code units of
the UTF-16 encoding, but there are actually some characters that
are encoded as two UTF-16 code units.

Note the JavaScript String.prototype.length property returns the
number of UTF-16 code units, which may be different from the number
of characters. So, getting the true character count is not common
with JavaScript, and is probably best left to specialized
internationalization libraries.

PR-URL: https://github.com/nodejs/node/pull/12496
Fixes: https://github.com/nodejs/abi-stable-node/issues/226
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2017-04-20 13:27:17 -04:00