I was curious how the CTRL+C worked in linenoise and whether it could be "caught".


One interesting side effect of enabling SOL_CHECK_ARGUMENTS in luaengine.h is in whether sol will throw an exception when it executes this in luaengine.cpp:

Code
auto ret = func();

if (ret.valid()) {
	const char *tmp = ret.get<const char *>();
        if (tmp != nullptr)
			ctx.result = tmp;
		else
			exit(0);
	}


So when you type a CTRL+C on the console, it will return a nil from linenoise, tmp will be null so it calls exit(0);

When you've got SOL_CHECK_ARGUMENTS enabled, it will throw a sol::error exception immediately when it executes the following line

Code
const char *tmp = ret.get<const char *>();

It drops out and never sees the following if (tmp != nullptr) statement.

I had to put a bunch of printfs around to figure out exactly where it was blowing up.

And you get this kind of exit.

Code
[MAME]> 
terminate called after throwing an instance of 'sol::error'
  what():  lua: error: stack index 2, expected string, received nil
Aborted (core dumped)

So if we insert a little try catch block, we can catch that sol::error on the get.
Code
if (ret.valid()) {

    printf("calling ret.get const char *\n");

    try{
       const char *tmp = ret.get<const char *>();

	if (tmp != nullptr)
		ctx.result = tmp;
	else
		exit(0);
	
 catch (const std::exception& e) {  
     printf("Caught Exception:\n"); std::cout << e.what() << std::endl;
          }
}
	
		

With the catch it will catch the sol::error and keep you in the console. Of course, it's so convenient to just hit CTRL+C to drop out of mame, but there are other ways, like manager:machine():exit()


Code
[MAME]> 
calling ret.get const char *
Caught exception:
lua: error: stack index 2, expected string, received nil
[MAME]>