Implements unknown types
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
This commit is contained in:
parent
01e5441d62
commit
0e9c9abf7c
|
@ -124,6 +124,8 @@ namespace Porygon::Binder {
|
||||||
return ScriptType::BoolType;
|
return ScriptType::BoolType;
|
||||||
case HashedString::ConstHash("string"):
|
case HashedString::ConstHash("string"):
|
||||||
return StringScriptType::Dynamic;
|
return StringScriptType::Dynamic;
|
||||||
|
case HashedString::ConstHash("table"):
|
||||||
|
return make_shared<TableScriptType>();
|
||||||
default:
|
default:
|
||||||
if (!UserData::UserDataStorage::HasUserDataType(hash)) {
|
if (!UserData::UserDataStorage::HasUserDataType(hash)) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
|
@ -10,8 +10,8 @@ vector<Diagnostic> DiagnosticsHolder::GetDiagnostics() {
|
||||||
void DiagnosticsHolder::Log(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length,
|
void DiagnosticsHolder::Log(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length,
|
||||||
const std::vector<string>& arguments) {
|
const std::vector<string>& arguments) {
|
||||||
_diagnostics.emplace_back(severity, code, start, length, arguments);
|
_diagnostics.emplace_back(severity, code, start, length, arguments);
|
||||||
if (severity >= DiagnosticSeverity::Error){
|
if (severity == DiagnosticSeverity::Error){
|
||||||
_hasErrors = true;
|
_hasErrors = static_cast<byte>(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ void DiagnosticsHolder::LogInfo(DiagnosticCode code, unsigned int start, unsigne
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DiagnosticsHolder::HasErrors() {
|
bool DiagnosticsHolder::HasErrors() {
|
||||||
return _hasErrors;
|
return _hasErrors == static_cast<byte>(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int DiagnosticsHolder::DiagnosticsCount() {
|
int DiagnosticsHolder::DiagnosticsCount() {
|
||||||
|
|
|
@ -13,13 +13,13 @@ using namespace std;
|
||||||
|
|
||||||
namespace Porygon::Diagnostics {
|
namespace Porygon::Diagnostics {
|
||||||
class DiagnosticsHolder {
|
class DiagnosticsHolder {
|
||||||
bool _hasErrors;
|
byte _hasErrors;
|
||||||
vector<Diagnostic> _diagnostics;
|
vector<Diagnostic> _diagnostics;
|
||||||
vector<size_t> _lineStarts;
|
vector<size_t> _lineStarts;
|
||||||
vector<size_t> _lineLength;
|
vector<size_t> _lineLength;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit DiagnosticsHolder(const u16string& str) :_hasErrors(false), _lineStarts(vector<size_t>{0}) {
|
explicit DiagnosticsHolder(const u16string& str) : _hasErrors(static_cast<byte>(0)), _lineStarts(vector<size_t>{0}) {
|
||||||
size_t lineLength = 0;
|
size_t lineLength = 0;
|
||||||
for (size_t i = 0; i < str.size(); i++){
|
for (size_t i = 0; i < str.size(); i++){
|
||||||
lineLength++;
|
lineLength++;
|
||||||
|
|
|
@ -77,6 +77,9 @@ namespace Porygon{
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] virtual CastResult CastableTo(const shared_ptr<const ScriptType>& castType, bool explicitCast) const{
|
[[nodiscard]] virtual CastResult CastableTo(const shared_ptr<const ScriptType>& castType, bool explicitCast) const{
|
||||||
|
if (_class == TypeClass::All){
|
||||||
|
return CastResult ::UncheckedCast;
|
||||||
|
}
|
||||||
if (explicitCast)
|
if (explicitCast)
|
||||||
return CastResult::InvalidCast;
|
return CastResult::InvalidCast;
|
||||||
return CastResult::InvalidCast;
|
return CastResult::InvalidCast;
|
||||||
|
|
|
@ -15,9 +15,18 @@ namespace Porygon{
|
||||||
_localVariableCount(localVariableCount)
|
_localVariableCount(localVariableCount)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
explicit TableScriptType()
|
||||||
|
: ScriptType(TypeClass::Table),
|
||||||
|
_values(nullptr),
|
||||||
|
_localVariableCount(0)
|
||||||
|
{}
|
||||||
|
|
||||||
|
|
||||||
~TableScriptType() final{
|
~TableScriptType() final{
|
||||||
for (auto i : *_values){
|
if (_values != nullptr){
|
||||||
delete i.second;
|
for (const auto& i : *_values){
|
||||||
|
delete i.second;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
delete _values;
|
delete _values;
|
||||||
}
|
}
|
||||||
|
@ -33,10 +42,10 @@ namespace Porygon{
|
||||||
|
|
||||||
shared_ptr<const ScriptType> GetIndexedType(const ScriptType* indexer) const final{
|
shared_ptr<const ScriptType> GetIndexedType(const ScriptType* indexer) const final{
|
||||||
auto stringKey = dynamic_cast<const StringScriptType*>(indexer);
|
auto stringKey = dynamic_cast<const StringScriptType*>(indexer);
|
||||||
if (stringKey->IsKnownAtBind()){
|
if (stringKey != nullptr && stringKey->IsKnownAtBind() && _values != nullptr){
|
||||||
return _values-> at(Utilities::HashedString::CreateLookup(stringKey->GetHashValue()))->GetType();
|
return _values-> at(Utilities::HashedString::CreateLookup(stringKey->GetHashValue()))->GetType();
|
||||||
}
|
}
|
||||||
throw "TODO: indexing with dynamic keys";
|
return make_shared<ScriptType>(TypeClass::All);
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] inline shared_ptr<const ScriptType> GetIndexedType(uint32_t hash) const final{
|
[[nodiscard]] inline shared_ptr<const ScriptType> GetIndexedType(uint32_t hash) const final{
|
||||||
|
@ -46,6 +55,16 @@ namespace Porygon{
|
||||||
[[nodiscard]] inline const map<Utilities::HashedString, BoundVariable*>* GetValues() const{
|
[[nodiscard]] inline const map<Utilities::HashedString, BoundVariable*>* GetValues() const{
|
||||||
return _values;
|
return _values;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
bool CanBeIterated() const final {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]]
|
||||||
|
shared_ptr<const ScriptType> GetIteratorKeyType() const final {
|
||||||
|
return make_shared<ScriptType>(TypeClass::All);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,20 @@ class ModuleHandler{
|
||||||
Internal(){
|
Internal(){
|
||||||
MODULES = {
|
MODULES = {
|
||||||
{"simple_return", Script::Create(u"return 500")},
|
{"simple_return", Script::Create(u"return 500")},
|
||||||
{"simple_variables", Script::Create(u"foo = 50\nbar = \'test\'")}
|
{"simple_variables", Script::Create(u"foo = 50\nbar = \'test\'")},
|
||||||
|
{
|
||||||
|
"complex_module", Script::Create(uR"(
|
||||||
|
local module = {
|
||||||
|
function contains(table table, string s)
|
||||||
|
for _, v in table do
|
||||||
|
if (v == s) then return true end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
}
|
||||||
|
return module
|
||||||
|
)")
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +35,8 @@ class ModuleHandler{
|
||||||
MODULES.clear();
|
MODULES.clear();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
public:
|
||||||
|
|
||||||
static Internal* _internal;
|
static Internal* _internal;
|
||||||
|
|
||||||
static Internal* GetInternal(){
|
static Internal* GetInternal(){
|
||||||
|
@ -35,10 +50,11 @@ class ModuleHandler{
|
||||||
}
|
}
|
||||||
|
|
||||||
inline static Script* ResolveModule(const char* moduleName, size_t size){
|
inline static Script* ResolveModule(const char* moduleName, size_t size){
|
||||||
return GetInternal()->MODULES[moduleName];
|
auto module = GetInternal()->MODULES[moduleName];
|
||||||
|
REQUIRE(!module->Diagnostics->HasErrors());
|
||||||
|
return module;
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
|
||||||
static void Initialize(){
|
static void Initialize(){
|
||||||
ScriptOptions::GetDefaultScriptOptions()->SetModuleExistsFunc(DoesModuleExist);
|
ScriptOptions::GetDefaultScriptOptions()->SetModuleExistsFunc(DoesModuleExist);
|
||||||
ScriptOptions::GetDefaultScriptOptions()->SetResolveModuleFunc(ResolveModule);
|
ScriptOptions::GetDefaultScriptOptions()->SetResolveModuleFunc(ResolveModule);
|
||||||
|
@ -77,4 +93,18 @@ require("simple_variables")
|
||||||
delete script;
|
delete script;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE( "Require string table contains", "[integration]" ) {
|
||||||
|
ModuleHandler::Initialize();
|
||||||
|
auto script = Script::Create(uR"(
|
||||||
|
local list = require("complex_module")
|
||||||
|
return list.contains({"foo", "bar"}, "bar")
|
||||||
|
)");
|
||||||
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
||||||
|
auto result = script->Evaluate();
|
||||||
|
CHECK(result->EvaluateBool());
|
||||||
|
|
||||||
|
delete script;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue