lbp_addons.cpp
Go to the documentation of this file.
1/** @file lbp_addons.cpp addon function for Lua integration */
2
3/*
4FAU Discrete Event Systems Library (libfaudes)
5
6Copyright (C) 2023, 2025 Thomas Moor
7
8This library is free software; you can redistribute it and/or
9modify it under the terms of the GNU Lesser General Public
10License as published by the Free Software Foundation; either
11version 2.1 of the License, or (at your option) any later version.
12
13This library is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16Lesser General Public License for more details.
17
18You should have received a copy of the GNU Lesser General Public
19License along with this library; if not, write to the Free Software
20Foundation, 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
32namespace faudes{
33
34
35/*
36**************************************************
37**************************************************
38**************************************************
39
40try to be version agnostic
41
42**************************************************
43**************************************************
44**************************************************
45*/
46
47
48/*
49**************************************************
50**************************************************
51**************************************************
52
53LoopCallback: have Lua-print do LoopCallback;
54install 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
64int 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{
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
97void 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()
106void 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
122void faudes_hook_register(lua_State* L) {
123 lua_sethook(L, &faudes_hook, LUA_MASKLINE | LUA_MASKCOUNT, 10);
124}
125
126/*
127**************************************************
128**************************************************
129**************************************************
130
131Extension loader interface
132
133**************************************************
134**************************************************
135**************************************************
136*/
137
138
139
140// lua style interface to initialize lua state
141void faudes_initialize(lua_State* pL) {
143}
144
145// lua style interface to install extensions
146int 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
157int 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
169char **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)
virtual void Write(const std::string &message, long int cntnow=0, long int cntdone=0, int verb=1)
static void Register(const std::string &rFilename)
void Install(const std::string &rFilename)
static void Initialize(lua_State *pLL)
std::list< std::string > Complete(const std::string &word)
#define LUA_QL(x)
void faudes_hook_register(lua_State *L)
void faudes_hook(lua_State *L, lua_Debug *ar)
void LoopCallback(void)
int faudes_loadext(lua_State *pL, const char *filename)
void faudes_initialize(lua_State *pL)
char ** faudes_complete(lua_State *pL, const char *text, int start, int end)
int faudes_loaddefext(lua_State *pL, const char *arg0)
void faudes_print_register(lua_State *L)
bool FileExists(const std::string &rFilename)
int faudes_print(lua_State *L)

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