diff options
author | Linus Groh <mail@linusgroh.de> | 2020-12-06 16:55:19 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-12-06 18:52:52 +0100 |
commit | 5eb1f752ab1db98d3825320f20e4a543b71bffe6 (patch) | |
tree | 9dcf8874abc6ceec0f1e596c05d0928d27616deb /Libraries/LibJS/Runtime | |
parent | 2313e583930bd0bd8eeec18afc8d72c4b8350093 (diff) | |
download | serenity-5eb1f752ab1db98d3825320f20e4a543b71bffe6.zip |
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.
Diffstat (limited to 'Libraries/LibJS/Runtime')
-rw-r--r-- | Libraries/LibJS/Runtime/Cell.cpp | 8 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Cell.h | 17 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/GlobalObject.cpp | 3 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Object.cpp | 25 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/PropertyAttributes.cpp | 36 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/PropertyAttributes.h | 16 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Reference.h | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/VM.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Value.cpp | 13 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Value.h | 15 |
10 files changed, 60 insertions, 77 deletions
diff --git a/Libraries/LibJS/Runtime/Cell.cpp b/Libraries/LibJS/Runtime/Cell.cpp index 342085264f..22a9979a35 100644 --- a/Libraries/LibJS/Runtime/Cell.cpp +++ b/Libraries/LibJS/Runtime/Cell.cpp @@ -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) << '}'; -} - } diff --git a/Libraries/LibJS/Runtime/Cell.h b/Libraries/LibJS/Runtime/Cell.h index 97f555a764..825887f24e 100644 --- a/Libraries/LibJS/Runtime/Cell.h +++ b/Libraries/LibJS/Runtime/Cell.h @@ -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))); + } +}; } diff --git a/Libraries/LibJS/Runtime/GlobalObject.cpp b/Libraries/LibJS/Runtime/GlobalObject.cpp index edba9ff2ff..a51d353973 100644 --- a/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -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(); } diff --git a/Libraries/LibJS/Runtime/Object.cpp b/Libraries/LibJS/Runtime/Object.cpp index e9631f7621..012ea4a0c2 100644 --- a/Libraries/LibJS/Runtime/Object.cpp +++ b/Libraries/LibJS/Runtime/Object.cpp @@ -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; } diff --git a/Libraries/LibJS/Runtime/PropertyAttributes.cpp b/Libraries/LibJS/Runtime/PropertyAttributes.cpp deleted file mode 100644 index 689c1b1653..0000000000 --- a/Libraries/LibJS/Runtime/PropertyAttributes.cpp +++ /dev/null @@ -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(); -} - -} diff --git a/Libraries/LibJS/Runtime/PropertyAttributes.h b/Libraries/LibJS/Runtime/PropertyAttributes.h index d2fbe50ad7..44a3343947 100644 --- a/Libraries/LibJS/Runtime/PropertyAttributes.h +++ b/Libraries/LibJS/Runtime/PropertyAttributes.h @@ -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()); + } +}; + +} diff --git a/Libraries/LibJS/Runtime/Reference.h b/Libraries/LibJS/Runtime/Reference.h index 149bb31082..c81d964440 100644 --- a/Libraries/LibJS/Runtime/Reference.h +++ b/Libraries/LibJS/Runtime/Reference.h @@ -98,6 +98,4 @@ private: bool m_global_variable { false }; }; -const LogStream& operator<<(const LogStream&, const Value&); - } diff --git a/Libraries/LibJS/Runtime/VM.cpp b/Libraries/LibJS/Runtime/VM.cpp index 75a966e1b2..29fad69f4b 100644 --- a/Libraries/LibJS/Runtime/VM.cpp +++ b/Libraries/LibJS/Runtime/VM.cpp @@ -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); diff --git a/Libraries/LibJS/Runtime/Value.cpp b/Libraries/LibJS/Runtime/Value.cpp index eb00a5116e..4822aae8e5 100644 --- a/Libraries/LibJS/Runtime/Value.cpp +++ b/Libraries/LibJS/Runtime/Value.cpp @@ -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()) diff --git a/Libraries/LibJS/Runtime/Value.h b/Libraries/LibJS/Runtime/Value.h index 6ad54f12d0..1b0b921326 100644 --- a/Libraries/LibJS/Runtime/Value.h +++ b/Libraries/LibJS/Runtime/Value.h @@ -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()); + } +}; } |