mirror of
https://github.com/RGBCube/serenity
synced 2025-05-15 10:04:59 +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:
parent
4efccbd030
commit
6a0d1fa259
16 changed files with 173 additions and 82 deletions
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]);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue