Skip to content

Commit

Permalink
SLUDGE: Move copyVariable to struct Variable
Browse files Browse the repository at this point in the history
  • Loading branch information
yinsimei committed May 31, 2018
1 parent 56f0821 commit 98f7640
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 37 deletions.
8 changes: 4 additions & 4 deletions engines/sludge/builtin.cpp
Expand Up @@ -443,7 +443,7 @@ builtIn(pickOne) {
// Return value
while (numParams--) {
if (i == numParams)
copyVariable(fun->stack->thisVar, fun->reg);
fun->reg.copyFrom(fun->stack->thisVar);
trimStack(fun->stack);
}
return BR_CONTINUE;
Expand Down Expand Up @@ -646,7 +646,7 @@ builtIn(popFromStack) {
}

// Return value
copyVariable(fun->stack->thisVar.varData.theStack->first->thisVar, fun->reg);
fun->reg.copyFrom(fun->stack->thisVar.varData.theStack->first->thisVar);
trimStack(fun->stack->thisVar.varData.theStack->first);
trimStack(fun->stack);
return BR_CONTINUE;
Expand All @@ -664,7 +664,7 @@ builtIn(peekStart) {
}

// Return value
copyVariable(fun->stack->thisVar.varData.theStack->first->thisVar, fun->reg);
fun->reg.copyFrom(fun->stack->thisVar.varData.theStack->first->thisVar);
trimStack(fun->stack);
return BR_CONTINUE;
}
Expand All @@ -681,7 +681,7 @@ builtIn(peekEnd) {
}

// Return value
copyVariable(fun->stack->thisVar.varData.theStack->last->thisVar, fun->reg);
fun->reg.copyFrom(fun->stack->thisVar.varData.theStack->last->thisVar);
trimStack(fun->stack);
return BR_CONTINUE;
}
Expand Down
21 changes: 8 additions & 13 deletions engines/sludge/function.cpp
Expand Up @@ -190,7 +190,7 @@ bool continueFunction(LoadedFunction *fun) {
if (fun->calledBy) {
LoadedFunction *returnTo = fun->calledBy;
if (fun->returnSomething)
copyVariable(fun->reg, returnTo->reg);
returnTo->reg.copyFrom(fun->reg);
finishFunction(fun);
fun = returnTo;
restartFunction(fun);
Expand Down Expand Up @@ -290,7 +290,7 @@ bool continueFunction(LoadedFunction *fun) {
break;

case SLU_LOAD_LOCAL:
if (!copyVariable(fun->localVars[param], fun->reg))
if (!fun->reg.copyFrom(fun->localVars[param]))
return false;
break;

Expand Down Expand Up @@ -379,7 +379,7 @@ bool continueFunction(LoadedFunction *fun) {
break;

default:
if (!copyVariable(*grab, fun->reg))
if (!fun->reg.copyFrom(*grab))
return false;
}
}
Expand Down Expand Up @@ -418,7 +418,7 @@ bool continueFunction(LoadedFunction *fun) {
fun->stack->thisVar.varData.fastArray, ii);
if (v == NULL)
return fatal("Not within bounds of fast array.");
if (!copyVariable(fun->stack->next->thisVar, *v))
if (!v->copyFrom(fun->stack->next->thisVar))
return false;
trimStack(fun->stack);
trimStack(fun->stack);
Expand Down Expand Up @@ -470,22 +470,17 @@ bool continueFunction(LoadedFunction *fun) {
break;

case SLU_SET_LOCAL:
if (!copyVariable(fun->reg, fun->localVars[param]))
if (!fun->localVars[param].copyFrom(fun->reg))
return false;
break;

case SLU_SET_GLOBAL:
// newDebug (" Copying TO global variable", param);
// newDebug (" Global type at the moment", globalVars[param].varType);
if (!copyVariable(fun->reg, globalVars[param]))
if (!globalVars[param].copyFrom(fun->reg))
return false;
// newDebug (" New type", globalVars[param].varType);
break;

case SLU_LOAD_GLOBAL:
// newDebug (" Copying FROM global variable", param);
// newDebug (" Global type at the moment", globalVars[param].varType);
if (!copyVariable(globalVars[param], fun->reg))
if (!fun->reg.copyFrom(globalVars[param]))
return false;
break;

Expand Down Expand Up @@ -685,7 +680,7 @@ int startNewFunctionNum(uint funcNum, uint numParamsExpected,
if (vStack == NULL)
return fatal(
"Corrupted file!The stack's empty and there were still parameters expected");
copyVariable(vStack->thisVar, newFunc->localVars[numParamsExpected]);
newFunc->localVars[numParamsExpected].copyFrom(vStack->thisVar);
trimStack(vStack);
}

Expand Down
36 changes: 18 additions & 18 deletions engines/sludge/variable.cpp
Expand Up @@ -332,37 +332,37 @@ bool getBoolean(const Variable &from) {
return true;
}

bool copyMain(const Variable &from, Variable &to) {
to.varType = from.varType;
switch (to.varType) {
bool Variable::copyMain(const Variable &from) {
varType = from.varType;
switch (varType) {
case SVT_INT:
case SVT_FUNC:
case SVT_BUILT:
case SVT_FILE:
case SVT_OBJTYPE:
to.varData.intValue = from.varData.intValue;
varData.intValue = from.varData.intValue;
return true;

case SVT_FASTARRAY:
to.varData.fastArray = from.varData.fastArray;
to.varData.fastArray->timesUsed++;
varData.fastArray = from.varData.fastArray;
varData.fastArray->timesUsed++;
return true;

case SVT_STRING:
to.varData.theString = createCString(from.varData.theString);
return to.varData.theString ? true : false;
varData.theString = createCString(from.varData.theString);
return varData.theString ? true : false;

case SVT_STACK:
to.varData.theStack = from.varData.theStack;
to.varData.theStack->timesUsed++;
varData.theStack = from.varData.theStack;
varData.theStack->timesUsed++;
return true;

case SVT_COSTUME:
to.varData.costumeHandler = from.varData.costumeHandler;
varData.costumeHandler = from.varData.costumeHandler;
return true;

case SVT_ANIM:
to.varData.animHandler = new PersonaAnimation(from.varData.animHandler);
varData.animHandler = new PersonaAnimation(from.varData.animHandler);
return true;

case SVT_NULL:
Expand All @@ -375,9 +375,9 @@ bool copyMain(const Variable &from, Variable &to) {
return false;
}

bool copyVariable(const Variable &from, Variable &to) {
to.unlinkVar();
return copyMain(from, to);
bool Variable::copyFrom(const Variable &from) {
unlinkVar();
return copyMain(from);
}

Variable *fastArrayGetByIndex(FastArrayHandler *vS, uint theIndex) {
Expand Down Expand Up @@ -412,7 +412,7 @@ bool makeFastArrayFromStack(Variable &to, const StackHandler *stacky) {
VariableStack *allV = stacky->first;
size = 0;
while (allV) {
copyMain(allV->thisVar, to.varData.fastArray->fastVariables[size]);
to.varData.fastArray->fastVariables[size].copyMain(allV->thisVar);
size++;
allV = allV->next;
}
Expand All @@ -424,7 +424,7 @@ bool addVarToStack(const Variable &va, VariableStack *&thisStack) {
if (!checkNew(newStack))
return false;

if (!copyMain(va, newStack->thisVar))
if (!newStack->thisVar.copyMain(va))
return false;
newStack->next = thisStack;
thisStack = newStack;
Expand Down Expand Up @@ -454,7 +454,7 @@ bool stackSetByIndex(VariableStack *vS, uint theIndex, const Variable &va) {
if (!vS)
return fatal("Index past end of stack.");
}
return copyVariable(va, vS->thisVar);
return vS->thisVar.copyFrom(va);
}

Variable *stackGetByIndex(VariableStack *vS, uint theIndex) {
Expand Down
6 changes: 4 additions & 2 deletions engines/sludge/variable.h
Expand Up @@ -77,6 +77,10 @@ struct Variable {
void unlinkVar();
void setVariable(VariableType vT, int value);

// Copy from another variable
bool copyFrom(const Variable &from);
bool copyMain(const Variable &from); // without variable unlink

// Load & save
bool save(Common::WriteStream *stream);
bool load(Common::SeekableReadStream *stream);
Expand All @@ -88,8 +92,6 @@ struct VariableStack {
};

// Setting variables

bool copyVariable(const Variable &from, Variable &to);
bool loadStringToVar(Variable &thisVar, int value);
void newAnimationVariable(Variable &thisVar, struct PersonaAnimation *i);
void newCostumeVariable(Variable &thisVar, struct Persona *i);
Expand Down

0 comments on commit 98f7640

Please sign in to comment.