From 4662314839c73d67c675f7e632427ac586586c0f Mon Sep 17 00:00:00 2001 From: Vasilii Rogin Date: Thu, 20 Apr 2023 02:46:24 +0300 Subject: [PATCH] Add FreeArray --- src/main.cc | 4 +++- src/sfall_arrays.cc | 13 +++++++++++++ src/sfall_arrays.h | 3 ++- src/sfall_opcodes.cc | 14 +++++++++----- src/sfall_script_value.cc | 5 +++++ src/sfall_script_value.h | 2 ++ 6 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/main.cc b/src/main.cc index a016f16..7698046 100644 --- a/src/main.cc +++ b/src/main.cc @@ -32,6 +32,7 @@ #include "scripts.h" #include "selfrun.h" #include "settings.h" +#include "sfall_arrays.h" #include "sfall_config.h" #include "svga.h" #include "text_font.h" @@ -40,7 +41,6 @@ #include "window_manager_private.h" #include "word_wrap.h" #include "worldmap.h" - namespace fallout { #define DEATH_WINDOW_WIDTH 640 @@ -375,6 +375,8 @@ static void mainLoop() renderPresent(); sharedFpsLimiter.throttle(); + + DeleteAllTempArrays(); } scriptsDisable(); diff --git a/src/sfall_arrays.cc b/src/sfall_arrays.cc index 4394405..e4d02cf 100644 --- a/src/sfall_arrays.cc +++ b/src/sfall_arrays.cc @@ -131,4 +131,17 @@ void SetArray(ArrayId array_id, const SFallScriptValue& key, const SFallScriptVa } } +void FreeArray(ArrayId array_id) +{ + // TODO: remove from saved_arrays + arrays.erase(array_id); +} + +void DeleteAllTempArrays() +{ + for (auto it = temporaryArrays.begin(); it != temporaryArrays.end(); ++it) { + FreeArray(*it); + } + temporaryArrays.clear(); +} } \ No newline at end of file diff --git a/src/sfall_arrays.h b/src/sfall_arrays.h index acb8915..098afe7 100644 --- a/src/sfall_arrays.h +++ b/src/sfall_arrays.h @@ -20,6 +20,7 @@ ProgramValue GetArrayKey(ArrayId array_id, int index); int LenArray(ArrayId array_id); ProgramValue GetArray(ArrayId array_id, const SFallScriptValue& key); void SetArray(ArrayId array_id, const SFallScriptValue& key, const SFallScriptValue& val, bool allowUnset); - +void FreeArray(ArrayId array_id); +void DeleteAllTempArrays(); } #endif /* SFALL_ARRAYS */ \ No newline at end of file diff --git a/src/sfall_opcodes.cc b/src/sfall_opcodes.cc index 18abbe4..16ed986 100644 --- a/src/sfall_opcodes.cc +++ b/src/sfall_opcodes.cc @@ -304,9 +304,7 @@ static void opCreateArray(Program* program) { auto flags = programStackPopInteger(program); auto len = programStackPopInteger(program); - auto array_id = CreateArray(len, flags); - programStackPushInteger(program, array_id); } @@ -315,9 +313,7 @@ static void opTempArray(Program* program) { auto flags = programStackPopInteger(program); auto len = programStackPopInteger(program); - auto array_id = CreateTempArray(len, flags); - programStackPushInteger(program, array_id); } @@ -330,6 +326,13 @@ static void opGetArray(Program* program) programStackPushValue(program, value); } +// free_array +static void opFreeArray(Program* program) +{ + auto arrayId = programStackPopInteger(program); + FreeArray(arrayId); +} + // len_array static void opLenArray(Program* program) { @@ -346,7 +349,7 @@ static void opPartyMemberList(Program* program) for (int i = 0; i < LenArray(array_id); i++) { SetArray(array_id, SFallScriptValue { i }, SFallScriptValue { objects[i] }, false); } - programStackPushInteger(program, 100); + programStackPushInteger(program, array_id); } // round @@ -393,6 +396,7 @@ void sfallOpcodesInit() interpreterRegisterOpcode(0x8221, opGetScreenHeight); interpreterRegisterOpcode(0x822D, opCreateArray); interpreterRegisterOpcode(0x822F, opGetArray); + interpreterRegisterOpcode(0x8230, opFreeArray); interpreterRegisterOpcode(0x8231, opLenArray); interpreterRegisterOpcode(0x8233, opTempArray); interpreterRegisterOpcode(0x8237, opParseInt); diff --git a/src/sfall_script_value.cc b/src/sfall_script_value.cc index f3e8348..a17c0ef 100644 --- a/src/sfall_script_value.cc +++ b/src/sfall_script_value.cc @@ -27,6 +27,11 @@ SFallScriptValue::SFallScriptValue(ProgramValue& value) // TODO: If type is string then copy string } +SFallScriptValue::~SFallScriptValue() +{ + // TODO: If type is string then free it +} + bool SFallScriptValue::isInt() const { return opcode == VALUE_TYPE_INT; diff --git a/src/sfall_script_value.h b/src/sfall_script_value.h index 47e2a60..6b0b3af 100644 --- a/src/sfall_script_value.h +++ b/src/sfall_script_value.h @@ -16,6 +16,8 @@ public: bool isFloat() const; bool isPointer() const; int asInt() const; + + ~SFallScriptValue(); }; }