summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/luaengine.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/emu/luaengine.cpp')
-rw-r--r--src/emu/luaengine.cpp60
1 files changed, 37 insertions, 23 deletions
diff --git a/src/emu/luaengine.cpp b/src/emu/luaengine.cpp
index 8e1ddbf8d93..34b78ba2890 100644
--- a/src/emu/luaengine.cpp
+++ b/src/emu/luaengine.cpp
@@ -1720,33 +1720,35 @@ lua_engine::~lua_engine()
close();
}
-int lua_engine::compile_with_env(const char *env, const char *script)
+int lua_engine::compile_with_env(const char *envname, const char *script, const char *env)
{
- std::string field = std::string("env_").append(env);
+ std::string field = std::string("env_").append(envname);
+ int error;
lua_settop(m_lua_state, 0);
lua_getfield(m_lua_state, LUA_REGISTRYINDEX, field.c_str());
if(!lua_istable(m_lua_state, -1))
{
- emu_file file(m_machine->manager().options().plugins_path(), OPEN_FLAG_READ);
- // optionally load a script to prepare the environment
- if(file.open(env, ".lua") != osd_file::error::NONE)
+ lua_newtable(m_lua_state);
+ lua_setfield(m_lua_state, LUA_REGISTRYINDEX, field.c_str());
+ lua_getfield(m_lua_state, LUA_REGISTRYINDEX, field.c_str());
+ }
+
+ // optionally load a string to prepare the environment
+ if(env)
+ {
+ error = luaL_loadstring(m_lua_state, env);
+ lua_pushvalue(m_lua_state, -2);
+ if((error != LUA_OK) || ((error = lua_pcall(m_lua_state, 1, 0, 0)) != LUA_OK))
{
- int error = luaL_loadfile(m_lua_state, file.fullpath());
- if(error || (error = lua_pcall(m_lua_state, 0, 0, 0) != LUA_OK))
- {
- if(error == LUA_ERRRUN)
- printf("%s\n", lua_tostring(m_lua_state, -1));
- lua_pop(m_lua_state, 1);
- }
+ if((error == LUA_ERRSYNTAX) || (error == LUA_ERRRUN))
+ printf("%s\n", lua_tostring(m_lua_state, -1));
+ lua_pop(m_lua_state, 1);
}
- if(!lua_istable(m_lua_state, -1))
- lua_newtable(m_lua_state);
- lua_setfield(m_lua_state, LUA_REGISTRYINDEX, field.c_str());
lua_getfield(m_lua_state, LUA_REGISTRYINDEX, field.c_str());
}
- if(int error = luaL_loadstring(m_lua_state, script) != LUA_OK)
+ if((error = luaL_loadstring(m_lua_state, script)) != LUA_OK)
{
if(error == LUA_ERRSYNTAX)
printf("%s\n", lua_tostring(m_lua_state, -1));
@@ -1801,6 +1803,8 @@ void lua_engine::run(const char *env, int ref)
run_internal(env, ref);
lua_pop(m_lua_state, 1);
}
+// create specialization so luabridge doesn't have to be included everywhere
+template int lua_engine::run<int>(const char *env, int ref);
void lua_engine::run_internal(const char *env, int ref)
{
@@ -1812,7 +1816,8 @@ void lua_engine::run_internal(const char *env, int ref)
if(lua_isfunction(m_lua_state, -1))
{
lua_pushvalue(m_lua_state, -3);
- if(int error = lua_pcall(m_lua_state, 1, 1, 0) != LUA_OK)
+ int error;
+ if((error = lua_pcall(m_lua_state, 1, 1, 0)) != LUA_OK)
{
if(error == LUA_ERRRUN)
printf("%s\n", lua_tostring(m_lua_state, -1));
@@ -1824,6 +1829,7 @@ void lua_engine::run_internal(const char *env, int ref)
lua_replace(m_lua_state, 1);
lua_pop(m_lua_state, 1);
}
+
void lua_engine::menu_populate(std::string &menu, std::vector<menu_item> &menu_list)
{
std::string field = "menu_pop_" + menu;
@@ -1835,7 +1841,8 @@ void lua_engine::menu_populate(std::string &menu, std::vector<menu_item> &menu_l
lua_pop(m_lua_state, 1);
return;
}
- if(int error = lua_pcall(m_lua_state, 0, 1, 0) != LUA_OK)
+ int error;
+ if((error = lua_pcall(m_lua_state, 0, 1, 0)) != LUA_OK)
{
if(error == LUA_ERRRUN)
printf("%s\n", lua_tostring(m_lua_state, -1));
@@ -1881,7 +1888,8 @@ bool lua_engine::menu_callback(std::string &menu, int index, std::string event)
{
lua_pushinteger(m_lua_state, index);
lua_pushstring(m_lua_state, event.c_str());
- if(int error = lua_pcall(m_lua_state, 2, 1, 0))
+ int error;
+ if((error = lua_pcall(m_lua_state, 2, 1, 0)) != LUA_OK)
{
if(error == 2)
printf("%s\n", lua_tostring(m_lua_state, -1));
@@ -1922,7 +1930,8 @@ void lua_engine::execute_function(const char *id)
{
if (lua_isfunction(m_lua_state, -1))
{
- if(int error = lua_pcall(m_lua_state, 0, 0, 0))
+ int error;
+ if((error = lua_pcall(m_lua_state, 0, 0, 0)) != LUA_OK)
{
if(error == 2)
printf("%s\n", lua_tostring(m_lua_state, -1));
@@ -2062,8 +2071,10 @@ void lua_engine::attach_notifiers()
int lua_engine::lua_machine::l_popmessage(lua_State *L)
{
running_machine *m = luabridge::Stack<running_machine *>::get(L, 1);
- luaL_argcheck(L, lua_isstring(L, 2), 2, "message (string) expected");
- m->popmessage("%s", luaL_checkstring(L, 2));
+ if(!lua_isstring(L, 2))
+ m->popmessage();
+ else
+ m->popmessage("%s", luaL_checkstring(L, 2));
return 0;
}
@@ -2245,6 +2256,9 @@ void lua_engine::initialize()
.addFunction ("tag", &ioport_port::tag)
.addFunction ("active", &ioport_port::active)
.addFunction ("live", &ioport_port::live)
+ .addFunction ("read", &ioport_port::read)
+ .addFunction ("write", &ioport_port::write)
+ .addFunction ("field", &ioport_port::field)
.addProperty <luabridge::LuaRef, void> ("fields", &lua_engine::l_ioports_port_get_fields)
.endClass()
.beginClass <ioport_field> ("ioport_field")
@@ -2466,7 +2480,7 @@ void lua_engine::initialize()
.addCFunction ("read", &lua_emu_file::l_emu_file_read)
.endClass()
// make sure there's a reference to the emu_file search path in your script as long as you need it
- // otherwise it might be garbage collected as emu_file don't copy the string
+ // otherwise it might be garbage collected as emu_file doesn't copy the string
.deriveClass <emu_file, lua_emu_file> ("file")
.addConstructor <void (*)(const char *, UINT32)> ()
.addFunction ("open", static_cast<osd_file::error (emu_file::*)(const char *)>(&emu_file::open))