Fix modules without return type throwing segmentation fault
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-09-12 21:50:48 +02:00
parent 80f3af22ac
commit 3a9f1f976a
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
3 changed files with 38 additions and 5 deletions

View File

@ -25,7 +25,7 @@ Porygon::Script::Script(const u16string& s, ScriptOptions* options)
: Diagnostics(make_shared<Diagnostics::DiagnosticsHolder>(s)),
_boundScript(nullptr),
_scriptVariables(new map<Utilities::HashedString, EvalValuePointer>()),
_scriptTypes(new unordered_map<Utilities::HashedString, shared_ptr<ScriptType>>{}),
_scriptTypes(new unordered_map<Utilities::HashedString, shared_ptr<const ScriptType>>{}),
_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<BoundScriptStatement> boundScript,
: _boundScript(std::move(boundScript)),
Diagnostics(std::move(diagnostics)),
_scriptVariables(new map<Utilities::HashedString, EvalValuePointer>()),
_scriptTypes(new unordered_map<Utilities::HashedString, shared_ptr<ScriptType>>{}),
_scriptTypes(new unordered_map<Utilities::HashedString, shared_ptr<const ScriptType>>{}),
_scriptOptions(nullptr)
{
_evaluator = new Evaluator(_scriptVariables, this -> GetScriptOptions());

View File

@ -19,7 +19,7 @@ namespace Porygon{
class Script {
Evaluator* _evaluator;
map<Utilities::HashedString, EvalValuePointer>* _scriptVariables;
unordered_map<Utilities::HashedString, shared_ptr<ScriptType>>* _scriptTypes;
unordered_map<Utilities::HashedString, shared_ptr<const ScriptType>>* _scriptTypes;
shared_ptr<Binder::BoundScriptStatement> _boundScript;
shared_ptr<const ScriptType> _returnType = nullptr;
ScriptOptions* _scriptOptions;

View File

@ -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