@@ -254,12 +254,19 @@ static const luaL_Reg mtstates_stdlibs[] =
254254 { NULL , NULL }
255255};
256256
257- static int Mtstates_newState2 (lua_State * L );
258257
258+ static int Mtstates_newState1 (lua_State * L , bool isNewState );
259259static int Mtstates_newState (lua_State * L )
260+ {
261+ return Mtstates_newState1 (L , true);
262+ }
263+
264+ static int Mtstates_newState2 (lua_State * L );
265+ static int Mtstates_newState1 (lua_State * L , bool isNewState )
260266{
261267 NewStateVars vars ; memset (& vars , 0 , sizeof (NewStateVars ));
262268 NewStateVars * this = & vars ;
269+ this -> isNewState = isNewState ;
263270
264271 int nargs = lua_gettop (L );
265272
@@ -301,14 +308,71 @@ static int Mtstates_newState3(lua_State* L);
301308
302309static int Mtstates_newState2 (lua_State * L )
303310{
304- int arg = 1 ;
311+ NewStateVars * this = ( NewStateVars * ) lua_touserdata ( L , 1 ) ;
305312
306- NewStateVars * this = (NewStateVars * )lua_touserdata (L , arg ++ );
307-
308- int firstArg = arg ;
309- int lastArg = lua_gettop (L );
310- int nargs = lastArg - firstArg + 1 ;
313+ const int firstArg = 2 ;
314+ const int lastArg = lua_gettop (L );
315+ const int nargs = lastArg - firstArg + 1 ;
316+
317+ if (!this -> isNewState )
318+ {
319+ int arg = firstArg ;
320+
321+ bool hasArg = false;
322+ const char * stateName = NULL ;
323+ size_t stateNameLength = 0 ;
324+ lua_Integer stateId = 0 ;
325+
326+ if (arg <= lastArg ) {
327+ if (lua_type (L , arg ) == LUA_TSTRING ) {
328+ stateName = lua_tolstring (L , arg ++ , & stateNameLength );
329+ hasArg = true;
330+ }
331+ else if (lua_type (L , arg ) == LUA_TNUMBER && lua_isinteger (L , arg )) {
332+ stateId = lua_tointeger (L , arg ++ );
333+ hasArg = true;
334+ }
335+ }
336+ if (!hasArg ) {
337+ this -> errorArg = arg ;
338+ return luaL_error (L , "state name or id expected" );
339+ }
340+
341+ /* Lock */
342+
343+ async_mutex_lock (mtstates_global_lock ); this -> globalLocked = true;
344+
345+ MtState * state = NULL ;
346+ if (stateName != NULL ) {
347+ bool unique ;
348+ state = findStateWithName (stateName , stateNameLength , & unique );
349+ if (!state && arg > lastArg ) {
350+ return mtstates_ERROR_UNKNOWN_OBJECT_state_name (L , stateName , stateNameLength );
351+ } else if (state && !unique ) {
352+ return mtstates_ERROR_AMBIGUOUS_NAME_state_name (L , stateName , stateNameLength );
353+ }
354+ } else {
355+ state = findStateWithId (stateId );
356+ if (!state ) {
357+ return mtstates_ERROR_UNKNOWN_OBJECT_state_id (L , stateId );
358+ }
359+ }
360+ if (state ) {
361+ StateUserData * userData = lua_newuserdata (L , sizeof (StateUserData ));
362+ memset (userData , 0 , sizeof (StateUserData ));
363+ pushStateMeta (L ); /* -> udata, meta */
364+ lua_setmetatable (L , -2 ); /* -> udata */
365+
366+ userData -> state = state ;
367+ atomic_inc (& state -> used );
311368
369+ this -> nrslts = 1 ;
370+ return this -> nrslts ;
371+ }
372+ // else we have a stateName and more args -> create new state
373+ }
374+
375+ int arg = firstArg ;
312376 const char * stateName = NULL ;
313377 size_t stateNameLength = 0 ;
314378 if (arg <= lastArg && nargs >= 2 ) {
@@ -373,7 +437,10 @@ static int Mtstates_newState2(lua_State* L)
373437 pushStateMeta (L ); /* -> udata, meta */
374438 lua_setmetatable (L , -2 ); /* -> udata */
375439
376- async_mutex_lock (mtstates_global_lock ); this -> globalLocked = true;
440+ if (!this -> globalLocked ) {
441+ async_mutex_lock (mtstates_global_lock );
442+ this -> globalLocked = true;
443+ }
377444
378445 /* ------------------------------------------------------------------------------------ */
379446
@@ -524,62 +591,10 @@ static int Mtstates_newState3(lua_State* L2)
524591}
525592
526593
594+ static int Mtstates_newState1 (lua_State * L , bool isNewState );
527595static int Mtstates_state (lua_State * L )
528596{
529- int arg = 1 ;
530-
531- bool hasArg = false;
532- const char * stateName = NULL ;
533- size_t stateNameLength = 0 ;
534- lua_Integer stateId = 0 ;
535-
536- if (lua_gettop (L ) >= arg ) {
537- if (lua_type (L , arg ) == LUA_TSTRING ) {
538- stateName = lua_tolstring (L , arg ++ , & stateNameLength );
539- hasArg = true;
540- }
541- else if (lua_type (L , arg ) == LUA_TNUMBER && lua_isinteger (L , arg )) {
542- stateId = lua_tointeger (L , arg ++ );
543- hasArg = true;
544- }
545- }
546- if (!hasArg ) {
547- return luaL_argerror (L , arg , "state name or id expected" );
548- }
549-
550- StateUserData * userData = lua_newuserdata (L , sizeof (StateUserData ));
551- memset (userData , 0 , sizeof (StateUserData ));
552- pushStateMeta (L ); /* -> udata, meta */
553- lua_setmetatable (L , -2 ); /* -> udata */
554-
555- /* Lock */
556-
557- async_mutex_lock (mtstates_global_lock );
558-
559- MtState * state ;
560- if (stateName != NULL ) {
561- bool unique ;
562- state = findStateWithName (stateName , stateNameLength , & unique );
563- if (!state ) {
564- async_mutex_unlock (mtstates_global_lock );
565- return mtstates_ERROR_UNKNOWN_OBJECT_state_name (L , stateName , stateNameLength );
566- } else if (!unique ) {
567- async_mutex_unlock (mtstates_global_lock );
568- return mtstates_ERROR_AMBIGUOUS_NAME_state_name (L , stateName , stateNameLength );
569- }
570- } else {
571- state = findStateWithId (stateId );
572- if (!state ) {
573- async_mutex_unlock (mtstates_global_lock );
574- return mtstates_ERROR_UNKNOWN_OBJECT_state_id (L , stateId );
575- }
576- }
577-
578- userData -> state = state ;
579- atomic_inc (& state -> used );
580-
581- async_mutex_unlock (mtstates_global_lock );
582- return 1 ;
597+ return Mtstates_newState1 (L , false);
583598}
584599
585600
0 commit comments