Skip to content

Commit

Permalink
Optimize library_nodepath.js. NFC (emscripten-core#23086)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 authored Dec 5, 2024
1 parent b95a669 commit 0732751
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
12 changes: 12 additions & 0 deletions src/closure-externs/node-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,15 @@ fs.Stats.prototype.mtimeMs;
* @type {number}
*/
fs.Stats.prototype.ctimeMs;

/**
* @param {string} p
* @return {boolean}
* @nosideeffects
*/
path.isAbsolute;

/**
* @type {Object.<string,*>}
*/
path.posix;
20 changes: 10 additions & 10 deletions src/library_nodepath.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@
// operations. Hence, using `nodePath` should be safe here.

addToLibrary({
$PATH: {
isAbs: (path) => nodePath['isAbsolute'](path),
normalize: (path) => nodePath['normalize'](path),
dirname: (path) => nodePath['dirname'](path),
basename: (path) => nodePath['basename'](path),
join: (...args) => nodePath['join'](...args),
join2: (l, r) => nodePath['join'](l, r),
},
$PATH: `{
isAbs: nodePath.isAbsolute,
normalize: nodePath.normalize,
dirname: nodePath.dirname,
basename: nodePath.basename,
join: nodePath.join,
join2: nodePath.join,
}`,
// The FS-using parts are split out into a separate object, so simple path
// usage does not require the FS.
$PATH_FS__deps: ['$FS'],
$PATH_FS__docs: '/** @type{{resolve: function(...*)}} */',
$PATH_FS: {
resolve: (...paths) => {
paths.unshift(FS.cwd());
return nodePath['posix']['resolve'](...paths);
return nodePath.posix.resolve(...paths);
},
relative: (from, to) => nodePath['posix']['relative'](from || FS.cwd(), to || FS.cwd()),
relative: (from, to) => nodePath.posix.relative(from || FS.cwd(), to || FS.cwd()),
}
});
2 changes: 1 addition & 1 deletion src/library_noderawfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ addToLibrary({
},
lookupPath(path, opts = {}) {
if (opts.parent) {
path = nodePath.dirname(path);
path = PATH.dirname(path);
}
var st = fs.lstatSync(path);
var mode = NODEFS.getMode(path);
Expand Down

0 comments on commit 0732751

Please sign in to comment.