don't create a new virtual machine for each script and frame

actorid
Marc Zinnschlag 14 years ago
parent a133920eb0
commit 80691250ec

@ -223,11 +223,11 @@ namespace MWGui
try try
{ {
ConsoleInterpreterContext interpreterContext (*this, mEnvironment, MWWorld::Ptr()); ConsoleInterpreterContext interpreterContext (*this, mEnvironment, MWWorld::Ptr());
Interpreter::Interpreter interpreter (interpreterContext); Interpreter::Interpreter interpreter;
MWScript::installOpcodes (interpreter); MWScript::installOpcodes (interpreter);
std::vector<Interpreter::Type_Code> code; std::vector<Interpreter::Type_Code> code;
output.getCode (code); output.getCode (code);
interpreter.run (&code[0], code.size()); interpreter.run (&code[0], code.size(), interpreterContext);
} }
catch (const std::exception& error) catch (const std::exception& error)
{ {

@ -12,8 +12,6 @@
#include <components/compiler/scanner.hpp> #include <components/compiler/scanner.hpp>
#include <components/compiler/context.hpp> #include <components/compiler/context.hpp>
#include <components/interpreter/interpreter.hpp>
#include "extensions.hpp" #include "extensions.hpp"
namespace MWScript namespace MWScript
@ -21,7 +19,8 @@ namespace MWScript
ScriptManager::ScriptManager (const ESMS::ESMStore& store, bool verbose, ScriptManager::ScriptManager (const ESMS::ESMStore& store, bool verbose,
Compiler::Context& compilerContext) Compiler::Context& compilerContext)
: mErrorHandler (std::cerr), mStore (store), mVerbose (verbose), : mErrorHandler (std::cerr), mStore (store), mVerbose (verbose),
mCompilerContext (compilerContext), mParser (mErrorHandler, mCompilerContext) mCompilerContext (compilerContext), mParser (mErrorHandler, mCompilerContext),
mOpcodesInstalled (false)
{} {}
bool ScriptManager::compile (const std::string& name) bool ScriptManager::compile (const std::string& name)
@ -99,9 +98,13 @@ namespace MWScript
if (!iter->second.empty()) if (!iter->second.empty())
try try
{ {
Interpreter::Interpreter interpreter (interpreterContext); if (!mOpcodesInstalled)
installOpcodes (interpreter); {
interpreter.run (&iter->second[0], iter->second.size()); installOpcodes (mInterpreter);
mOpcodesInstalled = true;
}
mInterpreter.run (&iter->second[0], iter->second.size(), interpreterContext);
} }
catch (const std::exception& e) catch (const std::exception& e)
{ {

@ -8,6 +8,7 @@
#include <components/compiler/streamerrorhandler.hpp> #include <components/compiler/streamerrorhandler.hpp>
#include <components/compiler/fileparser.hpp> #include <components/compiler/fileparser.hpp>
#include <components/interpreter/interpreter.hpp>
#include <components/interpreter/types.hpp> #include <components/interpreter/types.hpp>
namespace ESMS namespace ESMS
@ -35,6 +36,8 @@ namespace MWScript
bool mVerbose; bool mVerbose;
Compiler::Context& mCompilerContext; Compiler::Context& mCompilerContext;
Compiler::FileParser mParser; Compiler::FileParser mParser;
Interpreter::Interpreter mInterpreter;
bool mOpcodesInstalled;
std::map<std::string, std::vector<Interpreter::Type_Code> > mScripts; std::map<std::string, std::vector<Interpreter::Type_Code> > mScripts;

@ -134,8 +134,7 @@ namespace Interpreter
throw std::runtime_error (error.str()); throw std::runtime_error (error.str());
} }
Interpreter::Interpreter (Context& context) Interpreter::Interpreter()
: mRuntime (context)
{} {}
Interpreter::~Interpreter() Interpreter::~Interpreter()
@ -195,11 +194,11 @@ namespace Interpreter
mSegment5.insert (std::make_pair (code, opcode)); mSegment5.insert (std::make_pair (code, opcode));
} }
void Interpreter::run (const Type_Code *code, int codeSize) void Interpreter::run (const Type_Code *code, int codeSize, Context& context)
{ {
assert (codeSize>=4); assert (codeSize>=4);
mRuntime.configure (code, codeSize); mRuntime.configure (code, codeSize, context);
int opcodes = static_cast<int> (code[0]); int opcodes = static_cast<int> (code[0]);

@ -34,7 +34,7 @@ namespace Interpreter
public: public:
Interpreter (Context& context); Interpreter();
~Interpreter(); ~Interpreter();
@ -56,9 +56,8 @@ namespace Interpreter
void installSegment5 (int code, Opcode0 *opcode); void installSegment5 (int code, Opcode0 *opcode);
///< ownership of \a opcode is transferred to *this. ///< ownership of \a opcode is transferred to *this.
void run (const Type_Code *code, int codeSize); void run (const Type_Code *code, int codeSize, Context& context);
}; };
} }
#endif #endif

@ -7,7 +7,7 @@
namespace Interpreter namespace Interpreter
{ {
Runtime::Runtime (Context& context) : mContext (context), mCode (0), mPC (0) {} Runtime::Runtime() : mContext (0), mCode (0), mPC (0) {}
int Runtime::getPC() const int Runtime::getPC() const
{ {
@ -47,10 +47,11 @@ namespace Interpreter
return literalBlock; return literalBlock;
} }
void Runtime::configure (const Interpreter::Type_Code *code, int codeSize) void Runtime::configure (const Interpreter::Type_Code *code, int codeSize, Context& context)
{ {
clear(); clear();
mContext = &context;
mCode = code; mCode = code;
mCodeSize = codeSize; mCodeSize = codeSize;
mPC = 0; mPC = 0;
@ -58,6 +59,7 @@ namespace Interpreter
void Runtime::clear() void Runtime::clear()
{ {
mContext = 0;
mCode = 0; mCode = 0;
mCodeSize = 0; mCodeSize = 0;
mStack.clear(); mStack.clear();
@ -105,7 +107,7 @@ namespace Interpreter
Context& Runtime::getContext() Context& Runtime::getContext()
{ {
return mContext; assert (mContext);
return *mContext;
} }
} }

@ -14,7 +14,7 @@ namespace Interpreter
class Runtime class Runtime
{ {
Context& mContext; Context *mContext;
const Type_Code *mCode; const Type_Code *mCode;
int mCodeSize; int mCodeSize;
int mPC; int mPC;
@ -22,7 +22,7 @@ namespace Interpreter
public: public:
Runtime (Context& context); Runtime ();
int getPC() const; int getPC() const;
///< return program counter. ///< return program counter.
@ -33,7 +33,7 @@ namespace Interpreter
std::string getStringLiteral (int index) const; std::string getStringLiteral (int index) const;
void configure (const Type_Code *code, int codeSize); void configure (const Type_Code *code, int codeSize, Context& context);
///< \a context and \a code must exist as least until either configure, clear or ///< \a context and \a code must exist as least until either configure, clear or
/// the destructor is called. \a codeSize is given in 32-bit words. /// the destructor is called. \a codeSize is given in 32-bit words.

Loading…
Cancel
Save