@@ -360,7 +360,7 @@ const char *ExprEvalState::getStringVariable()
360360 return currentVariable ? currentVariable->getStringValue () : " " ;
361361}
362362
363- Vector<ConsoleValue>* ExprEvalState::getVectorVariable ()
363+ std::shared_ptr< Vector<ConsoleValue>> ExprEvalState::getVectorVariable ()
364364{
365365 return currentVariable ? currentVariable->getVectorValue () : NULL ;
366366}
@@ -385,7 +385,7 @@ void ExprEvalState::setStringVariable(const char *val)
385385 currentVariable->setStringValue (val);
386386}
387387
388- void ExprEvalState::setVectorVariable (Vector<ConsoleValue>* val)
388+ void ExprEvalState::setVectorVariable (std::shared_ptr< Vector<ConsoleValue>> val)
389389{
390390 AssertFatal (currentVariable != NULL , " Invalid evaluator state - trying to set null variable!" );
391391 currentVariable->setVectorValue (val);
@@ -1458,7 +1458,7 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi
14581458 ConsoleValue& vecVal = stack[_STK--]; // vector
14591459 ConsoleValue exprVal = stack[_STK--]; // value
14601460
1461- Vector<ConsoleValue>* vec = vecVal.getVector ();
1461+ std::shared_ptr< Vector<ConsoleValue>> vec = vecVal.getVector ();
14621462 if (!vec)
14631463 {
14641464 Con::errorf (" Assigning to non-vector variable, promoting to a vector." );
@@ -1471,7 +1471,8 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi
14711471 vec->setSize (index + 1 );
14721472 }
14731473
1474- (*vec)[index] = exprVal;
1474+ vec->insert (index, exprVal);
1475+ vec.reset ();
14751476 break ;
14761477 }
14771478 case OP_SETCURVAR_VECTOR_MEMBER_GLOBAL:
@@ -1480,21 +1481,20 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi
14801481 U32 index = stack[_STK--].getInt (); // pop index
14811482 ConsoleValue exprVal = stack[_STK--]; // pop assigned value
14821483
1483- Vector<ConsoleValue>* vec = Script::gEvalState .getVectorVariable ();
1484+ std::shared_ptr< Vector<ConsoleValue>> vec = Script::gEvalState .getVectorVariable ();
14841485
14851486 if (!vec)
14861487 {
1487- Script::gEvalState .setVectorVariable (NULL );
1488+ Script::gEvalState .setVectorVariable (std::make_shared<Vector<ConsoleValue>>() );
14881489 vec = Script::gEvalState .getVectorVariable ();
14891490 }
14901491
14911492 // Grow vector if necessary
14921493 if (index >= vec->size ())
14931494 vec->setSize (index + 1 );
14941495
1495- // Assign (deep copy)
1496- (*vec)[index] = exprVal;
1497-
1496+ vec->insert (index, exprVal);
1497+ vec.reset ();
14981498 break ;
14991499 }
15001500 case OP_SETCURVAR_VECTOR_MEMBER_LOCAL:
@@ -1504,18 +1504,18 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi
15041504 reg = code[ip++]; // read the local variable register
15051505 currentRegister = reg;
15061506
1507- Vector<ConsoleValue>* vec = Script::gEvalState .getLocalVectorVariable (reg);
1507+ std::shared_ptr< Vector<ConsoleValue>> vec = Script::gEvalState .getLocalVectorVariable (reg);
15081508 if (!vec)
15091509 {
1510- Script::gEvalState .setLocalVectorVariable (reg, NULL );
1510+ Script::gEvalState .setLocalVectorVariable (reg, std::make_shared<Vector<ConsoleValue>>() );
15111511 vec = Script::gEvalState .getLocalVectorVariable (reg);
15121512 }
15131513
15141514 if (index >= vec->size ())
15151515 vec->setSize (index + 1 );
15161516
1517- (* vec)[ index] = exprVal;
1518-
1517+ vec-> insert ( index, exprVal) ;
1518+ vec. reset ();
15191519 break ;
15201520 }
15211521 case OP_LOADVAR_UINT:
@@ -1540,7 +1540,7 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi
15401540 currentRegister = -1 ;
15411541 if (!Script::gEvalState .getVectorVariable ())
15421542 {
1543- Script::gEvalState .setVectorVariable (NULL );
1543+ Script::gEvalState .setVectorVariable (std::make_shared<Vector<ConsoleValue>>() );
15441544 }
15451545 stack[_STK + 1 ].setVector (Script::gEvalState .getVectorVariable ());
15461546 _STK++;
@@ -1553,11 +1553,11 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi
15531553
15541554 TypeReq type = (TypeReq)code[ip++];
15551555
1556- Vector<ConsoleValue>* vec = vecVal.getVector ();
1556+ std::shared_ptr< Vector<ConsoleValue>> vec = vecVal.getVector ();
15571557 if (!vec)
15581558 {
15591559 Con::errorf (" Tried to index a non-vector variable. Promoting to vector." );
1560- vecVal.setVector (NULL );
1560+ vecVal.setVector (std::make_shared<Vector<ConsoleValue>>() );
15611561 vec = vecVal.getVector ();
15621562 vec->setSize (index + 1 );
15631563 }
@@ -1570,7 +1570,7 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi
15701570 {
15711571 case TypeReqUInt: stack[_STK].setInt (-1 ); break ;
15721572 case TypeReqFloat: stack[_STK].setFloat (0 .0f ); break ;
1573- case TypeReqVector: stack[_STK].setVector (NULL ); break ;
1573+ case TypeReqVector: stack[_STK].setVector (std::make_shared<Vector<ConsoleValue>>() ); break ;
15741574 case TypeReqString:
15751575 case TypeReqNone: stack[_STK].setString (" " ); break ;
15761576 }
@@ -1587,6 +1587,8 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi
15871587 case TypeReqNone: stack[_STK].setString ((*vec)[index].getString ()); break ;
15881588 }
15891589 }
1590+
1591+ vec.reset ();
15901592 break ;
15911593 }
15921594 case OP_LOADVAR_VECTOR_MEMBER_GLOBAL:
@@ -1595,10 +1597,10 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi
15951597 TypeReq type = (TypeReq)code[ip++];
15961598
15971599 // Grab global vector variable
1598- Vector<ConsoleValue>* vec = Script::gEvalState .getVectorVariable ();
1600+ std::shared_ptr< Vector<ConsoleValue>> vec = Script::gEvalState .getVectorVariable ();
15991601 if (!vec)
16001602 {
1601- Script::gEvalState .setVectorVariable (NULL );
1603+ Script::gEvalState .setVectorVariable (std::make_shared<Vector<ConsoleValue>>() );
16021604 vec = Script::gEvalState .getVectorVariable ();
16031605 }
16041606
@@ -1609,7 +1611,7 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi
16091611 {
16101612 case TypeReqUInt: stack[_STK + 1 ].setInt (-1 ); break ;
16111613 case TypeReqFloat: stack[_STK + 1 ].setFloat (0 .0f ); break ;
1612- case TypeReqVector: stack[_STK + 1 ].setVector (NULL ); break ;
1614+ case TypeReqVector: stack[_STK + 1 ].setVector (std::make_shared<Vector<ConsoleValue>>() ); break ;
16131615 case TypeReqString:
16141616 case TypeReqNone: stack[_STK + 1 ].setString (" " ); break ;
16151617 }
@@ -1625,7 +1627,7 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi
16251627 case TypeReqNone: stack[_STK + 1 ].setString ((*vec)[index].getString ()); break ;
16261628 }
16271629 }
1628-
1630+ vec. reset ();
16291631 _STK++;
16301632 break ;
16311633 }
@@ -1642,10 +1644,10 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi
16421644 prevObject = NULL ;
16431645 curObject = NULL ;
16441646
1645- Vector<ConsoleValue>* vec = Script::gEvalState .getLocalVectorVariable (reg);
1647+ std::shared_ptr< Vector<ConsoleValue>> vec = Script::gEvalState .getLocalVectorVariable (reg);
16461648 if (!vec)
16471649 {
1648- Script::gEvalState .setLocalVectorVariable (reg, NULL );
1650+ Script::gEvalState .setLocalVectorVariable (reg, std::make_shared<Vector<ConsoleValue>>() );
16491651 vec = Script::gEvalState .getLocalVectorVariable (reg);
16501652 }
16511653
@@ -1656,7 +1658,7 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi
16561658 {
16571659 case TypeReqUInt: stack[_STK + 1 ].setInt (-1 ); break ;
16581660 case TypeReqFloat: stack[_STK + 1 ].setFloat (0 .0f ); break ;
1659- case TypeReqVector: stack[_STK + 1 ].setVector (NULL ); break ;
1661+ case TypeReqVector: stack[_STK + 1 ].setVector (std::make_shared<Vector<ConsoleValue>>() ); break ;
16601662 case TypeReqString:
16611663 case TypeReqNone: stack[_STK + 1 ].setString (" " ); break ;
16621664 }
@@ -1672,7 +1674,7 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi
16721674 case TypeReqNone: stack[_STK + 1 ].setString ((*vec)[index].getString ()); break ;
16731675 }
16741676 }
1675-
1677+ vec. reset ();
16761678 _STK++;
16771679 break ;
16781680 }
@@ -1743,7 +1745,7 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi
17431745
17441746 if (!Script::gEvalState .getLocalVectorVariable (reg))
17451747 {
1746- Script::gEvalState .setLocalVectorVariable (reg, NULL );
1748+ Script::gEvalState .setLocalVectorVariable (reg, std::make_shared<Vector<ConsoleValue>>() );
17471749 }
17481750 stack[_STK + 1 ].setVector (Script::gEvalState .getLocalVectorVariable (reg));
17491751 _STK++;
@@ -2491,7 +2493,7 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi
24912493 {
24922494 U32 count = code[ip++];
24932495
2494- stack[_STK + 1 ].setVector (NULL );
2496+ stack[_STK + 1 ].setVector (std::make_shared<Vector<ConsoleValue>>() );
24952497 stack[_STK + 1 ].getVector ()->reserve (count);
24962498 _STK++;
24972499 break ;
@@ -2508,20 +2510,10 @@ Con::EvalResult CodeBlock::exec(U32 ip, const char* functionName, Namespace* thi
25082510 break ;
25092511 }
25102512
2511- // Element is right above vector on the stack
2512- // Push element into vector
2513- if (elem.getType () == ConsoleValueType::cvVector)
2514- {
2515- // Store a reference to the same vector, not a deep copy
2516- ConsoleValue ref;
2517- ref.setVectorRef (new Vector<ConsoleValue>(*elem.getVector ()));
2518- stack[_STK].getVector ()->push_back (ref);
2519- }
2520- else
2521- {
2522- // Regular copy for numbers, strings, etc.
2523- stack[_STK].getVector ()->push_back (elem);
2524- }
2513+ stack[_STK].getVector ()->push_back (elem);
2514+ if (elem.getType () == cvVector)
2515+ elem.getVector ().reset ();
2516+
25252517 break ;
25262518 }
25272519
0 commit comments