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
|
// Copyright (C) 2003 Graydon Hoare <graydon@pobox.com>
//
// This program is made available under the GNU GPL version 2.0 or
// greater. See the accompanying file COPYING for details.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.
#ifndef __LUA_HH__
#define __LUA_HH__
#include <lua.hpp>
#include <map>
#include <set>
struct lua_State;
// This Lua object represents a single imperative transaction with the lua
// interpreter. if it fails at any point, all further commands in the
// transaction are ignored. it cleans the lua stack up when it is
// destructed, so no need to pop values when you're done.
struct
Lua
{
lua_State * st;
bool failed;
static std::set<std::string> missing_functions;
Lua(lua_State * s);
~Lua();
void fail(std::string const & reason);
bool ok();
void report_error();
bool check_stack(int count);
// getters (0 is an invalid index in lua, and is used here to represent the global table)
Lua & get(int idx = 0);
Lua & get_fn(int idx = 0);
Lua & get_tab(int idx = 0);
Lua & get_str(int idx = 0);
Lua & get_num(int idx = 0);
Lua & get_bool(int idx = 0);
// extractors
Lua & extract_str_nolog(std::string & str);
Lua & extract_str(std::string & str);
Lua & extract_classified_str(std::string & str);
Lua & extract_int(int & i);
Lua & extract_double(double & i);
Lua & extract_bool(bool & i);
// table iteration
Lua & begin();
bool next();
// pushers
Lua & push_str(std::string const & str);
Lua & push_int(int num);
Lua & push_double(double num);
Lua & push_bool(bool b);
Lua & push_nil();
Lua & push_table();
Lua & set_field(std::string const & key, int idx = -2);
Lua & set_table(int idx = -3);
Lua & call(int in, int out);
Lua & pop(int count = 1);
Lua & func(std::string const & fname);
Lua & loadstring(char const * str, char const * identity);
Lua & loadfile(char const * filename);
};
bool run_string(lua_State * st, char const * str, char const * identity);
bool run_file(lua_State * st, char const * filename);
void run_directory(lua_State * st, char const * dirname, char const * pattern);
void add_functions(lua_State * st);
namespace luaext
{
typedef std::map<std::string, int (*)(lua_State*)> fmap;
typedef std::map<std::string, fmap> ftmap;
extern ftmap *fns;
struct extfn
{
extfn(std::string const & name, std::string const & table,
int (*func) (lua_State *));
};
}
// use as:
// LUAEXT(foo, ) {...}
// -- a function available to lua as "foo".
// LUAEXT(foo, bar) {...}
// -- a function available to lua as member "foo" of table "bar"
#define LUAEXT(NAME, TABLE) \
namespace luaext { \
struct extfn_ ## NAME ## _ ## TABLE : public extfn { \
extfn_ ## NAME ## _ ## TABLE (); \
int call(lua_State * LS); \
}; \
extfn_ ## NAME ## _ ## TABLE TABLE ## _ ## NAME ## _extfn; \
extern "C" { \
static int TABLE ## _ ## NAME ## _for_lua(lua_State * LS) \
{ \
try { \
return TABLE ## _ ## NAME ## _extfn . call(LS); \
} catch (std::exception & e) { \
return luaL_error(LS, e.what()); \
} \
} \
} \
extfn_ ## NAME ## _ ## TABLE :: extfn_ ## NAME ## _ ## TABLE () \
: extfn( #NAME , #TABLE , & TABLE ## _## NAME ## _for_lua ) {} \
} \
int luaext :: extfn_ ## NAME ## _ ## TABLE :: call(lua_State * LS)
#endif
// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
|