2020-04-10 09:14:02 -03:00
|
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2020-04-10 09:14:02 -03:00
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-10 09:14:02 -03:00
|
|
|
|
*/
|
|
|
|
|
|
|
2025-06-12 18:39:05 -03:00
|
|
|
|
#include <AK/UnicodeUtils.h>
|
2025-07-09 15:13:22 -03:00
|
|
|
|
#include <AK/Utf16String.h>
|
2026-06-21 20:34:34 -03:00
|
|
|
|
#include <AK/Utf16StringBuilder.h>
|
2021-08-02 18:02:17 -03:00
|
|
|
|
#include <AK/Utf16View.h>
|
2021-06-28 07:14:41 -03:00
|
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
2020-05-06 21:49:48 -03:00
|
|
|
|
#include <LibJS/Runtime/Array.h>
|
2020-04-10 09:14:02 -03:00
|
|
|
|
#include <LibJS/Runtime/Error.h>
|
2020-04-18 08:18:06 -03:00
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-04-10 09:14:02 -03:00
|
|
|
|
#include <LibJS/Runtime/StringConstructor.h>
|
|
|
|
|
|
#include <LibJS/Runtime/StringObject.h>
|
2023-10-06 12:54:21 -03:00
|
|
|
|
#include <LibJS/Runtime/ValueInlines.h>
|
2020-04-10 09:14:02 -03:00
|
|
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
|
GC_DEFINE_ALLOCATOR(StringConstructor);
|
2023-11-19 05:45:05 -03:00
|
|
|
|
|
2022-08-15 20:20:49 -03:00
|
|
|
|
StringConstructor::StringConstructor(Realm& realm)
|
2023-04-12 19:47:15 -03:00
|
|
|
|
: NativeFunction(realm.vm().names.String.as_string(), realm.intrinsics().function_prototype())
|
2020-04-10 09:14:02 -03:00
|
|
|
|
{
|
2020-06-20 10:40:48 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-07 03:41:28 -03:00
|
|
|
|
void StringConstructor::initialize(Realm& realm)
|
2020-06-20 10:40:48 -03:00
|
|
|
|
{
|
2020-10-13 18:49:19 -03:00
|
|
|
|
auto& vm = this->vm();
|
2023-08-07 03:41:28 -03:00
|
|
|
|
Base::initialize(realm);
|
2021-06-12 20:22:35 -03:00
|
|
|
|
|
|
|
|
|
|
// 22.1.2.3 String.prototype, https://tc39.es/ecma262/#sec-string.prototype
|
2022-08-26 20:54:55 -03:00
|
|
|
|
define_direct_property(vm.names.prototype, realm.intrinsics().string_prototype(), 0);
|
2021-06-12 20:22:35 -03:00
|
|
|
|
|
2020-05-30 20:47:28 -03:00
|
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
2022-08-22 17:47:35 -03:00
|
|
|
|
define_native_function(realm, vm.names.raw, raw, 1, attr);
|
2026-04-12 11:12:40 -03:00
|
|
|
|
define_native_function(realm, vm.names.fromCharCode, from_char_code, 1, attr, Bytecode::Builtin::StringFromCharCode);
|
2022-08-22 17:47:35 -03:00
|
|
|
|
define_native_function(realm, vm.names.fromCodePoint, from_code_point, 1, attr);
|
2021-07-07 20:49:53 -03:00
|
|
|
|
|
|
|
|
|
|
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
|
2020-04-10 09:14:02 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
|
// 22.1.1.1 String ( value ), https://tc39.es/ecma262/#sec-string-constructor-string-value
|
2021-10-20 17:16:30 -03:00
|
|
|
|
ThrowCompletionOr<Value> StringConstructor::call()
|
2020-04-10 09:14:02 -03:00
|
|
|
|
{
|
2022-08-21 10:00:56 -03:00
|
|
|
|
auto& vm = this->vm();
|
2023-04-12 18:12:16 -03:00
|
|
|
|
auto value = vm.argument(0);
|
|
|
|
|
|
|
|
|
|
|
|
// 1. If value is not present, let s be the empty String.
|
2022-08-21 10:00:56 -03:00
|
|
|
|
if (!vm.argument_count())
|
2026-06-21 14:03:10 -03:00
|
|
|
|
return PrimitiveString::create(vm, Utf16String {});
|
2023-04-12 18:12:16 -03:00
|
|
|
|
|
|
|
|
|
|
// 2. Else,
|
|
|
|
|
|
// a. If NewTarget is undefined and value is a Symbol, return SymbolDescriptiveString(value).
|
|
|
|
|
|
if (value.is_symbol())
|
2025-08-02 20:27:29 -03:00
|
|
|
|
return PrimitiveString::create(vm, value.as_symbol().descriptive_string());
|
2023-04-12 18:12:16 -03:00
|
|
|
|
|
|
|
|
|
|
// b. Let s be ? ToString(value).
|
|
|
|
|
|
// 3. If NewTarget is undefined, return s.
|
|
|
|
|
|
return TRY(value.to_primitive_string(vm));
|
2020-04-10 09:14:02 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
|
// 22.1.1.1 String ( value ), https://tc39.es/ecma262/#sec-string-constructor-string-value
|
2024-11-14 12:01:23 -03:00
|
|
|
|
ThrowCompletionOr<GC::Ref<Object>> StringConstructor::construct(FunctionObject& new_target)
|
2020-04-10 09:14:02 -03:00
|
|
|
|
{
|
2022-08-15 20:20:49 -03:00
|
|
|
|
auto& vm = this->vm();
|
2022-08-22 15:00:49 -03:00
|
|
|
|
auto& realm = *vm.current_realm();
|
2023-04-12 18:12:16 -03:00
|
|
|
|
auto value = vm.argument(0);
|
2021-06-30 12:13:51 -03:00
|
|
|
|
|
|
|
|
|
|
PrimitiveString* primitive_string;
|
2023-04-12 18:12:16 -03:00
|
|
|
|
|
|
|
|
|
|
// 1. If value is not present, let s be the empty String.
|
|
|
|
|
|
if (!vm.argument_count()) {
|
2026-06-21 14:03:10 -03:00
|
|
|
|
primitive_string = PrimitiveString::create(vm, Utf16String {});
|
2023-04-12 18:12:16 -03:00
|
|
|
|
}
|
|
|
|
|
|
// 2. Else,
|
|
|
|
|
|
else {
|
|
|
|
|
|
// b. Let s be ? ToString(value).
|
|
|
|
|
|
primitive_string = TRY(value.to_primitive_string(vm));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 4. Return StringCreate(s, ? GetPrototypeFromConstructor(NewTarget, "%String.prototype%")).
|
2022-08-26 20:54:55 -03:00
|
|
|
|
auto* prototype = TRY(get_prototype_from_constructor(vm, new_target, &Intrinsics::string_prototype));
|
2023-08-13 08:05:26 -03:00
|
|
|
|
return StringObject::create(realm, *primitive_string, *prototype);
|
2020-04-10 09:14:02 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-12 11:12:40 -03:00
|
|
|
|
// 22.1.2.1 String.fromCharCode ( ...codeUnits ), https://tc39.es/ecma262/#sec-string.fromcharcode
|
|
|
|
|
|
ThrowCompletionOr<Value> StringConstructor::from_char_code_impl(VM& vm, Value code_unit)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto value = static_cast<char16_t>(TRY(code_unit.to_u16(vm)));
|
|
|
|
|
|
return PrimitiveString::create(vm, Utf16View(&value, 1));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-12 20:22:35 -03:00
|
|
|
|
// 22.1.2.1 String.fromCharCode ( ...codeUnits ), https://tc39.es/ecma262/#sec-string.fromcharcode
|
2021-10-22 20:54:12 -03:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_char_code)
|
2020-05-30 20:47:28 -03:00
|
|
|
|
{
|
2026-04-12 11:12:40 -03:00
|
|
|
|
if (vm.argument_count() == 1)
|
|
|
|
|
|
return from_char_code_impl(vm, vm.argument(0));
|
|
|
|
|
|
|
2023-04-12 18:12:16 -03:00
|
|
|
|
// 1. Let result be the empty String.
|
2026-06-21 20:34:34 -03:00
|
|
|
|
Utf16StringBuilder builder(vm.argument_count());
|
2021-08-02 18:02:17 -03:00
|
|
|
|
|
2023-04-12 18:12:16 -03:00
|
|
|
|
// 2. For each element next of codeUnits, do
|
|
|
|
|
|
for (size_t i = 0; i < vm.argument_count(); ++i) {
|
|
|
|
|
|
// a. Let nextCU be the code unit whose numeric value is ℝ(? ToUint16(next)).
|
|
|
|
|
|
auto next_code_unit = TRY(vm.argument(i).to_u16(vm));
|
2021-08-02 18:02:17 -03:00
|
|
|
|
|
2023-04-12 18:12:16 -03:00
|
|
|
|
// b. Set result to the string-concatenation of result and nextCU.
|
2025-07-09 15:13:22 -03:00
|
|
|
|
builder.append_code_unit(next_code_unit);
|
2023-04-12 18:12:16 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Return result.
|
2026-06-21 20:34:34 -03:00
|
|
|
|
return PrimitiveString::create(vm, builder.to_string());
|
2020-05-30 20:47:28 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-16 05:36:23 -03:00
|
|
|
|
// 22.1.2.2 String.fromCodePoint ( ...codePoints ), https://tc39.es/ecma262/#sec-string.fromcodepoint
|
2021-10-22 20:54:12 -03:00
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_code_point)
|
2021-06-16 05:36:23 -03:00
|
|
|
|
{
|
2023-04-12 18:12:16 -03:00
|
|
|
|
// 1. Let result be the empty String.
|
2025-07-09 15:13:22 -03:00
|
|
|
|
// NOTE: This will be an under-estimate if any code point is > 0xffff.
|
2026-06-21 20:34:34 -03:00
|
|
|
|
Utf16StringBuilder builder(vm.argument_count());
|
2021-08-02 18:02:17 -03:00
|
|
|
|
|
2023-04-12 18:12:16 -03:00
|
|
|
|
// 2. For each element next of codePoints, do
|
2021-06-16 05:36:23 -03:00
|
|
|
|
for (size_t i = 0; i < vm.argument_count(); ++i) {
|
2023-04-12 18:12:16 -03:00
|
|
|
|
// a. Let nextCP be ? ToNumber(next).
|
2022-08-21 10:00:56 -03:00
|
|
|
|
auto next_code_point = TRY(vm.argument(i).to_number(vm));
|
2023-04-12 18:12:16 -03:00
|
|
|
|
|
|
|
|
|
|
// b. If IsIntegralNumber(nextCP) is false, throw a RangeError exception.
|
2021-10-22 20:54:12 -03:00
|
|
|
|
if (!next_code_point.is_integral_number())
|
2025-12-05 04:01:44 -03:00
|
|
|
|
return vm.throw_completion<RangeError>(ErrorType::InvalidCodePoint, next_code_point);
|
2023-04-12 18:12:16 -03:00
|
|
|
|
|
|
|
|
|
|
auto code_point = MUST(next_code_point.to_i32(vm));
|
|
|
|
|
|
|
|
|
|
|
|
// c. If ℝ(nextCP) < 0 or ℝ(nextCP) > 0x10FFFF, throw a RangeError exception.
|
2021-10-22 20:54:12 -03:00
|
|
|
|
if (code_point < 0 || code_point > 0x10FFFF)
|
2025-12-05 04:01:44 -03:00
|
|
|
|
return vm.throw_completion<RangeError>(ErrorType::InvalidCodePoint, next_code_point);
|
2021-08-02 18:02:17 -03:00
|
|
|
|
|
2023-04-12 18:12:16 -03:00
|
|
|
|
// d. Set result to the string-concatenation of result and UTF16EncodeCodePoint(ℝ(nextCP)).
|
2025-06-12 18:39:05 -03:00
|
|
|
|
(void)AK::UnicodeUtils::code_point_to_utf16(static_cast<u32>(code_point), [&](auto code_unit) {
|
2025-07-09 15:13:22 -03:00
|
|
|
|
builder.append_code_unit(code_unit);
|
2025-06-12 18:39:05 -03:00
|
|
|
|
});
|
2021-06-16 05:36:23 -03:00
|
|
|
|
}
|
2021-08-02 18:02:17 -03:00
|
|
|
|
|
2023-04-12 18:12:16 -03:00
|
|
|
|
// 3. Assert: If codePoints is empty, then result is the empty String.
|
|
|
|
|
|
if (!vm.argument_count())
|
2025-07-09 15:13:22 -03:00
|
|
|
|
VERIFY(builder.is_empty());
|
2023-04-12 18:12:16 -03:00
|
|
|
|
|
|
|
|
|
|
// 4. Return result.
|
2026-06-21 20:34:34 -03:00
|
|
|
|
return PrimitiveString::create(vm, builder.to_string());
|
2021-06-16 05:36:23 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-12 18:12:16 -03:00
|
|
|
|
// 22.1.2.4 String.raw ( template, ...substitutions ), https://tc39.es/ecma262/#sec-string.raw
|
|
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(StringConstructor::raw)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto template_ = vm.argument(0);
|
|
|
|
|
|
|
|
|
|
|
|
// 1. Let substitutionCount be the number of elements in substitutions.
|
|
|
|
|
|
auto substitution_count = vm.argument_count() > 0 ? vm.argument_count() - 1 : 0;
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Let cooked be ? ToObject(template).
|
2023-04-13 10:26:41 -03:00
|
|
|
|
auto cooked = TRY(template_.to_object(vm));
|
2023-04-12 18:12:16 -03:00
|
|
|
|
|
|
|
|
|
|
// 3. Let literals be ? ToObject(? Get(cooked, "raw")).
|
2023-04-13 10:26:41 -03:00
|
|
|
|
auto literals = TRY(TRY(cooked->get(vm.names.raw)).to_object(vm));
|
2023-04-12 18:12:16 -03:00
|
|
|
|
|
|
|
|
|
|
// 4. Let literalCount be ? LengthOfArrayLike(literals).
|
2023-04-13 10:26:41 -03:00
|
|
|
|
auto literal_count = TRY(length_of_array_like(vm, literals));
|
2023-04-12 18:12:16 -03:00
|
|
|
|
|
|
|
|
|
|
// 5. If literalCount ≤ 0, return the empty String.
|
|
|
|
|
|
if (literal_count == 0)
|
2026-06-21 14:03:10 -03:00
|
|
|
|
return PrimitiveString::create(vm, Utf16String {});
|
2023-04-12 18:12:16 -03:00
|
|
|
|
|
|
|
|
|
|
// 6. Let R be the empty String.
|
2026-06-21 20:34:34 -03:00
|
|
|
|
Utf16StringBuilder builder;
|
2023-04-12 18:12:16 -03:00
|
|
|
|
|
|
|
|
|
|
// 7. Let nextIndex be 0.
|
|
|
|
|
|
// 8. Repeat,
|
|
|
|
|
|
for (size_t i = 0; i < literal_count; ++i) {
|
|
|
|
|
|
// a. Let nextLiteralVal be ? Get(literals, ! ToString(𝔽(nextIndex))).
|
2025-03-16 19:44:29 -03:00
|
|
|
|
auto next_literal_value = TRY(literals->get(PropertyKey(i)));
|
2023-04-12 18:12:16 -03:00
|
|
|
|
|
|
|
|
|
|
// b. Let nextLiteral be ? ToString(nextLiteralVal).
|
2026-06-21 20:34:34 -03:00
|
|
|
|
auto next_literal = TRY(next_literal_value.to_utf16_string(vm));
|
2023-04-12 18:12:16 -03:00
|
|
|
|
|
|
|
|
|
|
// c. Set R to the string-concatenation of R and nextLiteral.
|
2026-06-21 20:34:34 -03:00
|
|
|
|
builder.append(next_literal.utf16_view());
|
2023-04-12 18:12:16 -03:00
|
|
|
|
|
|
|
|
|
|
// d. If nextIndex + 1 = literalCount, return R.
|
|
|
|
|
|
if (i + 1 == literal_count)
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
// e. If nextIndex < substitutionCount, then
|
|
|
|
|
|
if (i < substitution_count) {
|
|
|
|
|
|
// i. Let nextSubVal be substitutions[nextIndex].
|
|
|
|
|
|
auto next_substitution_value = vm.argument(i + 1);
|
|
|
|
|
|
|
|
|
|
|
|
// ii. Let nextSub be ? ToString(nextSubVal).
|
2026-06-21 20:34:34 -03:00
|
|
|
|
auto next_substitution = TRY(next_substitution_value.to_utf16_string(vm));
|
2023-04-12 18:12:16 -03:00
|
|
|
|
|
|
|
|
|
|
// iii. Set R to the string-concatenation of R and nextSub.
|
2026-06-21 20:34:34 -03:00
|
|
|
|
builder.append(next_substitution.utf16_view());
|
2023-04-12 18:12:16 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// f. Set nextIndex to nextIndex + 1.
|
|
|
|
|
|
}
|
2026-06-21 20:34:34 -03:00
|
|
|
|
return PrimitiveString::create(vm, builder.to_string());
|
2023-04-12 18:12:16 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-10 09:14:02 -03:00
|
|
|
|
}
|