Freeciv-3.2
Loading...
Searching...
No Matches
luascript.h
Go to the documentation of this file.
1/*****************************************************************************
2 Freeciv - Copyright (C) 2005 - The Freeciv Project
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12*****************************************************************************/
13#ifndef FC__LUASCRIPT_H
14#define FC__LUASCRIPT_H
15
16#ifdef __cplusplus
17extern "C" {
18#endif /* __cplusplus */
19
20/* dependencies/tolua */
21#include "tolua.h"
22
23/* utility */
24#include "log.h"
25#include "support.h" /* fc__attribute() */
26
27/* common/scriptcore */
28#include "luascript_types.h"
29#include "luascript_func.h"
30#include "luascript_signal.h"
31
32struct section_file;
36struct connection;
37struct fc_lua;
38
39typedef void (*luascript_log_func_t) (struct fc_lua *fcl,
40 enum log_level level,
41 const char *format, ...)
43
44struct fc_lua {
46
48 /* This is needed for server 'lua' and 'luafile' commands. */
50
52
55};
56
57/* Error functions for lua scripts. */
58int luascript_error(lua_State *L, const char *format, ...)
61int luascript_arg_error(lua_State *L, int narg, const char *msg);
62
63/* Create / destroy a freeciv lua instance. */
68
70 const char *format, ...)
73 const char *format, va_list args);
74
75void luascript_push_args(struct fc_lua *fcl, int nargs,
78 int nreturns, enum api_types *preturn_types,
79 va_list args);
81
82int luascript_call(struct fc_lua *fcl, int narg, int nret, const char *code);
84 const char *name);
85int luascript_do_file(struct fc_lua *fcl, const char *filename);
86
87/* Callback invocation function. */
89 int nargs, enum api_types *parg_types,
90 va_list args);
91
93
94/* Load / save variables. */
96 const char *section);
98 const char *section);
99
101
102/* Returns additional arguments on failure. */
103#define LUASCRIPT_ASSERT_CAT(str1, str2) str1 ## str2
104
105/* Script assertion (for debugging only) */
106#ifdef FREECIV_DEBUG
107#define LUASCRIPT_ASSERT(L, check, ...) \
108 if (!(check)) { \
109 luascript_error(L, "in %s() [%s::%d]: the assertion '%s' failed.", \
110 __FUNCTION__, __FILE__, __FC_LINE__, #check); \
111 return LUASCRIPT_ASSERT_CAT(, __VA_ARGS__); \
112 }
113#else
114#define LUASCRIPT_ASSERT(check, ...)
115#endif
116
117#define LUASCRIPT_CHECK_STATE(L, ...) \
118 if (!L) { \
119 log_error("No lua state available"); \
120 return LUASCRIPT_ASSERT_CAT(, __VA_ARGS__); \
121 }
122
123/* script_error on failed check */
124#define LUASCRIPT_CHECK(L, check, msg, ...) \
125 if (!(check)) { \
126 luascript_error(L, msg); \
127 return LUASCRIPT_ASSERT_CAT(, __VA_ARGS__); \
128 }
129
130/* script_arg_error on failed check */
131#define LUASCRIPT_CHECK_ARG(L, check, narg, msg, ...) \
132 if (!(check)) { \
133 luascript_arg_error(L, narg, msg); \
134 return LUASCRIPT_ASSERT_CAT(, __VA_ARGS__); \
135 }
136
137/* script_arg_error on nil value */
138#define LUASCRIPT_CHECK_ARG_NIL(L, value, narg, type, ...) \
139 if ((value) == NULL) { \
140 luascript_arg_error(L, narg, "got 'nil', '" #type "' expected"); \
141 return LUASCRIPT_ASSERT_CAT(, __VA_ARGS__); \
142 }
143
144/* script_arg_error on nil value. The first argument is the lua state and the
145 * second is the pointer to self. */
146#define LUASCRIPT_CHECK_SELF(L, value, ...) \
147 if ((value) == NULL) { \
148 luascript_arg_error(L, 2, "got 'nil' for self"); \
149 return LUASCRIPT_ASSERT_CAT(, __VA_ARGS__); \
150 }
151
152#ifdef __cplusplus
153}
154#endif /* __cplusplus */
155
156#endif /* FC__LUASCRIPT_H */
#define str
Definition astring.c:76
char * incite_cost
Definition comments.c:75
const char * name
Definition inputfile.c:127
log_level
Definition log.h:28
void luascript_pop_returns(struct fc_lua *fcl, const char *func_name, int nreturns, enum api_types *preturn_types, va_list args)
Definition luascript.c:442
void luascript_push_args(struct fc_lua *fcl, int nargs, enum api_types *parg_types, va_list args)
Definition luascript.c:501
void luascript_log(struct fc_lua *fcl, enum log_level level, const char *format,...) fc__attribute((__format__(__printf__
bool luascript_check_function(struct fc_lua *fcl, const char *funcname)
Definition luascript.c:552
int luascript_error(lua_State *L, const char *format,...) fc__attribute((__format__(__printf__
int int luascript_error_vargs(lua_State *L, const char *format, va_list vargs)
Definition luascript.c:308
int luascript_arg_error(lua_State *L, int narg, const char *msg)
Definition luascript.c:324
bool luascript_callback_invoke(struct fc_lua *fcl, const char *callback_name, int nargs, enum api_types *parg_types, va_list args)
Definition luascript.c:657
void luascript_remove_exported_object(struct fc_lua *fcl, void *object)
Definition luascript.c:700
int luascript_do_string(struct fc_lua *fcl, const char *str, const char *name)
Definition luascript.c:617
int luascript_do_file(struct fc_lua *fcl, const char *filename)
Definition luascript.c:637
void(* luascript_log_func_t)(struct fc_lua *fcl, enum log_level level, const char *format,...) fc__attribute((__format__(__printf__
Definition luascript.h:39
void luascript_vars_load(struct fc_lua *fcl, struct section_file *file, const char *section)
Definition luascript.c:763
void void luascript_log_vargs(struct fc_lua *fcl, enum log_level level, const char *format, va_list args)
Definition luascript.c:422
const Direction * luascript_dir(enum direction8)
Definition luascript.c:789
struct fc_lua * luascript_new(luascript_log_func_t outputfct, bool secured_environment)
Definition luascript.c:332
void luascript_vars_save(struct fc_lua *fcl, struct section_file *file, const char *section)
Definition luascript.c:737
struct fc_lua * luascript_get_fcl(lua_State *L)
Definition luascript.c:366
int luascript_call(struct fc_lua *fcl, int narg, int nret, const char *code)
Definition luascript.c:578
void luascript_destroy(struct fc_lua *fcl)
Definition luascript.c:386
enum direction8 Direction
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
struct connection * caller
Definition luascript.h:49
struct luascript_signal_hash * signals
Definition luascript.h:53
struct luascript_signal_name_list * signal_names
Definition luascript.h:54
luascript_log_func_t output_fct
Definition luascript.h:47
struct luascript_func_hash * funcs
Definition luascript.h:51
lua_State * state
Definition luascript.h:45
#define fc__attribute(x)
Definition support.h:99