-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluapp_obj.cpp
More file actions
150 lines (127 loc) · 3.16 KB
/
luapp_obj.cpp
File metadata and controls
150 lines (127 loc) · 3.16 KB
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
/// luapp_obj.cpp -- cpp wrapper for lua
/// Author: Zhang Yichao <echaozh@gmail.com>
/// Created: 2011-08-21
///
#include "luapp.hpp"
#include "luapp_obj.hpp"
#include <lua.h>
using namespace std;
namespace luapp
{
namespace details
{
has_lua::has_lua (const table &parent)
: l_ (parent.l ())
{
}
void global_env::get_at (const string &name) const
{
lua_getglobal (ll (), name.c_str ());
}
void global_env::get_at (ssize_t index) const
{
throw logic_error ("global enviroment cannot be indexed by "
"integers");
}
void global_env::set_at (const string &field) const
{
lua_setglobal (ll (), field.c_str ());
}
void global_env::set_at (ssize_t index) const
{
throw logic_error ("global enviroment cannot be indexed by "
"integers");
}
}
object::object (const table &parent, const string &name)
: details::has_lua (parent), env_ (0), parent_ (parent), name_ (name)
{
}
object::object (const table &parent, ssize_t index)
: details::has_lua (parent), env_ (0), parent_ (parent), index_ (index)
{
}
bool object::is_nil () const
{
push ();
bool res = lua_isnil (ll (), -1);
pop ();
return res;
}
void object::push () const
{
parent_.push ();
if (name_.empty ())
parent_.get_at (index_);
else
parent_.get_at (name_);
parent_.dequeue ();
}
void object::pop () const
{
l_.pop ();
}
void object::set () const
{
if (name_.empty ())
parent_.set_at (index_);
else
parent_.set_at (name_);
}
void table::create (size_t arr, size_t hash) const
{
parent_.push ();
lua_createtable (ll (), arr, hash);
set ();
parent_.pop ();
}
size_t table::array_size () const
{
push ();
lua_len (ll (), -1);
size_t n;
l_ >> n;
pop ();
return n;
}
deque<string> table::string_keys () const
{
deque<string> keys;
push ();
lua_pushnil (ll ());
while (lua_next (ll (), -2)) {
if (lua_isstring (ll (), -2))
keys.push_back (lua_tostring (ll (), -2));
l_.pop ();
}
pop ();
return keys;
}
void table::get_at (const string &field) const
{
lua_getfield (ll (), -1, field.c_str ());
}
void table::get_at (ssize_t index) const
{
l_ << index;
lua_gettable (ll (), -2);
}
void table::set_at (const string &field) const
{
lua_setfield (ll (), -2, field.c_str ());
}
void table::set_at (ssize_t index) const
{
l_ << index;
lua_pushvalue (ll (), -2);
lua_remove (ll (), -3);
lua_settable (ll (), -3);
}
void function::define (lua_CFunction func) const
{
parent_.push ();
l_ << func;
set ();
parent_.pop ();
}
}