summaryrefslogtreecommitdiff
path: root/tests/test-Lua.cc
blob: 39ccbee5c5ba888c6e4a26da87f1cf2209a57ab8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <Main.h>
#include <BLua.h>

using namespace Balau;

static int objGotDestroyed = 0;

static int callCount = 0;

class ObjectTest {
  public:
      ObjectTest() { Printer::log(M_DEBUG, "ObjectTest at %p created.", this); }
      ~ObjectTest() { Printer::log(M_DEBUG, "ObjectTest at %p destroyed.", this); objGotDestroyed++; }
    void someMethod1() { Printer::log(M_DEBUG, "ObjectTest::someMethod1() called on %p.", this); callCount++; }
    int someMethod2(int p) { Printer::log(M_DEBUG, "ObjectTest::someMethod2() called on %p.", this); callCount++; return p * 2; }
    static void someFunction() { Printer::log(M_DEBUG, "ObjectTest::someFunction() called."); callCount++; }
};

enum ObjectTest_methods_t {
    OBJECTTEST_SOMEMETHOD1,
    OBJECTTEST_SOMEMETHOD2,
};

enum ObjectTest_functions_t {
    OBJECTTEST_CREATEOBJECTTEST,
    OBJECTTEST_SOMEFUNCTION,
};

struct lua_functypes_t ObjectTest_methods[] = {
    { OBJECTTEST_SOMEMETHOD1,           "someMethod1",          0, 0, { } },
    { OBJECTTEST_SOMEMETHOD2,           "someMethod2",          1, 1, { BLUA_NUMBER } },
    { -1, 0, 0, 0, 0 },
};

struct lua_functypes_t ObjectTest_functions[] = {
    { OBJECTTEST_CREATEOBJECTTEST,      "createObjectTest",     0, 0, { } },
    { OBJECTTEST_SOMEFUNCTION,          "ObjectTestFunction",   0, 0, { } },
    { -1, 0, 0, 0, 0 },
};

class sLua_ObjectTest {
  public:
    DECLARE_METHOD(ObjectTest, OBJECTTEST_SOMEMETHOD1);
    DECLARE_METHOD(ObjectTest, OBJECTTEST_SOMEMETHOD2);

    DECLARE_FUNCTION(ObjectTest, OBJECTTEST_CREATEOBJECTTEST);
    DECLARE_FUNCTION(ObjectTest, OBJECTTEST_SOMEFUNCTION);
  private:
    static int ObjectTest_proceed(Lua & L, int n, ObjectTest * obj, int caller);
    static int ObjectTest_proceed_statics(Lua & L, int n, int caller);
};

class LuaObjectTestFactory : public LuaObjectFactory {
  public:
      LuaObjectTestFactory(ObjectTest * obj) : m_obj(obj) { }
    static void pushStatics(Lua & L) {
        CHECK_METHODS(ObjectTest);
        CHECK_FUNCTIONS(ObjectTest);

        PUSH_FUNCTION(ObjectTest, OBJECTTEST_CREATEOBJECTTEST);
        PUSH_FUNCTION(ObjectTest, OBJECTTEST_SOMEFUNCTION);
    }
  private:
    void pushObjectAndMembers(Lua & L) {
        pushObj(L, m_obj, "ObjectTest");

        PUSH_METHOD(ObjectTest, OBJECTTEST_SOMEMETHOD1);
        PUSH_METHOD(ObjectTest, OBJECTTEST_SOMEMETHOD2);
    }
    ObjectTest * m_obj;
};

int sLua_ObjectTest::ObjectTest_proceed(Lua & L, int n, ObjectTest * obj, int caller) {
    switch (caller) {
    case OBJECTTEST_SOMEMETHOD1:
        obj->someMethod1();
        break;

    case OBJECTTEST_SOMEMETHOD2:
        L.push((lua_Number) obj->someMethod2(L.tonumber(-1)));
        return 1;
        break;
    }

    return 0;
}

int sLua_ObjectTest::ObjectTest_proceed_statics(Lua & L, int n, int caller) {
    switch (caller) {
    case OBJECTTEST_CREATEOBJECTTEST:
        {
            ObjectTest * ot = new ObjectTest;
            LuaObjectTestFactory factory(ot);
            factory.pushDestruct(L);
        }
        return 1;
        break;

    case OBJECTTEST_SOMEFUNCTION:
        ObjectTest::someFunction();
        break;
    }

    return 0;
}

void MainTask::Do() {
    Printer::log(M_STATUS, "Test::Lua running.");

    Lua L;

    // yeah, they really should be the same thing.
    TAssert(sizeof(L) == sizeof(lua_State *));

    L.open_base();
    L.open_table();
    L.open_string();
    L.open_math();
    L.open_debug();
    L.open_bit();
    L.open_jit();
    L.open_ffi();

    LuaObjectTestFactory::pushStatics(L);

    TAssert(L.gettop() == 0);
    L.load("return 42");
    TAssert(L.gettop() == 1);
    int r = L.tonumber();
    TAssert(r == 42);
    L.pop();
    TAssert(L.gettop() == 0);

    L.push("obj");
    {
        ObjectTest * ot = new ObjectTest;
        LuaObjectTestFactory factory(ot);
        factory.pushDestruct(L);
    }
    L.settable(LUA_GLOBALSINDEX);

    L.load("return type(obj)");
    TAssert(L.gettop() == 1);
    String t = L.tostring();
    TAssert(t == "table");
    L.pop();

    TAssert(callCount == 0);

    L.load("obj:someMethod1()");
    TAssert(L.gettop() == 0);

    TAssert(callCount == 1);

    L.load("return obj:someMethod2(21)");
    TAssert(L.gettop() == 1);
    TAssert(L.tonumber() == 42);
    L.pop();

    TAssert(callCount == 2);

    L.load("ObjectTestFunction()");
    TAssert(L.gettop() == 0);

    TAssert(callCount == 3);

    TAssert(objGotDestroyed == 0);
    L.load("obj2 = createObjectTest() obj2:destroy()");
    TAssert(objGotDestroyed == 1);
    L.load("createObjectTest() collectgarbage('collect')");
    TAssert(objGotDestroyed == 2);
    L.close();
    TAssert(objGotDestroyed == 3);

    Printer::log(M_STATUS, "Test::Lua passed.");
}