2013-11-07 21:56:04 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
2013-11-08 19:59:47 +00:00
|
|
|
#pragma once
|
2013-11-07 21:56:04 +00:00
|
|
|
#include <memory>
|
2013-11-27 17:25:56 +00:00
|
|
|
#include <mutex>
|
2013-11-11 23:05:10 +00:00
|
|
|
#include <lua.hpp>
|
2013-11-27 03:15:49 +00:00
|
|
|
#include "util/lua_exception.h"
|
2013-11-15 20:13:03 +00:00
|
|
|
#include "library/script_evaluator.h"
|
2013-11-07 21:56:04 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2013-11-10 18:12:43 +00:00
|
|
|
class environment;
|
2013-11-21 23:51:29 +00:00
|
|
|
class io_state;
|
2013-11-07 21:56:04 +00:00
|
|
|
/**
|
|
|
|
\brief Wrapper for lua_State objects which contains all Lean bindings.
|
|
|
|
*/
|
2013-11-15 20:13:03 +00:00
|
|
|
class leanlua_state : public script_evaluator {
|
2013-11-15 23:55:15 +00:00
|
|
|
public:
|
2013-11-07 21:56:04 +00:00
|
|
|
struct imp;
|
2013-11-15 23:55:15 +00:00
|
|
|
private:
|
2013-11-07 21:56:04 +00:00
|
|
|
std::shared_ptr<imp> m_ptr;
|
2013-11-15 23:55:15 +00:00
|
|
|
leanlua_state(std::weak_ptr<imp> const & ptr);
|
|
|
|
friend leanlua_state to_leanlua_state(lua_State * L);
|
2013-11-27 17:25:56 +00:00
|
|
|
std::recursive_mutex & get_mutex();
|
|
|
|
lua_State * get_state();
|
2013-11-07 21:56:04 +00:00
|
|
|
public:
|
|
|
|
leanlua_state();
|
2013-11-15 20:13:03 +00:00
|
|
|
virtual ~leanlua_state();
|
2013-11-07 21:56:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Execute the file with the given name.
|
|
|
|
This method throws an exception if an error occurs.
|
|
|
|
*/
|
|
|
|
void dofile(char const * fname);
|
|
|
|
/**
|
|
|
|
\brief Execute the given string.
|
|
|
|
This method throws an exception if an error occurs.
|
|
|
|
*/
|
2013-11-15 20:13:03 +00:00
|
|
|
virtual void dostring(char const * str);
|
2013-11-11 23:05:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Execute the given script, but sets the registry with the given environment object.
|
|
|
|
The registry can be accessed by \str by invoking the function <tt>env()</tt>.
|
|
|
|
The script \c str should not store a reference to the environment \c env.
|
|
|
|
*/
|
2013-11-21 23:51:29 +00:00
|
|
|
virtual void dostring(char const * str, environment & env, io_state & st);
|
2013-11-27 17:25:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Execute \c f in the using the internal Lua State.
|
|
|
|
*/
|
|
|
|
template<typename F>
|
|
|
|
typename std::result_of<F(lua_State * L)>::type apply(F && f) {
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(get_mutex());
|
|
|
|
return f(get_state());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Similar to \c apply, but a lock is not used to guarantee
|
|
|
|
exclusive access to the lua_State object.
|
|
|
|
|
|
|
|
\warning It is the caller resposability to guarantee that the object is not being
|
|
|
|
concurrently accessed.
|
|
|
|
*/
|
|
|
|
template<typename F>
|
|
|
|
typename std::result_of<F(lua_State * L)>::type unguarded_apply(F && f) {
|
|
|
|
return f(get_state());
|
|
|
|
}
|
2013-11-07 21:56:04 +00:00
|
|
|
};
|
2013-11-15 23:55:15 +00:00
|
|
|
/**
|
|
|
|
\brief Return a reference to the leanlua_state object that is wrapping \c L.
|
|
|
|
*/
|
|
|
|
leanlua_state to_leanlua_state(lua_State * L);
|
2013-11-07 21:56:04 +00:00
|
|
|
}
|