From 9a02de7084e3f3c959d1c7e32bda67cc1d7002bd Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Wed, 16 May 2018 11:05:32 -0400 Subject: [PATCH] n-api: throw when entry point is null PR-URL: https://github.com/nodejs/node/pull/20779 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig --- src/node_api.cc | 6 ++++++ test/addons-napi/test_null_init/binding.gyp | 8 ++++++++ test/addons-napi/test_null_init/test.js | 7 +++++++ test/addons-napi/test_null_init/test_null_init.c | 3 +++ 4 files changed, 24 insertions(+) create mode 100644 test/addons-napi/test_null_init/binding.gyp create mode 100644 test/addons-napi/test_null_init/test.js create mode 100644 test/addons-napi/test_null_init/test_null_init.c diff --git a/src/node_api.cc b/src/node_api.cc index fb3ea5df2ce006..a83244131ff5a8 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -868,6 +868,12 @@ void napi_module_register_by_symbol(v8::Local exports, v8::Local module, v8::Local context, napi_addon_register_func init) { + if (init == nullptr) { + node::Environment::GetCurrent(context)->ThrowError( + "Module has no declared entry point."); + return; + } + // Create a new napi_env for this module or reference one if a pre-existing // one is found. napi_env env = v8impl::GetEnv(context); diff --git a/test/addons-napi/test_null_init/binding.gyp b/test/addons-napi/test_null_init/binding.gyp new file mode 100644 index 00000000000000..27701616e338db --- /dev/null +++ b/test/addons-napi/test_null_init/binding.gyp @@ -0,0 +1,8 @@ +{ + 'targets': [ + { + 'target_name': 'test_null_init', + 'sources': [ 'test_null_init.c' ] + } + ] +} diff --git a/test/addons-napi/test_null_init/test.js b/test/addons-napi/test_null_init/test.js new file mode 100644 index 00000000000000..6e6bf51839bed0 --- /dev/null +++ b/test/addons-napi/test_null_init/test.js @@ -0,0 +1,7 @@ +'use strict'; +const common = require('../../common'); +const assert = require('assert'); + +assert.throws( + () => require(`./build/${common.buildType}/test_null_init`), + /Module has no declared entry point[.]/); diff --git a/test/addons-napi/test_null_init/test_null_init.c b/test/addons-napi/test_null_init/test_null_init.c new file mode 100644 index 00000000000000..d9d2200488ce41 --- /dev/null +++ b/test/addons-napi/test_null_init/test_null_init.c @@ -0,0 +1,3 @@ +#include + +NAPI_MODULE(NODE_GYP_MODULE_NAME, NULL)