lua/VirtualMachine.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
3 
4  This file is part of the TerraLib - a Framework for building GIS enabled
5  applications.
6 
7  TerraLib is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation, either version 3 of the License,
10  or (at your option) any later version.
11 
12  TerraLib is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with TerraLib. See COPYING. If not, write to
19  TerraLib Team at <terralib-team@terralib.org>.
20  */
21 
22 /*!
23  \file terralib/vm/lua/VirtualMachine.cpp
24 
25  \brief Implementation of a virtual machine for the Lua Programming Language.
26 
27  \author Frederico Augusto BedĂȘ
28  \author Gilberto Ribeiro de Queiroz
29  */
30 
31 // TerraLib
32 #include "VirtualMachine.h"
33 #include "../../core/translator/Translator.h"
34 #include "../core/Exception.h"
35 
36 // Lua
37 #include <QtLua/State>
38 #include <QtLua/TableTreeModel>
39 #include <QtLua/Console>
40 #include <lua.hpp>
41 
42 // QT
43 #include <QFile>
44 #include <QTextStream>
45 #include <QThread>
46 
47 // STL
48 #include <cstdio>
49 
50 void stdinHack(lua_State *L, FILE *in, FILE *out, FILE *err) {
51  FILE **pf;
52  lua_getglobal(L, "io"); /* Get the IO library */
53  lua_pushstring(L, "open");
54  lua_gettable(L, -2); /* io, io.open */
55  lua_getuservalue(L, -1); /* io, io.open, io.open's env */
56  lua_getglobal(L, "io"); /* io, open, env, io */
57 
58  lua_pushstring(L, "stdin");
59  lua_gettable(L, -2); /* io, open, env, io, io.stdin */
60  pf = (FILE **)lua_touserdata(L, -1);
61  *pf = in;
62  lua_rawseti(L, -3, 1); /* IO_INPUT = 1 */
63 
64  lua_pushstring(L, "stdout");
65  lua_gettable(L, -2); /* io, open, env, io, io.stdout */
66  pf = (FILE **)lua_touserdata(L, -1);
67  *pf = out;
68  lua_rawseti(L, -3, 2); /* IO_OUTPUT = 2 */
69 
70  lua_pushstring(L, "stderr");
71  lua_gettable(L, -2); /* io, open, env, io, io.stderr */
72  pf = (FILE **)lua_touserdata(L, -1);
73  *pf = err;
74  lua_pop(L, 5);
75 }
76 
78 {
79  std::unique_ptr<QtLua::State> state; //!< The Lua environment (or state).
80  FILE* m_stdin;
81  QString m_filename;
82  Impl() : state(nullptr)
83  {
84  m_stdin = tmpfile();
85  }
87  {
88 // fclose(m_stdin);
89  }
90 };
91 
93 {
94  m_pimpl.reset(new Impl);
95 }
96 
98 
100 {
101  m_pimpl->m_filename = file;
102 }
103 
105 {
106  m_pimpl->state.reset(new QtLua::State());
107  m_pimpl->state->openlib(QtLua::AllLibs);
108 // stdinHack(m_pimpl->state->get_lua_state(), m_pimpl->m_stdin, stdout, stderr);
109  connect(m_pimpl->state.get(), SIGNAL(output(const QString&)), this,
110  SLOT(write(const QString&)), Qt::UniqueConnection);
111  QFile script(m_pimpl->m_filename);
112  script.open(QIODevice::ReadOnly);
113  QTextStream in(&script);
114  m_pimpl->state->exec(in.readAll());
115 }
116 
117 void te::vm::lua::VirtualMachine::write(const QString& str)
118 {
119  emit output(str);
120  QThread::currentThread()->usleep(1);
121 }
122 
124 {
125 // msg = msg + '\0';
126 // if(m_pimpl->state)
127 // {
128 // std::fputs(msg.toUtf8().data(), m_pimpl->m_stdin);
129 // }
130 }
131 
133 {
134  return "";
135 }
136 
138 {
139 }
140 
142 {
143  return "Lua";
144 }
145 
147 {
148  std::string msg(
149  TE_TR("TerraLib Virtual Machine for the Lua Programming Language"));
150 
151  return msg;
152 }
153 
155 {
156  std::string msg(
157  TE_TR("This Virtual Machine allows applications to execute lua scripts."
158  "\nAll the TerraLib API will be available for TerraLib programmers "
159  "in the Lua environment"));
160 
161  return msg;
162 }
std::unique_ptr< QtLua::State > state
The Lua environment (or state).
void write(const QString &str)
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
std::unique_ptr< Impl > m_pimpl
void setFileName(QString file)
setFileName Sets the file that will be used in the Virtual Machine.
std::string getDescription() const
Returns a brief description about the underlying Virtual Machine.
void stdinHack(lua_State *L, FILE *in, FILE *out, FILE *err)
std::string getTitle() const
Return the title of Virtual Machine: a name that can be used by applications to show in a GUI...
Implementation of a virtual machine for the Lua Programming Language.
void output(QString msg)
output Signal emmited when the Virtual Machine receives a string.
~VirtualMachine()
Virtual destructor.
std::string readline()
readline Method to be used by the language of the Virtual Machine when doing a input operation...
file(WRITE ${CMAKE_BINARY_DIR}/config_qhelp.cmake"configure_file (${TERRALIB_ABSOLUTE_ROOT_DIR}/doc/qhelp/help.qhcp.in ${CMAKE_BINARY_DIR}/share/terraview/help/help.qhcp @ONLY)") add_custom_command(OUTPUT del_dir COMMAND $