From 3a9f1f976a0832bcdb8f9d1dc429fb3b120ce577 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Thu, 12 Sep 2019 21:50:48 +0200 Subject: [PATCH] Fix modules without return type throwing segmentation fault --- src/Script.cpp | 6 +++--- src/Script.hpp | 2 +- tests/integration/ModuleTests.cpp | 35 ++++++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/Script.cpp b/src/Script.cpp index 67a7fd3..0a1c055 100644 --- a/src/Script.cpp +++ b/src/Script.cpp @@ -25,7 +25,7 @@ Porygon::Script::Script(const u16string& s, ScriptOptions* options) : Diagnostics(make_shared(s)), _boundScript(nullptr), _scriptVariables(new map()), - _scriptTypes(new unordered_map>{}), + _scriptTypes(new unordered_map>{}), _scriptOptions(options) { _evaluator = new Evaluator(this -> _scriptVariables, this -> GetScriptOptions()); @@ -63,7 +63,7 @@ void Porygon::Script::Parse(const u16string& script) { delete v.second; } for (const auto& v : variableTypes){ - this->_scriptTypes -> insert({v.first, nullptr}); + this->_scriptTypes -> insert({v.first, v.second}); } scriptScope.clear(); variableTypes.clear(); @@ -109,7 +109,7 @@ Porygon::Script::Script(shared_ptr boundScript, : _boundScript(std::move(boundScript)), Diagnostics(std::move(diagnostics)), _scriptVariables(new map()), - _scriptTypes(new unordered_map>{}), + _scriptTypes(new unordered_map>{}), _scriptOptions(nullptr) { _evaluator = new Evaluator(_scriptVariables, this -> GetScriptOptions()); diff --git a/src/Script.hpp b/src/Script.hpp index aa776e6..5ee8702 100644 --- a/src/Script.hpp +++ b/src/Script.hpp @@ -19,7 +19,7 @@ namespace Porygon{ class Script { Evaluator* _evaluator; map* _scriptVariables; - unordered_map>* _scriptTypes; + unordered_map>* _scriptTypes; shared_ptr _boundScript; shared_ptr _returnType = nullptr; ScriptOptions* _scriptOptions; diff --git a/tests/integration/ModuleTests.cpp b/tests/integration/ModuleTests.cpp index 309c805..382e23a 100644 --- a/tests/integration/ModuleTests.cpp +++ b/tests/integration/ModuleTests.cpp @@ -24,7 +24,14 @@ local module = { } return module )") - } + }, + {"simple_no_return", Script::Create(u"foo = 500")}, + {"no_return_with_local", Script::Create(uR"( +local foo = 684 +function bar() + return foo +end +)")}, }; } @@ -106,5 +113,31 @@ return list.contains({"foo", "bar"}, "bar") delete script; } +TEST_CASE( "Simple no return module", "[integration]" ) { + ModuleHandler::Initialize(); + auto script = Script::Create(uR"( +require("simple_no_return") +return foo +)"); + REQUIRE(!script->Diagnostics -> HasErrors()); + auto result = script->Evaluate(); + CHECK(result->EvaluateInteger() == 500); + + delete script; +} + +TEST_CASE( "Simple no return module with local variable", "[integration]" ) { + ModuleHandler::Initialize(); + auto script = Script::Create(uR"( +require("no_return_with_local") +return bar() +)"); + REQUIRE(!script->Diagnostics -> HasErrors()); + auto result = script->Evaluate(); + CHECK(result->EvaluateInteger() == 684); + + delete script; +} + #endif