1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 05:27:43 +00:00

LibJS: Use new format functions everywhere

This changes the remaining uses of the following functions across LibJS:

- String::format() => String::formatted()
- dbg() => dbgln()
- printf() => out(), outln()
- fprintf() => warnln()

I also removed the relevant 'LogStream& operator<<' overloads as they're
not needed anymore.
This commit is contained in:
Linus Groh 2020-12-06 16:55:19 +00:00 committed by Andreas Kling
parent 2313e58393
commit 5eb1f752ab
15 changed files with 151 additions and 171 deletions

View file

@ -24,7 +24,6 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/LogStream.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Heap/HeapBlock.h>
#include <LibJS/Runtime/Cell.h>
@ -56,11 +55,4 @@ VM& Cell::vm() const
return heap().vm();
}
const LogStream& operator<<(const LogStream& stream, const Cell* cell)
{
if (!cell)
return stream << "Cell{nullptr}";
return stream << cell->class_name() << '{' << static_cast<const void*>(cell) << '}';
}
}

View file

@ -26,8 +26,10 @@
#pragma once
#include <AK/Format.h>
#include <AK/Forward.h>
#include <AK/Noncopyable.h>
#include <AK/String.h>
#include <LibJS/Forward.h>
namespace JS {
@ -70,6 +72,19 @@ private:
bool m_live { true };
};
const LogStream& operator<<(const LogStream&, const Cell*);
}
namespace AK {
template<>
struct Formatter<JS::Cell> : Formatter<StringView> {
void format(TypeErasedFormatParams& params, FormatBuilder& builder, const JS::Cell* cell)
{
if (!cell)
Formatter<StringView>::format(params, builder, "Cell{nullptr}");
else
Formatter<StringView>::format(params, builder, String::formatted("{}{{{}}}", cell->class_name(), static_cast<const void*>(cell)));
}
};
}

View file

@ -25,7 +25,6 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/LogStream.h>
#include <AK/Utf8View.h>
#include <LibJS/Console.h>
#include <LibJS/Heap/DeferGC.h>
@ -179,7 +178,7 @@ void GlobalObject::visit_edges(Visitor& visitor)
JS_DEFINE_NATIVE_FUNCTION(GlobalObject::gc)
{
dbg() << "Forced garbage collection requested!";
dbgln("Forced garbage collection requested!");
vm.heap().collect_garbage();
return js_undefined();
}

View file

@ -198,11 +198,11 @@ Value Object::get_own_properties(const Object& this_object, PropertyKind kind, b
if (kind == PropertyKind::Key) {
properties_array->define_property(i, js_string(vm(), String::number(i)));
} else if (kind == PropertyKind::Value) {
properties_array->define_property(i, js_string(vm(), String::format("%c", str[i])));
properties_array->define_property(i, js_string(vm(), String::formatted("{:c}", str[i])));
} else {
auto* entry_array = Array::create(global_object());
entry_array->define_property(0, js_string(vm(), String::number(i)));
entry_array->define_property(1, js_string(vm(), String::format("%c", str[i])));
entry_array->define_property(1, js_string(vm(), String::formatted("{:c}", str[i])));
properties_array->define_property(i, entry_array);
}
if (vm().exception())
@ -400,9 +400,7 @@ bool Object::define_property(const StringOrSymbol& property_name, const Object&
}
#ifdef OBJECT_DEBUG
dbg() << "Defining new property " << property_name.to_display_string() << " with accessor descriptor { attributes=" << attributes << ", "
<< "getter=" << getter.to_string_without_side_effects() << ", "
<< "setter=" << setter.to_string_without_side_effects() << "}";
dbgln("Defining new property {} with accessor descriptor {{ attributes={}, getter={}, setter={} }}", property_name.to_display_string(), attributes, getter, setter);
#endif
return define_property(property_name, Accessor::create(vm, getter_function, setter_function), attributes, throw_exceptions);
@ -422,8 +420,7 @@ bool Object::define_property(const StringOrSymbol& property_name, const Object&
return {};
#ifdef OBJECT_DEBUG
dbg() << "Defining new property " << property_name.to_display_string() << " with data descriptor { attributes=" << attributes
<< ", value=" << (value.is_empty() ? "<empty>" : value.to_string_without_side_effects()) << " }";
dbgln("Defining new property {} with data descriptor {{ attributes={}, value={} }}", property_name.to_display_string(), attributes, value);
#endif
return define_property(property_name, value, attributes, throw_exceptions);
@ -503,7 +500,7 @@ bool Object::put_own_property(Object& this_object, const StringOrSymbol& propert
if (!is_extensible() && new_property) {
#ifdef OBJECT_DEBUG
dbg() << "Disallow define_property of non-extensible object";
dbgln("Disallow define_property of non-extensible object");
#endif
if (throw_exceptions && vm().in_strict_mode())
vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_name.to_display_string());
@ -532,7 +529,7 @@ bool Object::put_own_property(Object& this_object, const StringOrSymbol& propert
if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !metadata.value().attributes.is_configurable() && attributes != metadata.value().attributes) {
#ifdef OBJECT_DEBUG
dbg() << "Disallow reconfig of non-configurable property";
dbgln("Disallow reconfig of non-configurable property");
#endif
if (throw_exceptions)
vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_name.to_display_string());
@ -548,14 +545,14 @@ bool Object::put_own_property(Object& this_object, const StringOrSymbol& propert
metadata = shape().lookup(property_name);
#ifdef OBJECT_DEBUG
dbg() << "Reconfigured property " << property_name.to_display_string() << ", new shape says offset is " << metadata.value().offset << " and my storage capacity is " << m_storage.size();
dbgln("Reconfigured property {}, new shape says offset is {} and my storage capacity is {}", property_name.to_display_string(), metadata.value().offset, m_storage.size());
#endif
}
auto value_here = m_storage[metadata.value().offset];
if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !metadata.value().attributes.is_writable()) {
#ifdef OBJECT_DEBUG
dbg() << "Disallow write to non-writable property";
dbgln("Disallow write to non-writable property");
#endif
return false;
}
@ -580,7 +577,7 @@ bool Object::put_own_property_by_index(Object& this_object, u32 property_index,
if (!is_extensible() && new_property) {
#ifdef OBJECT_DEBUG
dbg() << "Disallow define_property of non-extensible object";
dbgln("Disallow define_property of non-extensible object");
#endif
if (throw_exceptions && vm().in_strict_mode())
vm().throw_exception<TypeError>(global_object(), ErrorType::NonExtensibleDefine, property_index);
@ -599,7 +596,7 @@ bool Object::put_own_property_by_index(Object& this_object, u32 property_index,
if (!new_property && mode == PutOwnPropertyMode::DefineProperty && !existing_attributes.is_configurable() && attributes != existing_attributes) {
#ifdef OBJECT_DEBUG
dbg() << "Disallow reconfig of non-configurable property";
dbgln("Disallow reconfig of non-configurable property");
#endif
if (throw_exceptions)
vm().throw_exception<TypeError>(global_object(), ErrorType::DescChangeNonConfigurable, property_index);
@ -609,7 +606,7 @@ bool Object::put_own_property_by_index(Object& this_object, u32 property_index,
auto value_here = new_property ? Value() : existing_property.value().value;
if (!new_property && mode == PutOwnPropertyMode::Put && !value_here.is_accessor() && !existing_attributes.is_writable()) {
#ifdef OBJECT_DEBUG
dbg() << "Disallow write to non-writable property";
dbgln("Disallow write to non-writable property");
#endif
return false;
}

View file

@ -1,36 +0,0 @@
/*
* Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibJS/Runtime/PropertyAttributes.h>
namespace JS {
const LogStream& operator<<(const LogStream& stream, const PropertyAttributes& attributes)
{
return stream << attributes.bits();
}
}

View file

@ -26,7 +26,7 @@
#pragma once
#include <AK/LogStream.h>
#include <AK/Format.h>
#include <AK/Types.h>
namespace JS {
@ -87,8 +87,18 @@ private:
u8 m_bits;
};
const LogStream& operator<<(const LogStream& stream, const PropertyAttributes& attributes);
const PropertyAttributes default_attributes = Attribute::Configurable | Attribute::Writable | Attribute::Enumerable;
}
namespace AK {
template<>
struct Formatter<JS::PropertyAttributes> : Formatter<u8> {
void format(TypeErasedFormatParams& params, FormatBuilder& builder, const JS::PropertyAttributes& attributes)
{
Formatter<u8>::format(params, builder, attributes.bits());
}
};
}

View file

@ -98,6 +98,4 @@ private:
bool m_global_variable { false };
};
const LogStream& operator<<(const LogStream&, const Value&);
}

View file

@ -47,7 +47,7 @@ VM::VM()
{
m_empty_string = m_heap.allocate_without_global_object<PrimitiveString>(String::empty());
for (size_t i = 0; i < 128; ++i) {
m_single_ascii_character_strings[i] = m_heap.allocate_without_global_object<PrimitiveString>(String::format("%c", i));
m_single_ascii_character_strings[i] = m_heap.allocate_without_global_object<PrimitiveString>(String::formatted("{:c}", i));
}
m_scope_object_shape = m_heap.allocate_without_global_object<Shape>(Shape::ShapeWithoutGlobalObjectTag::Tag);

View file

@ -118,7 +118,8 @@ String Value::to_string_without_side_effects() const
return is_negative_infinity() ? "-Infinity" : "Infinity";
if (is_integer())
return String::number(as_i32());
return String::format("%.4f", m_value.as_double);
// FIXME: This should be more sophisticated: don't cut off decimals, don't include trailing zeros
return String::formatted("{:.4}", m_value.as_double);
case Type::String:
return m_value.as_string->string();
case Type::Symbol:
@ -162,7 +163,8 @@ String Value::to_string(GlobalObject& global_object, bool legacy_null_to_empty_s
return is_negative_infinity() ? "-Infinity" : "Infinity";
if (is_integer())
return String::number(as_i32());
return String::format("%.4f", m_value.as_double);
// FIXME: This should be more sophisticated: don't cut off decimals, don't include trailing zeros
return String::formatted("{:.4}", m_value.as_double);
case Type::String:
return m_value.as_string->string();
case Type::Symbol:
@ -237,7 +239,7 @@ Object* Value::to_object(GlobalObject& global_object) const
case Type::Object:
return &const_cast<Object&>(as_object());
default:
dbg() << "Dying because I can't to_object() on " << *this;
dbgln("Dying because I can't to_object() on {}", *this);
ASSERT_NOT_REACHED();
}
}
@ -822,11 +824,6 @@ Value ordinary_has_instance(GlobalObject& global_object, Value lhs, Value rhs)
}
}
const LogStream& operator<<(const LogStream& stream, const Value& value)
{
return stream << (value.is_empty() ? "<empty>" : value.to_string_without_side_effects());
}
bool same_value(Value lhs, Value rhs)
{
if (lhs.type() != rhs.type())

View file

@ -27,8 +27,9 @@
#pragma once
#include <AK/Assertions.h>
#include <AK/Format.h>
#include <AK/Forward.h>
#include <AK/LogStream.h>
#include <AK/String.h>
#include <AK/Types.h>
#include <LibJS/Forward.h>
#include <math.h>
@ -337,6 +338,16 @@ bool same_value_non_numeric(Value lhs, Value rhs);
TriState abstract_relation(GlobalObject&, bool left_first, Value lhs, Value rhs);
size_t length_of_array_like(GlobalObject&, Value);
const LogStream& operator<<(const LogStream&, const Value&);
}
namespace AK {
template<>
struct Formatter<JS::Value> : Formatter<StringView> {
void format(TypeErasedFormatParams& params, FormatBuilder& builder, const JS::Value& value)
{
Formatter<StringView>::format(params, builder, value.is_empty() ? "<empty>" : value.to_string_without_side_effects());
}
};
}