Skip to content

Commit

Permalink
Use JavaScript class for FS.ErrnoError. NFC
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed Jan 24, 2024
1 parent fb4bdc7 commit 3909666
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 59 deletions.
76 changes: 31 additions & 45 deletions src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,36 @@ FS.staticInit();` +
filesystems: null,
syncFSRequests: 0, // we warn if there are multiple in flight at once
#if ASSERTIONS && !MINIMAL_RUNTIME
ErrnoError: class extends Error {
#else
ErrnoError: class {
#endif
// We set the `name` property to be able to identify `FS.ErrnoError`
// - the `name` is a standard ECMA-262 property of error objects. Kind of good to have it anyway.
// - when using PROXYFS, an error can come from an underlying FS
// as different FS objects have their own FS.ErrnoError each,
// the test `err instanceof FS.ErrnoError` won't detect an error coming from another filesystem, causing bugs.
// we'll use the reliable test `err.name == "ErrnoError"` instead
constructor(errno) {
#if ASSERTIONS && !MINIMAL_RUNTIME
super(ERRNO_MESSAGES[errno]);
#endif
// TODO(sbc): Use the inline member delclaration syntax once we
// support it in acorn and closure.
this.name = 'ErrnoError';
this.errno = errno;
#if ASSERTIONS
for (var key in ERRNO_CODES) {
if (ERRNO_CODES[key] === errno) {
this.code = key;
break;
}
}
#endif
}
},

//
// paths
//
Expand Down Expand Up @@ -1407,54 +1437,12 @@ FS.staticInit();` +
assert(stderr.fd === 2, `invalid handle for stderr (${stderr.fd})`);
#endif
},
ensureErrnoError() {
if (FS.ErrnoError) return;
FS.ErrnoError = /** @this{Object} */ function ErrnoError(errno) {
// We set the `name` property to be able to identify `FS.ErrnoError`
// - the `name` is a standard ECMA-262 property of error objects. Kind of good to have it anyway.
// - when using PROXYFS, an error can come from an underlying FS
// as different FS objects have their own FS.ErrnoError each,
// the test `err instanceof FS.ErrnoError` won't detect an error coming from another filesystem, causing bugs.
// we'll use the reliable test `err.name == "ErrnoError"` instead
this.name = 'ErrnoError';
this.setErrno = /** @this{Object} */ function(errno) {
this.errno = errno;
#if ASSERTIONS
for (var key in ERRNO_CODES) {
if (ERRNO_CODES[key] === errno) {
this.code = key;
break;
}
}
#endif
};
this.setErrno(errno);
#if ASSERTIONS
this.message = ERRNO_MESSAGES[errno];
#else
this.message = 'FS error';
#endif

#if ASSERTIONS && !MINIMAL_RUNTIME
// Try to get a maximally helpful stack trace. On Node.js, getting Error.stack
// now ensures it shows what we want.
if (this.stack) {
// Define the stack property for Node.js 4, which otherwise errors on the next line.
Object.defineProperty(this, "stack", { value: (new Error).stack, writable: true });
this.stack = demangleAll(this.stack);
}
#endif // ASSERTIONS
};
FS.ErrnoError.prototype = new Error();
FS.ErrnoError.prototype.constructor = FS.ErrnoError;
staticInit() {
// Some errors may happen quite a bit, to avoid overhead we reuse them (and suffer a lack of stack info)
[{{{ cDefs.ENOENT }}}].forEach((code) => {
FS.genericErrors[code] = new FS.ErrnoError(code);
FS.genericErrors[code].stack = '<generic error, no stack>';
});
},
staticInit() {
FS.ensureErrnoError();

FS.nameTable = new Array(4096);

Expand Down Expand Up @@ -1486,8 +1474,6 @@ FS.staticInit();` +
#endif
FS.init.initialized = true;

FS.ensureErrnoError();

// Allow Module.stdin etc. to provide defaults, if none explicitly passed to us here
Module['stdin'] = input || Module['stdin'];
Module['stdout'] = output || Module['stdout'];
Expand Down
1 change: 1 addition & 0 deletions src/library_noderawfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ addToLibrary({
}
}
};
/** @suppress {partialAlias} */
var VFS = Object.assign({}, FS);
for (var _key in NODERAWFS) {
FS[_key] = _wrapNodeError(NODERAWFS[_key]);
Expand Down
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors1.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9932
9950
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24693
24550
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors2.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9915
9933
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24661
24518
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
11010
11019
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
28583
28439
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except_wasm.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9899
9913
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except_wasm.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24586
24442
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_mangle.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
11016
11029
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_mangle.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
28583
28439
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_noexcept.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9932
9950
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_noexcept.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24693
24550
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_files_js_fs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7871
7883
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_files_js_fs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19715
19571

0 comments on commit 3909666

Please sign in to comment.