diff options
author | Timothy Flynn <trflynn89@pm.me> | 2023-02-09 13:27:43 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-02-10 09:08:52 +0000 |
commit | 604d5f5bcae5f89778acf416334a823bcb114402 (patch) | |
tree | 46b56a3ede6c89c31e609c79b396d9e2c2f28e1f /Userland/Libraries/LibJS | |
parent | 4a916cd3796cef0ef10374fcf9a602c96e226e6c (diff) | |
download | serenity-604d5f5bcae5f89778acf416334a823bcb114402.zip |
AK+Everywhere: Do not implicitly copy variables in TRY macros
For example, consider cases where we want to propagate errors only in
specific instances:
auto result = read_data(); // something like ErrorOr<ByteBuffer>
if (result.is_error() && result.error().code() != EINTR)
continue;
auto bytes = TRY(result);
The TRY invocation will currently copy the byte buffer when the
expression (in this case, just a local variable) is stored into
_temporary_result.
This patch binds the expression to a reference to prevent such copies.
In less trival invocations (such as TRY(some_function()), this will
incur only temporary lifetime extensions, i.e. no functional change.
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Completion.h | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/PromiseCapability.h | 4 |
2 files changed, 4 insertions, 4 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Completion.h b/Userland/Libraries/LibJS/Runtime/Completion.h index 294938a8e6..7fe307bf01 100644 --- a/Userland/Libraries/LibJS/Runtime/Completion.h +++ b/Userland/Libraries/LibJS/Runtime/Completion.h @@ -21,7 +21,7 @@ namespace JS { ({ \ /* Ignore -Wshadow to allow nesting the macro. */ \ AK_IGNORE_DIAGNOSTIC("-Wshadow", \ - auto _temporary_result = (expression)); \ + auto&& _temporary_result = (expression)); \ if (_temporary_result.is_error()) { \ VERIFY(_temporary_result.error().code() == ENOMEM); \ return vm.throw_completion<JS::InternalError>(JS::ErrorType::OutOfMemory); \ @@ -35,7 +35,7 @@ namespace JS { ({ \ /* Ignore -Wshadow to allow nesting the macro. */ \ AK_IGNORE_DIAGNOSTIC("-Wshadow", \ - auto _temporary_result = (expression)); \ + auto&& _temporary_result = (expression)); \ if (_temporary_result.is_error()) { \ auto _completion = _temporary_result.release_error(); \ \ diff --git a/Userland/Libraries/LibJS/Runtime/PromiseCapability.h b/Userland/Libraries/LibJS/Runtime/PromiseCapability.h index 41c4b81170..a5eb801f48 100644 --- a/Userland/Libraries/LibJS/Runtime/PromiseCapability.h +++ b/Userland/Libraries/LibJS/Runtime/PromiseCapability.h @@ -43,7 +43,7 @@ private: // 27.2.1.1.1 IfAbruptRejectPromise ( value, capability ), https://tc39.es/ecma262/#sec-ifabruptrejectpromise #define __TRY_OR_REJECT(vm, capability, expression, CALL_CHECK) \ ({ \ - auto _temporary_try_or_reject_result = (expression); \ + auto&& _temporary_try_or_reject_result = (expression); \ /* 1. If value is an abrupt completion, then */ \ if (_temporary_try_or_reject_result.is_error()) { \ /* a. Perform ? Call(capability.[[Reject]], undefined, « value.[[Value]] »). */ \ @@ -69,7 +69,7 @@ private: // 27.2.1.1.1 IfAbruptRejectPromise ( value, capability ), https://tc39.es/ecma262/#sec-ifabruptrejectpromise #define TRY_OR_REJECT_WITH_VALUE(vm, capability, expression) \ ({ \ - auto _temporary_try_or_reject_result = (expression); \ + auto&& _temporary_try_or_reject_result = (expression); \ /* 1. If value is an abrupt completion, then */ \ if (_temporary_try_or_reject_result.is_error()) { \ /* a. Perform ? Call(capability.[[Reject]], undefined, « value.[[Value]] »). */ \ |