Many fixes for script handling.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2021-01-22 14:11:03 +01:00
parent c56b001ce4
commit f8427fa594
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
8 changed files with 43 additions and 16 deletions

View File

@ -1,4 +1,7 @@
#include "Core.hpp"
std::string ExceptionHandler::_pkmnLibLastException = "";
std::string ExceptionHandler::_pkmnLibLastExceptionStacktrace = "Unset";
export const char* PkmnLib_C_GetLastException() { return ExceptionHandler::GetLastException(); }
export const char* PkmnLib_C_GetLastExceptionStacktrace() { return ExceptionHandler::GetLastExceptionStacktrace(); }

View File

@ -10,18 +10,36 @@
class ExceptionHandler {
static std::string _pkmnLibLastException;
static std::string _pkmnLibLastExceptionStacktrace;
public:
static void SetLastException(const std::exception& e) { _pkmnLibLastException = std::string(e.what()); }
static void SetLastArbUtException(const std::string& function, const ArbUt::Exception& e, size_t depth = 6) {
std::stringstream ss;
ss << "[" << function << "] " << e.what();
_pkmnLibLastException = ss.str();
_pkmnLibLastExceptionStacktrace = e.GetStacktrace(depth, true);
if (_pkmnLibLastExceptionStacktrace.empty())
_pkmnLibLastExceptionStacktrace = "err";
}
static void SetLastException(const std::string& function, const std::exception& e) {
std::stringstream ss;
ss << "[" << function << "] " << e.what();
_pkmnLibLastException = ss.str();
_pkmnLibLastExceptionStacktrace = "Exception did not have a stacktrace.";
}
static const char* GetLastException() { return _pkmnLibLastException.c_str(); }
static const char* GetLastExceptionStacktrace() { return _pkmnLibLastExceptionStacktrace.c_str(); }
};
#define Try(data) \
try { \
data; \
return 0; \
} catch (const ArbUt::Exception& e) { \
ExceptionHandler::SetLastArbUtException(__FUNCTION__, e); \
return PkmnLibException; \
} catch (const std::exception& e) { \
ExceptionHandler::SetLastException(e); \
ExceptionHandler::SetLastException(__FUNCTION__, e); \
return PkmnLibException; \
}

View File

@ -143,7 +143,7 @@ find_package(Threads REQUIRED)
if (STATICC)
message(STATUS "Linking C library statically")
if (NOT UNIX AND NOT APPLE)
if (NOT UNIX AND NOT APPLE OR WINDOWS)
SET(_LINKS ${_LINKS} -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread)
SET(_TESTLINKS ${_TESTLINKS} -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread)
else()

View File

@ -312,7 +312,7 @@ void AngelScriptResolver::InitializeByteCode(
ArbUt::Dictionary<uint32_t, asITypeInfo*> objectTypes;
for (asUINT i = 0; i < typeCount; i++) {
auto t = _mainModule->GetObjectTypeByIndex(i);
objectTypes.Insert(ArbUt::StringView::CalculateHash(t->GetName()), t);
objectTypes.Set(ArbUt::StringView::CalculateHash(t->GetName()), t);
}
ArbUt::Dictionary<ScriptCategory, ArbUt::Dictionary<ArbUt::StringView, AngelScriptTypeInfo*>> typeDatabase;
for (const auto& innerDb : types) {
@ -320,9 +320,9 @@ void AngelScriptResolver::InitializeByteCode(
for (const auto& val : innerDb.second) {
auto decl = val.second;
auto type = objectTypes[decl];
newInnerDb.Insert(val.first, new AngelScriptTypeInfo(val.first, type));
newInnerDb.Set(val.first, new AngelScriptTypeInfo(val.first, type));
}
typeDatabase.Insert(innerDb.first, newInnerDb);
typeDatabase.Set(innerDb.first, newInnerDb);
}
_typeDatabase = typeDatabase;
}

View File

@ -26,6 +26,7 @@ public:
~AngelScriptScript() override { _obj->Release(); }
[[nodiscard]] const ArbUt::StringView& GetName() const noexcept override { return _type->GetName(); }
const AngelScriptTypeInfo* GetType() const noexcept { return _type; }
asIScriptFunction* PrepareMethod(const ArbUt::BasicStringView& name, asIScriptContext* ctx) {
auto func = _type->GetFunction(name);

View File

@ -21,9 +21,6 @@ private:
if (val == nullptr) {
return FunctionInfo{.Exists = false, .Function = nullptr};
}
if (!val->IsOverride()) {
return FunctionInfo{.Exists = false, .Function = nullptr};
}
return FunctionInfo{.Exists = true, .Function = val};
}

View File

@ -68,7 +68,8 @@ public:
if (isDecl) {
// Insert the name and decl into the dictionary. Close off the decl with eof as well.
decl[pos] = '\0';
innerDb.Insert(ArbUt::StringView(name), ArbUt::StringView::CalculateHash(decl));
innerDb.Set(ArbUt::StringView(name), ArbUt::StringView::CalculateHash(decl));
}
// If we have found \1, we are done with the current category, so break.
break;
@ -79,7 +80,7 @@ public:
if (isDecl) {
// Insert the name and decl into the dictionary. Close off the decl with eof as well.
decl[pos] = '\0';
innerDb.Insert(ArbUt::StringView(name), ArbUt::StringView::CalculateHash(decl));
innerDb.Set(ArbUt::StringView(name), ArbUt::StringView::CalculateHash(decl));
// Reset our position and toggle back to name.
pos = 0;
isDecl = false;
@ -101,7 +102,7 @@ public:
}
}
}
types.Insert(categoryArr[0], innerDb);
types.Set(categoryArr[0], innerDb);
}
return types;
}

View File

@ -7,7 +7,7 @@ static std::unordered_map<const char*, const char*> _scripts =
std::unordered_map<const char*, const char*>{{"testScript1", R"(
namespace Pokemon{
[Pokemon effect=testScript1]
class testScript1 {
class testScript1 : PkmnScript {
int add(int a, int b) {
return a + b;
}
@ -17,6 +17,8 @@ class testScript1 {
throw("err");
}
}
void OnInitialize(const array<EffectParameter@> &in parameters) override{ }
}
}
)"}};
@ -83,14 +85,19 @@ TEST_CASE("Get a script resolver, save the byte code to memory, create new scrip
originLib->Initialize(TestLibrary::GetLibrary());
originLib->CreateScript("testScript1", _scripts["testScript1"]);
originLib->FinalizeModule();
auto obj = dynamic_cast<AngelScriptScript*>(originLib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc));
REQUIRE(obj != nullptr);
REQUIRE(obj->GetType()->GetOnInitialize().Exists);
delete obj;
size_t size;
auto byteCode = originLib->WriteByteCodeToMemory(size);
auto newLib = dynamic_cast<AngelScriptResolver*>(PkmnLib::Battling::BattleLibrary::CreateScriptResolver());
newLib->Initialize(TestLibrary::GetLibrary());
newLib->LoadByteCodeFromMemory(byteCode, size);
auto obj = dynamic_cast<AngelScriptScript*>(newLib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc));
obj = dynamic_cast<AngelScriptScript*>(newLib->LoadScript(ScriptCategory::Creature, "testScript1"_cnc));
REQUIRE(obj != nullptr);
REQUIRE(obj->GetType()->GetOnInitialize().Exists);
delete obj;
delete originLib;
delete newLib;