forked from cleolibrary/CLEO4
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathScmFunction.cpp
More file actions
158 lines (126 loc) · 4.89 KB
/
ScmFunction.cpp
File metadata and controls
158 lines (126 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "stdafx.h"
#include "ScmFunction.h"
#include "CCustomOpcodeSystem.h"
#include "CScriptEngine.h"
namespace CLEO
{
ScmFunction* ScmFunction::store[Store_Size] = {0};
WORD ScmFunction::lastAllocIdx = 0;
ScmFunction* ScmFunction::Get(WORD id)
{
if (id == Id_None) return nullptr;
auto idx = id - 1; // id to index
if (idx >= Store_Size || store[idx] == nullptr)
{
SHOW_ERROR("CLEO function with id %d not found in the storage!", id);
return nullptr;
}
return store[idx];
}
void ScmFunction::Clear()
{
for (auto& scmFunc : store)
{
if (scmFunc != nullptr)
{
::operator delete(scmFunc); // delete without extra checks
scmFunc = nullptr;
}
}
lastAllocIdx = 0;
}
void* ScmFunction::operator new(size_t size)
{
auto searchStart = lastAllocIdx;
while (store[lastAllocIdx] != nullptr) // find first unused position in store
{
lastAllocIdx++;
if (lastAllocIdx >= Store_Size) lastAllocIdx = 0; // warp around
if (lastAllocIdx == searchStart)
{
SHOW_ERROR("CLEO function storage stack overflow!");
throw std::bad_alloc(); // the store is filled up
}
}
auto func = reinterpret_cast<ScmFunction*>(::operator new(size));
store[lastAllocIdx] = func;
return func;
}
void ScmFunction::operator delete(void* mem)
{
auto idx = reinterpret_cast<ScmFunction*>(mem)->thisScmFunctionId - 1; // id to index
if (idx >= Store_Size || store[idx] != mem)
{
SHOW_ERROR("Failed to delete corrupted CLEO function from storage!");
return; // keep allocated
}
store[idx] = nullptr;
::operator delete(mem);
}
ScmFunction::ScmFunction(CLEO::CRunningScript* thread, BYTE* callIP)
{
auto cs = reinterpret_cast<CCustomScript*>(thread);
thisScmFunctionId = ScmFunction::lastAllocIdx + 1; // index to id
prevScmFunctionId = cs->GetScmFunction();
caller = cs;
this->callIP = callIP;
// create snapshot of current scope
savedBaseIP = cs->BaseIP;
savedCodeSize = cs->IsCustom() ? cs->GetCodeSize() : -1;
std::copy(std::begin(cs->Stack), std::end(cs->Stack), std::begin(savedStack));
savedSP = cs->SP;
auto scope = cs->IsMission() ? missionLocals : cs->LocalVar;
std::copy(scope, scope + _countof(CRunningScript::LocalVar), savedLocalVar.begin());
savedCondResult = cs->bCondResult;
savedLogicalOp = cs->LogicalOp;
savedNotFlag = cs->NotFlag;
savedScriptFileDir = cs->GetScriptFileDir();
savedScriptFileName = cs->GetScriptFileName();
// init new scope
std::fill(std::begin(cs->Stack), std::end(cs->Stack), nullptr);
cs->SP = 0;
cs->bCondResult = false;
cs->LogicalOp = eLogicalOperation::NONE;
cs->NotFlag = false;
cs->SetScmFunction(thisScmFunctionId);
}
void ScmFunction::Return(CRunningScript* thread)
{
auto cs = reinterpret_cast<CCustomScript*>(thread);
cs->BaseIP = savedBaseIP;
if (cs->IsCustom()) cs->SetCodeSize(savedCodeSize);
// restore parent scope's gosub call stack
std::copy(savedStack.begin(), savedStack.end(), std::begin(cs->Stack));
cs->SP = savedSP;
// restore parent scope's local variables
std::copy(savedLocalVar.begin(), savedLocalVar.end(), cs->IsMission() ? missionLocals : cs->LocalVar);
// process conditional result of just ended function in parent scope
bool condResult = cs->bCondResult;
if (savedNotFlag) condResult = !condResult;
if (savedLogicalOp >= eLogicalOperation::ANDS_1 && savedLogicalOp < eLogicalOperation::AND_END)
{
cs->bCondResult = savedCondResult && condResult;
cs->LogicalOp = --savedLogicalOp;
}
else if (savedLogicalOp >= eLogicalOperation::ORS_1 && savedLogicalOp < eLogicalOperation::OR_END)
{
cs->bCondResult = savedCondResult || condResult;
cs->LogicalOp = --savedLogicalOp;
}
else // eLogicalOperation::NONE
{
cs->bCondResult = condResult;
cs->LogicalOp = savedLogicalOp;
}
cs->SetScriptFileDir(savedScriptFileDir.c_str());
cs->SetScriptFileName(savedScriptFileName.c_str());
cs->SetIp(retnAddress);
cs->SetScmFunction(prevScmFunctionId);
delete this; // remove from storage
}
size_t ScmFunction::GetCallStackSize() const
{
auto parent = Get(prevScmFunctionId);
return parent ? 1 + parent->GetCallStackSize() : 0;
}
}; // namespace CLEO