From 80d294cb5782c49a27db215f31a82abc11cc6cb4 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Tue, 23 Jan 2024 00:10:24 +0000 Subject: [PATCH] Use JavaScript class for FS.ErrnoError. NFC --- src/library_fs.js | 83 +++++++++---------- .../metadce/test_metadce_cxx_ctors1.gzsize | 2 +- .../metadce/test_metadce_cxx_ctors1.jssize | 2 +- .../metadce/test_metadce_cxx_ctors2.gzsize | 2 +- .../metadce/test_metadce_cxx_ctors2.jssize | 2 +- .../metadce/test_metadce_cxx_except.gzsize | 2 +- .../metadce/test_metadce_cxx_except.jssize | 2 +- .../test_metadce_cxx_except_wasm.gzsize | 2 +- .../test_metadce_cxx_except_wasm.jssize | 2 +- .../metadce/test_metadce_cxx_mangle.gzsize | 2 +- .../metadce/test_metadce_cxx_mangle.jssize | 2 +- .../metadce/test_metadce_cxx_noexcept.gzsize | 2 +- .../metadce/test_metadce_cxx_noexcept.jssize | 2 +- .../metadce/test_metadce_files_js_fs.gzsize | 2 +- .../metadce/test_metadce_files_js_fs.jssize | 2 +- 15 files changed, 52 insertions(+), 59 deletions(-) diff --git a/src/library_fs.js b/src/library_fs.js index 3b9a29b31d86d..670e901858ba1 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -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 // @@ -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 = ''; }); - }, - staticInit() { - FS.ensureErrnoError(); FS.nameTable = new Array(4096); @@ -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']; diff --git a/test/other/metadce/test_metadce_cxx_ctors1.gzsize b/test/other/metadce/test_metadce_cxx_ctors1.gzsize index 87e1a92de80b2..cd791627e99c1 100644 --- a/test/other/metadce/test_metadce_cxx_ctors1.gzsize +++ b/test/other/metadce/test_metadce_cxx_ctors1.gzsize @@ -1 +1 @@ -9932 +9971 diff --git a/test/other/metadce/test_metadce_cxx_ctors1.jssize b/test/other/metadce/test_metadce_cxx_ctors1.jssize index ef8d02c7b4c52..305b6b764bd3c 100644 --- a/test/other/metadce/test_metadce_cxx_ctors1.jssize +++ b/test/other/metadce/test_metadce_cxx_ctors1.jssize @@ -1 +1 @@ -24693 +24582 diff --git a/test/other/metadce/test_metadce_cxx_ctors2.gzsize b/test/other/metadce/test_metadce_cxx_ctors2.gzsize index e075aaa54a983..0b713b42c5542 100644 --- a/test/other/metadce/test_metadce_cxx_ctors2.gzsize +++ b/test/other/metadce/test_metadce_cxx_ctors2.gzsize @@ -1 +1 @@ -9915 +9954 diff --git a/test/other/metadce/test_metadce_cxx_ctors2.jssize b/test/other/metadce/test_metadce_cxx_ctors2.jssize index b32c1ee20e951..9f92ad5a75e08 100644 --- a/test/other/metadce/test_metadce_cxx_ctors2.jssize +++ b/test/other/metadce/test_metadce_cxx_ctors2.jssize @@ -1 +1 @@ -24661 +24550 diff --git a/test/other/metadce/test_metadce_cxx_except.gzsize b/test/other/metadce/test_metadce_cxx_except.gzsize index deca973733bad..e93cea717e493 100644 --- a/test/other/metadce/test_metadce_cxx_except.gzsize +++ b/test/other/metadce/test_metadce_cxx_except.gzsize @@ -1 +1 @@ -11010 +11040 diff --git a/test/other/metadce/test_metadce_cxx_except.jssize b/test/other/metadce/test_metadce_cxx_except.jssize index d7bf552030bbc..802c0b81ab98b 100644 --- a/test/other/metadce/test_metadce_cxx_except.jssize +++ b/test/other/metadce/test_metadce_cxx_except.jssize @@ -1 +1 @@ -28583 +28471 diff --git a/test/other/metadce/test_metadce_cxx_except_wasm.gzsize b/test/other/metadce/test_metadce_cxx_except_wasm.gzsize index 55223f1554dc1..5e23669ffcc6c 100644 --- a/test/other/metadce/test_metadce_cxx_except_wasm.gzsize +++ b/test/other/metadce/test_metadce_cxx_except_wasm.gzsize @@ -1 +1 @@ -9899 +9935 diff --git a/test/other/metadce/test_metadce_cxx_except_wasm.jssize b/test/other/metadce/test_metadce_cxx_except_wasm.jssize index b85dcbb09ed54..2560a061a3a15 100644 --- a/test/other/metadce/test_metadce_cxx_except_wasm.jssize +++ b/test/other/metadce/test_metadce_cxx_except_wasm.jssize @@ -1 +1 @@ -24586 +24474 diff --git a/test/other/metadce/test_metadce_cxx_mangle.gzsize b/test/other/metadce/test_metadce_cxx_mangle.gzsize index d817e31f8830c..e32d46aa741f3 100644 --- a/test/other/metadce/test_metadce_cxx_mangle.gzsize +++ b/test/other/metadce/test_metadce_cxx_mangle.gzsize @@ -1 +1 @@ -11016 +11050 diff --git a/test/other/metadce/test_metadce_cxx_mangle.jssize b/test/other/metadce/test_metadce_cxx_mangle.jssize index d7bf552030bbc..802c0b81ab98b 100644 --- a/test/other/metadce/test_metadce_cxx_mangle.jssize +++ b/test/other/metadce/test_metadce_cxx_mangle.jssize @@ -1 +1 @@ -28583 +28471 diff --git a/test/other/metadce/test_metadce_cxx_noexcept.gzsize b/test/other/metadce/test_metadce_cxx_noexcept.gzsize index 87e1a92de80b2..cd791627e99c1 100644 --- a/test/other/metadce/test_metadce_cxx_noexcept.gzsize +++ b/test/other/metadce/test_metadce_cxx_noexcept.gzsize @@ -1 +1 @@ -9932 +9971 diff --git a/test/other/metadce/test_metadce_cxx_noexcept.jssize b/test/other/metadce/test_metadce_cxx_noexcept.jssize index ef8d02c7b4c52..305b6b764bd3c 100644 --- a/test/other/metadce/test_metadce_cxx_noexcept.jssize +++ b/test/other/metadce/test_metadce_cxx_noexcept.jssize @@ -1 +1 @@ -24693 +24582 diff --git a/test/other/metadce/test_metadce_files_js_fs.gzsize b/test/other/metadce/test_metadce_files_js_fs.gzsize index fc1347e76e0df..f568806187f90 100644 --- a/test/other/metadce/test_metadce_files_js_fs.gzsize +++ b/test/other/metadce/test_metadce_files_js_fs.gzsize @@ -1 +1 @@ -7871 +7906 diff --git a/test/other/metadce/test_metadce_files_js_fs.jssize b/test/other/metadce/test_metadce_files_js_fs.jssize index b3945f1ded561..8fb0ce6cf0658 100644 --- a/test/other/metadce/test_metadce_files_js_fs.jssize +++ b/test/other/metadce/test_metadce_files_js_fs.jssize @@ -1 +1 @@ -19715 +19603