From 25e9fee61c1f3096bad4c3f9714614c9b979a087 Mon Sep 17 00:00:00 2001 From: Yazhong Liu Date: Wed, 16 Jan 2019 21:50:46 +0800 Subject: [PATCH] Revert "module: add require.resolve (#445)" (#485) This reverts commit 7807fe43806e86d43726e67cf7e493d65f551aa1. This causes the `require` performance problems. --- benchmark/module/require.js | 18 ---------- benchmark/module/require.resolve.js | 18 ---------- docs/api/Module.md | 6 +--- src/js/module.js | 49 +++++---------------------- test/common/fixtures.js | 16 --------- test/run_pass/test_require_resolve.js | 17 ---------- test/testsets.json | 2 -- 7 files changed, 9 insertions(+), 117 deletions(-) delete mode 100644 benchmark/module/require.js delete mode 100644 benchmark/module/require.resolve.js delete mode 100644 test/common/fixtures.js delete mode 100644 test/run_pass/test_require_resolve.js diff --git a/benchmark/module/require.js b/benchmark/module/require.js deleted file mode 100644 index 2974e7277..000000000 --- a/benchmark/module/require.js +++ /dev/null @@ -1,18 +0,0 @@ -'use strict'; - -var common = require('../common.js'); - -var bench = common.createBenchmark(main, { - n: [512] -}); - -function main(opts) { - var n = opts.n; - bench.start(); - for (var i = 0; i < n; ++i) { - require('os'); - require('../common.js'); - require.cache = {}; - } - bench.end(n); -} diff --git a/benchmark/module/require.resolve.js b/benchmark/module/require.resolve.js deleted file mode 100644 index 0bcc86bd8..000000000 --- a/benchmark/module/require.resolve.js +++ /dev/null @@ -1,18 +0,0 @@ -'use strict'; - -var common = require('../common.js'); - -var bench = common.createBenchmark(main, { - n: [512] -}); - -function main(opts) { - var n = opts.n; - bench.start(); - for (var i = 0; i < n; ++i) { - require.resolve('os'); - require.resolve('../common.js'); - require.cache = {}; - } - bench.end(n); -} diff --git a/docs/api/Module.md b/docs/api/Module.md index f4efd79ca..9bdc01e50 100644 --- a/docs/api/Module.md +++ b/docs/api/Module.md @@ -47,12 +47,8 @@ For each directory in search paths above: - If `id/package.json` contains **main** property, load the file named **main** property. - If `id/package.json` exists, but neither the **main** property nor the file named **main** property exist, load `index.js`. -### require.resolve(request) -* `request` {string} Module name to be queried. - -Get the module absolute path about `request` module but not load it. - ### require.main * accessing the main module When a file is run directly, `require.main` is set to its module. That means that it is possible to determine whether a file has been run directly by testing `require.main === module`. For a file foo.js, this will be true if run via cmd directly, but false if run by `require('./foo')`, because `module` object is set to the `foo` module but `require.main` is still your entry file module. So you can get the entry point of the current application from checking `require.main.filename` anywhere. + diff --git a/src/js/module.js b/src/js/module.js index b40f2398f..347a64504 100644 --- a/src/js/module.js +++ b/src/js/module.js @@ -126,21 +126,18 @@ iotjs_module_t.resolveFilepath = function(id, directories) { }; -iotjs_module_t._resolveFilepath = function(id, root, ext_index) { +iotjs_module_t._resolveFilepath = function(id, root) { var modulePath = root ? path.join(root, id) : id; var filepath; - var exts = ['.js', '.json', '.node']; - if (ext_index === undefined) { - ext_index = 0; - } + var ext = '.js'; // id[.ext] - if (filepath = tryPath(modulePath, exts[ext_index])) { + if (filepath = tryPath(modulePath, ext)) { return filepath; } // id/index[.ext] - if (filepath = tryPath(modulePath + '/index', exts[ext_index])) { + if (filepath = tryPath(modulePath + '/index', ext)) { return filepath; } @@ -152,14 +149,10 @@ iotjs_module_t._resolveFilepath = function(id, root, ext_index) { var pkgMainFile = JSON.parse(pkgSrc).main; // pkgmain[.ext] - if (filepath = tryPath(modulePath + '/' + pkgMainFile, exts[ext_index])) { + if (filepath = tryPath(modulePath + '/' + pkgMainFile, ext)) { return filepath; } } - ext_index++; - if (ext_index < exts.length) { - return iotjs_module_t._resolveFilepath(id, root, ext_index); - } }; @@ -278,34 +271,6 @@ iotjs_module_t.load = function(id, parent, isMain) { return module.exports; }; -function _makeRequireFunction(mod) { - var Module = mod.constructor; - function require(id) { - return mod.require(id); - } - - function _resolve(request) { - if (!request || typeof request !== 'string') { - throw new TypeError('module must be a non-null string'); - } - - if (process.builtin_modules[request]) { - return request; - } - - var path = Module.resolveModPath(request, mod); - if (!path) { - throw new Error('Module not found: ' + request); - } - return path; - } - require.resolve = _resolve; - require.main = mainModule; - require.cache = Module.cache; - - return require; -} - iotjs_module_t.prototype.compile = function(snapshot) { var __filename = this.filename; var __dirname = path.dirname(__filename); @@ -318,7 +283,9 @@ iotjs_module_t.prototype.compile = function(snapshot) { throw new TypeError('Invalid snapshot file.'); } - var _require = _makeRequireFunction(this); + var _require = this.require.bind(this); + _require.main = mainModule; + _require.cache = iotjs_module_t.cache; fn.apply(this.exports, [ this.exports, // exports diff --git a/test/common/fixtures.js b/test/common/fixtures.js deleted file mode 100644 index 261b9109f..000000000 --- a/test/common/fixtures.js +++ /dev/null @@ -1,16 +0,0 @@ -'use strict'; - -var path = require('path'); - -var fixturesDir = path.join(__dirname, '../../'); - -function fixturesPath() { - return path.join.apply( - this, - [fixturesDir].concat(Array.prototype.slice.apply(arguments))); -} - -module.exports = { - fixturesDir: fixturesDir, - path: fixturesPath -}; diff --git a/test/run_pass/test_require_resolve.js b/test/run_pass/test_require_resolve.js deleted file mode 100644 index 74bc5bb6c..000000000 --- a/test/run_pass/test_require_resolve.js +++ /dev/null @@ -1,17 +0,0 @@ -'use strict'; - -var assert = require('assert'); -var fixtures = require('../common/fixtures'); - -assert.strictEqual( - require.resolve('./test_util').toLowerCase(), - fixtures.path('./test/run_pass/test_util.js').toLowerCase()); - -assert.strictEqual( - require.resolve('os').toLowerCase(), 'os'); - -[1, false, null, undefined, {}].forEach((value) => { - assert.throws(function() { - require.resolve(value); - }, TypeError); -}); diff --git a/test/testsets.json b/test/testsets.json index 89a4b2275..28c004e08 100644 --- a/test/testsets.json +++ b/test/testsets.json @@ -122,8 +122,6 @@ { "name": "test_process_unhandled_rejection.js" }, { "name": "test_pwm_async.js", "skip": ["all"], "reason": "need to setup test environment" }, { "name": "test_pwm_sync.js", "skip": ["all"], "reason": "need to setup test environment" }, - { "name": "test_require_main.js" }, - { "name": "test_require_resolve.js" }, { "name": "test_shebang.js" }, { "name": "test_signal_simple.js" }, { "name": "test_spi.js", "skip": ["linux"], "reason": "Differend env on Linux desktop/travis/rpi" },