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;
72  faudes::ConsoleOut::G()->Mute(false);
73  faudes::ConsoleOut::G()->Write(line.str());
75  // still do loop callback (the below note on hooks also applies here)
76  try{
77  LoopCallback();
78  } catch(...) {
79  lua_pushstring(L,"break on application request");
80  lua_error(L);
81  }
82  return 0;
83 }
84 
85 // register pring with Lua
86 void faudes_print_register(lua_State* L) {
87  lua_pushstring(L, "print");
88  lua_pushcfunction(L, faudes_print);
89  lua_rawset(L, LUA_GLOBALSINDEX);
90 }
91 
92 // Lua line hook to call faudes LoopCallback()
93 void faudes_hook(lua_State *L, lua_Debug *ar){
94  if(ar->event != LUA_HOOKLINE) return;
95  // note: this resides within lua, no SWIG interface, so we must not throw
96  // exceptions (this would set the lua interpreter to an inconsistent state);
97  // thus, we must catch the exception and convert to a lua error; when e.g. the hook
98  // was during a faudes::LuaFunction, DoExceuteC() will sense the error and then
99  // throw the faudes excpetion
100  try{
101  LoopCallback();
102  } catch(...) {
103  lua_pushstring(L,"break on application request");
104  lua_error(L);
105  }
106 }
107 
108 // register LoopCallback() as Lua hook
109 void faudes_hook_register(lua_State* L) {
110  lua_sethook(L, &faudes_hook, LUA_MASKLINE | LUA_MASKCOUNT, 10);
111 }
112 
113 /*
114 **************************************************
115 **************************************************
116 **************************************************
117 
118 Extension loader interface
119 
120 **************************************************
121 **************************************************
122 **************************************************
123 */
124 
125 
126 
127 // lua style interface to initialize lua state
128 void faudes_initialize(lua_State* pL) {
130 }
131 
132 // lua style interface to install extensions
133 int faudes_loadext(lua_State* pL, const char* filename) {
134  try{
135  LuaFunctionDefinition::Register(std::string(filename));
136  LuaState::Install(pL,std::string(filename));
137  } catch(const Exception& except) {
138  return 1;
139  }
140  return 0;
141 }
142 
143 // lua style interface to load default extension
144 int faudes_loaddefext(lua_State* pL, const char* arg0) {
145  std::string flxfile= std::string(arg0)+".flx";
146  if(!FileExists(flxfile)) return 1;
147  try{
148  LuaState::Install(pL,flxfile);
149  } catch(const Exception& except) {
150  return 1;
151  }
152  return 0;
153 }
154 
155 // lua/libreadline style interface to completer
156 char **faudes_complete(lua_State* pL, const char *text, int start, int end) {
157  std::string word(text,end-start);
158  std::list< std::string > mlist = LuaState::Complete(pL,word);
159  if(mlist.size()==0) return NULL;
160  char** res = (char**) malloc(sizeof(char *) * (mlist.size() + 1));
161  if(!res) return (char**) NULL;
162  std::list< std::string >::iterator lit;
163  char** dst=res;
164  for(lit=mlist.begin(); lit!=mlist.end(); lit++)
165  *(dst++)=strdup(lit->c_str());
166  *dst=NULL;
167  return res;
168 }
169 
170 
171 
172 
173 } //namespace
static ConsoleOut * G(void)
Acess static instance.
Definition: cfl_helper.cpp:403
virtual void Write(const std::string &message, long int cntnow=0, long int cntdone=0, int verb=0)
Write a std::string message (optional progress report and verbosity)
Definition: cfl_helper.cpp:422
void Mute(bool on)
Mute.
Definition: cfl_helper.h:337
Faudes exception class.
static void Register(const std::string &rFilename)
static void Initialize(lua_State *pLL)
Initialze.
void Install(const std::string &rFilename)
Install LuaExtension to Lua state.
std::list< std::string > Complete(const std::string &word)
Complete Lua identifier.
Includes all luabindings plug-in headers.
libFAUDES resides within the namespace faudes.
void LoopCallback(bool pBreak(void))
Definition: cfl_helper.cpp:621
void faudes_hook_register(lua_State *L)
Definition: lbp_addons.cpp:109
void faudes_hook(lua_State *L, lua_Debug *ar)
Definition: lbp_addons.cpp:93
int faudes_loadext(lua_State *pL, const char *filename)
Definition: lbp_addons.cpp:133
void faudes_initialize(lua_State *pL)
Definition: lbp_addons.cpp:128
char ** faudes_complete(lua_State *pL, const char *text, int start, int end)
Definition: lbp_addons.cpp:156
int faudes_loaddefext(lua_State *pL, const char *arg0)
Definition: lbp_addons.cpp:144
void faudes_print_register(lua_State *L)
Definition: lbp_addons.cpp:86
bool FileExists(const std::string &rFilename)
Test existence of file.
Definition: cfl_helper.cpp:373
int faudes_print(lua_State *L)
Definition: lbp_addons.cpp:49

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen