00001
00002
00003
00004
00005
00006
00007 #ifndef __CLUA_H__
00008 #define __CLUA_H__
00009
00010 extern "C" {
00011 #include <lua.h>
00012 #include <lauxlib.h>
00013 #include <lualib.h>
00014 }
00015
00016 #include <cstdio>
00017 #include <cstdarg>
00018 #include <string>
00019 #include <map>
00020 #include <set>
00021
00022 #ifndef CLUA_MAX_MEMORY_USE
00023 #define CLUA_MAX_MEMORY_USE (6 * 1024)
00024 #endif
00025
00026 class CLua;
00027
00028 class lua_stack_cleaner
00029 {
00030 public:
00031 lua_stack_cleaner(lua_State *_ls) : ls(_ls), top(lua_gettop(_ls)) { }
00032 ~lua_stack_cleaner() { lua_settop(ls, top); }
00033 private:
00034 lua_State *ls;
00035 int top;
00036 };
00037
00038 class lua_call_throttle
00039 {
00040 public:
00041 lua_call_throttle(CLua *handle);
00042 ~lua_call_throttle();
00043
00044 static CLua *find_clua(lua_State *ls);
00045
00046 private:
00047 CLua *lua;
00048
00049 typedef std::map<lua_State *, CLua *> lua_clua_map;
00050 static lua_clua_map lua_map;
00051 };
00052
00053 class lua_shutdown_listener
00054 {
00055 public:
00056 virtual ~lua_shutdown_listener();
00057 virtual void shutdown(CLua &lua) = 0;
00058 };
00059
00060
00061
00062
00063 class lua_datum : public lua_shutdown_listener
00064 {
00065 public:
00066 lua_datum(CLua &lua, int stackpos = -1, bool pop = true);
00067 lua_datum(const lua_datum &other);
00068
00069 const lua_datum &operator = (const lua_datum &other);
00070
00071 void shutdown(CLua &lua);
00072
00073 ~lua_datum();
00074
00075
00076 void push() const;
00077
00078 bool is_table() const;
00079 bool is_function() const;
00080 bool is_number() const;
00081 bool is_string() const;
00082 bool is_udata() const;
00083
00084 public:
00085 CLua &lua;
00086
00087 private:
00088 bool need_cleanup;
00089
00090 private:
00091 void set_from(const lua_datum &o);
00092 void cleanup();
00093 };
00094
00095 class CLua
00096 {
00097 public:
00098 CLua(bool managed = true);
00099 ~CLua();
00100
00101 static CLua &get_vm(lua_State *);
00102
00103 lua_State *state();
00104
00105 operator lua_State * ()
00106 {
00107 return state();
00108 }
00109
00110 void save(writer &outf);
00111 void gc();
00112
00113 void setglobal(const char *name);
00114 void getglobal(const char *name);
00115
00116
00117
00118 std::string setuniqregistry();
00119
00120 void setregistry(const char *name);
00121 void getregistry(const char *name);
00122
00123 int loadbuffer(const char *buf, size_t size, const char *context);
00124 int loadstring(const char *str, const char *context);
00125 int execstring(const char *str, const char *context = "init.txt",
00126 int nresults = 0);
00127 int execfile(const char *filename,
00128 bool trusted = false,
00129 bool die_on_fail = false,
00130 bool force = false);
00131
00132 void pushglobal(const std::string &name);
00133
00134 maybe_bool callmbooleanfn(const char *fn, const char *params, ...);
00135 bool callbooleanfn(bool defval, const char *fn, const char *params, ...);
00136 bool callfn(const char *fn, int nargs, int nret = 1);
00137 bool callfn(const char *fn, const char *params, ...);
00138 void fnreturns(const char *params, ...);
00139 bool runhook(const char *hook, const char *params, ...);
00140
00141 void add_shutdown_listener(lua_shutdown_listener *);
00142 void remove_shutdown_listener(lua_shutdown_listener *);
00143
00144 static int file_write(lua_State *ls);
00145 static int loadfile(lua_State *ls, const char *file,
00146 bool trusted = false, bool die_on_fail = false);
00147 static bool is_path_safe(std::string file, bool trusted = false);
00148
00149 static bool is_managed_vm(lua_State *ls);
00150
00151 void print_stack();
00152
00153 public:
00154 std::string error;
00155
00156
00157
00158 bool managed_vm;
00159 bool shutting_down;
00160 int throttle_unit_lines;
00161 int throttle_sleep_ms;
00162 int throttle_sleep_start, throttle_sleep_end;
00163 int n_throttle_sleeps;
00164 int mixed_call_depth;
00165 int lua_call_depth;
00166 int max_mixed_call_depth;
00167 int max_lua_call_depth;
00168
00169 long memory_used;
00170
00171 static const int MAX_THROTTLE_SLEEPS = 100;
00172
00173 private:
00174 lua_State *_state;
00175 typedef std::set<std::string> sfset;
00176 sfset sourced_files;
00177 unsigned int uniqindex;
00178
00179 std::vector<lua_shutdown_listener*> shutdown_listeners;
00180
00181 private:
00182 void init_lua();
00183 void set_error(int err, lua_State *ls = NULL);
00184 void load_cmacro();
00185 void load_chooks();
00186 void init_throttle();
00187
00188 static void _getregistry(lua_State *, const char *name);
00189
00190 void vfnreturns(const char *par, va_list va);
00191
00192 bool proc_returns(const char *par) const;
00193
00194 bool calltopfn(lua_State *ls, const char *format, va_list args,
00195 int retc = -1, va_list *fnr = NULL);
00196 maybe_bool callmbooleanfn(const char *fn, const char *params,
00197 va_list args);
00198
00199 int push_args(lua_State *ls, const char *format, va_list args,
00200 va_list *cpto = NULL);
00201 int return_count(lua_State *ls, const char *format);
00202
00203 struct CLuaSave
00204 {
00205 const char *filename;
00206 FILE *handle;
00207
00208 FILE *get_file();
00209 };
00210
00211 friend class lua_call_throttle;
00212 };
00213
00214 class lua_text_pattern : public base_pattern
00215 {
00216 public:
00217 lua_text_pattern(const std::string &pattern);
00218 ~lua_text_pattern();
00219
00220 bool valid() const;
00221 bool matches(const std::string &s) const;
00222
00223 static bool is_lua_pattern(const std::string &s);
00224
00225 private:
00226 bool translated;
00227 bool isvalid;
00228 std::string pattern;
00229 std::string lua_fn_name;
00230
00231 static unsigned int lfndx;
00232
00233 bool translate() const;
00234 void pre_pattern(std::string &pat, std::string &fn) const;
00235 void post_pattern(std::string &pat, std::string &fn) const;
00236
00237 static std::string new_fn_name();
00238 };
00239
00240
00241 #ifdef DEBUG_GLOBALS
00242 #define clua (*real_clua)
00243 #endif
00244 extern CLua clua;
00245
00246 void lua_set_exclusive_item(const item_def *item = NULL);
00247
00248 std::string quote_lua_string(const std::string &s);
00249
00250 #endif