From cd83df386bc0c567bda9a00ed297467b6046d785 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Fri, 20 Apr 2018 22:57:33 -0400 Subject: n-api: initialize a module via a special symbol Much like regular modules, N-API modules can also benefit from having a special symbol which they can expose. Fixes: https://github.com/nodejs/node/issues/19845 PR-URL: https://github.com/nodejs/node/pull/20161 Reviewed-By: Ben Noordhuis --- test/addons-napi/1_hello_world/binding.c | 4 +--- test/addons-napi/1_hello_world/test.js | 11 +++++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) (limited to 'test/addons-napi/1_hello_world') diff --git a/test/addons-napi/1_hello_world/binding.c b/test/addons-napi/1_hello_world/binding.c index 27d49079966..6efc14ee66e 100644 --- a/test/addons-napi/1_hello_world/binding.c +++ b/test/addons-napi/1_hello_world/binding.c @@ -10,10 +10,8 @@ napi_value Method(napi_env env, napi_callback_info info) { return world; } -napi_value Init(napi_env env, napi_value exports) { +NAPI_MODULE_INIT() { napi_property_descriptor desc = DECLARE_NAPI_PROPERTY("hello", Method); NAPI_CALL(env, napi_define_properties(env, exports, 1, &desc)); return exports; } - -NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) diff --git a/test/addons-napi/1_hello_world/test.js b/test/addons-napi/1_hello_world/test.js index c975c48a733..d1d67d34f68 100644 --- a/test/addons-napi/1_hello_world/test.js +++ b/test/addons-napi/1_hello_world/test.js @@ -1,6 +1,13 @@ 'use strict'; const common = require('../../common'); const assert = require('assert'); -const addon = require(`./build/${common.buildType}/binding`); +const bindingPath = require.resolve(`./build/${common.buildType}/binding`); +const binding = require(bindingPath); +assert.strictEqual(binding.hello(), 'world'); +console.log('binding.hello() =', binding.hello()); -assert.strictEqual(addon.hello(), 'world'); +// Test multiple loading of the same module. +delete require.cache[bindingPath]; +const rebinding = require(bindingPath); +assert.strictEqual(rebinding.hello(), 'world'); +assert.notStrictEqual(binding.hello, rebinding.hello); -- cgit v1.2.3