2020-03-11 18:55:54 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2021-06-06 16:46:14 +01:00
|
|
|
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
|
2020-03-11 18:55:54 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-11 18:55:54 +01:00
|
|
|
*/
|
|
|
|
|
2020-04-17 19:01:31 +02:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/PrimitiveString.h>
|
|
|
|
#include <LibJS/Runtime/StringObject.h>
|
2020-03-11 18:55:54 +01:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2020-04-17 19:01:31 +02:00
|
|
|
StringObject* StringObject::create(GlobalObject& global_object, PrimitiveString& primitive_string)
|
|
|
|
{
|
2020-06-20 15:40:48 +02:00
|
|
|
return global_object.heap().allocate<StringObject>(global_object, primitive_string, *global_object.string_prototype());
|
2020-04-17 19:01:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
StringObject::StringObject(PrimitiveString& string, Object& prototype)
|
2020-06-23 17:21:53 +02:00
|
|
|
: Object(prototype)
|
2020-04-18 10:27:57 +02:00
|
|
|
, m_string(string)
|
2020-03-11 18:55:54 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
StringObject::~StringObject()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-06 16:46:14 +01:00
|
|
|
void StringObject::initialize(GlobalObject& global_object)
|
|
|
|
{
|
|
|
|
auto& vm = this->vm();
|
|
|
|
Object::initialize(global_object);
|
|
|
|
define_property(vm.names.length, Value(m_string.string().length()), 0);
|
|
|
|
}
|
|
|
|
|
2020-11-28 14:33:36 +01:00
|
|
|
void StringObject::visit_edges(Cell::Visitor& visitor)
|
2020-03-11 18:55:54 +01:00
|
|
|
{
|
2020-11-28 14:33:36 +01:00
|
|
|
Object::visit_edges(visitor);
|
2020-04-17 19:01:31 +02:00
|
|
|
visitor.visit(&m_string);
|
2020-03-11 18:55:54 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 11:34:19 +02:00
|
|
|
Optional<PropertyDescriptor> StringObject::get_own_property_descriptor(PropertyName const& property_name) const
|
|
|
|
{
|
|
|
|
if (!property_name.is_number() || property_name.as_number() >= m_string.string().length())
|
|
|
|
return Base::get_own_property_descriptor(property_name);
|
|
|
|
|
|
|
|
PropertyDescriptor descriptor;
|
|
|
|
descriptor.value = js_string(heap(), m_string.string().substring(property_name.as_number(), 1));
|
|
|
|
descriptor.attributes.set_has_configurable();
|
|
|
|
descriptor.attributes.set_has_enumerable();
|
|
|
|
descriptor.attributes.set_has_writable();
|
|
|
|
descriptor.attributes.set_enumerable();
|
|
|
|
return descriptor;
|
|
|
|
}
|
|
|
|
|
2020-03-11 18:55:54 +01:00
|
|
|
}
|