Skip to content

Commit 3f07a44

Browse files
committed
Unify importing of node fs and path modules
We were importing these modules in different places and under different names. I went with `fs` for the name of the `fs` import since that is what `library_nodefs.js` and it is by far the heaviest user of this module.
1 parent aca650f commit 3f07a44

File tree

8 files changed

+23
-19
lines changed

8 files changed

+23
-19
lines changed

src/library_nodefs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
mergeInto(LibraryManager.library, {
88
$NODEFS__deps: ['$FS', '$PATH', '$ERRNO_CODES', '$mmapAlloc'],
9-
$NODEFS__postset: 'if (ENVIRONMENT_IS_NODE) { var fs = require("fs"); var NODEJS_PATH = require("path"); NODEFS.staticInit(); }',
9+
$NODEFS__postset: 'if (ENVIRONMENT_IS_NODE) { requireNodeFS(); NODEFS.staticInit(); }',
1010
$NODEFS: {
1111
isWindows: false,
1212
staticInit: function() {
@@ -227,7 +227,7 @@ mergeInto(LibraryManager.library, {
227227
var path = NODEFS.realPath(node);
228228
try {
229229
path = fs.readlinkSync(path);
230-
path = NODEJS_PATH.relative(NODEJS_PATH.resolve(node.mount.opts.root), path);
230+
path = nodePath.relative(nodePath.resolve(node.mount.opts.root), path);
231231
return path;
232232
} catch (e) {
233233
if (!e.code) throw e;

src/library_noderawfs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mergeInto(LibraryManager.library, {
1919
lookupPath: function(path, opts) {
2020
opts = opts || {};
2121
if (opts.parent) {
22-
path = NODEJS_PATH.dirname(path);
22+
path = nodePath.dirname(path);
2323
}
2424
var st = fs.lstatSync(path);
2525
var mode = NODEFS.getMode(path);

src/library_tty.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ mergeInto(LibraryManager.library, {
112112
var bytesRead = 0;
113113

114114
try {
115-
bytesRead = nodeFS.readSync(process.stdin.fd, buf, 0, BUFSIZE, null);
115+
bytesRead = fs.readSync(process.stdin.fd, buf, 0, BUFSIZE, null);
116116
} catch(e) {
117117
// Cross-platform differences: on Windows, reading EOF throws an exception, but on other OSes,
118118
// reading EOF returns 0. Uniformize behavior by treating the EOF exception to return 0.

src/node_shell_read.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44
* SPDX-License-Identifier: MIT
55
*/
66

7+
requireNodeFS = function() {
8+
if (!fs) fs = require('fs');
9+
if (!nodePath) nodePath = require('path');
10+
}
11+
712
read_ = function shell_read(filename, binary) {
813
#if SUPPORT_BASE64_EMBEDDING
914
var ret = tryParseAsDataURI(filename);
1015
if (ret) {
1116
return binary ? ret : ret.toString();
1217
}
1318
#endif
14-
if (!nodeFS) nodeFS = require('fs');
15-
if (!nodePath) nodePath = require('path');
19+
requireNodeFS();
1620
filename = nodePath['normalize'](filename);
17-
return nodeFS['readFileSync'](filename, binary ? null : 'utf8');
21+
return fs.readFileSync(filename, binary ? null : 'utf8');
1822
};
1923

2024
readBinary = function readBinary(filename) {
@@ -33,10 +37,9 @@ readAsync = function readAsync(filename, onload, onerror) {
3337
onload(ret);
3438
}
3539
#endif
36-
if (!nodeFS) nodeFS = require('fs');
37-
if (!nodePath) nodePath = require('path');
40+
requireNodeFS();
3841
filename = nodePath['normalize'](filename);
39-
nodeFS['readFile'](filename, function(err, data) {
42+
fs.readFile(filename, function(err, data) {
4043
if (err) onerror(err);
4144
else onload(data.buffer);
4245
});

src/preamble.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -925,14 +925,14 @@ function instantiateSync(file, info) {
925925
// to load ok, but we do actually recompile the binary every time).
926926
var cachedCodeFile = '{{{ WASM_BINARY_FILE }}}.' + v8.cachedDataVersionTag() + '.cached';
927927
cachedCodeFile = locateFile(cachedCodeFile);
928-
if (!nodeFS) nodeFS = require('fs');
929-
var hasCached = nodeFS.existsSync(cachedCodeFile);
928+
requireNodeFS();
929+
var hasCached = fs.existsSync(cachedCodeFile);
930930
if (hasCached) {
931931
#if RUNTIME_LOGGING
932932
err('NODE_CODE_CACHING: loading module');
933933
#endif
934934
try {
935-
module = v8.deserialize(nodeFS.readFileSync(cachedCodeFile));
935+
module = v8.deserialize(fs.readFileSync(cachedCodeFile));
936936
} catch (e) {
937937
err('NODE_CODE_CACHING: failed to deserialize, bad cache file? (' + cachedCodeFile + ')');
938938
// Save the new compiled code when we have it.
@@ -947,7 +947,7 @@ function instantiateSync(file, info) {
947947
#if RUNTIME_LOGGING
948948
err('NODE_CODE_CACHING: saving module');
949949
#endif
950-
nodeFS.writeFileSync(cachedCodeFile, v8.serialize(module));
950+
fs.writeFileSync(cachedCodeFile, v8.serialize(module));
951951
}
952952
#else // NODE_CODE_CACHING
953953
module = new WebAssembly.Module(binary);

src/shell.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,9 @@ function logExceptionOnExit(e) {
174174
#endif
175175

176176
#if ENVIRONMENT_MAY_BE_NODE
177-
var nodeFS;
177+
var fs;
178178
var nodePath;
179+
var requireNodeFS;
179180

180181
if (ENVIRONMENT_IS_NODE) {
181182
#if ENVIRONMENT
@@ -248,7 +249,7 @@ if (ENVIRONMENT_IS_NODE) {
248249
#if WASM == 2
249250
// If target shell does not support Wasm, load the JS version of the code.
250251
if (typeof WebAssembly === 'undefined') {
251-
var fs = require('fs');
252+
requireNodeFS();
252253
eval(fs.readFileSync(locateFile('{{{ TARGET_BASENAME }}}.wasm.js'))+'');
253254
}
254255
#endif
@@ -403,7 +404,7 @@ if (ENVIRONMENT_IS_NODE) {
403404
var defaultPrint = console.log.bind(console);
404405
var defaultPrintErr = console.warn.bind(console);
405406
if (ENVIRONMENT_IS_NODE) {
406-
var fs = require('fs');
407+
requireNodeFS();
407408
defaultPrint = function(str) { fs.writeSync(1, str + '\n'); };
408409
defaultPrintErr = function(str) { fs.writeSync(2, str + '\n'); };
409410
}

tests/other/test_split_module.post.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function saveProfileData() {
55
var offset = _malloc(len);
66
var actualLen = __write_profile(offset, len);
77
var profile_data = new Uint8Array(buffer, offset, len);
8-
nodeFS.writeFileSync('profile.data', profile_data);
8+
fs.writeFileSync('profile.data', profile_data);
99
console.log('profile size is', actualLen, 'bytes (allocated', len, 'bytes)');
1010
console.log('wrote profile data')
1111
_free(offset);

tests/test_other.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9439,7 +9439,7 @@ def test(args):
94399439
# Changing this option to [] should decrease code size.
94409440
self.assertLess(changed, normal)
94419441
# Check an absolute code size as well, with some slack.
9442-
self.assertLess(abs(changed - 5001), 150)
9442+
self.assertLess(abs(changed - 5174), 150)
94439443

94449444
def test_llvm_includes(self):
94459445
create_file('atomics.c', '#include <stdatomic.h>')

0 commit comments

Comments
 (0)