lbp_addons.cpp
Go to the documentation of this file.
1 /** @file lbp_addons.cpp addon function for Lua integration */
2 
3 /*
4 FAU Discrete Event Systems Library (libfaudes)
5 
6 Copyright (C) 2023, 2025 Thomas Moor
7 
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12 
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 
23 */
24 
25 
26 // my header
27 #include "lbp_addons.h"
28 
29 // all lua headers, incl Lua
30 #include "lbp_include.h"
31 
32 namespace faudes{
33 
34 
35 /*
36 **************************************************
37 **************************************************
38 **************************************************
39 
40 try to be version agnostic
41 
42 **************************************************
43 **************************************************
44 **************************************************
45 */
46 
47 
48 /*
49 **************************************************
50 **************************************************
51 **************************************************
52 
53 LoopCallback: have Lua-print do LoopCallback;
54 install LoopCallback as and Lua-line-hook
55 
56 **************************************************
57 **************************************************
58 **************************************************
59 */
60 
61 
62 // variation of luaB_print, programmatic registration
63 // this is still Lua 5.1.3, could be adapted for 5.4.8
64 int faudes_print(lua_State *L) {
65  int n,i,m;
66  // line buffer
67  std::ostringstream line;
68  // loop all args, use Lua's "tostring" for conversion
69  n = lua_gettop(L); // number of args
70  lua_getglobal(L, "tostring");
71  for(i=1; i<=n; i++) {
72  const char *s;
73  lua_pushvalue(L, -1); // push "tostring" fnct
74  lua_pushvalue(L, i); // push i-th arg
75  lua_call(L, 1, 1); // execute
76  s = lua_tostring(L, -1); // retrieve result string
77  if(s == NULL)
78  return luaL_error(L, LUA_QL("tostring") " must return a string to " LUA_QL("print"));
79  if(i>1) line << "\t";
80  line << s;
81  lua_pop(L, 1); // pop result string
82  }
83  // linefeed/flush/restore mute
84  line << std::endl;
85  faudes::ConsoleOut::G()->Write(line.str(),0,0,0); // verb 0 <> always
86  // still do loop callback (the below note on hooks also applies here)
87  try{
88  LoopCallback();
89  } catch(...) {
90  lua_pushstring(L,"break on application request");
91  lua_error(L);
92  }
93  return 0;
94 }
95 
96 // register pring with Lua
97 void faudes_print_register(lua_State* L) {
98  //lua_pushstring(L, "print");
99  //lua_pushcfunction(L, faudes_print);
100  //lua_rawset(L, LUA_GLOBALSINDEX);
101  lua_pushcfunction(L, faudes_print);
102  lua_setglobal(L, "print");
103 }
104 
105 // Lua line hook to call faudes LoopCallback()
106 void faudes_hook(lua_State *L, lua_Debug *ar){
107  if(ar->event != LUA_HOOKLINE) return;
108  // note: this resides within lua, no SWIG interface, so we must not throw
109  // exceptions (this would set the lua interpreter to an inconsistent state);
110  // thus, we must catch the exception and convert to a lua error; when e.g. the hook
111  // was during a faudes::LuaFunction, DoExceuteC() will sense the error and then
112  // throw the faudes excpetion
113  try{
114  LoopCallback();
115  } catch(...) {
116  lua_pushstring(L,"break on application request");
117  lua_error(L);
118  }
119 }
120 
121 // register LoopCallback() as Lua hook
122 void faudes_hook_register(lua_State* L) {
123  lua_sethook(L, &faudes_hook, LUA_MASKLINE | LUA_MASKCOUNT, 10);
124 }
125 
126 /*
127 **************************************************
128 **************************************************
129 **************************************************
130 
131 Extension loader interface
132 
133 **************************************************
134 **************************************************
135 **************************************************
136 */
137 
138 
139 
140 // lua style interface to initialize lua state
141 void faudes_initialize(lua_State* pL) {
143 }
144 
145 // lua style interface to install extensions
146 int faudes_loadext(lua_State* pL, const char* filename) {
147  try{
148  LuaFunctionDefinition::Register(std::string(filename));
149  LuaState::Install(pL,std::string(filename));
150  } catch(const Exception& except) {
151  return 1;
152  }
153  return 0;
154 }
155 
156 // lua style interface to load default extension
157 int faudes_loaddefext(lua_State* pL, const char* arg0) {
158  std::string flxfile= std::string(arg0)+".flx";
159  if(!FileExists(flxfile)) return 1;
160  try{
161  LuaState::Install(pL,flxfile);
162  } catch(const Exception& except) {
163  return 1;
164  }
165  return 0;
166 }
167 
168 // lua/libreadline style interface to completer
169 char **faudes_complete(lua_State* pL, const char *text, int start, int end) {
170  std::string word(text,end-start);
171  std::list< std::string > mlist = LuaState::Complete(pL,word);
172  if(mlist.size()==0) return NULL;
173  char** res = (char**) malloc(sizeof(char *) * (mlist.size() + 1));
174  if(!res) return (char**) NULL;
175  std::list< std::string >::iterator lit;
176  char** dst=res;
177  for(lit=mlist.begin(); lit!=mlist.end(); lit++)
178  *(dst++)=strdup(lit->c_str());
179  *dst=NULL;
180  return res;
181 }
182 
183 
184 
185 
186 } //namespace
static ConsoleOut * G(void)
Definition: cfl_utils.cpp:456
virtual void Write(const std::string &message, long int cntnow=0, long int cntdone=0, int verb=1)
Definition: cfl_utils.cpp:475
static void Register(const std::string &rFilename)
static void Initialize(lua_State *pLL)
void Install(const std::string &rFilename)
std::list< std::string > Complete(const std::string &word)
#define LUA_QL(x)
Definition: lbp_include.h:216
void LoopCallback(bool pBreak(void))
Definition: cfl_utils.cpp:693
void faudes_hook_register(lua_State *L)
Definition: lbp_addons.cpp:122
void faudes_hook(lua_State *L, lua_Debug *ar)
Definition: lbp_addons.cpp:106
int faudes_loadext(lua_State *pL, const char *filename)
Definition: lbp_addons.cpp:146
void faudes_initialize(lua_State *pL)
Definition: lbp_addons.cpp:141
char ** faudes_complete(lua_State *pL, const char *text, int start, int end)
Definition: lbp_addons.cpp:169
int faudes_loaddefext(lua_State *pL, const char *arg0)
Definition: lbp_addons.cpp:157
void faudes_print_register(lua_State *L)
Definition: lbp_addons.cpp:97
bool FileExists(const std::string &rFilename)
Definition: cfl_utils.cpp:426
int faudes_print(lua_State *L)
Definition: lbp_addons.cpp:64

libFAUDES 2.33l --- 2025.09.16 --- c++ api documentaion by doxygen