From 243caa8fa9a3f255e96bc77130f5d094969a5090 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 21 Jan 2023 12:36:47 -0500 Subject: [PATCH] LibJS: Port the VM's single-character ASCII strings to String This creates the Strings representing the ASCII characters at compile time, then creates the PrimitiveStrings from those Strings when the VM is created. --- Userland/Libraries/LibJS/Runtime/VM.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/VM.cpp b/Userland/Libraries/LibJS/Runtime/VM.cpp index d0bfa88021..d0ee2edea2 100644 --- a/Userland/Libraries/LibJS/Runtime/VM.cpp +++ b/Userland/Libraries/LibJS/Runtime/VM.cpp @@ -6,6 +6,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -37,14 +38,22 @@ NonnullRefPtr VM::create(OwnPtr custom_data) return adopt_ref(*new VM(move(custom_data))); } +template +static constexpr auto make_single_ascii_character_strings(IndexSequence) +{ + return AK::Array { (String::from_code_point(code_points))... }; +} + +static constexpr auto single_ascii_character_strings = make_single_ascii_character_strings(MakeIndexSequence<128>()); + VM::VM(OwnPtr custom_data) : m_heap(*this) , m_custom_data(move(custom_data)) { m_empty_string = m_heap.allocate_without_realm(String {}); - for (size_t i = 0; i < 128; ++i) { - m_single_ascii_character_strings[i] = m_heap.allocate_without_realm(DeprecatedString::formatted("{:c}", i)); - } + + for (size_t i = 0; i < single_ascii_character_strings.size(); ++i) + m_single_ascii_character_strings[i] = m_heap.allocate_without_realm(single_ascii_character_strings[i]); // Default hook implementations. These can be overridden by the host, for example, LibWeb overrides the default hooks to place promise jobs on the microtask queue. host_promise_rejection_tracker = [this](Promise& promise, Promise::RejectionOperation operation) {