mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2024-12-28 00:15:06 +00:00
b88f0d2dbd
Mostly to avoid string literal lookup by index with iteration over all preciding literals and calling strlen. This is very inefficient. In genral this makes code much more straightforward but also makes it portable since now int and float of different sizes are properly supported.
91 lines
2.1 KiB
C++
91 lines
2.1 KiB
C++
#include "runtime.hpp"
|
|
#include "program.hpp"
|
|
|
|
#include <cassert>
|
|
#include <cstring>
|
|
#include <stdexcept>
|
|
|
|
namespace Interpreter
|
|
{
|
|
int Runtime::getIntegerLiteral(int index) const
|
|
{
|
|
if (index < 0 || mProgram->mIntegers.size() <= static_cast<std::size_t>(index))
|
|
throw std::out_of_range("Invalid integer index");
|
|
|
|
return mProgram->mIntegers[static_cast<std::size_t>(index)];
|
|
}
|
|
|
|
float Runtime::getFloatLiteral(int index) const
|
|
{
|
|
if (index < 0 || mProgram->mFloats.size() <= static_cast<std::size_t>(index))
|
|
throw std::out_of_range("Invalid float index");
|
|
|
|
return mProgram->mFloats[static_cast<std::size_t>(index)];
|
|
}
|
|
|
|
std::string_view Runtime::getStringLiteral(int index) const
|
|
{
|
|
if (index < 0 || mProgram->mStrings.size() <= static_cast<std::size_t>(index))
|
|
throw std::out_of_range("Invalid string literal index");
|
|
|
|
return mProgram->mStrings[static_cast<std::size_t>(index)];
|
|
}
|
|
|
|
void Runtime::configure(const Program& program, Context& context)
|
|
{
|
|
clear();
|
|
|
|
mContext = &context;
|
|
mProgram = &program;
|
|
mPC = 0;
|
|
}
|
|
|
|
void Runtime::clear()
|
|
{
|
|
mContext = nullptr;
|
|
mProgram = nullptr;
|
|
mStack.clear();
|
|
}
|
|
|
|
void Runtime::push(const Data& data)
|
|
{
|
|
mStack.push_back(data);
|
|
}
|
|
|
|
void Runtime::push(Type_Integer value)
|
|
{
|
|
Data data;
|
|
data.mInteger = value;
|
|
push(data);
|
|
}
|
|
|
|
void Runtime::push(Type_Float value)
|
|
{
|
|
Data data;
|
|
data.mFloat = value;
|
|
push(data);
|
|
}
|
|
|
|
void Runtime::pop()
|
|
{
|
|
if (mStack.empty())
|
|
throw std::runtime_error("stack underflow");
|
|
|
|
mStack.pop_back();
|
|
}
|
|
|
|
Data& Runtime::operator[](int index)
|
|
{
|
|
if (index < 0 || index >= static_cast<int>(mStack.size()))
|
|
throw std::runtime_error("stack index out of range");
|
|
|
|
return mStack[mStack.size() - index - 1];
|
|
}
|
|
|
|
Context& Runtime::getContext()
|
|
{
|
|
assert(mContext);
|
|
return *mContext;
|
|
}
|
|
}
|