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-11 23:05:10 +00:00
|
|
|
#include <lua.hpp>
|
2013-12-09 22:56:48 +00:00
|
|
|
#include "util/thread.h"
|
2013-11-27 23:40:37 +00:00
|
|
|
#include "util/unlock_guard.h"
|
2013-11-07 21:56:04 +00:00
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
/**
|
|
|
|
\brief Wrapper for lua_State objects which contains all Lean bindings.
|
|
|
|
*/
|
2013-11-27 22:57:33 +00:00
|
|
|
class script_state {
|
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-27 22:57:33 +00:00
|
|
|
friend script_state to_script_state(lua_State * L);
|
2013-12-09 22:56:48 +00:00
|
|
|
mutex & get_mutex();
|
2013-11-27 17:25:56 +00:00
|
|
|
lua_State * get_state();
|
2013-11-27 23:14:26 +00:00
|
|
|
friend class data_channel;
|
2013-11-07 21:56:04 +00:00
|
|
|
public:
|
2013-12-04 17:57:33 +00:00
|
|
|
static void set_check_interrupt_freq(unsigned count);
|
|
|
|
|
2013-11-30 08:44:31 +00:00
|
|
|
typedef std::weak_ptr<imp> weak_ref;
|
|
|
|
|
2013-11-27 22:57:33 +00:00
|
|
|
script_state();
|
2013-11-30 08:44:31 +00:00
|
|
|
script_state(weak_ref const & r);
|
2013-11-27 23:40:37 +00:00
|
|
|
~script_state();
|
2013-11-07 21:56:04 +00:00
|
|
|
|
2013-11-30 08:44:31 +00:00
|
|
|
weak_ref to_weak_ref() const { return weak_ref(m_ptr); }
|
|
|
|
|
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-27 23:40:37 +00:00
|
|
|
void dostring(char const * str);
|
2013-11-11 23:05:10 +00:00
|
|
|
|
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) {
|
2013-12-09 22:56:48 +00:00
|
|
|
lock_guard<mutex> lock(get_mutex());
|
2013-11-27 17:25:56 +00:00
|
|
|
return f(get_state());
|
|
|
|
}
|
|
|
|
|
2013-11-27 22:57:33 +00:00
|
|
|
typedef void (*reg_fn)(lua_State *); // NOLINT
|
|
|
|
static void register_module(reg_fn f);
|
|
|
|
|
|
|
|
static void register_code(char const * code);
|
2013-11-27 23:40:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Auxiliary function for writing API bindings
|
|
|
|
that release the lock to this object while executing
|
|
|
|
\c f.
|
|
|
|
*/
|
|
|
|
template<typename F>
|
|
|
|
void exec_unprotected(F && f) {
|
|
|
|
unlock_guard unlock(get_mutex());
|
|
|
|
f();
|
|
|
|
}
|
2013-11-28 01:47:29 +00:00
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
void exec_protected(F && f) {
|
2013-12-09 22:56:48 +00:00
|
|
|
lock_guard<mutex> lock(get_mutex());
|
2013-11-28 01:47:29 +00:00
|
|
|
f();
|
|
|
|
}
|
2013-11-07 21:56:04 +00:00
|
|
|
};
|
2013-11-15 23:55:15 +00:00
|
|
|
/**
|
2013-11-27 22:57:33 +00:00
|
|
|
\brief Return a reference to the script_state object that is wrapping \c L.
|
2013-11-15 23:55:15 +00:00
|
|
|
*/
|
2013-11-27 22:57:33 +00:00
|
|
|
script_state to_script_state(lua_State * L);
|
2013-11-07 21:56:04 +00:00
|
|
|
}
|