Bug 1834224 - Move JS_TRY macros to internal header. r=jandem

These macros are not usable by embedders because they expand to
JSContext method calls which are not public. However, they are used in
several places in the codebase, so we need to keep them somewhere.
JSContext.h seems like a good place for that.

Depends on D180556

Differential Revision: https://phabricator.services.mozilla.com/D180557
This commit is contained in:
Philip Chimento 2023-06-13 07:05:56 +00:00
Родитель 9a457be444
Коммит 171f8241ee
2 изменённых файлов: 60 добавлений и 57 удалений

Просмотреть файл

@ -59,33 +59,6 @@
* value of `GetObjectThrug(cx, obj)` is assigned to the variable `thrug`.
*
*
* ## Checking Results when your return type is not Result
*
* This header defines alternatives to MOZ_TRY and MOZ_TRY_VAR for when you
* need to call a `Result` function from a function that uses false or nullptr
* to indicate errors:
*
* JS_TRY_OR_RETURN_FALSE(cx, DefenestrateObject(cx, obj));
* JS_TRY_VAR_OR_RETURN_FALSE(cx, v, GetObjectThrug(cx, obj));
*
* JS_TRY_VAR_OR_RETURN_NULL(cx, v, GetObjectThrug(cx, obj));
*
* When TRY is not what you want, because you need to do some cleanup or
* recovery on error, use this idiom:
*
* if (!cx->resultToBool(expr_that_is_a_Result)) {
* ... your recovery code here ...
* }
*
* In place of a tail call, you can use one of these methods:
*
* return cx->resultToBool(expr); // false on error
* return cx->resultToPtr(expr); // null on error
*
* Once we are using `Result` everywhere, including in public APIs, all of
* these will go away.
*
*
* ## GC safety
*
* When a function returns a `JS::Result<JSObject*>`, it is the program's
@ -119,36 +92,6 @@
#include "mozilla/Result.h"
/**
* JS_TRY_OR_RETURN_FALSE(cx, expr) runs expr to compute a Result value.
* On success, nothing happens; on error, it returns false immediately.
*
* Implementation note: this involves cx because this may eventually
* do the work of setting a pending exception or reporting OOM.
*/
#define JS_TRY_OR_RETURN_FALSE(cx, expr) \
do { \
auto tmpResult_ = (expr); \
if (tmpResult_.isErr()) return (cx)->resultToBool(tmpResult_); \
} while (0)
#define JS_TRY_VAR_OR_RETURN_FALSE(cx, target, expr) \
do { \
auto tmpResult_ = (expr); \
if (tmpResult_.isErr()) return (cx)->resultToBool(tmpResult_); \
(target) = tmpResult_.unwrap(); \
} while (0)
#define JS_TRY_VAR_OR_RETURN_NULL(cx, target, expr) \
do { \
auto tmpResult_ = (expr); \
if (tmpResult_.isErr()) { \
MOZ_ALWAYS_FALSE((cx)->resultToBool(tmpResult_)); \
return nullptr; \
} \
(target) = tmpResult_.unwrap(); \
} while (0)
namespace JS {
using mozilla::Ok;

Просмотреть файл

@ -1135,4 +1135,64 @@ class MOZ_RAII AutoUnsafeCallWithABI {
MOZ_ASSERT_IF(cx, !cx->isHelperThreadContext() && \
js::CurrentThreadCanAccessRuntime(cx->runtime()))
/**
* [SMDOC] JS::Result transitional macros
*
* ## Checking Results when your return type is not Result
*
* This header defines alternatives to MOZ_TRY and MOZ_TRY_VAR for when you
* need to call a `Result` function from a function that uses false or nullptr
* to indicate errors:
*
* JS_TRY_OR_RETURN_FALSE(cx, DefenestrateObject(cx, obj));
* JS_TRY_VAR_OR_RETURN_FALSE(cx, v, GetObjectThrug(cx, obj));
*
* JS_TRY_VAR_OR_RETURN_NULL(cx, v, GetObjectThrug(cx, obj));
*
* When TRY is not what you want, because you need to do some cleanup or
* recovery on error, use this idiom:
*
* if (!cx->resultToBool(expr_that_is_a_Result)) {
* ... your recovery code here ...
* }
*
* In place of a tail call, you can use one of these methods:
*
* return cx->resultToBool(expr); // false on error
* return cx->resultToPtr(expr); // null on error
*
* Once we are using `Result` everywhere, including in public APIs, all of
* these will go away.
*/
/**
* JS_TRY_OR_RETURN_FALSE(cx, expr) runs expr to compute a Result value.
* On success, nothing happens; on error, it returns false immediately.
*
* Implementation note: this involves cx because this may eventually
* do the work of setting a pending exception or reporting OOM.
*/
#define JS_TRY_OR_RETURN_FALSE(cx, expr) \
do { \
auto tmpResult_ = (expr); \
if (tmpResult_.isErr()) return (cx)->resultToBool(tmpResult_); \
} while (0)
#define JS_TRY_VAR_OR_RETURN_FALSE(cx, target, expr) \
do { \
auto tmpResult_ = (expr); \
if (tmpResult_.isErr()) return (cx)->resultToBool(tmpResult_); \
(target) = tmpResult_.unwrap(); \
} while (0)
#define JS_TRY_VAR_OR_RETURN_NULL(cx, target, expr) \
do { \
auto tmpResult_ = (expr); \
if (tmpResult_.isErr()) { \
MOZ_ALWAYS_FALSE((cx)->resultToBool(tmpResult_)); \
return nullptr; \
} \
(target) = tmpResult_.unwrap(); \
} while (0)
#endif /* vm_JSContext_h */