ladybird/Userland/Libraries/LibJS/Bytecode/StringTable.cpp
Gunnar Beutner 6a0d1fa259 LibJS: Store strings in a string table
Instead of using Strings in the bytecode ops this adds a global string
table to the Executable struct which individual operations can refer
to using indices. This brings bytecode ops one step closer to being
pointer free.
2021-06-09 17:42:52 +02:00

34 lines
684 B
C++

/*
* Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Bytecode/StringTable.h>
namespace JS::Bytecode {
StringTableIndex StringTable::insert(StringView string)
{
for (size_t i = 0; i < m_strings.size(); i++) {
if (m_strings[i] == string)
return i;
}
m_strings.append(string);
return m_strings.size() - 1;
}
String const& StringTable::get(StringTableIndex index) const
{
return m_strings[index.value()];
}
void StringTable::dump() const
{
outln("String Table:");
for (size_t i = 0; i < m_strings.size(); i++)
outln("{}: {}", i, m_strings[i]);
}
}