diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2019-01-05 06:02:33 +0800 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2019-01-12 22:56:02 +0800 |
commit | 92e95f17b64838d4cf77343c1a814d4ebd795217 (patch) | |
tree | 25aa45e582b5657505fff57ba78578e2f4dc248e /test | |
parent | 18d3aebb06903a0151bdecbad2b6b7077b965d03 (diff) | |
download | android-node-v8-92e95f17b64838d4cf77343c1a814d4ebd795217.tar.gz android-node-v8-92e95f17b64838d4cf77343c1a814d4ebd795217.tar.bz2 android-node-v8-92e95f17b64838d4cf77343c1a814d4ebd795217.zip |
src: simplify NativeModule caching and remove redundant data
- Remove `NativeModule._source` - the compilation is now entirely
done in C++ and `process.binding('natives')` is implemented
directly in the binding loader so there is no need to store
additional source code strings.
- Instead of using an object as `NativeModule._cached` and insert
into it after compilation of each native module, simply prebuild
a JS map filled with all the native modules and infer the
state of compilation through `mod.loading`/`mod.loaded`.
- Rename `NativeModule.nonInternalExists` to
`NativeModule.canBeRequiredByUsers` and precompute that
property for all the native modules during bootstrap instead
of branching in every require call during runtime. This also fixes
the bug where `worker_threads` can be made available with
`--expose-internals`.
- Rename `NativeModule.requireForDeps` to
`NativeModule.requireWithFallbackInDeps`.
- Add a test to make sure we do not accidentally leak any module
to the global namespace.
PR-URL: https://github.com/nodejs/node/pull/25352
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'test')
-rw-r--r-- | test/code-cache/test-code-cache.js | 5 | ||||
-rw-r--r-- | test/parallel/test-internal-module-require.js | 112 |
2 files changed, 113 insertions, 4 deletions
diff --git a/test/code-cache/test-code-cache.js b/test/code-cache/test-code-cache.js index 367e72168a..54bd0b0e2a 100644 --- a/test/code-cache/test-code-cache.js +++ b/test/code-cache/test-code-cache.js @@ -5,15 +5,12 @@ // and the cache is used when built in modules are compiled. // Otherwise, verifies that no cache is used when compiling builtins. -require('../common'); +const { isMainThread } = require('../common'); const assert = require('assert'); const { cachableBuiltins, cannotUseCache } = require('internal/bootstrap/cache'); -const { - isMainThread -} = require('worker_threads'); const { internalBinding diff --git a/test/parallel/test-internal-module-require.js b/test/parallel/test-internal-module-require.js new file mode 100644 index 0000000000..17559d2287 --- /dev/null +++ b/test/parallel/test-internal-module-require.js @@ -0,0 +1,112 @@ +'use strict'; + +// Flags: --expose-internals +// This verifies that +// 1. We do not leak internal modules unless the --require-internals option +// is on. +// 2. We do not accidentally leak any modules to the public global scope. +// 3. Deprecated modules are properly deprecated. + +const common = require('../common'); + +if (!common.isMainThread) { + common.skip('Cannot test the existence of --expose-internals from worker'); +} + +const assert = require('assert'); +const fork = require('child_process').fork; + +const expectedPublicModules = new Set([ + '_http_agent', + '_http_client', + '_http_common', + '_http_incoming', + '_http_outgoing', + '_http_server', + '_stream_duplex', + '_stream_passthrough', + '_stream_readable', + '_stream_transform', + '_stream_wrap', + '_stream_writable', + '_tls_common', + '_tls_wrap', + 'assert', + 'async_hooks', + 'buffer', + 'child_process', + 'cluster', + 'console', + 'constants', + 'crypto', + 'dgram', + 'dns', + 'domain', + 'events', + 'fs', + 'http', + 'http2', + 'https', + 'inspector', + 'module', + 'net', + 'os', + 'path', + 'perf_hooks', + 'process', + 'punycode', + 'querystring', + 'readline', + 'repl', + 'stream', + 'string_decoder', + 'sys', + 'timers', + 'tls', + 'trace_events', + 'tty', + 'url', + 'util', + 'v8', + 'vm', + 'worker_threads', + 'zlib' +]); + +if (process.argv[2] === 'child') { + assert(!process.execArgv.includes('--expose-internals')); + process.once('message', ({ allBuiltins }) => { + const publicModules = new Set(); + for (const id of allBuiltins) { + if (id.startsWith('internal/')) { + common.expectsError(() => { + require(id); + }, { + code: 'MODULE_NOT_FOUND', + message: `Cannot find module '${id}'` + }); + } else { + require(id); + publicModules.add(id); + } + } + assert(allBuiltins.length > publicModules.size); + // Make sure all the public modules are available through + // require('module').builtinModules + assert.deepStrictEqual( + publicModules, + new Set(require('module').builtinModules) + ); + assert.deepStrictEqual(publicModules, expectedPublicModules); + }); +} else { + assert(process.execArgv.includes('--expose-internals')); + const child = fork(__filename, ['child'], { + execArgv: [] + }); + const { builtinModules } = require('module'); + // When --expose-internals is on, require('module').builtinModules + // contains internal modules. + const message = { allBuiltins: builtinModules }; + child.send(message); +} |