-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluapp.cpp
More file actions
65 lines (55 loc) · 1.3 KB
/
luapp.cpp
File metadata and controls
65 lines (55 loc) · 1.3 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
/// luapp.cpp -- cpp wrapper for lua
/// Author: Zhang Yichao <echaozh@gmail.com>
/// Created: 2011-08-21
///
#include "luapp.hpp"
using namespace std;
namespace luapp
{
void lua::load (const char *s) {
int ret;
if ((ret = luaL_loadstring (l (), s))) {
if (ret == LUA_ERRSYNTAX)
throw std::invalid_argument ("bad chunk of lua code");
else
throw std::bad_alloc ();
}
pcall (0);
clear ();
}
void lua::load_lib (lua_CFunction f)
{
*this << f;
pcall (0);
}
void lua::load_safe_libs ()
{
load_base ();
load_package ();
load_string ();
load_table ();
load_math ();
}
void lua::load_libs ()
{
luaL_openlibs (l ());
}
void lua::pcall (size_t argc)
{
int ret;
if ((ret = lua_pcall (l (), argc, LUA_MULTRET, 0))) {
std::string msg;
*this >> msg;
if (ret == LUA_ERRRUN)
throw std::runtime_error (msg);
else
throw std::bad_alloc ();
}
}
size_t lua::memory_consumption () {
return lua_gc (l (), LUA_GCCOUNT, 0);
}
void lua::collect_garbage () {
lua_gc (l (), LUA_GCCOLLECT, 0);
}
}