The return value of an event must be the first value

This commit is contained in:
smallmodel 2023-11-02 19:53:26 +01:00
parent 2c3180813a
commit 1698f6d1bf
No known key found for this signature in database
GPG key ID: 9F2D623CEDF08512
3 changed files with 42 additions and 26 deletions

View file

@ -2039,6 +2039,22 @@ void Event::AddVector(const Vector& vector)
variable.setVectorValue(vector);
}
/*
=======================
SetValue
=======================
*/
void Event::CopyValues(const ScriptVariable* values, size_t count)
{
assert(count <= maxDataSize);
for (size_t i = 0; i < count; i++) {
data[i] = values[i];
}
dataSize = count;
}
/*
=======================
Clear
@ -2206,14 +2222,29 @@ GetValue
*/
ScriptVariable& Event::GetValue(void)
{
if (dataSize == maxDataSize) {
ScriptVariable *tmp = data;
ScriptVariable* tmp;
int i;
maxDataSize++;
if (fromScript) {
// an event method will emit the return value
// to the first index of the array
// so there is no reallocation
if (!data) {
data = new ScriptVariable[1];
dataSize = 1;
maxDataSize = 1;
}
return data[0];
}
if (dataSize == maxDataSize) {
tmp = data;
maxDataSize += 3;
data = new ScriptVariable[maxDataSize];
if (tmp != NULL) {
for (int i = 0; i < dataSize; i++) {
for (i = 0; i < dataSize; i++) {
data[i] = std::move(tmp[i]);
}
@ -2226,11 +2257,6 @@ ScriptVariable& Event::GetValue(void)
return data[dataSize - 1];
}
ScriptVariable& Event::GetLastValue()
{
return GetValue(NumArgs());
}
/*
=======================
GetVector

View file

@ -377,6 +377,7 @@ public:
void AddTokens(int argc, const char **argv);
void AddValue(const ScriptVariable& value);
void AddVector(const Vector& vector);
void CopyValues(const ScriptVariable* values, size_t count);
void Clear(void);
@ -402,7 +403,6 @@ public:
str GetToken(int pos);
ScriptVariable& GetValue(int pos);
ScriptVariable& GetValue(void);
ScriptVariable& GetLastValue();
Vector GetVector(int pos);
class Waypoint *GetWaypoint(int pos);

View file

@ -487,11 +487,7 @@ void ScriptVM::executeCommandInternal<true>(
}
ScriptVariable& pTop = m_VMStack.GetTop();
if (ev.NumArgs() > iParamCount) {
pTop = std::move(ev.GetLastValue());
} else {
pTop.Clear();
}
pTop = std::move(ev.GetValue());
}
template<>
@ -512,7 +508,7 @@ template<>
void ScriptVM::executeCommand<false, true>(Listener *listener, op_parmNum_t iParamCount, op_evName_t eventnum)
{
ScriptCommandEvent ev =
iParamCount ? ScriptCommandEvent(eventnum, iParamCount + 1) : ScriptCommandEvent(eventnum, 1);
iParamCount ? ScriptCommandEvent(eventnum, iParamCount) : ScriptCommandEvent(eventnum);
return executeCommandInternal<true>(ev, listener, m_VMStack.GetTopArray(), iParamCount);
}
@ -520,15 +516,13 @@ template<>
void ScriptVM::executeCommand<true, true>(Listener *listener, op_parmNum_t iParamCount, op_evName_t eventnum)
{
ScriptCommandEvent ev =
iParamCount ? ScriptCommandEvent(eventnum, iParamCount + 1) : ScriptCommandEvent(eventnum, 1);
iParamCount ? ScriptCommandEvent(eventnum, iParamCount) : ScriptCommandEvent(eventnum);
return executeCommandInternal<true>(ev, listener, m_VMStack.GetTopArray(), iParamCount);
}
void ScriptVM::transferVarsToEvent(Event& ev, ScriptVariable *fromVar, op_parmNum_t count)
{
for (uint16_t i = 0; i < count; i++) {
ev.AddValue(fromVar[i]);
}
ev.CopyValues(fromVar, count);
}
bool ScriptVM::executeGetter(Listener *listener, op_evName_t eventName)
@ -541,11 +535,7 @@ bool ScriptVM::executeGetter(Listener *listener, op_evName_t eventName)
listener->ProcessScriptEvent(ev);
ScriptVariable& pTop = m_VMStack.GetTop();
if (ev.NumArgs() > 0) {
pTop = std::move(ev.GetLastValue());
} else {
pTop.Clear();
}
pTop = std::move(ev.GetValue());
return true;
} else {
@ -567,7 +557,7 @@ bool ScriptVM::executeSetter(Listener *listener, op_evName_t eventName)
ScriptCommandEvent ev(eventNum, 1);
ScriptVariable& pTop = m_VMStack.GetTop();
ev.AddValue(pTop);
ev.CopyValues(&pTop, 1);
listener->ProcessScriptEvent(ev);