Support for loading variables from module with require
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -4,20 +4,32 @@
|
||||
using namespace Porygon;
|
||||
|
||||
class ModuleHandler{
|
||||
static unordered_map<string, Script*> MODULES;
|
||||
class Internal{
|
||||
public:
|
||||
unordered_map<string, Script*> MODULES;
|
||||
|
||||
~ModuleHandler(){
|
||||
for (auto v: MODULES){
|
||||
delete v.second;
|
||||
Internal(){
|
||||
MODULES = {
|
||||
{"simple_return", Script::Create(u"return 500")},
|
||||
{"simple_variables", Script::Create(u"foo = 50\nbar = \'test\'")}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
~Internal(){
|
||||
for (const auto& v: MODULES){
|
||||
delete v.second;
|
||||
}
|
||||
MODULES.clear();
|
||||
}
|
||||
};
|
||||
static Internal _internal;
|
||||
|
||||
inline static bool DoesModuleExist(const string& moduleName){
|
||||
return MODULES.find(moduleName) != MODULES.end();
|
||||
return _internal.MODULES.find(moduleName) != _internal.MODULES.end();
|
||||
}
|
||||
|
||||
inline static Script* ResolveModule(const string& moduleName){
|
||||
return MODULES[moduleName];
|
||||
return _internal.MODULES[moduleName];
|
||||
}
|
||||
|
||||
public:
|
||||
@@ -27,9 +39,7 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
unordered_map<string, Script*> ModuleHandler::MODULES = unordered_map<string, Script*>{
|
||||
{"simple_return", Script::Create(u"return 500")}
|
||||
};
|
||||
ModuleHandler::Internal ModuleHandler::_internal;
|
||||
|
||||
TEST_CASE( "Require simple return script", "[integration]" ) {
|
||||
ModuleHandler::Initialize();
|
||||
@@ -42,4 +52,23 @@ return require("simple_return")
|
||||
delete script;
|
||||
}
|
||||
|
||||
TEST_CASE( "Require simple variables script", "[integration]" ) {
|
||||
ModuleHandler::Initialize();
|
||||
auto script = Script::Create(uR"(
|
||||
require("simple_variables")
|
||||
)");
|
||||
REQUIRE(!script->Diagnostics -> HasErrors());
|
||||
script->Evaluate();
|
||||
REQUIRE(script->HasVariable(u"foo"));
|
||||
REQUIRE(script->HasVariable(u"bar"));
|
||||
auto foo = script->GetVariable(u"foo");
|
||||
auto bar = script->GetVariable(u"bar");
|
||||
CHECK(foo->EvaluateInteger() == 50);
|
||||
CHECK(bar->EvaluateString() == u"test");
|
||||
|
||||
delete foo;
|
||||
delete bar;
|
||||
delete script;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user