diff --git a/sp/src/vscript/vscript_squirrel.cpp b/sp/src/vscript/vscript_squirrel.cpp index d468a073de..2c80041b21 100644 --- a/sp/src/vscript/vscript_squirrel.cpp +++ b/sp/src/vscript/vscript_squirrel.cpp @@ -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 @@ -3176,10 +3186,9 @@ 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; } @@ -3187,8 +3196,7 @@ void SquirrelVM::ReadObject(CUtlBuffer* pBuffer, ReadStateMap& 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); @@ -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) { @@ -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; } @@ -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 { @@ -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); @@ -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_); @@ -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; } @@ -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) { @@ -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) { @@ -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); @@ -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; } @@ -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); @@ -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; } @@ -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); } } @@ -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; } @@ -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; } @@ -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; } @@ -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();