Skip to content

Commit

Permalink
DIRECTOR: Lingo: Store and restore local variables in scope.
Browse files Browse the repository at this point in the history
  • Loading branch information
sev- committed Aug 3, 2016
1 parent a279faf commit 0b6d950
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
19 changes: 19 additions & 0 deletions engines/director/lingo/lingo-code.cpp
Expand Up @@ -404,6 +404,13 @@ void Lingo::c_call() {
fp->sp = sym;
fp->retpc = g_lingo->_pc;
fp->retscript = g_lingo->_currentScript;
fp->localvars = g_lingo->_localvars;

// Clean up current scope local variables
for (SymbolHash::const_iterator h = g_lingo->_localvars->begin(); h != g_lingo->_localvars->end(); ++h)
g_lingo->_vars.erase(h->_key);

g_lingo->_localvars = new SymbolHash;

g_lingo->_callstack.push_back(fp);

Expand All @@ -419,6 +426,18 @@ void Lingo::c_procret() {
g_lingo->_currentScript = fp->retscript;
g_lingo->_pc = fp->retpc;

// Clean up current scope local variables and clean up memory
for (SymbolHash::const_iterator h = g_lingo->_localvars->begin(); h != g_lingo->_localvars->end(); ++h) {
g_lingo->_vars.erase(h->_key);
delete h->_value;
}
delete g_lingo->_localvars;

// Restore local variables
g_lingo->_localvars = fp->localvars;
for (SymbolHash::const_iterator h = g_lingo->_localvars->begin(); h != g_lingo->_localvars->end(); ++h)
g_lingo->_vars[h->_key] = h->_value;

delete fp;

g_lingo->_returning = true;
Expand Down
12 changes: 1 addition & 11 deletions engines/director/lingo/lingo-codegen.cpp
Expand Up @@ -64,16 +64,6 @@ void Lingo::execute(int pc) {
}
}

void Lingo::pushContext() {
Context *con = new Context;

_contexts.push_back(con);
}

void Lingo::popContext() {
_contexts.pop_back();
}

Symbol *Lingo::lookupVar(const char *name, bool create, bool putInLocalList) {
Symbol *sym;

Expand All @@ -93,7 +83,7 @@ Symbol *Lingo::lookupVar(const char *name, bool create, bool putInLocalList) {
}

if (putInLocalList)
_localvars[name] = true;
(*_localvars)[name] = sym;

return sym;
}
Expand Down
4 changes: 4 additions & 0 deletions engines/director/lingo/lingo.cpp
Expand Up @@ -163,7 +163,11 @@ void Lingo::executeScript(ScriptType type, uint16 id) {
_pc = 0;
_returning = false;

_localvars = new SymbolHash;

execute(_pc);

delete _localvars;
}

void Lingo::processEvent(LEvent event, int entityId) {
Expand Down
16 changes: 5 additions & 11 deletions engines/director/lingo/lingo.h
Expand Up @@ -103,18 +103,14 @@ struct Datum { /* interpreter stack type */
Datum() { u.sym = NULL; type = VOID; }
};

struct CFrame { /* proc/func call stack frame */
Symbol *sp; /* symbol table entry */
int retpc; /* where to resume after return */
ScriptData *retscript; /* which script to resume after return */
};

typedef Common::HashMap<int32, ScriptData *> ScriptHash;
typedef Common::Array<Datum> StackData;
typedef Common::HashMap<Common::String, Symbol *, Common::IgnoreCase_Hash, Common::IgnoreCase_EqualTo> SymbolHash;

struct Context { /* execution context */
Symbol *handler;
struct CFrame { /* proc/func call stack frame */
Symbol *sp; /* symbol table entry */
int retpc; /* where to resume after return */
ScriptData *retscript; /* which script to resume after return */
SymbolHash *localvars;
};

Expand Down Expand Up @@ -209,11 +205,9 @@ class Lingo {
ScriptHash _scripts[kMaxScriptType + 1];

SymbolHash _vars;
Common::HashMap<Common::String, bool, Common::IgnoreCase_Hash, Common::IgnoreCase_EqualTo> _localvars;
SymbolHash *_localvars;
SymbolHash _handlers;

Common::Array<Context *> _contexts;

int _pc;

StackData _stack;
Expand Down

0 comments on commit 0b6d950

Please sign in to comment.