From 7974b1cb0ff0b1d4e20550a3bc731b5c7df7ed10 Mon Sep 17 00:00:00 2001 From: Robert Vollmert Date: Wed, 21 Oct 2009 11:31:49 +0200 Subject: Move more macros and functions to cluautil.cc. --- crawl-ref/source/cluautil.cc | 17 ++++++++++++ crawl-ref/source/cluautil.h | 64 +++++++++++++++++++++++++++++++++++++++++++- crawl-ref/source/dlua.cc | 16 ----------- crawl-ref/source/dlua.h | 7 ----- crawl-ref/source/l_crawl.cc | 2 +- crawl-ref/source/l_debug.cc | 2 +- crawl-ref/source/l_dgn_bf.cc | 2 +- crawl-ref/source/l_dgnbld.cc | 2 +- crawl-ref/source/l_dgnevt.cc | 2 +- crawl-ref/source/l_dgngrd.cc | 3 ++- crawl-ref/source/l_dgnlvl.cc | 2 +- crawl-ref/source/l_dgntil.cc | 2 +- crawl-ref/source/l_food.cc | 2 +- crawl-ref/source/l_libs.h | 48 +-------------------------------- crawl-ref/source/l_los.cc | 2 +- crawl-ref/source/l_mapmrk.cc | 2 +- crawl-ref/source/l_you.cc | 2 +- 17 files changed, 94 insertions(+), 83 deletions(-) (limited to 'crawl-ref/source') diff --git a/crawl-ref/source/cluautil.cc b/crawl-ref/source/cluautil.cc index 291b717a21..13f5ab7267 100644 --- a/crawl-ref/source/cluautil.cc +++ b/crawl-ref/source/cluautil.cc @@ -58,3 +58,20 @@ void clua_push_dgn_event(lua_State *ls, const dgn_event *devent) clua_new_userdata(ls, DEVENT_METATABLE); *de = devent; } + + +void luaopen_setmeta(lua_State *ls, + const char *global, + const luaL_reg *lua_lib, + const char *meta) +{ + luaL_newmetatable(ls, meta); + lua_setglobal(ls, global); + + luaL_openlib(ls, global, lua_lib, 0); + + // Do .__index = + lua_pushstring(ls, "__index"); + lua_pushvalue(ls, -2); + lua_settable(ls, -3); +} diff --git a/crawl-ref/source/cluautil.h b/crawl-ref/source/cluautil.h index da7d3a5531..1af4d76485 100644 --- a/crawl-ref/source/cluautil.h +++ b/crawl-ref/source/cluautil.h @@ -1,6 +1,25 @@ +/* + * File: cluautil.h + * Summary: Utility functions and macros for Lua bindings. + */ + #ifndef CLUAUTIL_H #define CLUAUTIL_H +/* + * Function definitions. + */ +#define LUAFN(name) static int name(lua_State *ls) + + +void luaopen_setmeta(lua_State *ls, + const char *global, + const luaL_reg *lua_lib, + const char *meta); + +/* + * Passing objects from and to Lua. + */ struct lua_State; struct activity_interrupt_data; @@ -14,9 +33,52 @@ void clua_push_coord(lua_State *ls, const coord_def &c); class dgn_event; void clua_push_dgn_event(lua_State *ls, const dgn_event *devent); -// XXX: currently defined outside cluautil.cc. +// XXX: These are currently defined outside cluautil.cc. class monsters; void push_monster(lua_State *ls, monsters* mons); void lua_push_items(lua_State *ls, int link); +dungeon_feature_type check_lua_feature(lua_State *ls, int idx); +item_def *clua_check_item(lua_State *ls, int n); +unsigned int get_tile_idx(lua_State *ls, int arg); +level_id dlua_level_id(lua_State *ls, int ndx); + + +#define GETCOORD(c, p1, p2, boundfn) \ + coord_def c; \ + c.x = luaL_checkint(ls, p1); \ + c.y = luaL_checkint(ls, p2); \ + if (!boundfn(c)) \ + luaL_error( \ + ls, \ + make_stringf("Point (%d,%d) is out of bounds", \ + c.x, c.y).c_str()); \ + else ; + + +#define COORDS(c, p1, p2) \ + GETCOORD(c, p1, p2, in_bounds) + +#define FEAT(f, pos) \ +dungeon_feature_type f = check_lua_feature(ls, pos) + +#define LUA_ITEM(name, n) \ + item_def *name = clua_check_item(ls, n); + +#define LEVEL(lev, br, pos) \ +const char *level_name = luaL_checkstring(ls, pos); \ +level_area_type lev = str_to_level_area_type(level_name); \ +if (lev == NUM_LEVEL_AREA_TYPES) \ +luaL_error(ls, "Expected level name"); \ +const char *branch_name = luaL_checkstring(ls, pos); \ +branch_type br = str_to_branch(branch_name); \ +if (lev == LEVEL_DUNGEON && br == NUM_BRANCHES) \ +luaL_error(ls, "Expected branch name"); + +#define MAP(ls, n, var) \ +map_def *var = *(map_def **) luaL_checkudata(ls, n, MAP_METATABLE) +#define DEVENT(ls, n, var) \ +dgn_event *var = *(dgn_event **) luaL_checkudata(ls, n, DEVENT_METATABLE) +#define MAPMARKER(ls, n, var) \ +map_marker *var = *(map_marker **) luaL_checkudata(ls, n, MAPMARK_METATABLE) #endif diff --git a/crawl-ref/source/dlua.cc b/crawl-ref/source/dlua.cc index a49f2e9fd6..5aae875edc 100644 --- a/crawl-ref/source/dlua.cc +++ b/crawl-ref/source/dlua.cc @@ -286,22 +286,6 @@ std::string dlua_chunk::get_chunk_prefix(const std::string &sorig) const return rewrite_chunk_prefix(sorig, true); } -void luaopen_setmeta(lua_State *ls, - const char *global, - const luaL_reg *lua_lib, - const char *meta) -{ - luaL_newmetatable(ls, meta); - lua_setglobal(ls, global); - - luaL_openlib(ls, global, lua_lib, 0); - - // Do .__index = - lua_pushstring(ls, "__index"); - lua_pushvalue(ls, -2); - lua_settable(ls, -3); -} - void init_dungeon_lua() { lua_stack_cleaner clean(dlua); diff --git a/crawl-ref/source/dlua.h b/crawl-ref/source/dlua.h index 7dc8c1bbb1..fcc1978604 100644 --- a/crawl-ref/source/dlua.h +++ b/crawl-ref/source/dlua.h @@ -95,13 +95,6 @@ static void dlua_push_object_type(lua_State *ls, const char *meta, const T &data void print_dlua_stack(); -void luaopen_setmeta(lua_State *ls, - const char *global, - const luaL_reg *lua_lib, - const char *meta); - -#define LUAFN(name) static int name(lua_State *ls) - ////////////////////////////////////////////////////////////////////////// #endif diff --git a/crawl-ref/source/l_crawl.cc b/crawl-ref/source/l_crawl.cc index 12ad1e682f..0f9ef6bd31 100644 --- a/crawl-ref/source/l_crawl.cc +++ b/crawl-ref/source/l_crawl.cc @@ -5,8 +5,8 @@ #include "AppHdr.h" -#include "clua.h" #include "dlua.h" +#include "cluautil.h" #include "l_libs.h" #include "cio.h" diff --git a/crawl-ref/source/l_debug.cc b/crawl-ref/source/l_debug.cc index d305ff8c08..810970dddd 100644 --- a/crawl-ref/source/l_debug.cc +++ b/crawl-ref/source/l_debug.cc @@ -5,7 +5,7 @@ #include "AppHdr.h" -#include "dlua.h" +#include "cluautil.h" #include "l_libs.h" #include "chardump.h" diff --git a/crawl-ref/source/l_dgn_bf.cc b/crawl-ref/source/l_dgn_bf.cc index e79e6799e6..c7c633f0ea 100644 --- a/crawl-ref/source/l_dgn_bf.cc +++ b/crawl-ref/source/l_dgn_bf.cc @@ -5,7 +5,7 @@ #include "AppHdr.h" -#include "dlua.h" +#include "cluautil.h" #include "l_libs.h" #include "mapdef.h" diff --git a/crawl-ref/source/l_dgnbld.cc b/crawl-ref/source/l_dgnbld.cc index 634fea0d47..7f84b5db1b 100644 --- a/crawl-ref/source/l_dgnbld.cc +++ b/crawl-ref/source/l_dgnbld.cc @@ -7,7 +7,7 @@ #include -#include "dlua.h" +#include "cluautil.h" #include "l_libs.h" #include "dungeon.h" diff --git a/crawl-ref/source/l_dgnevt.cc b/crawl-ref/source/l_dgnevt.cc index f776f5c402..c379bad317 100644 --- a/crawl-ref/source/l_dgnevt.cc +++ b/crawl-ref/source/l_dgnevt.cc @@ -1,6 +1,6 @@ #include "AppHdr.h" -#include "dlua.h" +#include "cluautil.h" #include "l_libs.h" #include "dgnevent.h" diff --git a/crawl-ref/source/l_dgngrd.cc b/crawl-ref/source/l_dgngrd.cc index f85f7ed305..d9c953c5fb 100644 --- a/crawl-ref/source/l_dgngrd.cc +++ b/crawl-ref/source/l_dgngrd.cc @@ -5,7 +5,8 @@ #include "AppHdr.h" -#include "dlua.h" +#include "clua.h" +#include "cluautil.h" #include "l_libs.h" #include "directn.h" diff --git a/crawl-ref/source/l_dgnlvl.cc b/crawl-ref/source/l_dgnlvl.cc index 9109b9ae2c..c09a86023a 100644 --- a/crawl-ref/source/l_dgnlvl.cc +++ b/crawl-ref/source/l_dgnlvl.cc @@ -5,7 +5,7 @@ #include "AppHdr.h" -#include "dlua.h" +#include "cluautil.h" #include "l_libs.h" #include "branch.h" diff --git a/crawl-ref/source/l_dgntil.cc b/crawl-ref/source/l_dgntil.cc index 94b6b685e3..cc77e16c3c 100644 --- a/crawl-ref/source/l_dgntil.cc +++ b/crawl-ref/source/l_dgntil.cc @@ -5,7 +5,7 @@ #include "AppHdr.h" -#include "dlua.h" +#include "cluautil.h" #include "l_libs.h" #include "branch.h" diff --git a/crawl-ref/source/l_food.cc b/crawl-ref/source/l_food.cc index 0855b4bad0..90fc6e1d72 100644 --- a/crawl-ref/source/l_food.cc +++ b/crawl-ref/source/l_food.cc @@ -1,6 +1,6 @@ #include "AppHdr.h" -#include "clua.h" +#include "cluautil.h" #include "l_libs.h" #include "food.h" diff --git a/crawl-ref/source/l_libs.h b/crawl-ref/source/l_libs.h index 705ff65aae..9913a5748e 100644 --- a/crawl-ref/source/l_libs.h +++ b/crawl-ref/source/l_libs.h @@ -35,7 +35,7 @@ void cluaopen_globals(lua_State *ls); /* * Libraries and loaders for dlua, accessed from init_dungeon_lua(). - * TODO: Rename these to dluaopen_*. + * TODO: Rename these to dluaopen_*? */ extern const struct luaL_reg debug_dlib[]; @@ -63,48 +63,6 @@ void dluaopen_file(lua_State *ls); void dluaopen_mapgrd(lua_State *ls); void dluaopen_you(lua_State *ls); -/* - * Macros for processing object arguments. - * TODO: Collect these in cluautil.{h,cc}. - */ -#define GETCOORD(c, p1, p2, boundfn) \ - coord_def c; \ - c.x = luaL_checkint(ls, p1); \ - c.y = luaL_checkint(ls, p2); \ - if (!boundfn(c)) \ - luaL_error( \ - ls, \ - make_stringf("Point (%d,%d) is out of bounds", \ - c.x, c.y).c_str()); \ - else ; - - -#define COORDS(c, p1, p2) \ - GETCOORD(c, p1, p2, in_bounds) - -#define FEAT(f, pos) \ -dungeon_feature_type f = check_lua_feature(ls, pos) - -#define LUA_ITEM(name, n) \ - item_def *name = clua_check_item(ls, n); - -#define LEVEL(lev, br, pos) \ -const char *level_name = luaL_checkstring(ls, pos); \ -level_area_type lev = str_to_level_area_type(level_name); \ -if (lev == NUM_LEVEL_AREA_TYPES) \ -luaL_error(ls, "Expected level name"); \ -const char *branch_name = luaL_checkstring(ls, pos); \ -branch_type br = str_to_branch(branch_name); \ -if (lev == LEVEL_DUNGEON && br == NUM_BRANCHES) \ -luaL_error(ls, "Expected branch name"); - -#define MAP(ls, n, var) \ -map_def *var = *(map_def **) luaL_checkudata(ls, n, MAP_METATABLE) -#define DEVENT(ls, n, var) \ -dgn_event *var = *(dgn_event **) luaL_checkudata(ls, n, DEVENT_METATABLE) -#define MAPMARKER(ls, n, var) \ -map_marker *var = *(map_marker **) luaL_checkudata(ls, n, MAPMARK_METATABLE) - /* * Some shared helper functions. @@ -112,9 +70,5 @@ map_marker *var = *(map_marker **) luaL_checkudata(ls, n, MAPMARK_METATABLE) class map_lines; int dgn_map_add_transform(lua_State *ls, std::string (map_lines::*add)(const std::string &s)); -unsigned int get_tile_idx(lua_State *ls, int arg); -level_id dlua_level_id(lua_State *ls, int ndx); -dungeon_feature_type check_lua_feature(lua_State *ls, int idx); -item_def *clua_check_item(lua_State *ls, int n); #endif diff --git a/crawl-ref/source/l_los.cc b/crawl-ref/source/l_los.cc index 5c841fc50d..a3d6141a3b 100644 --- a/crawl-ref/source/l_los.cc +++ b/crawl-ref/source/l_los.cc @@ -6,9 +6,9 @@ #include "AppHdr.h" -#include "dlua.h" #include "l_libs.h" +#include "cluautil.h" #include "los.h" #include "ray.h" #include "stuff.h" diff --git a/crawl-ref/source/l_mapmrk.cc b/crawl-ref/source/l_mapmrk.cc index 2e25a2ff2b..8165babe85 100644 --- a/crawl-ref/source/l_mapmrk.cc +++ b/crawl-ref/source/l_mapmrk.cc @@ -1,6 +1,6 @@ #include "AppHdr.h" -#include "dlua.h" +#include "cluautil.h" #include "l_libs.h" #include "mapmark.h" diff --git a/crawl-ref/source/l_you.cc b/crawl-ref/source/l_you.cc index 5718402139..1327c49a23 100644 --- a/crawl-ref/source/l_you.cc +++ b/crawl-ref/source/l_you.cc @@ -1,6 +1,6 @@ #include "AppHdr.h" -#include "dlua.h" +#include "cluautil.h" #include "l_libs.h" #include "abl-show.h" -- cgit v1.2.3-54-g00ecf