Freeciv-3.2: /fast/freeciv/nightly/S3_2/common/scriptcore/luascript.h Source File

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;
33struct luascript_func_hash;
34struct luascript_signal_hash;
35struct luascript_signal_name_list;
36struct connection;
37struct fc_lua;
38
39 typedef void (*luascript_log_func_t) (struct fc_lua *fcl,
40 enum log_level level,
41 const char *format, ...)
42 fc__attribute((__format__ (__printf__, 3, 4)));
43
44 struct fc_lua {
45 lua_State *state;
46
47 luascript_log_func_t output_fct;
48 /* This is needed for server 'lua' and 'luafile' commands. */
49 struct connection *caller;
50
51 struct luascript_func_hash *funcs;
52
53 struct luascript_signal_hash *signals;
54 struct luascript_signal_name_list *signal_names;
55};
56
57/* Error functions for lua scripts. */
58 int luascript_error(lua_State *L, const char *format, ...)
59 fc__attribute((__format__ (__printf__, 2, 3)));
60int luascript_error_vargs(lua_State *L, const char *format, va_list vargs);
61int luascript_arg_error(lua_State *L, int narg, const char *msg);
62
63/* Create / destroy a freeciv lua instance. */
64 struct fc_lua *luascript_new(luascript_log_func_t outputfct,
65 bool secured_environment);
66 struct fc_lua *luascript_get_fcl(lua_State *L);
67void luascript_destroy(struct fc_lua *fcl);
68
69 void luascript_log(struct fc_lua *fcl, enum log_level level,
70 const char *format, ...)
71 fc__attribute((__format__ (__printf__, 3, 4)));
72void luascript_log_vargs(struct fc_lua *fcl, enum log_level level,
73 const char *format, va_list args);
74
75void luascript_push_args(struct fc_lua *fcl, int nargs,
76 enum api_types *parg_types, va_list args);
77void luascript_pop_returns(struct fc_lua *fcl, const char *func_name,
78 int nreturns, enum api_types *preturn_types,
79 va_list args);
80bool luascript_check_function(struct fc_lua *fcl, const char *funcname);
81
82int luascript_call(struct fc_lua *fcl, int narg, int nret, const char *code);
83int luascript_do_string(struct fc_lua *fcl, const char *str,
84 const char *name);
85int luascript_do_file(struct fc_lua *fcl, const char *filename);
86
87/* Callback invocation function. */
88bool luascript_callback_invoke(struct fc_lua *fcl, const char *callback_name,
89 int nargs, enum api_types *parg_types,
90 va_list args);
91
92void luascript_remove_exported_object(struct fc_lua *fcl, void *object);
93
94/* Load / save variables. */
95void luascript_vars_save(struct fc_lua *fcl, struct section_file *file,
96 const char *section);
97void luascript_vars_load(struct fc_lua *fcl, struct section_file *file,
98 const char *section);
99
100 const Direction *luascript_dir(enum direction8);
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 */
str
#define str
Definition astring.c:76
incite_cost
char * incite_cost
Definition comments.c:74
name
const char * name
Definition inputfile.c:127
log.h
log_level
log_level
Definition log.h:28
luascript_pop_returns
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:443
luascript_push_args
void luascript_push_args(struct fc_lua *fcl, int nargs, enum api_types *parg_types, va_list args)
Definition luascript.c:502
luascript_log
void luascript_log(struct fc_lua *fcl, enum log_level level, const char *format,...) fc__attribute((__format__(__printf__
luascript_check_function
bool luascript_check_function(struct fc_lua *fcl, const char *funcname)
Definition luascript.c:553
luascript_error
int luascript_error(lua_State *L, const char *format,...) fc__attribute((__format__(__printf__
luascript_error_vargs
int int luascript_error_vargs(lua_State *L, const char *format, va_list vargs)
Definition luascript.c:309
luascript_arg_error
int luascript_arg_error(lua_State *L, int narg, const char *msg)
Definition luascript.c:325
luascript_callback_invoke
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:658
luascript_remove_exported_object
void luascript_remove_exported_object(struct fc_lua *fcl, void *object)
Definition luascript.c:701
luascript_do_string
int luascript_do_string(struct fc_lua *fcl, const char *str, const char *name)
Definition luascript.c:618
luascript_do_file
int luascript_do_file(struct fc_lua *fcl, const char *filename)
Definition luascript.c:638
luascript_log_func_t
void(* luascript_log_func_t)(struct fc_lua *fcl, enum log_level level, const char *format,...) fc__attribute((__format__(__printf__
Definition luascript.h:39
luascript_vars_load
void luascript_vars_load(struct fc_lua *fcl, struct section_file *file, const char *section)
Definition luascript.c:764
luascript_log_vargs
void void luascript_log_vargs(struct fc_lua *fcl, enum log_level level, const char *format, va_list args)
Definition luascript.c:423
luascript_dir
const Direction * luascript_dir(enum direction8)
Definition luascript.c:790
luascript_new
struct fc_lua * luascript_new(luascript_log_func_t outputfct, bool secured_environment)
Definition luascript.c:333
luascript_vars_save
void luascript_vars_save(struct fc_lua *fcl, struct section_file *file, const char *section)
Definition luascript.c:738
luascript_get_fcl
struct fc_lua * luascript_get_fcl(lua_State *L)
Definition luascript.c:367
luascript_call
int luascript_call(struct fc_lua *fcl, int narg, int nret, const char *code)
Definition luascript.c:579
luascript_destroy
void luascript_destroy(struct fc_lua *fcl)
Definition luascript.c:387
luascript_func.h
luascript_signal.h
luascript_types.h
Direction
enum direction8 Direction
Definition luascript_types.h:59
level
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
connection
Definition connection.h:136
fc_lua
Definition luascript.h:44
fc_lua::caller
struct connection * caller
Definition luascript.h:49
fc_lua::signals
struct luascript_signal_hash * signals
Definition luascript.h:53
fc_lua::signal_names
struct luascript_signal_name_list * signal_names
Definition luascript.h:54
fc_lua::output_fct
luascript_log_func_t output_fct
Definition luascript.h:47
fc_lua::funcs
struct luascript_func_hash * funcs
Definition luascript.h:51
fc_lua::state
lua_State * state
Definition luascript.h:45
section_file
Definition section_file.h:36
section
Definition section_file.h:28
support.h
fc__attribute
#define fc__attribute(x)
Definition support.h:99