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;
|
2020-10-13 23:49:19 +02:00
|
|
|
define_native_function(vm.names.raw, raw, 1, attr);
|
|
|
|
define_native_function(vm.names.fromCharCode, from_char_code, 1, attr);
|
2021-06-16 11:36:23 +03:00
|
|
|
define_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
|
2020-09-27 17:24:14 +02:00
|
|
|
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());
|
2020-09-27 18:36:49 +02:00
|
|
|
auto* string = vm().argument(0).to_primitive_string(global_object());
|
2020-09-27 17:24:14 +02:00
|
|
|
if (vm().exception())
|
2020-05-15 13:39:24 +02:00
|
|
|
return {};
|
|
|
|
return string;
|
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-06-30 18:13:51 +03:00
|
|
|
Value 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-06-30 18:13:51 +03:00
|
|
|
primitive_string = vm.argument(0).to_primitive_string(global_object());
|
2020-04-17 19:01:31 +02:00
|
|
|
if (!primitive_string)
|
|
|
|
return {};
|
2021-09-15 21:16:31 +01:00
|
|
|
auto* prototype = TRY_OR_DISCARD(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
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(StringConstructor::raw)
|
2020-05-06 17:49:48 -07:00
|
|
|
{
|
2021-06-28 13:14:41 +03:00
|
|
|
auto* cooked = vm.argument(0).to_object(global_object);
|
2020-09-27 18:36:49 +02:00
|
|
|
if (vm.exception())
|
2020-05-06 17:49:48 -07:00
|
|
|
return {};
|
|
|
|
|
2021-10-02 23:52:27 +01:00
|
|
|
auto* raw = TRY_OR_DISCARD(cooked->get(vm.names.raw)).to_object(global_object);
|
2021-06-28 13:14:41 +03:00
|
|
|
if (vm.exception())
|
2020-05-06 17:49:48 -07:00
|
|
|
return {};
|
2021-06-28 13:14:41 +03:00
|
|
|
|
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 (vm.exception())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
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-02 23:52:27 +01:00
|
|
|
auto next_segment = TRY_OR_DISCARD(raw->get(next_key)).to_string(global_object);
|
2020-09-27 18:36:49 +02:00
|
|
|
if (vm.exception())
|
LibJS: Object index properties have descriptors; Handle sparse indices
This patch adds an IndexedProperties object for storing indexed
properties within an Object. This accomplishes two goals: indexed
properties now have an associated descriptor, and objects now gracefully
handle sparse properties.
The IndexedProperties class is a wrapper around two other classes, one
for simple indexed properties storage, and one for general indexed
property storage. Simple indexed property storage is the common-case,
and is simply a vector of properties which all have attributes of
default_attributes (writable, enumerable, and configurable).
General indexed property storage is for a collection of indexed
properties where EITHER one or more properties have attributes other
than default_attributes OR there is a property with a large index (in
particular, large is '200' or higher).
Indexed properties are now treated relatively the same as storage within
the various Object methods. Additionally, there is a custom iterator
class for IndexedProperties which makes iteration easy. The iterator
skips empty values by default, but can be configured otherwise.
Likewise, it evaluates getters by default, but can be set not to.
2020-05-27 11:35:09 -07:00
|
|
|
return {};
|
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);
|
|
|
|
auto next_sub = next.to_string(global_object);
|
2020-09-27 18:36:49 +02:00
|
|
|
if (vm.exception())
|
2020-05-15 13:39:24 +02:00
|
|
|
return {};
|
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
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_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());
|
|
|
|
|
2020-09-27 18:36:49 +02:00
|
|
|
for (size_t i = 0; i < vm.argument_count(); ++i) {
|
2021-08-02 17:02:17 -04:00
|
|
|
auto code_unit = vm.argument(i).to_u16(global_object);
|
2020-09-27 18:36:49 +02:00
|
|
|
if (vm.exception())
|
2020-05-31 00:47:28 +01:00
|
|
|
return {};
|
2021-08-02 17:02:17 -04:00
|
|
|
string.append(code_unit);
|
2020-05-31 00:47:28 +01:00
|
|
|
}
|
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
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_code_point)
|
|
|
|
{
|
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) {
|
|
|
|
auto next_code_point = vm.argument(i).to_number(global_object);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
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 {};
|
|
|
|
}
|
|
|
|
auto code_point = next_code_point.to_i32(global_object);
|
|
|
|
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
|
|
|
}
|