Skip to content

Commit

Permalink
LuaPlus replication: LuaObject (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
4z0t authored Dec 3, 2024
1 parent 63718d1 commit 276b07f
Show file tree
Hide file tree
Showing 12 changed files with 943 additions and 429 deletions.
840 changes: 472 additions & 368 deletions include/LuaAPI.h

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions include/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "../workflow.cpp"
#include <type_traits>

#define SHARED extern "C"

#define GPtr(addr, type) \
(*(type*)addr)

Expand Down
6 changes: 0 additions & 6 deletions section/ForkThreadFix.cpp

This file was deleted.

6 changes: 6 additions & 0 deletions section/ForkThreadFix.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "LuaAPI.h"

SHARED bool __thiscall IsLuaFunction(LuaObject *obj)
{
return obj->Type() == LUA_TFUNCTION;
}
31 changes: 31 additions & 0 deletions section/Lua/Iterators/IPairs.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "IPairs.h"

IPairsIterator::IPairsIterator(const LuaObject &table)
: table{table}, index{0}, value{table.m_state} {}

IPairsIterator &IPairsIterator::operator++()
{
++index;
value = table[index];
return *this;
}

const std::pair<int, LuaObject> IPairsIterator::operator*() const
{
return {index, value};
}

std::pair<int, LuaObject> IPairsIterator::operator*() { return {index, value}; }

bool IPairsIterator::operator!=(const IPairsEndIterator &) const
{
return !value.IsNil();
}

IPairs::IPairs(const LuaObject &table) : table{table}
{
luaplus_assert(table.IsTable());
}

IPairsIterator IPairs::begin() { return ++IPairsIterator(table); }
IPairsEndIterator IPairs::end() { return IPairsEndIterator{}; }
38 changes: 38 additions & 0 deletions section/Lua/Iterators/IPairs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once
#include "LuaAPI.h"
#include <utility>

class IPairsEndIterator
{
};

class IPairsIterator
{
public:
IPairsIterator(const LuaObject &table);

IPairsIterator &operator++();

const std::pair<int, LuaObject> operator*() const;

std::pair<int, LuaObject> operator*();

bool operator!=(const IPairsEndIterator &) const;

private:
const LuaObject &table;
int index;
LuaObject value;
};

class IPairs
{
public:
IPairs(const LuaObject &table);

IPairsIterator begin();
IPairsEndIterator end();

private:
const LuaObject &table;
};
34 changes: 34 additions & 0 deletions section/Lua/Iterators/Pairs.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "Pairs.h"

PairsIterator::PairsIterator(const LuaObject &table)
: table{table}, key{table.m_state}, value{table.m_state}, done{false} {}

PairsIterator &PairsIterator::operator++()
{
LuaState *state = table.m_state;
if (!done && !LuaPlusH_next(state, &table, &key, &value))
{
done = true;
}
return *this;
}

const std::pair<LuaObject, LuaObject> PairsIterator::operator*() const
{
return {key, value};
}

std::pair<LuaObject, LuaObject> PairsIterator::operator*()
{
return {key, value};
}

bool PairsIterator::operator!=(const EndIterator &) const { return !done; }

Pairs::Pairs(const LuaObject &table) : table{table}
{
luaplus_assert(table.IsTable());
}

PairsIterator Pairs::begin() { return ++PairsIterator(table); }
EndIterator Pairs::end() { return EndIterator{}; }
37 changes: 37 additions & 0 deletions section/Lua/Iterators/Pairs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once
#include "LuaAPI.h"
#include <utility>

class EndIterator
{
};
class PairsIterator
{
public:
PairsIterator(const LuaObject &table);

PairsIterator &operator++();

const std::pair<LuaObject, LuaObject> operator*() const;

std::pair<LuaObject, LuaObject> operator*();

bool operator!=(const EndIterator &) const;

private:
const LuaObject &table;
LuaObject key;
LuaObject value;
bool done;
};

class Pairs
{
public:
Pairs(const LuaObject &table);
PairsIterator begin();
EndIterator end();

private:
const LuaObject &table;
};
Loading

0 comments on commit 276b07f

Please sign in to comment.