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