Skip to content

Commit ea74dae

Browse files
committed
moar updated
Split out setters and loads to be per scope this way vars do not delete vector value erroneously now able to clear the vector and delete it.
1 parent 4b2f3aa commit ea74dae

6 files changed

Lines changed: 218 additions & 89 deletions

File tree

Engine/source/console/console.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ char* ConsoleValue::convertToBuffer() const
7171

7272
char* ConsoleValue::convertVectorToBuffer() const
7373
{
74-
if (vec->size() == 0)
74+
if (!vec || vec->size() == 0)
7575
return (char*)"";
7676

7777
// use FrameAllocator to avoid static overwrite

Engine/source/console/console.h

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include "core/util/refBase.h"
3434
#endif
3535
#include <stdarg.h>
36-
3736
#include "core/util/str.h"
3837
#include "core/util/journal/journaledSignal.h"
3938
#include "core/stringTable.h"
@@ -173,11 +172,14 @@ class ConsoleValue
173172

174173
if (type == ConsoleValueType::cvVector)
175174
{
176-
if (ownsVector && vec && vec->size() > 0)
175+
if (ownsVector && vec)
177176
{
178177
vec->clear();
179-
vec = NULL;
178+
delete vec;
180179
}
180+
181+
vec = nullptr;
182+
ownsVector = false;
181183
}
182184

183185
type = ConsoleValueType::cvNULL;
@@ -187,7 +189,7 @@ class ConsoleValue
187189
type = ConsoleValueType::cvSTEntry;
188190
s = const_cast<char*>(StringTable->EmptyString());
189191
bufferLen = 0;
190-
vec = NULL;
192+
vec = nullptr;
191193
ownsVector = false;
192194
}
193195

@@ -196,7 +198,7 @@ class ConsoleValue
196198
type = ConsoleValueType::cvSTEntry;
197199
s = const_cast<char*>(StringTable->EmptyString());
198200
bufferLen = 0;
199-
vec = NULL;
201+
vec = nullptr;
200202
ownsVector = false;
201203
switch (ref.type)
202204
{
@@ -216,7 +218,8 @@ class ConsoleValue
216218
setString(ref.s);
217219
break;
218220
case cvVector:
219-
setVector(ref.vec);
221+
if (ref.vec)
222+
setVectorRef(ref.vec); // copy-by-reference, no ownership
220223
break;
221224
default:
222225
setConsoleData(ref.type, ref.dataPtr, ref.enumTable);
@@ -232,7 +235,8 @@ class ConsoleValue
232235
std::cout << "Ref already cleared!";
233236
break;
234237
case cvVector:
235-
setVector(ref.vec);
238+
if (ref.vec)
239+
setVectorRef(ref.vec);
236240
break;
237241
case cvInteger:
238242
setInt(ref.i);
@@ -295,17 +299,26 @@ class ConsoleValue
295299

296300
TORQUE_FORCEINLINE void setVectorRef(Vector<ConsoleValue>* v)
297301
{
298-
ownsVector = false;
299302
cleanupData();
300303
type = cvVector;
301304
vec = v;
305+
ownsVector = false;
302306
}
303307

304308
TORQUE_FORCEINLINE void setVector(Vector<ConsoleValue>* v)
305309
{
306310
cleanupData();
307-
type = cvVector;
308-
vec = v;
311+
type = ConsoleValueType::cvVector;
312+
313+
if (v)
314+
{
315+
vec = new Vector<ConsoleValue>(*v);
316+
}
317+
else
318+
{
319+
vec = new Vector<ConsoleValue>();
320+
}
321+
309322
ownsVector = true;
310323
}
311324

Engine/source/console/torquescript/astNodes.cpp

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,6 @@ U32 VectorIndexNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
405405
ip = innerVec->compile(codeStream, ip, TypeReqVector);
406406
else
407407
ip = base->compile(codeStream, ip, TypeReqVector);
408-
409408
// Compile index
410409
ip = index->compile(codeStream, ip, TypeReqUInt);
411410

@@ -752,22 +751,29 @@ U32 VarNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
752751
}
753752
else
754753
{
754+
// Compile the index expression
755+
ip = arrayIndex->compile(codeStream, ip, TypeReqUInt);
756+
757+
// Load the base vector
755758
if (isGlobal)
756759
{
757760
codeStream.emit(OP_SETCURVAR);
758761
codeStream.emitSTE(varName);
759-
codeStream.emit(OP_LOADVAR_VECTOR);
762+
}
763+
764+
// Emit the correct vector member load opcode
765+
if (isGlobal)
766+
{
767+
codeStream.emit(OP_LOADVAR_VECTOR_MEMBER_GLOBAL);
768+
codeStream.emit(type);
760769
}
761770
else
762771
{
763-
codeStream.emit(OP_LOAD_LOCAL_VAR_VECTOR);
772+
codeStream.emit(OP_LOADVAR_VECTOR_MEMBER_LOCAL);
773+
codeStream.emit(type);
764774
codeStream.emit(getFuncVars(dbgLineNumber)->lookup(varName, dbgLineNumber, TypeReqVector));
765775
}
766776

767-
ip = arrayIndex->compile(codeStream, ip, TypeReqUInt);
768-
codeStream.emit(OP_LOADVAR_VECTOR_MEMBER);
769-
codeStream.emit(type);
770-
771777
return codeStream.tell();
772778
}
773779
}
@@ -1089,6 +1095,8 @@ U32 AssignExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
10891095
return ip;
10901096
}
10911097

1098+
ip = arrayIndex->compile(codeStream, ip, TypeReqUInt);
1099+
10921100
if (isGlobal)
10931101
{
10941102
codeStream.emit(OP_SETCURVAR_CREATE);
@@ -1097,10 +1105,10 @@ U32 AssignExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
10971105
if (!ent)
10981106
{
10991107
ent = Con::gGlobalVars.add(varName);
1100-
ent->setVectorValue(new Vector<ConsoleValue>());
1108+
ent->setVectorValue(NULL);
11011109
}
11021110

1103-
codeStream.emit(OP_LOADVAR_VECTOR);
1111+
codeStream.emit(OP_SETCURVAR_VECTOR_MEMBER_GLOBAL);
11041112
}
11051113
else // the issue with locals is that the framestack only exists after the op_ codes are run.
11061114
{
@@ -1109,12 +1117,10 @@ U32 AssignExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
11091117
getFuncVars(dbgLineNumber)->assign(varName, TypeReqVector, dbgLineNumber);
11101118
}
11111119

1112-
codeStream.emit(OP_LOAD_LOCAL_VAR_VECTOR);
1120+
codeStream.emit(OP_SETCURVAR_VECTOR_MEMBER_LOCAL);
11131121
codeStream.emit(getFuncVars(dbgLineNumber)->lookup(varName, dbgLineNumber, TypeReqVector));
11141122
}
11151123

1116-
ip = arrayIndex->compile(codeStream, ip, TypeReqUInt);
1117-
codeStream.emit(OP_SETCURVAR_VECTOR_MEMBER);
11181124
return ip;
11191125
}
11201126
}
@@ -1308,6 +1314,8 @@ U32 AssignOpExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
13081314
}
13091315
else
13101316
{
1317+
ip = arrayIndex->compile(codeStream, ip, TypeReqUInt);
1318+
13111319
if (isGlobal)
13121320
{
13131321
codeStream.emit(OP_SETCURVAR_CREATE);
@@ -1316,10 +1324,11 @@ U32 AssignOpExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
13161324
if (!ent)
13171325
{
13181326
ent = Con::gGlobalVars.add(varName);
1319-
ent->setVectorValue(new Vector<ConsoleValue>());
1327+
ent->setVectorValue(NULL);
13201328
}
13211329

1322-
codeStream.emit(OP_LOADVAR_VECTOR);
1330+
codeStream.emit(OP_LOADVAR_VECTOR_MEMBER_GLOBAL);
1331+
codeStream.emit(subType);
13231332
}
13241333
else // the issue with locals is that the framestack only exists after the op_ codes are run.
13251334
{
@@ -1332,32 +1341,29 @@ U32 AssignOpExprNode::compile(CodeStream& codeStream, U32 ip, TypeReq type)
13321341
getFuncVars(dbgLineNumber)->assign(varName, TypeReqVector, dbgLineNumber);
13331342
}
13341343

1335-
codeStream.emit(OP_LOAD_LOCAL_VAR_VECTOR);
1344+
codeStream.emit(OP_LOADVAR_VECTOR_MEMBER_LOCAL);
1345+
codeStream.emit(subType);
13361346
codeStream.emit(getFuncVars(dbgLineNumber)->lookup(varName, dbgLineNumber, TypeReqVector));
13371347
}
13381348

1339-
ip = arrayIndex->compile(codeStream, ip, TypeReqUInt);
1340-
// Load element at index
1341-
codeStream.emit(OP_LOADVAR_VECTOR_MEMBER);
1342-
codeStream.emit(subType);
13431349
ip = expr->compile(codeStream, ip, subType);
13441350
codeStream.emit(operand);
13451351
codeStream.emit((subType == TypeReqFloat) ? OP_LOADVAR_FLT : OP_LOADVAR_UINT);
13461352

1353+
ip = arrayIndex->compile(codeStream, ip, TypeReqUInt);
1354+
13471355
if (isGlobal)
13481356
{
1349-
codeStream.emit(OP_LOADVAR_VECTOR);
1357+
codeStream.emit(OP_SETCURVAR_CREATE);
1358+
codeStream.emitSTE(varName);
1359+
codeStream.emit(OP_SETCURVAR_VECTOR_MEMBER_GLOBAL);
13501360
}
1351-
else
1361+
else // the issue with locals is that the framestack only exists after the op_ codes are run.
13521362
{
1353-
codeStream.emit(OP_LOAD_LOCAL_VAR_VECTOR);
1363+
codeStream.emit(OP_SETCURVAR_VECTOR_MEMBER_LOCAL);
13541364
codeStream.emit(getFuncVars(dbgLineNumber)->lookup(varName, dbgLineNumber, TypeReqVector));
13551365
}
13561366

1357-
ip = arrayIndex->compile(codeStream, ip, TypeReqUInt);
1358-
1359-
codeStream.emit(OP_SETCURVAR_VECTOR_MEMBER);
1360-
13611367
if (type == TypeReqNone)
13621368
codeStream.emit(OP_POP_STK);
13631369

0 commit comments

Comments
 (0)