ReFRACtor
lua_state.cc
Go to the documentation of this file.
1 #include "lua_state.h"
2 #include "luabind_object.h"
3 #include "fp_exception.h"
4 #include "dir_change.h"
5 #include "fe_disable_exception.h"
6 using namespace FullPhysics;
7 
8 //-----------------------------------------------------------------------
10 //-----------------------------------------------------------------------
11 
13 {
14  luabind::object obj = luabind::globals(lua_state());
16  return LuabindObject(obj, ls);
17 }
18 
19 //-----------------------------------------------------------------------
21 //-----------------------------------------------------------------------
22 
24 {
25  luabind::object obj = luabind::registry(lua_state());
27  return LuabindObject(obj, ls);
28 }
29 
30 //-----------------------------------------------------------------------
36 //-----------------------------------------------------------------------
37 
39 LuaState::load_file(const std::string& Fname)
40 {
41  // Lua may cause floating point exceptions when loading the
42  // configuration file. This is because it may copy garbage value,
43  // which are never used. By chance, the garbage values may cause a
44  // overflow. We suspend floating point exceptions when loading.
45  FeDisableException disable_fp;
46  size_t t = Fname.find_last_of("/");
47  std::string dirbase;
48  std::string bname;
49  if(t != std::string::npos) {
50  dirbase = Fname.substr(0, t);
51  bname = Fname.substr(t + 1);
52  } else {
53  dirbase = ".";
54  bname = Fname;
55  }
56  dirbase += "/";
57  boost::shared_ptr<LuaState> ls(new LuaState(dirbase));
58  ls->do_file(bname);
59  return ls;
60 }
61 
62 //-----------------------------------------------------------------------
68 //-----------------------------------------------------------------------
69 
70 LuaState::LuaState(const std::string& Dir_name)
71 : lsimp(new LuaStateImp(luaL_newstate(), Dir_name))
72 {
73 
75  luaL_openlibs(lua_state());
76  luabind::open(lua_state());
78 }
79 
81 {
82  lua_close(ls);
83  ls = 0;
84 }
85 
86 //-----------------------------------------------------------------------
88 //-----------------------------------------------------------------------
89 
90 void LuaState::do_file(const std::string& Fname)
91 {
93  int status = luaL_dofile(lua_state(), Fname.c_str());
94  if(status != 0) {
95  // If we are here, then there is an error message on the stack
96  Exception e;
97  e << "Lua error: " << lua_tostring(lua_state(), -1) << "\n";
98  lua_pop(lua_state(), 1); // Remove error message from stack.
99  throw e;
100  }
101 }
102 
103 //-----------------------------------------------------------------------
105 //-----------------------------------------------------------------------
106 
107 void LuaState::run(const std::string& S)
108 {
110  int status = luaL_dostring(lua_state(), S.c_str());
111  if(status != 0) {
112  // If we are here, then there is an error message on the stack
113  Exception e;
114  e << "Lua error: " << lua_tostring(lua_state(), -1) << "\n";
115  lua_pop(lua_state(), 1); // Remove error message from stack.
116  throw e;
117  }
118 }
LuabindObject globals()
Globals table.
Definition: lua_state.cc:12
void run(const std::string &S)
Run the given Lua code.
Definition: lua_state.cc:107
const std::string & base_dir_name() const
Base directory that we use when running Lua.
Definition: lua_state.h:41
LuabindObject registry()
Registery table.
Definition: lua_state.cc:23
void do_file(const std::string &Fname)
Load the given file and execute it.
Definition: lua_state.cc:90
static void register_lua(lua_State *ls)
lua_State * lua_state()
Lua state pointer.
Definition: lua_state.h:47
This is the base of the exception hierarchy for Full Physics code.
Definition: fp_exception.h:16
To detect things like divide by zero, we may turn on floating point exceptions.
This is a light wrapper around the luabind::object.
LuaState(const std::string &Dir_name="./")
Create a new instance of Lua.
Definition: lua_state.cc:70
Contains classes to abstract away details in various Spurr Radiative Transfer software.
Definition: doxygen_python.h:1
Utility class.
Definition: dir_change.h:15
static boost::shared_ptr< LuaState > load_file(const std::string &Fname)
Create a new LuaState, and then open the given file.
Definition: lua_state.cc:39

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:10