mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:17:35 +00:00
LibJS: Convert StringConstructor functions to ThrowCompletionOr
This commit is contained in:
parent
f27d768745
commit
28e1e03234
2 changed files with 23 additions and 27 deletions
|
@ -31,9 +31,9 @@ void StringConstructor::initialize(GlobalObject& global_object)
|
||||||
define_direct_property(vm.names.prototype, global_object.string_prototype(), 0);
|
define_direct_property(vm.names.prototype, global_object.string_prototype(), 0);
|
||||||
|
|
||||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||||
define_old_native_function(vm.names.raw, raw, 1, attr);
|
define_native_function(vm.names.raw, raw, 1, attr);
|
||||||
define_old_native_function(vm.names.fromCharCode, from_char_code, 1, attr);
|
define_native_function(vm.names.fromCharCode, from_char_code, 1, attr);
|
||||||
define_old_native_function(vm.names.fromCodePoint, from_code_point, 1, attr);
|
define_native_function(vm.names.fromCodePoint, from_code_point, 1, attr);
|
||||||
|
|
||||||
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
|
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
|
||||||
}
|
}
|
||||||
|
@ -67,12 +67,12 @@ ThrowCompletionOr<Object*> StringConstructor::construct(FunctionObject& new_targ
|
||||||
}
|
}
|
||||||
|
|
||||||
// 22.1.2.4 String.raw ( template, ...substitutions ), https://tc39.es/ecma262/#sec-string.raw
|
// 22.1.2.4 String.raw ( template, ...substitutions ), https://tc39.es/ecma262/#sec-string.raw
|
||||||
JS_DEFINE_OLD_NATIVE_FUNCTION(StringConstructor::raw)
|
JS_DEFINE_NATIVE_FUNCTION(StringConstructor::raw)
|
||||||
{
|
{
|
||||||
auto* cooked = TRY_OR_DISCARD(vm.argument(0).to_object(global_object));
|
auto* cooked = TRY(vm.argument(0).to_object(global_object));
|
||||||
auto raw_value = TRY_OR_DISCARD(cooked->get(vm.names.raw));
|
auto raw_value = TRY(cooked->get(vm.names.raw));
|
||||||
auto* raw = TRY_OR_DISCARD(raw_value.to_object(global_object));
|
auto* raw = TRY(raw_value.to_object(global_object));
|
||||||
auto literal_segments = TRY_OR_DISCARD(length_of_array_like(global_object, *raw));
|
auto literal_segments = TRY(length_of_array_like(global_object, *raw));
|
||||||
|
|
||||||
if (literal_segments == 0)
|
if (literal_segments == 0)
|
||||||
return js_string(vm, "");
|
return js_string(vm, "");
|
||||||
|
@ -82,8 +82,8 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(StringConstructor::raw)
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
for (size_t i = 0; i < literal_segments; ++i) {
|
for (size_t i = 0; i < literal_segments; ++i) {
|
||||||
auto next_key = String::number(i);
|
auto next_key = String::number(i);
|
||||||
auto next_segment_value = TRY_OR_DISCARD(raw->get(next_key));
|
auto next_segment_value = TRY(raw->get(next_key));
|
||||||
auto next_segment = TRY_OR_DISCARD(next_segment_value.to_string(global_object));
|
auto next_segment = TRY(next_segment_value.to_string(global_object));
|
||||||
|
|
||||||
builder.append(next_segment);
|
builder.append(next_segment);
|
||||||
|
|
||||||
|
@ -92,7 +92,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(StringConstructor::raw)
|
||||||
|
|
||||||
if (i < number_of_substituions) {
|
if (i < number_of_substituions) {
|
||||||
auto next = vm.argument(i + 1);
|
auto next = vm.argument(i + 1);
|
||||||
auto next_sub = TRY_OR_DISCARD(next.to_string(global_object));
|
auto next_sub = TRY(next.to_string(global_object));
|
||||||
builder.append(next_sub);
|
builder.append(next_sub);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -100,34 +100,30 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(StringConstructor::raw)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 22.1.2.1 String.fromCharCode ( ...codeUnits ), https://tc39.es/ecma262/#sec-string.fromcharcode
|
// 22.1.2.1 String.fromCharCode ( ...codeUnits ), https://tc39.es/ecma262/#sec-string.fromcharcode
|
||||||
JS_DEFINE_OLD_NATIVE_FUNCTION(StringConstructor::from_char_code)
|
JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_char_code)
|
||||||
{
|
{
|
||||||
Vector<u16, 1> string;
|
Vector<u16, 1> string;
|
||||||
string.ensure_capacity(vm.argument_count());
|
string.ensure_capacity(vm.argument_count());
|
||||||
|
|
||||||
for (size_t i = 0; i < vm.argument_count(); ++i)
|
for (size_t i = 0; i < vm.argument_count(); ++i)
|
||||||
string.append(TRY_OR_DISCARD(vm.argument(i).to_u16(global_object)));
|
string.append(TRY(vm.argument(i).to_u16(global_object)));
|
||||||
|
|
||||||
return js_string(vm, Utf16String(move(string)));
|
return js_string(vm, Utf16String(move(string)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 22.1.2.2 String.fromCodePoint ( ...codePoints ), https://tc39.es/ecma262/#sec-string.fromcodepoint
|
// 22.1.2.2 String.fromCodePoint ( ...codePoints ), https://tc39.es/ecma262/#sec-string.fromcodepoint
|
||||||
JS_DEFINE_OLD_NATIVE_FUNCTION(StringConstructor::from_code_point)
|
JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_code_point)
|
||||||
{
|
{
|
||||||
Vector<u16, 1> string;
|
Vector<u16, 1> string;
|
||||||
string.ensure_capacity(vm.argument_count()); // This will be an under-estimate if any code point is > 0xffff.
|
string.ensure_capacity(vm.argument_count()); // This will be an under-estimate if any code point is > 0xffff.
|
||||||
|
|
||||||
for (size_t i = 0; i < vm.argument_count(); ++i) {
|
for (size_t i = 0; i < vm.argument_count(); ++i) {
|
||||||
auto next_code_point = TRY_OR_DISCARD(vm.argument(i).to_number(global_object));
|
auto next_code_point = TRY(vm.argument(i).to_number(global_object));
|
||||||
if (!next_code_point.is_integral_number()) {
|
if (!next_code_point.is_integral_number())
|
||||||
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects());
|
return vm.throw_completion<RangeError>(global_object, ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects());
|
||||||
return {};
|
auto code_point = TRY(next_code_point.to_i32(global_object));
|
||||||
}
|
if (code_point < 0 || code_point > 0x10FFFF)
|
||||||
auto code_point = TRY_OR_DISCARD(next_code_point.to_i32(global_object));
|
return vm.throw_completion<RangeError>(global_object, ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects());
|
||||||
if (code_point < 0 || code_point > 0x10FFFF) {
|
|
||||||
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects());
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
AK::code_point_to_utf16(string, static_cast<u32>(code_point));
|
AK::code_point_to_utf16(string, static_cast<u32>(code_point));
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,9 +24,9 @@ public:
|
||||||
private:
|
private:
|
||||||
virtual bool has_constructor() const override { return true; }
|
virtual bool has_constructor() const override { return true; }
|
||||||
|
|
||||||
JS_DECLARE_OLD_NATIVE_FUNCTION(raw);
|
JS_DECLARE_NATIVE_FUNCTION(raw);
|
||||||
JS_DECLARE_OLD_NATIVE_FUNCTION(from_char_code);
|
JS_DECLARE_NATIVE_FUNCTION(from_char_code);
|
||||||
JS_DECLARE_OLD_NATIVE_FUNCTION(from_code_point);
|
JS_DECLARE_NATIVE_FUNCTION(from_code_point);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue