Repository: nodejs/node
Pull request: node-api: test passing NULL to napi_define_class

<!--
Before submitting a pull request, please read
https://github.com/nodejs/node/blob/HEAD/CONTRIBUTING.md.

Commit message formatting guidelines:
https://github.com/nodejs/node/blob/HEAD/doc/contributing/pull-requests.md#commit-message-guidelines

For code changes:
1. Include tests for any bug fixes or new features.
2. Update documentation if relevant.
3. Ensure that `make -j4 test` (UNIX), or `vcbuild test` (Windows) passes.

Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.
-->


Changed files:
filename: test/js-native-api/test_constructor/binding.gyp
patch: @@ -5,7 +5,8 @@
       "sources": [
         "../common.c",
         "../entry_point.c",
-        "test_constructor.c"
+        "test_constructor.c",
+        "test_null.c",
       ]
     }
   ]

filename: test/js-native-api/test_constructor/test_constructor.c
patch: @@ -1,5 +1,6 @@
 #include <js_native_api.h>
 #include "../common.h"
+#include "test_null.h"
 
 static double value_ = 1;
 static double static_value_ = 10;
@@ -191,6 +192,8 @@ napi_value Init(napi_env env, napi_value exports) {
   NODE_API_CALL(env, napi_define_class(env, "MyObject", NAPI_AUTO_LENGTH, New,
       NULL, sizeof(properties)/sizeof(*properties), properties, &cons));
 
+  init_test_null(env, cons);
+
   return cons;
 }
 EXTERN_C_END

filename: test/js-native-api/test_constructor/test_null.c
patch: @@ -0,0 +1,111 @@
+#include <js_native_api.h>
+
+#include "../common.h"
+#include "test_null.h"
+
+static int some_data = 0;
+
+static napi_value TestConstructor(napi_env env, napi_callback_info info) {
+  return NULL;
+}
+
+static napi_value TestDefineClass(napi_env env, napi_callback_info info) {
+  napi_value return_value, cons;
+
+  const napi_property_descriptor prop =
+      DECLARE_NODE_API_PROPERTY("testConstructor", TestConstructor);
+
+  NODE_API_CALL(env, napi_create_object(env, &return_value));
+  add_returned_status(env,
+                      "envIsNull",
+                      return_value,
+                      "Invalid argument",
+                      napi_invalid_arg,
+                      napi_define_class(NULL,
+                                        "TestClass",
+                                        NAPI_AUTO_LENGTH,
+                                        TestConstructor,
+                                        &some_data,
+                                        1,
+                                        &prop,
+                                        &cons));
+
+  napi_define_class(env,
+                    NULL,
+                    NAPI_AUTO_LENGTH,
+                    TestConstructor,
+                    &some_data,
+                    1,
+                    &prop,
+                    &cons);
+  add_last_status(env, "nameIsNull", return_value);
+
+  napi_define_class(
+      env, "TestClass", 0, TestConstructor, &some_data, 1, &prop, &cons);
+  add_last_status(env, "lengthIsZero", return_value);
+
+  napi_define_class(
+      env, "TestClass", NAPI_AUTO_LENGTH, NULL, &some_data, 1, &prop, &cons);
+  add_last_status(env, "nativeSideIsNull", return_value);
+
+  napi_define_class(env,
+                    "TestClass",
+                    NAPI_AUTO_LENGTH,
+                    TestConstructor,
+                    NULL,
+                    1,
+                    &prop,
+                    &cons);
+  add_last_status(env, "dataIsNull", return_value);
+
+  napi_define_class(env,
+                    "TestClass",
+                    NAPI_AUTO_LENGTH,
+                    TestConstructor,
+                    &some_data,
+                    0,
+                    &prop,
+                    &cons);
+  add_last_status(env, "propsLengthIsZero", return_value);
+
+  napi_define_class(env,
+                    "TestClass",
+                    NAPI_AUTO_LENGTH,
+                    TestConstructor,
+                    &some_data,
+                    1,
+                    NULL,
+                    &cons);
+  add_last_status(env, "propsIsNull", return_value);
+
+  napi_define_class(env,
+                    "TestClass",
+                    NAPI_AUTO_LENGTH,
+                    TestConstructor,
+                    &some_data,
+                    1,
+                    &prop,
+                    NULL);
+  add_last_status(env, "resultIsNull", return_value);
+
+  return return_value;
+}
+
+void init_test_null(napi_env env, napi_value exports) {
+  napi_value test_null;
+
+  const napi_property_descriptor test_null_props[] = {
+      DECLARE_NODE_API_PROPERTY("testDefineClass", TestDefineClass),
+  };
+
+  NODE_API_CALL_RETURN_VOID(env, napi_create_object(env, &test_null));
+  NODE_API_CALL_RETURN_VOID(
+      env,
+      napi_define_properties(env,
+                             test_null,
+                             sizeof(test_null_props) / sizeof(*test_null_props),
+                             test_null_props));
+
+  NODE_API_CALL_RETURN_VOID(
+      env, napi_set_named_property(env, exports, "testNull", test_null));
+}

filename: test/js-native-api/test_constructor/test_null.h
patch: @@ -0,0 +1,8 @@
+#ifndef TEST_JS_NATIVE_API_TEST_OBJECT_TEST_NULL_H_
+#define TEST_JS_NATIVE_API_TEST_OBJECT_TEST_NULL_H_
+
+#include <js_native_api.h>
+
+void init_test_null(napi_env env, napi_value exports);
+
+#endif  // TEST_JS_NATIVE_API_TEST_OBJECT_TEST_NULL_H_

filename: test/js-native-api/test_constructor/test_null.js
patch: @@ -0,0 +1,18 @@
+'use strict';
+const common = require('../../common');
+const assert = require('assert');
+
+// Test passing NULL to object-related N-APIs.
+const { testNull } = require(`./build/${common.buildType}/test_constructor`);
+const expectedResult = {
+  envIsNull: 'Invalid argument',
+  nameIsNull: 'Invalid argument',
+  lengthIsZero: 'napi_ok',
+  nativeSideIsNull: 'Invalid argument',
+  dataIsNull: 'napi_ok',
+  propsLengthIsZero: 'napi_ok',
+  propsIsNull: 'Invalid argument',
+  resultIsNull: 'Invalid argument',
+};
+
+assert.deepStrictEqual(testNull.testDefineClass(), expectedResult);