Skip to content

Commit

Permalink
Fixing crash in saving in PR mapbase-source#80
Browse files Browse the repository at this point in the history
  • Loading branch information
reductor committed Dec 27, 2020
1 parent ef7d9cc commit 4e72ce3
Showing 1 changed file with 63 additions and 61 deletions.
124 changes: 63 additions & 61 deletions sp/src/vscript/vscript_squirrel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,35 @@ struct ReadStateMap
}
}

bool CheckCache(CUtlBuffer* pBuffer, HSQOBJECT** obj)
bool CheckCache(CUtlBuffer* pBuffer, HSQUIRRELVM vm, int * outmarker)
{
int marker = pBuffer->GetInt();

auto idx = cache.Find(marker);
if (idx != cache.InvalidIndex())
{
*obj = &cache[idx];
sq_pushobject(vm, cache[idx]);
return true;
}
else
{
HSQOBJECT temp;
sq_resetobject(&temp);
auto idx = cache.Insert(marker, temp);
*obj = &cache[idx];
*outmarker = marker;
return false;
}
}

void StoreInCache(int marker, HSQOBJECT& obj)
{
cache.Insert(marker, obj);
}

void StoreTopInCache(int marker)
{
HSQOBJECT obj;
sq_getstackobj(vm_, -1, &obj);
sq_addref(vm_, &obj);
cache.Insert(marker, obj);
}
};

class SquirrelVM : public IScriptVM
Expand Down Expand Up @@ -3176,19 +3186,17 @@ void SquirrelVM::ReadObject(CUtlBuffer* pBuffer, ReadStateMap& readState)
}
case OT_TABLE:
{
HSQOBJECT* obj = nullptr;
if (readState.CheckCache(pBuffer, &obj))
int marker = 0;
if (readState.CheckCache(pBuffer, vm_, &marker))
{
sq_pushobject(vm_, *obj);
break;
}

ReadObject(pBuffer, readState);

int count = pBuffer->GetInt();
sq_newtableex(vm_, count);
sq_getstackobj(vm_, -1, obj);
sq_addref(vm_, obj);
readState.StoreTopInCache(marker);

sq_push(vm_, -2);
sq_setdelegate(vm_, -2);
Expand All @@ -3206,17 +3214,15 @@ void SquirrelVM::ReadObject(CUtlBuffer* pBuffer, ReadStateMap& readState)
}
case OT_ARRAY:
{
HSQOBJECT* obj = nullptr;
if (readState.CheckCache(pBuffer, &obj))
int marker = 0;
if (readState.CheckCache(pBuffer, vm_, &marker))
{
sq_pushobject(vm_, *obj);
break;
}

int count = pBuffer->GetInt();
sq_newarray(vm_, count);
sq_getstackobj(vm_, -1, obj);
sq_addref(vm_, obj);
readState.StoreTopInCache(marker);

for (int i = 0; i < count; ++i)
{
Expand All @@ -3228,10 +3234,9 @@ void SquirrelVM::ReadObject(CUtlBuffer* pBuffer, ReadStateMap& readState)
}
case OT_CLOSURE:
{
HSQOBJECT* obj = nullptr;
if (readState.CheckCache(pBuffer, &obj))
int marker = 0;
if (readState.CheckCache(pBuffer, vm_, &marker))
{
sq_pushobject(vm_, *obj);
break;
}

Expand All @@ -3243,8 +3248,8 @@ void SquirrelVM::ReadObject(CUtlBuffer* pBuffer, ReadStateMap& readState)
sq_pushnull(vm_);
break;
}
sq_getstackobj(vm_, -1, obj);
sq_addref(vm_, obj);

readState.StoreTopInCache(marker);
}
else
{
Expand Down Expand Up @@ -3278,9 +3283,8 @@ void SquirrelVM::ReadObject(CUtlBuffer* pBuffer, ReadStateMap& readState)
sq_poptop(vm_);
}

*obj = ret;
sq_addref(vm_, obj);
sq_pushobject(vm_, *obj);
vm_->Push(ret);
readState.StoreTopInCache(marker);
}

ReadObject(pBuffer, readState);
Expand All @@ -3289,10 +3293,12 @@ void SquirrelVM::ReadObject(CUtlBuffer* pBuffer, ReadStateMap& readState)
sq_getstackobj(vm_, -1, &env);
if (!sq_isnull(env))
{
if (_closure( *obj ) == nullptr)
HSQOBJECT obj;
sq_getstackobj(vm_, -2, &obj);
if (_closure(obj) == nullptr)
Warning("Closure is null\n");
else
_closure(*obj)->_env = _refcounted(env)->GetWeakRef(sq_type(env));
_closure(obj)->_env = _refcounted(env)->GetWeakRef(sq_type(env));
}
sq_poptop(vm_);

Expand All @@ -3317,10 +3323,9 @@ void SquirrelVM::ReadObject(CUtlBuffer* pBuffer, ReadStateMap& readState)
}
case OT_CLASS:
{
HSQOBJECT* obj = nullptr;
if (readState.CheckCache(pBuffer, &obj))
int marker = 0;
if (readState.CheckCache(pBuffer, vm_, &marker))
{
sq_pushobject(vm_, *obj);
break;
}

Expand All @@ -3329,8 +3334,7 @@ void SquirrelVM::ReadObject(CUtlBuffer* pBuffer, ReadStateMap& readState)
if (classType == VectorClassType)
{
sq_pushobject(vm_, vectorClass_);
sq_getstackobj(vm_, -1, obj);
sq_addref(vm_, obj);
readState.StoreTopInCache(marker);
}
else if (classType == NativeClassType)
{
Expand All @@ -3345,8 +3349,7 @@ void SquirrelVM::ReadObject(CUtlBuffer* pBuffer, ReadStateMap& readState)
sq_pushnull(vm_);
}
sq_remove(vm_, -2);
sq_getstackobj(vm_, -1, obj);
sq_addref(vm_, obj);
readState.StoreTopInCache(marker);
}
else if (classType == ScriptClassType)
{
Expand All @@ -3358,8 +3361,7 @@ void SquirrelVM::ReadObject(CUtlBuffer* pBuffer, ReadStateMap& readState)
}

sq_newclass(vm_, hasBase);
sq_getstackobj(vm_, -1, obj);
sq_addref(vm_, obj);
readState.StoreTopInCache(marker);

sq_pushnull(vm_);
ReadObject(pBuffer, readState);
Expand All @@ -3383,10 +3385,9 @@ void SquirrelVM::ReadObject(CUtlBuffer* pBuffer, ReadStateMap& readState)
}
case OT_INSTANCE:
{
HSQOBJECT* obj = nullptr;
if (readState.CheckCache(pBuffer, &obj))
int marker = 0;
if (readState.CheckCache(pBuffer, vm_, &marker))
{
sq_pushobject(vm_, *obj);
break;
}

Expand All @@ -3401,8 +3402,7 @@ void SquirrelVM::ReadObject(CUtlBuffer* pBuffer, ReadStateMap& readState)
ReadObject(pBuffer, readState);
sq_call(vm_, 2, SQTrue, SQFalse);

sq_getstackobj(vm_, -1, obj);
sq_addref(vm_, obj);
readState.StoreTopInCache(marker);

sq_remove(vm_, -2);

Expand Down Expand Up @@ -3431,8 +3431,9 @@ void SquirrelVM::ReadObject(CUtlBuffer* pBuffer, ReadStateMap& readState)
if (sq_isinstance(singleton) && _instance(singleton)->_class == _class(klass))
{
foundSingleton = true;
*obj = singleton;
sq_addref(vm_, obj);

readState.StoreInCache(marker, singleton);
sq_addref(vm_, &singleton);
sq_pop(vm_, 2);
break;
}
Expand All @@ -3446,25 +3447,28 @@ void SquirrelVM::ReadObject(CUtlBuffer* pBuffer, ReadStateMap& readState)
((ScriptClassDesc_t*)typetag)->m_pszScriptName);
}

sq_pushobject(vm_, *obj);
sq_pushobject(vm_, singleton);
break;
}


HSQOBJECT obj;
sq_createinstance(vm_, -1);
sq_getstackobj(vm_, -1, obj);
sq_addref(vm_, obj);
sq_getstackobj(vm_, -1, &obj);
sq_addref(vm_, &obj);
readState.StoreInCache(marker, obj);

sq_remove(vm_, -2);

{
// HACK: No way to get the default values part from accessing the class directly
SQUnsignedInteger nvalues = _instance(*obj)->_class->_defaultvalues.size();
SQUnsignedInteger nvalues = _instance(obj)->_class->_defaultvalues.size();
for (SQUnsignedInteger n = 0; n < nvalues; n++) {
ReadObject(pBuffer, readState);
HSQOBJECT val;
sq_resetobject(&val);
sq_getstackobj(vm_, -1, &val);
_instance(*obj)->_values[n] = val;
_instance(obj)->_values[n] = val;
sq_pop(vm_, 1);
}
}
Expand Down Expand Up @@ -3527,10 +3531,9 @@ void SquirrelVM::ReadObject(CUtlBuffer* pBuffer, ReadStateMap& readState)
}
case OT_FUNCPROTO: //internal usage only
{
HSQOBJECT* obj = nullptr;
if (readState.CheckCache(pBuffer, &obj))
int marker = 0;
if (readState.CheckCache(pBuffer, vm_, &marker))
{
sq_pushobject(vm_, *obj);
break;
}

Expand All @@ -3543,15 +3546,13 @@ void SquirrelVM::ReadObject(CUtlBuffer* pBuffer, ReadStateMap& readState)
}

vm_->Push(ret);
sq_getstackobj(vm_, -1, obj);
sq_addref(vm_, obj);
readState.StoreTopInCache(marker);
}
case OT_OUTER: //internal usage only
{
HSQOBJECT* obj = nullptr;
if (readState.CheckCache(pBuffer, &obj))
int marker = 0;
if (readState.CheckCache(pBuffer, vm_, &marker))
{
sq_pushobject(vm_, *obj);
break;
}

Expand All @@ -3564,8 +3565,7 @@ void SquirrelVM::ReadObject(CUtlBuffer* pBuffer, ReadStateMap& readState)
outer->_valptr = &(outer->_value);
sq_poptop(vm_);
vm_->Push(outer);
sq_getstackobj(vm_, -1, obj);
sq_addref(vm_, obj);
readState.StoreTopInCache(marker);

break;
}
Expand All @@ -3587,10 +3587,12 @@ void SquirrelVM::ReadState(CUtlBuffer* pBuffer)

sq_pushroottable(vm_);

HSQOBJECT* obj = nullptr;
readState.CheckCache(pBuffer, &obj);
sq_getstackobj(vm_, -1, obj);
sq_addref(vm_, obj);
HSQOBJECT obj;
int marker = 0;
readState.CheckCache(pBuffer, vm_, &marker);
sq_getstackobj(vm_, -1, &obj);
sq_addref(vm_, &obj);
readState.StoreInCache(marker, obj);

int count = pBuffer->GetInt();

Expand Down

0 comments on commit 4e72ce3

Please sign in to comment.