2020-04-10 14:14:02 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-10 14:14:02 +02:00
|
|
|
*/
|
|
|
|
|
2020-05-06 17:49:48 -07:00
|
|
|
#include <AK/StringBuilder.h>
|
2021-08-02 17:02:17 -04:00
|
|
|
#include <AK/Utf16View.h>
|
2020-05-31 00:47:28 +01:00
|
|
|
#include <AK/Utf32View.h>
|
2021-06-28 13:14:41 +03:00
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
2020-05-06 17:49:48 -07:00
|
|
|
#include <LibJS/Runtime/Array.h>
|
2020-04-10 14:14:02 +02:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
2020-04-18 13:18:06 +02:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-04-10 14:14:02 +02:00
|
|
|
#include <LibJS/Runtime/StringConstructor.h>
|
|
|
|
#include <LibJS/Runtime/StringObject.h>
|
2021-08-09 09:06:45 -04:00
|
|
|
#include <LibJS/Runtime/Utf16String.h>
|
2020-04-10 14:14:02 +02:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2020-06-20 15:40:48 +02:00
|
|
|
StringConstructor::StringConstructor(GlobalObject& global_object)
|
2021-06-28 03:45:49 +03:00
|
|
|
: NativeFunction(vm().names.String.as_string(), *global_object.function_prototype())
|
2020-04-10 14:14:02 +02:00
|
|
|
{
|
2020-06-20 15:40:48 +02:00
|
|
|
}
|
|
|
|
|
2020-07-22 17:50:18 +02:00
|
|
|
void StringConstructor::initialize(GlobalObject& global_object)
|
2020-06-20 15:40:48 +02:00
|
|
|
{
|
2020-10-13 23:49:19 +02:00
|
|
|
auto& vm = this->vm();
|
2020-07-22 17:50:18 +02:00
|
|
|
NativeFunction::initialize(global_object);
|
2021-06-13 00:22:35 +01:00
|
|
|
|
|
|
|
// 22.1.2.3 String.prototype, https://tc39.es/ecma262/#sec-string.prototype
|
2021-07-06 02:15:08 +03:00
|
|
|
define_direct_property(vm.names.prototype, global_object.string_prototype(), 0);
|
2021-06-13 00:22:35 +01:00
|
|
|
|
2020-05-31 00:47:28 +01:00
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
2021-10-20 01:40:40 +03:00
|
|
|
define_old_native_function(vm.names.raw, raw, 1, attr);
|
|
|
|
define_old_native_function(vm.names.fromCharCode, from_char_code, 1, attr);
|
|
|
|
define_old_native_function(vm.names.fromCodePoint, from_code_point, 1, attr);
|
2021-07-08 02:49:53 +03:00
|
|
|
|
|
|
|
define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
|
2020-04-10 14:14:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
StringConstructor::~StringConstructor()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
// 22.1.1.1 String ( value ), https://tc39.es/ecma262/#sec-string-constructor-string-value
|
2021-10-20 21:16:30 +01:00
|
|
|
ThrowCompletionOr<Value> StringConstructor::call()
|
2020-04-10 14:14:02 +02:00
|
|
|
{
|
2020-09-27 17:24:14 +02:00
|
|
|
if (!vm().argument_count())
|
|
|
|
return js_string(heap(), "");
|
|
|
|
if (vm().argument(0).is_symbol())
|
|
|
|
return js_string(heap(), vm().argument(0).as_symbol().to_string());
|
2021-10-20 21:16:30 +01:00
|
|
|
return TRY(vm().argument(0).to_primitive_string(global_object()));
|
2020-04-10 14:14:02 +02:00
|
|
|
}
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
// 22.1.1.1 String ( value ), https://tc39.es/ecma262/#sec-string-constructor-string-value
|
2021-10-20 21:16:30 +01:00
|
|
|
ThrowCompletionOr<Object*> StringConstructor::construct(FunctionObject& new_target)
|
2020-04-10 14:14:02 +02:00
|
|
|
{
|
2021-06-30 18:13:51 +03:00
|
|
|
auto& vm = global_object().vm();
|
|
|
|
|
|
|
|
PrimitiveString* primitive_string;
|
|
|
|
if (!vm.argument_count())
|
|
|
|
primitive_string = js_string(vm, "");
|
2020-04-10 14:14:02 +02:00
|
|
|
else
|
2021-10-20 21:16:30 +01:00
|
|
|
primitive_string = TRY(vm.argument(0).to_primitive_string(global_object()));
|
|
|
|
auto* prototype = TRY(get_prototype_from_constructor(global_object(), new_target, &GlobalObject::string_prototype));
|
2021-06-30 18:13:51 +03:00
|
|
|
return StringObject::create(global_object(), *primitive_string, *prototype);
|
2020-04-10 14:14:02 +02:00
|
|
|
}
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
// 22.1.2.4 String.raw ( template, ...substitutions ), https://tc39.es/ecma262/#sec-string.raw
|
2021-10-19 20:18:01 +03:00
|
|
|
JS_DEFINE_OLD_NATIVE_FUNCTION(StringConstructor::raw)
|
2020-05-06 17:49:48 -07:00
|
|
|
{
|
2021-10-12 19:24:57 +01:00
|
|
|
auto* cooked = TRY_OR_DISCARD(vm.argument(0).to_object(global_object));
|
|
|
|
auto raw_value = TRY_OR_DISCARD(cooked->get(vm.names.raw));
|
|
|
|
auto* raw = TRY_OR_DISCARD(raw_value.to_object(global_object));
|
2021-09-21 21:59:49 +03:00
|
|
|
auto literal_segments = TRY_OR_DISCARD(length_of_array_like(global_object, *raw));
|
2021-06-28 13:14:41 +03:00
|
|
|
|
|
|
|
if (literal_segments == 0)
|
2020-09-27 18:36:49 +02:00
|
|
|
return js_string(vm, "");
|
2020-05-06 17:49:48 -07:00
|
|
|
|
2021-06-28 13:14:41 +03:00
|
|
|
const auto number_of_substituions = vm.argument_count() - 1;
|
2020-05-06 17:49:48 -07:00
|
|
|
|
2021-06-28 13:14:41 +03:00
|
|
|
StringBuilder builder;
|
|
|
|
for (size_t i = 0; i < literal_segments; ++i) {
|
|
|
|
auto next_key = String::number(i);
|
2021-10-12 17:49:01 +01:00
|
|
|
auto next_segment_value = TRY_OR_DISCARD(raw->get(next_key));
|
|
|
|
auto next_segment = TRY_OR_DISCARD(next_segment_value.to_string(global_object));
|
2021-06-28 13:14:41 +03:00
|
|
|
|
|
|
|
builder.append(next_segment);
|
|
|
|
|
|
|
|
if (i + 1 == literal_segments)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (i < number_of_substituions) {
|
|
|
|
auto next = vm.argument(i + 1);
|
2021-10-12 17:49:01 +01:00
|
|
|
auto next_sub = TRY_OR_DISCARD(next.to_string(global_object));
|
2021-06-28 13:14:41 +03:00
|
|
|
builder.append(next_sub);
|
2020-05-15 13:39:24 +02:00
|
|
|
}
|
2020-05-06 17:49:48 -07:00
|
|
|
}
|
2020-09-27 18:36:49 +02:00
|
|
|
return js_string(vm, builder.build());
|
2020-05-06 17:49:48 -07:00
|
|
|
}
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
// 22.1.2.1 String.fromCharCode ( ...codeUnits ), https://tc39.es/ecma262/#sec-string.fromcharcode
|
2021-10-19 20:18:01 +03:00
|
|
|
JS_DEFINE_OLD_NATIVE_FUNCTION(StringConstructor::from_char_code)
|
2020-05-31 00:47:28 +01:00
|
|
|
{
|
2021-10-02 17:41:34 +02:00
|
|
|
Vector<u16, 1> string;
|
2021-08-02 17:02:17 -04:00
|
|
|
string.ensure_capacity(vm.argument_count());
|
|
|
|
|
2021-10-17 23:48:24 +03:00
|
|
|
for (size_t i = 0; i < vm.argument_count(); ++i)
|
|
|
|
string.append(TRY_OR_DISCARD(vm.argument(i).to_u16(global_object)));
|
2021-08-02 17:02:17 -04:00
|
|
|
|
2021-08-09 09:06:45 -04:00
|
|
|
return js_string(vm, Utf16String(move(string)));
|
2020-05-31 00:47:28 +01:00
|
|
|
}
|
|
|
|
|
2021-06-16 11:36:23 +03:00
|
|
|
// 22.1.2.2 String.fromCodePoint ( ...codePoints ), https://tc39.es/ecma262/#sec-string.fromcodepoint
|
2021-10-19 20:18:01 +03:00
|
|
|
JS_DEFINE_OLD_NATIVE_FUNCTION(StringConstructor::from_code_point)
|
2021-06-16 11:36:23 +03:00
|
|
|
{
|
2021-10-02 17:37:15 +02:00
|
|
|
Vector<u16, 1> string;
|
2021-08-02 17:02:17 -04:00
|
|
|
string.ensure_capacity(vm.argument_count()); // This will be an under-estimate if any code point is > 0xffff.
|
|
|
|
|
2021-06-16 11:36:23 +03:00
|
|
|
for (size_t i = 0; i < vm.argument_count(); ++i) {
|
2021-10-17 23:20:05 +03:00
|
|
|
auto next_code_point = TRY_OR_DISCARD(vm.argument(i).to_number(global_object));
|
2021-06-16 13:17:06 +03:00
|
|
|
if (!next_code_point.is_integral_number()) {
|
2021-06-16 11:36:23 +03:00
|
|
|
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects());
|
|
|
|
return {};
|
|
|
|
}
|
2021-10-17 23:33:35 +03:00
|
|
|
auto code_point = TRY_OR_DISCARD(next_code_point.to_i32(global_object));
|
2021-06-16 11:36:23 +03:00
|
|
|
if (code_point < 0 || code_point > 0x10FFFF) {
|
|
|
|
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects());
|
|
|
|
return {};
|
|
|
|
}
|
2021-08-02 17:02:17 -04:00
|
|
|
|
|
|
|
AK::code_point_to_utf16(string, static_cast<u32>(code_point));
|
2021-06-16 11:36:23 +03:00
|
|
|
}
|
2021-08-02 17:02:17 -04:00
|
|
|
|
2021-08-09 09:06:45 -04:00
|
|
|
return js_string(vm, Utf16String(move(string)));
|
2021-06-16 11:36:23 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 14:14:02 +02:00
|
|
|
}
|