88static int report (lua_State* L)
99{
1010 const char * str = luaL_tolstring (L, 1 , nullptr );
11- lua_getglobal (L, " reporter" );
12- if (lua_islightuserdata (L, -1 ))
13- {
14- // It's a light userdata, proceed to retrieve
15- void * rep = lua_touserdata (L, -1 ); // Get it from the top of the stack
16- TestReporter* reporter = static_cast <TestReporter*>(rep);
17- reporter->reportOutput (str);
18- }
19- else
20- {
21- throw std::runtime_error (" Expected reporter to be a light userdata" );
22- }
11+ lua_getfield (L, LUA_REGISTRYINDEX, " reporter" );
12+ TestReporter* reporter = static_cast <TestReporter*>(lua_touserdata (L, -1 ));
13+ reporter->reportOutput (str);
2314 return 0 ;
2415}
2516
@@ -31,7 +22,7 @@ CliRuntimeFixture::CliRuntimeFixture()
3122 [rep = reporter.get ()](lua_State* L)
3223 {
3324 lua_pushlightuserdata (L, (void *)rep);
34- lua_setglobal (L , " reporter" );
25+ lua_setfield (L, LUA_REGISTRYINDEX , " reporter" );
3526 lua_pushcfunction (L, report, " " );
3627 lua_setglobal (L, " report" );
3728 }
0 commit comments