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