Skip to content

Commit 3958a80

Browse files
authored
Use JavaScript class for FS.ErrnoError. NFC (#21149)
1 parent c365580 commit 3958a80

16 files changed

+46
-58
lines changed

src/library_fs.js

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,36 @@ FS.staticInit();` +
115115
filesystems: null,
116116
syncFSRequests: 0, // we warn if there are multiple in flight at once
117117

118+
#if ASSERTIONS
119+
ErrnoError: class extends Error {
120+
#else
121+
ErrnoError: class {
122+
#endif
123+
// We set the `name` property to be able to identify `FS.ErrnoError`
124+
// - the `name` is a standard ECMA-262 property of error objects. Kind of good to have it anyway.
125+
// - when using PROXYFS, an error can come from an underlying FS
126+
// as different FS objects have their own FS.ErrnoError each,
127+
// the test `err instanceof FS.ErrnoError` won't detect an error coming from another filesystem, causing bugs.
128+
// we'll use the reliable test `err.name == "ErrnoError"` instead
129+
constructor(errno) {
130+
#if ASSERTIONS
131+
super(ERRNO_MESSAGES[errno]);
132+
#endif
133+
// TODO(sbc): Use the inline member delclaration syntax once we
134+
// support it in acorn and closure.
135+
this.name = 'ErrnoError';
136+
this.errno = errno;
137+
#if ASSERTIONS
138+
for (var key in ERRNO_CODES) {
139+
if (ERRNO_CODES[key] === errno) {
140+
this.code = key;
141+
break;
142+
}
143+
}
144+
#endif
145+
}
146+
},
147+
118148
//
119149
// paths
120150
//
@@ -1404,53 +1434,12 @@ FS.staticInit();` +
14041434
assert(stderr.fd === 2, `invalid handle for stderr (${stderr.fd})`);
14051435
#endif
14061436
},
1407-
ensureErrnoError() {
1408-
if (FS.ErrnoError) return;
1409-
FS.ErrnoError = /** @this{Object} */ function ErrnoError(errno) {
1410-
// We set the `name` property to be able to identify `FS.ErrnoError`
1411-
// - the `name` is a standard ECMA-262 property of error objects. Kind of good to have it anyway.
1412-
// - when using PROXYFS, an error can come from an underlying FS
1413-
// as different FS objects have their own FS.ErrnoError each,
1414-
// the test `err instanceof FS.ErrnoError` won't detect an error coming from another filesystem, causing bugs.
1415-
// we'll use the reliable test `err.name == "ErrnoError"` instead
1416-
this.name = 'ErrnoError';
1417-
this.setErrno = /** @this{Object} */ function(errno) {
1418-
this.errno = errno;
1419-
#if ASSERTIONS
1420-
for (var key in ERRNO_CODES) {
1421-
if (ERRNO_CODES[key] === errno) {
1422-
this.code = key;
1423-
break;
1424-
}
1425-
}
1426-
#endif
1427-
};
1428-
this.setErrno(errno);
1429-
#if ASSERTIONS
1430-
this.message = ERRNO_MESSAGES[errno];
1431-
#else
1432-
this.message = 'FS error';
1433-
#endif
1434-
1435-
#if ASSERTIONS && !MINIMAL_RUNTIME
1436-
// Try to get a maximally helpful stack trace. On Node.js, getting Error.stack
1437-
// now ensures it shows what we want.
1438-
if (this.stack) {
1439-
// Define the stack property for Node.js 4, which otherwise errors on the next line.
1440-
Object.defineProperty(this, "stack", { value: (new Error).stack, writable: true });
1441-
}
1442-
#endif // ASSERTIONS
1443-
};
1444-
FS.ErrnoError.prototype = new Error();
1445-
FS.ErrnoError.prototype.constructor = FS.ErrnoError;
1437+
staticInit() {
14461438
// Some errors may happen quite a bit, to avoid overhead we reuse them (and suffer a lack of stack info)
14471439
[{{{ cDefs.ENOENT }}}].forEach((code) => {
14481440
FS.genericErrors[code] = new FS.ErrnoError(code);
14491441
FS.genericErrors[code].stack = '<generic error, no stack>';
14501442
});
1451-
},
1452-
staticInit() {
1453-
FS.ensureErrnoError();
14541443

14551444
FS.nameTable = new Array(4096);
14561445

@@ -1482,8 +1471,6 @@ FS.staticInit();` +
14821471
#endif
14831472
FS.init.initialized = true;
14841473

1485-
FS.ensureErrnoError();
1486-
14871474
// Allow Module.stdin etc. to provide defaults, if none explicitly passed to us here
14881475
Module['stdin'] = input || Module['stdin'];
14891476
Module['stdout'] = output || Module['stdout'];

src/library_noderawfs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ addToLibrary({
2020
}
2121
}
2222
};
23+
/** @suppress {partialAlias} */
2324
var VFS = Object.assign({}, FS);
2425
for (var _key in NODERAWFS) {
2526
FS[_key] = _wrapNodeError(NODERAWFS[_key]);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9932
1+
9950
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24693
1+
24550
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9915
1+
9933
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24661
1+
24518
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
11010
1+
11019
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
28583
1+
28439
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9899
1+
9913
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24586
1+
24442
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
11016
1+
11029
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
28583
1+
28439
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9932
1+
9950
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24693
1+
24550
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7871
1+
7883
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
19715
1+
19571

0 commit comments

Comments
 (0)