mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:17:44 +00:00
LibJS: Convert PrototypeObject::this_object() to ThrowCompletionOr
This commit is contained in:
parent
08db3b9f66
commit
7c29979e30
2 changed files with 14 additions and 47 deletions
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#include <AK/StringView.h>
|
#include <AK/StringView.h>
|
||||||
#include <AK/TypeCasts.h>
|
#include <AK/TypeCasts.h>
|
||||||
|
#include <LibJS/Runtime/Completion.h>
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
#include <LibJS/Runtime/Object.h>
|
#include <LibJS/Runtime/Object.h>
|
||||||
|
|
||||||
|
@ -25,17 +26,12 @@ class PrototypeObject : public Object {
|
||||||
public:
|
public:
|
||||||
virtual ~PrototypeObject() override = default;
|
virtual ~PrototypeObject() override = default;
|
||||||
|
|
||||||
static Object* this_object(GlobalObject& global_object)
|
static ThrowCompletionOr<Object*> this_object(GlobalObject& global_object)
|
||||||
{
|
{
|
||||||
auto& vm = global_object.vm();
|
auto& vm = global_object.vm();
|
||||||
|
|
||||||
auto this_value = vm.this_value(global_object);
|
auto this_value = vm.this_value(global_object);
|
||||||
|
if (!this_value.is_object())
|
||||||
if (!this_value.is_object()) {
|
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, this_value);
|
||||||
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, this_value);
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
return &this_value.as_object();
|
return &this_value.as_object();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -282,10 +282,7 @@ Value regexp_exec(GlobalObject& global_object, Object& regexp_object, Utf16Strin
|
||||||
#define __JS_ENUMERATE(flagName, flag_name, flag_char) \
|
#define __JS_ENUMERATE(flagName, flag_name, flag_char) \
|
||||||
JS_DEFINE_NATIVE_GETTER(RegExpPrototype::flag_name) \
|
JS_DEFINE_NATIVE_GETTER(RegExpPrototype::flag_name) \
|
||||||
{ \
|
{ \
|
||||||
auto* regexp_object = this_object(global_object); \
|
auto* regexp_object = TRY_OR_DISCARD(this_object(global_object)); \
|
||||||
if (!regexp_object) \
|
|
||||||
return {}; \
|
|
||||||
\
|
|
||||||
if (!is<RegExpObject>(regexp_object)) { \
|
if (!is<RegExpObject>(regexp_object)) { \
|
||||||
if (same_value(regexp_object, global_object.regexp_prototype())) \
|
if (same_value(regexp_object, global_object.regexp_prototype())) \
|
||||||
return js_undefined(); \
|
return js_undefined(); \
|
||||||
|
@ -302,10 +299,7 @@ JS_ENUMERATE_REGEXP_FLAGS
|
||||||
// 22.2.5.4 get RegExp.prototype.flags, https://tc39.es/ecma262/#sec-get-regexp.prototype.flags
|
// 22.2.5.4 get RegExp.prototype.flags, https://tc39.es/ecma262/#sec-get-regexp.prototype.flags
|
||||||
JS_DEFINE_NATIVE_GETTER(RegExpPrototype::flags)
|
JS_DEFINE_NATIVE_GETTER(RegExpPrototype::flags)
|
||||||
{
|
{
|
||||||
auto* regexp_object = this_object(global_object);
|
auto* regexp_object = TRY_OR_DISCARD(this_object(global_object));
|
||||||
if (!regexp_object)
|
|
||||||
return {};
|
|
||||||
|
|
||||||
StringBuilder builder(8);
|
StringBuilder builder(8);
|
||||||
|
|
||||||
#define __JS_ENUMERATE(flagName, flag_name, flag_char) \
|
#define __JS_ENUMERATE(flagName, flag_name, flag_char) \
|
||||||
|
@ -321,10 +315,7 @@ JS_DEFINE_NATIVE_GETTER(RegExpPrototype::flags)
|
||||||
// 22.2.5.12 get RegExp.prototype.source, https://tc39.es/ecma262/#sec-get-regexp.prototype.source
|
// 22.2.5.12 get RegExp.prototype.source, https://tc39.es/ecma262/#sec-get-regexp.prototype.source
|
||||||
JS_DEFINE_NATIVE_GETTER(RegExpPrototype::source)
|
JS_DEFINE_NATIVE_GETTER(RegExpPrototype::source)
|
||||||
{
|
{
|
||||||
auto* regexp_object = this_object(global_object);
|
auto* regexp_object = TRY_OR_DISCARD(this_object(global_object));
|
||||||
if (!regexp_object)
|
|
||||||
return {};
|
|
||||||
|
|
||||||
if (!is<RegExpObject>(regexp_object)) {
|
if (!is<RegExpObject>(regexp_object)) {
|
||||||
if (same_value(regexp_object, global_object.regexp_prototype()))
|
if (same_value(regexp_object, global_object.regexp_prototype()))
|
||||||
return js_string(vm, "(?:)");
|
return js_string(vm, "(?:)");
|
||||||
|
@ -350,10 +341,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::exec)
|
||||||
// 22.2.5.15 RegExp.prototype.test ( S ), https://tc39.es/ecma262/#sec-regexp.prototype.test
|
// 22.2.5.15 RegExp.prototype.test ( S ), https://tc39.es/ecma262/#sec-regexp.prototype.test
|
||||||
JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::test)
|
JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::test)
|
||||||
{
|
{
|
||||||
auto* regexp_object = this_object(global_object);
|
auto* regexp_object = TRY_OR_DISCARD(this_object(global_object));
|
||||||
if (!regexp_object)
|
|
||||||
return {};
|
|
||||||
|
|
||||||
auto string = TRY_OR_DISCARD(vm.argument(0).to_utf16_string(global_object));
|
auto string = TRY_OR_DISCARD(vm.argument(0).to_utf16_string(global_object));
|
||||||
|
|
||||||
auto match = regexp_exec(global_object, *regexp_object, move(string));
|
auto match = regexp_exec(global_object, *regexp_object, move(string));
|
||||||
|
@ -366,10 +354,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::test)
|
||||||
// 22.2.5.16 RegExp.prototype.toString ( ), https://tc39.es/ecma262/#sec-regexp.prototype.tostring
|
// 22.2.5.16 RegExp.prototype.toString ( ), https://tc39.es/ecma262/#sec-regexp.prototype.tostring
|
||||||
JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::to_string)
|
JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::to_string)
|
||||||
{
|
{
|
||||||
auto* regexp_object = this_object(global_object);
|
auto* regexp_object = TRY_OR_DISCARD(this_object(global_object));
|
||||||
if (!regexp_object)
|
|
||||||
return {};
|
|
||||||
|
|
||||||
auto source_attr = TRY_OR_DISCARD(regexp_object->get(vm.names.source));
|
auto source_attr = TRY_OR_DISCARD(regexp_object->get(vm.names.source));
|
||||||
auto pattern = TRY_OR_DISCARD(source_attr.to_string(global_object));
|
auto pattern = TRY_OR_DISCARD(source_attr.to_string(global_object));
|
||||||
|
|
||||||
|
@ -382,10 +367,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::to_string)
|
||||||
// 22.2.5.7 RegExp.prototype [ @@match ] ( string ), https://tc39.es/ecma262/#sec-regexp.prototype-@@match
|
// 22.2.5.7 RegExp.prototype [ @@match ] ( string ), https://tc39.es/ecma262/#sec-regexp.prototype-@@match
|
||||||
JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_match)
|
JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_match)
|
||||||
{
|
{
|
||||||
auto* regexp_object = this_object(global_object);
|
auto* regexp_object = TRY_OR_DISCARD(this_object(global_object));
|
||||||
if (!regexp_object)
|
|
||||||
return {};
|
|
||||||
|
|
||||||
auto string = TRY_OR_DISCARD(vm.argument(0).to_utf16_string(global_object));
|
auto string = TRY_OR_DISCARD(vm.argument(0).to_utf16_string(global_object));
|
||||||
|
|
||||||
bool global = TRY_OR_DISCARD(regexp_object->get(vm.names.global)).to_boolean();
|
bool global = TRY_OR_DISCARD(regexp_object->get(vm.names.global)).to_boolean();
|
||||||
|
@ -434,10 +416,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_match)
|
||||||
// 22.2.5.8 RegExp.prototype [ @@matchAll ] ( string ), https://tc39.es/ecma262/#sec-regexp-prototype-matchall
|
// 22.2.5.8 RegExp.prototype [ @@matchAll ] ( string ), https://tc39.es/ecma262/#sec-regexp-prototype-matchall
|
||||||
JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_match_all)
|
JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_match_all)
|
||||||
{
|
{
|
||||||
auto* regexp_object = this_object(global_object);
|
auto* regexp_object = TRY_OR_DISCARD(this_object(global_object));
|
||||||
if (!regexp_object)
|
|
||||||
return {};
|
|
||||||
|
|
||||||
auto string = TRY_OR_DISCARD(vm.argument(0).to_utf16_string(global_object));
|
auto string = TRY_OR_DISCARD(vm.argument(0).to_utf16_string(global_object));
|
||||||
|
|
||||||
auto* constructor = TRY_OR_DISCARD(species_constructor(global_object, *regexp_object, *global_object.regexp_constructor()));
|
auto* constructor = TRY_OR_DISCARD(species_constructor(global_object, *regexp_object, *global_object.regexp_constructor()));
|
||||||
|
@ -469,9 +448,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_replace)
|
||||||
auto string_value = vm.argument(0);
|
auto string_value = vm.argument(0);
|
||||||
auto replace_value = vm.argument(1);
|
auto replace_value = vm.argument(1);
|
||||||
|
|
||||||
auto* regexp_object = this_object(global_object);
|
auto* regexp_object = TRY_OR_DISCARD(this_object(global_object));
|
||||||
if (!regexp_object)
|
|
||||||
return {};
|
|
||||||
auto string = TRY_OR_DISCARD(string_value.to_utf16_string(global_object));
|
auto string = TRY_OR_DISCARD(string_value.to_utf16_string(global_object));
|
||||||
auto string_view = string.view();
|
auto string_view = string.view();
|
||||||
|
|
||||||
|
@ -580,10 +557,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_replace)
|
||||||
// 22.2.5.11 RegExp.prototype [ @@search ] ( string ), https://tc39.es/ecma262/#sec-regexp.prototype-@@search
|
// 22.2.5.11 RegExp.prototype [ @@search ] ( string ), https://tc39.es/ecma262/#sec-regexp.prototype-@@search
|
||||||
JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_search)
|
JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_search)
|
||||||
{
|
{
|
||||||
auto* regexp_object = this_object(global_object);
|
auto* regexp_object = TRY_OR_DISCARD(this_object(global_object));
|
||||||
if (!regexp_object)
|
|
||||||
return {};
|
|
||||||
|
|
||||||
auto string = TRY_OR_DISCARD(vm.argument(0).to_utf16_string(global_object));
|
auto string = TRY_OR_DISCARD(vm.argument(0).to_utf16_string(global_object));
|
||||||
|
|
||||||
auto previous_last_index = TRY_OR_DISCARD(regexp_object->get(vm.names.lastIndex));
|
auto previous_last_index = TRY_OR_DISCARD(regexp_object->get(vm.names.lastIndex));
|
||||||
|
@ -608,10 +582,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_search)
|
||||||
// 22.2.5.13 RegExp.prototype [ @@split ] ( string, limit ), https://tc39.es/ecma262/#sec-regexp.prototype-@@split
|
// 22.2.5.13 RegExp.prototype [ @@split ] ( string, limit ), https://tc39.es/ecma262/#sec-regexp.prototype-@@split
|
||||||
JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_split)
|
JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_split)
|
||||||
{
|
{
|
||||||
auto* regexp_object = this_object(global_object);
|
auto* regexp_object = TRY_OR_DISCARD(this_object(global_object));
|
||||||
if (!regexp_object)
|
|
||||||
return {};
|
|
||||||
|
|
||||||
auto string = TRY_OR_DISCARD(vm.argument(0).to_utf16_string(global_object));
|
auto string = TRY_OR_DISCARD(vm.argument(0).to_utf16_string(global_object));
|
||||||
auto string_view = string.view();
|
auto string_view = string.view();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue