monotone

monotone Mtn Source Tree

Root/src/lua.hh

1// Copyright (C) 2003 Graydon Hoare <graydon@pobox.com>
2//
3// This program is made available under the GNU GPL version 2.0 or
4// greater. See the accompanying file COPYING for details.
5//
6// This program is distributed WITHOUT ANY WARRANTY; without even the
7// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8// PURPOSE.
9
10#ifndef __LUA_HH__
11#define __LUA_HH__
12
13#include <lua.hpp>
14
15#include <map>
16#include <set>
17
18struct lua_State;
19
20// This Lua object represents a single imperative transaction with the lua
21// interpreter. if it fails at any point, all further commands in the
22// transaction are ignored. it cleans the lua stack up when it is
23// destructed, so no need to pop values when you're done.
24
25struct
26Lua
27{
28 lua_State * st;
29 bool failed;
30 static std::set<std::string> missing_functions;
31
32 Lua(lua_State * s);
33 ~Lua();
34
35 void fail(std::string const & reason);
36 bool ok();
37 void report_error();
38 bool check_stack(int count);
39
40 // getters (0 is an invalid index in lua, and is used here to represent the global table)
41 Lua & get(int idx = 0);
42 Lua & get_fn(int idx = 0);
43 Lua & get_tab(int idx = 0);
44 Lua & get_str(int idx = 0);
45 Lua & get_num(int idx = 0);
46 Lua & get_bool(int idx = 0);
47
48 // extractors
49 Lua & extract_str_nolog(std::string & str);
50 Lua & extract_str(std::string & str);
51 Lua & extract_classified_str(std::string & str);
52 Lua & extract_int(int & i);
53 Lua & extract_double(double & i);
54 Lua & extract_bool(bool & i);
55
56 // table iteration
57 Lua & begin();
58 bool next();
59
60 // pushers
61 Lua & push_str(std::string const & str);
62 Lua & push_int(int num);
63 Lua & push_double(double num);
64 Lua & push_bool(bool b);
65 Lua & push_nil();
66 Lua & push_table();
67 Lua & set_field(std::string const & key, int idx = -2);
68 Lua & set_table(int idx = -3);
69 Lua & call(int in, int out);
70 Lua & pop(int count = 1);
71 Lua & func(std::string const & fname);
72 Lua & loadstring(char const * str, char const * identity);
73 Lua & loadfile(char const * filename);
74};
75
76bool run_string(lua_State * st, char const * str, char const * identity);
77bool run_file(lua_State * st, char const * filename);
78void run_directory(lua_State * st, char const * dirname, char const * pattern);
79void add_functions(lua_State * st);
80
81namespace luaext
82{
83 typedef std::map<std::string, int (*)(lua_State*)> fmap;
84 typedef std::map<std::string, fmap> ftmap;
85 extern ftmap *fns;
86 struct extfn
87 {
88 extfn(std::string const & name, std::string const & table,
89 int (*func) (lua_State *));
90 };
91}
92
93// use as:
94// LUAEXT(foo, ) {...}
95// -- a function available to lua as "foo".
96// LUAEXT(foo, bar) {...}
97// -- a function available to lua as member "foo" of table "bar"
98#define LUAEXT(NAME, TABLE) \
99namespace luaext { \
100 struct extfn_ ## NAME ## _ ## TABLE : public extfn { \
101 extfn_ ## NAME ## _ ## TABLE (); \
102 int call(lua_State * LS); \
103 }; \
104 extfn_ ## NAME ## _ ## TABLE TABLE ## _ ## NAME ## _extfn; \
105 extern "C" { \
106 static int TABLE ## _ ## NAME ## _for_lua(lua_State * LS) \
107 { \
108 try { \
109 return TABLE ## _ ## NAME ## _extfn . call(LS); \
110 } catch (std::exception & e) { \
111 return luaL_error(LS, e.what()); \
112 } \
113 } \
114 } \
115 extfn_ ## NAME ## _ ## TABLE :: extfn_ ## NAME ## _ ## TABLE () \
116 : extfn( #NAME , #TABLE , & TABLE ## _## NAME ## _for_lua ) {} \
117} \
118int luaext :: extfn_ ## NAME ## _ ## TABLE :: call(lua_State * LS)
119
120#endif
121
122// Local Variables:
123// mode: C++
124// fill-column: 76
125// c-file-style: "gnu"
126// indent-tabs-mode: nil
127// End:
128// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status