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 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 LoopCallback: have Lua-print do LoopCallback;
41 install LoopCallback as and Lua-line-hook
42 
43 **************************************************
44 **************************************************
45 **************************************************
46 */
47 
48 // variation of luaB_print, programmatic registration
49 int faudes_print(lua_State *L) {
50  int n,i,m;
51  // line buffer
52  std::ostringstream line;
53  // loop all args, use Lua's "tostring" for conversion
54  n = lua_gettop(L); // number of args
55  lua_getglobal(L, "tostring");
56  for(i=1; i<=n; i++) {
57  const char *s;
58  lua_pushvalue(L, -1); // push "tostring" fnct
59  lua_pushvalue(L, i); // push i-th arg
60  lua_call(L, 1, 1); // execute
61  s = lua_tostring(L, -1); // retrieve result string
62  if(s == NULL)
63  return luaL_error(L, LUA_QL("tostring") " must return a string to "
64  LUA_QL("print"));
65  if(i>1) line << "\t";
66  line << s;
67  lua_pop(L, 1); // pop result string
68  }
69  // linefeed/flush/restore mute
70  line << std::endl;
71  faudes::ConsoleOut::G()->Write(line.str(),0,0,0); // verb 0 <> always
72  // still do loop callback (the below note on hooks also applies here)
73  try{
74  LoopCallback();
75  } catch(...) {
76  lua_pushstring(L,"break on application request");
77  lua_error(L);
78  }
79  return 0;
80 }
81 
82 // register pring with Lua
83 void faudes_print_register(lua_State* L) {
84  lua_pushstring(L, "print");
85  lua_pushcfunction(L, faudes_print);
86  lua_rawset(L, LUA_GLOBALSINDEX);
87 }
88 
89 // Lua line hook to call faudes LoopCallback()
90 void faudes_hook(lua_State *L, lua_Debug *ar){
91  if(ar->event != LUA_HOOKLINE) return;
92  // note: this resides within lua, no SWIG interface, so we must not throw
93  // exceptions (this would set the lua interpreter to an inconsistent state);
94  // thus, we must catch the exception and convert to a lua error; when e.g. the hook
95  // was during a faudes::LuaFunction, DoExceuteC() will sense the error and then
96  // throw the faudes excpetion
97  try{
98  LoopCallback();
99  } catch(...) {
100  lua_pushstring(L,"break on application request");
101  lua_error(L);
102  }
103 }
104 
105 // register LoopCallback() as Lua hook
106 void faudes_hook_register(lua_State* L) {
107  lua_sethook(L, &faudes_hook, LUA_MASKLINE | LUA_MASKCOUNT, 10);
108 }
109 
110 /*
111 **************************************************
112 **************************************************
113 **************************************************
114 
115 Extension loader interface
116 
117 **************************************************
118 **************************************************
119 **************************************************
120 */
121 
122 
123 
124 // lua style interface to initialize lua state
125 void faudes_initialize(lua_State* pL) {
127 }
128 
129 // lua style interface to install extensions
130 int faudes_loadext(lua_State* pL, const char* filename) {
131  try{
132  LuaFunctionDefinition::Register(std::string(filename));
133  LuaState::Install(pL,std::string(filename));
134  } catch(const Exception& except) {
135  return 1;
136  }
137  return 0;
138 }
139 
140 // lua style interface to load default extension
141 int faudes_loaddefext(lua_State* pL, const char* arg0) {
142  std::string flxfile= std::string(arg0)+".flx";
143  if(!FileExists(flxfile)) return 1;
144  try{
145  LuaState::Install(pL,flxfile);
146  } catch(const Exception& except) {
147  return 1;
148  }
149  return 0;
150 }
151 
152 // lua/libreadline style interface to completer
153 char **faudes_complete(lua_State* pL, const char *text, int start, int end) {
154  std::string word(text,end-start);
155  std::list< std::string > mlist = LuaState::Complete(pL,word);
156  if(mlist.size()==0) return NULL;
157  char** res = (char**) malloc(sizeof(char *) * (mlist.size() + 1));
158  if(!res) return (char**) NULL;
159  std::list< std::string >::iterator lit;
160  char** dst=res;
161  for(lit=mlist.begin(); lit!=mlist.end(); lit++)
162  *(dst++)=strdup(lit->c_str());
163  *dst=NULL;
164  return res;
165 }
166 
167 
168 
169 
170 } //namespace
static ConsoleOut * G(void)
Definition: cfl_utils.cpp:442
virtual void Write(const std::string &message, long int cntnow=0, long int cntdone=0, int verb=1)
Definition: cfl_utils.cpp:461
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)
void LoopCallback(bool pBreak(void))
Definition: cfl_utils.cpp:679
void faudes_hook_register(lua_State *L)
Definition: lbp_addons.cpp:106
void faudes_hook(lua_State *L, lua_Debug *ar)
Definition: lbp_addons.cpp:90
int faudes_loadext(lua_State *pL, const char *filename)
Definition: lbp_addons.cpp:130
void faudes_initialize(lua_State *pL)
Definition: lbp_addons.cpp:125
char ** faudes_complete(lua_State *pL, const char *text, int start, int end)
Definition: lbp_addons.cpp:153
int faudes_loaddefext(lua_State *pL, const char *arg0)
Definition: lbp_addons.cpp:141
void faudes_print_register(lua_State *L)
Definition: lbp_addons.cpp:83
bool FileExists(const std::string &rFilename)
Definition: cfl_utils.cpp:412
int faudes_print(lua_State *L)
Definition: lbp_addons.cpp:49

libFAUDES 2.33h --- 2025.06.18 --- c++ api documentaion by doxygen