mirror of
https://github.com/fergalmoran/ladybird.git
synced 2025-12-25 10:48:41 +00:00
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.
This commit is contained in:
committed by
Andreas Kling
parent
4efccbd030
commit
6a0d1fa259
33
Userland/Libraries/LibJS/Bytecode/StringTable.cpp
Normal file
33
Userland/Libraries/LibJS/Bytecode/StringTable.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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]);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user