Skip to content

Commit c3abb13

Browse files
trflynn89linusg
authored andcommitted
LibJS+LibWeb: Convert string view PrimitiveString instances to String
First, this adds an overload of PrimitiveString::create for StringView. This overload will throw an OOM completion if creating a String fails. This is not only a bit more convenient, but it also ensures at compile time that all PrimitiveString::create(string_view) invocations will be handled as String and OOM-aware. Next, this wraps all invocations to PrimitiveString::create(string_view) with MUST_OR_THROW_OOM. A small PrimitiveString::create(DeprecatedFlyString) overload also had to be added to disambiguate between the StringView and DeprecatedString overloads.
1 parent 69a56a8 commit c3abb13

File tree

69 files changed

+223
-186
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+223
-186
lines changed

Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,7 +2576,7 @@ JS::ThrowCompletionOr<void> @prototype_class@::initialize(JS::Realm& realm)
25762576
}
25772577

25782578
generator.append(R"~~~(
2579-
define_direct_property(*vm.well_known_symbol_to_string_tag(), JS::PrimitiveString::create(vm, "@name@"), JS::Attribute::Configurable);
2579+
define_direct_property(*vm.well_known_symbol_to_string_tag(), MUST_OR_THROW_OOM(JS::PrimitiveString::create(vm, "@name@"sv)), JS::Attribute::Configurable);
25802580
25812581
MUST_OR_THROW_OOM(Object::initialize(realm));
25822582
return {};
@@ -2890,7 +2890,7 @@ JS::ThrowCompletionOr<void> @prototype_class@::initialize(JS::Realm& realm)
28902890
MUST_OR_THROW_OOM(Object::initialize(realm));
28912891
28922892
define_native_function(realm, vm.names.next, next, 0, JS::Attribute::Writable | JS::Attribute::Enumerable | JS::Attribute::Configurable);
2893-
define_direct_property(*vm.well_known_symbol_to_string_tag(), JS::PrimitiveString::create(vm, "Iterator"), JS::Attribute::Configurable);
2893+
define_direct_property(*vm.well_known_symbol_to_string_tag(), MUST_OR_THROW_OOM(JS::PrimitiveString::create(vm, "Iterator"sv)), JS::Attribute::Configurable);
28942894
28952895
return {};
28962896
}

Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateWindowOrWorkerInterfaces.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ void Intrinsics::create_web_prototype_and_constructor<@prototype_class@>(JS::Rea
181181
m_constructors.set("@interface_name@"sv, constructor);
182182
183183
prototype->define_direct_property(vm.names.constructor, constructor.ptr(), JS::Attribute::Writable | JS::Attribute::Configurable);
184-
constructor->define_direct_property(vm.names.name, JS::PrimitiveString::create(vm, "@interface_name@"sv), JS::Attribute::Configurable);
184+
constructor->define_direct_property(vm.names.name, JS::PrimitiveString::create(vm, "@interface_name@"sv).release_allocated_value_but_fixme_should_propagate_errors(), JS::Attribute::Configurable);
185185
)~~~");
186186

187187
if (legacy_constructor.has_value()) {
@@ -191,7 +191,7 @@ void Intrinsics::create_web_prototype_and_constructor<@prototype_class@>(JS::Rea
191191
auto legacy_constructor = heap().allocate<@legacy_constructor_class@>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
192192
m_constructors.set("@legacy_interface_name@"sv, legacy_constructor);
193193
194-
legacy_constructor->define_direct_property(vm.names.name, JS::PrimitiveString::create(vm, "@legacy_interface_name@"sv), JS::Attribute::Configurable);)~~~");
194+
legacy_constructor->define_direct_property(vm.names.name, JS::PrimitiveString::create(vm, "@legacy_interface_name@"sv).release_allocated_value_but_fixme_should_propagate_errors(), JS::Attribute::Configurable);)~~~");
195195
}
196196

197197
gen.append(R"~~~(

Userland/Libraries/LibJS/AST.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,7 @@ Completion UnaryExpression::execute(Interpreter& interpreter) const
16271627
case UnaryOp::Minus:
16281628
return TRY(unary_minus(vm, lhs_result));
16291629
case UnaryOp::Typeof:
1630-
return Value { PrimitiveString::create(vm, lhs_result.typeof()) };
1630+
return Value { MUST_OR_THROW_OOM(PrimitiveString::create(vm, lhs_result.typeof())) };
16311631
case UnaryOp::Void:
16321632
return js_undefined();
16331633
case UnaryOp::Delete:

Userland/Libraries/LibJS/Bytecode/Op.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static ThrowCompletionOr<Value> not_(VM&, Value value)
150150

151151
static ThrowCompletionOr<Value> typeof_(VM& vm, Value value)
152152
{
153-
return Value(PrimitiveString::create(vm, value.typeof()));
153+
return MUST_OR_THROW_OOM(PrimitiveString::create(vm, value.typeof()));
154154
}
155155

156156
#define JS_DEFINE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
@@ -1035,7 +1035,7 @@ ThrowCompletionOr<void> TypeofVariable::execute_impl(Bytecode::Interpreter& inte
10351035
// 2. If val is a Reference Record, then
10361036
// a. If IsUnresolvableReference(val) is true, return "undefined".
10371037
if (reference.is_unresolvable()) {
1038-
interpreter.accumulator() = PrimitiveString::create(vm, "undefined"sv);
1038+
interpreter.accumulator() = MUST_OR_THROW_OOM(PrimitiveString::create(vm, "undefined"sv));
10391039
return {};
10401040
}
10411041

@@ -1044,7 +1044,7 @@ ThrowCompletionOr<void> TypeofVariable::execute_impl(Bytecode::Interpreter& inte
10441044

10451045
// 4. NOTE: This step is replaced in section B.3.6.3.
10461046
// 5. Return a String according to Table 41.
1047-
interpreter.accumulator() = PrimitiveString::create(vm, value.typeof());
1047+
interpreter.accumulator() = MUST_OR_THROW_OOM(PrimitiveString::create(vm, value.typeof()));
10481048
return {};
10491049
}
10501050

Userland/Libraries/LibJS/Console.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ ThrowCompletionOr<Value> Console::assert_()
186186
return js_undefined();
187187

188188
// 2. Let message be a string without any formatting specifiers indicating generically an assertion failure (such as "Assertion failed").
189-
auto message = PrimitiveString::create(vm, "Assertion failed");
189+
auto message = MUST_OR_THROW_OOM(PrimitiveString::create(vm, "Assertion failed"sv));
190190

191191
// NOTE: Assemble `data` from the function arguments.
192192
MarkedVector<Value> data { vm.heap() };

Userland/Libraries/LibJS/Print.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,26 @@
6464
#include <LibJS/Runtime/WeakSet.h>
6565

6666
namespace {
67+
6768
ErrorOr<void> print_value(JS::PrintContext&, JS::Value value, HashTable<JS::Object*>& seen_objects);
6869

70+
template<typename T>
71+
ErrorOr<void> print_value(JS::PrintContext& print_context, JS::ThrowCompletionOr<T> value_or_error, HashTable<JS::Object*>& seen_objects)
72+
{
73+
if (value_or_error.is_error()) {
74+
auto error = value_or_error.release_error();
75+
76+
// We can't explicitly check for OOM because InternalError does not store the ErrorType
77+
VERIFY(error.value().has_value());
78+
VERIFY(error.value()->is_object());
79+
VERIFY(is<JS::InternalError>(error.value()->as_object()));
80+
81+
return Error::from_errno(ENOMEM);
82+
}
83+
84+
return print_value(print_context, value_or_error.release_value(), seen_objects);
85+
}
86+
6987
DeprecatedString strip_ansi(StringView format_string)
7088
{
7189
if (format_string.is_empty())

Userland/Libraries/LibJS/Runtime/AggregateErrorPrototype.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ThrowCompletionOr<void> AggregateErrorPrototype::initialize(Realm& realm)
2020
auto& vm = this->vm();
2121
MUST_OR_THROW_OOM(Base::initialize(realm));
2222
u8 attr = Attribute::Writable | Attribute::Configurable;
23-
define_direct_property(vm.names.name, PrimitiveString::create(vm, "AggregateError"), attr);
23+
define_direct_property(vm.names.name, MUST_OR_THROW_OOM(PrimitiveString::create(vm, "AggregateError"sv)), attr);
2424
define_direct_property(vm.names.message, PrimitiveString::create(vm, String {}), attr);
2525

2626
return {};

Userland/Libraries/LibJS/Runtime/ArrayIteratorPrototype.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ThrowCompletionOr<void> ArrayIteratorPrototype::initialize(Realm& realm)
2727
define_native_function(realm, vm.names.next, next, 0, Attribute::Configurable | Attribute::Writable);
2828

2929
// 23.1.5.2.2 %ArrayIteratorPrototype% [ @@toStringTag ], https://tc39.es/ecma262/#sec-%arrayiteratorprototype%-@@tostringtag
30-
define_direct_property(*vm.well_known_symbol_to_string_tag(), PrimitiveString::create(vm, "Array Iterator"), Attribute::Configurable);
30+
define_direct_property(*vm.well_known_symbol_to_string_tag(), MUST_OR_THROW_OOM(PrimitiveString::create(vm, "Array Iterator"sv)), Attribute::Configurable);
3131

3232
return {};
3333
}

Userland/Libraries/LibJS/Runtime/AsyncGeneratorPrototype.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ThrowCompletionOr<void> AsyncGeneratorPrototype::initialize(Realm& realm)
2020
MUST_OR_THROW_OOM(Base::initialize(realm));
2121

2222
// 27.6.1.5 AsyncGenerator.prototype [ @@toStringTag ], https://tc39.es/ecma262/#sec-asyncgenerator-prototype-tostringtag
23-
define_direct_property(*vm.well_known_symbol_to_string_tag(), PrimitiveString::create(vm, "AsyncGenerator"), Attribute::Configurable);
23+
define_direct_property(*vm.well_known_symbol_to_string_tag(), MUST_OR_THROW_OOM(PrimitiveString::create(vm, "AsyncGenerator"sv)), Attribute::Configurable);
2424

2525
return {};
2626
}

Userland/Libraries/LibJS/Runtime/AtomicsObject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ ThrowCompletionOr<void> AtomicsObject::initialize(Realm& realm)
147147
define_native_function(realm, vm.names.xor_, xor_, 3, attr);
148148

149149
// 25.4.15 Atomics [ @@toStringTag ], https://tc39.es/ecma262/#sec-atomics-@@tostringtag
150-
define_direct_property(*vm.well_known_symbol_to_string_tag(), PrimitiveString::create(vm, "Atomics"), Attribute::Configurable);
150+
define_direct_property(*vm.well_known_symbol_to_string_tag(), MUST_OR_THROW_OOM(PrimitiveString::create(vm, "Atomics"sv)), Attribute::Configurable);
151151

152152
return {};
153153
}

0 commit comments

Comments
 (0)