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 23, 2024
1 parent b25e4c1 commit 80d294c
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 59 deletions.
83 changes: 38 additions & 45 deletions src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,43 @@ FS.staticInit();` +
filesystems: null,
syncFSRequests: 0, // we warn if there are multiple in flight at once
ErrnoError: class extends Error {
// 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
var message = ERRNO_MESSAGES[errno];
#else
var message = 'FS error';
#endif
super(message);
this.name = 'ErrnoError';
this.errno = errno;
#if ASSERTIONS
for (var key in ERRNO_CODES) {
if (ERRNO_CODES[key] === errno) {
this.code = key;
break;
}
}
#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 && !MINIMAL_RUNTIME
}
},

//
// paths
//
Expand Down Expand Up @@ -1407,54 +1444,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 +1481,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
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
9971
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
24582
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
9954
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
24550
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
11040
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
28471
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
9935
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
24474
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
11050
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
28471
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
9971
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
24582
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
7906
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
19603

0 comments on commit 80d294c

Please sign in to comment.