Skip to content

Commit

Permalink
Merge pull request orbitersim#520 from TheGondos/lua_bogus_userdata
Browse files Browse the repository at this point in the history
[Lua]Return nil instead of bogus userdata when functions fail
  • Loading branch information
jarmonik authored Dec 15, 2024
2 parents ce6db86 + 20c96e7 commit 2205bfe
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions Src/Module/LuaScript/LuaInterpreter/lua_vessel_mtd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3055,7 +3055,11 @@ int Interpreter::v_get_surfaceref (lua_State *L)
static const char *funcname = "get_surfaceref";
AssertMtdMinPrmCount(L, 1, funcname);
VESSEL *v = lua_tovessel_safe(L, 1, funcname);
lua_pushlightuserdata (L, v->GetSurfaceRef());
OBJHANDLE hRef = v->GetSurfaceRef();
if(hRef)
lua_pushlightuserdata (L, hRef);
else
lua_pushnil(L);
return 1;
}

Expand Down Expand Up @@ -7931,7 +7935,10 @@ int Interpreter::v_add_animationcomponent (lua_State *L)
hparent = (ANIMATIONCOMPONENT_HANDLE)luamtd_tolightuserdata_safe(L, 6, funcname);
ANIMATIONCOMPONENT_HANDLE hanimcomp =
v->AddAnimationComponent (anim, state0, state1, trans, hparent);
lua_pushlightuserdata (L,hanimcomp);
if(hanimcomp)
lua_pushlightuserdata (L,hanimcomp);
else
lua_pushnil(L);
return 1;
}

Expand Down Expand Up @@ -8938,7 +8945,10 @@ int Interpreter::v_add_exhauststream(lua_State* L)
PSTREAM_HANDLE hp;
if (do_pos) hp = v->AddExhaustStream(ht, pos, &pss);
else hp = v->AddExhaustStream(ht, &pss);
lua_pushlightuserdata(L, hp);
if(hp)
lua_pushlightuserdata(L, hp);
else
lua_pushnil(L);
return 1;
}

Expand Down Expand Up @@ -9057,7 +9067,11 @@ int Interpreter::v_add_reentrystream(lua_State* L)

PSTREAM_HANDLE hp;
hp = v->AddReentryStream(&pss);
lua_pushlightuserdata(L, hp);
if(hp)
lua_pushlightuserdata(L, hp);
else
lua_pushnil(L);

return 1;
}

Expand Down Expand Up @@ -9170,6 +9184,10 @@ int Interpreter::v_add_particlestream(lua_State* L)
*lvl = lua_tonumber(L, 5);

PSTREAM_HANDLE hp = v->AddParticleStream(&pss, pos, dir , lvl);
if(!hp) {
lua_pushnil(L);
return 1;
}

// Add the numberref in the registry to prevent its collection if the script does not recover it
// Use the PSTREAM_HANDLE as the key so we can remove it when deleting the stream
Expand Down

0 comments on commit 2205bfe

Please sign in to comment.