Skip to content

Commit

Permalink
Safer checking for luaIsJitProfilerDefined.
Browse files Browse the repository at this point in the history
  • Loading branch information
Neloreck committed Jan 17, 2025
1 parent b734d1f commit d3d5aa3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 27 deletions.
4 changes: 3 additions & 1 deletion src/xrScriptEngine/script_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,9 @@ void CScriptEngine::init(ExporterFunc exporterFunc, bool loadGlobalNamespace)
// if (jit == nil) then
// profiler.setup_hook()
// end
if (!strstr(Core.Params, "-nojit"))
//
// Update: '-nojit' option adds garbage to stack and luabind calls fail
if (!strstr(Core.Params, ARGUMENT_ENGINE_NOJIT))
{
luajit::open_lib(lua(), LUA_JITLIBNAME, luaopen_jit);
// Xottab_DUTY: commented this. Let's use default opt level, which is 3
Expand Down
3 changes: 2 additions & 1 deletion src/xrScriptEngine/script_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ extern Flags32 XRSCRIPTENGINE_API g_LuaDebug;
class XRSCRIPTENGINE_API CScriptEngine
{
public:
typedef AssociativeVector<ScriptProcessor, CScriptProcess*> CScriptProcessStorage;
constexpr static cpcstr ARGUMENT_ENGINE_NOJIT = "-nojit";
typedef AssociativeVector<ScriptProcessor, CScriptProcess*> CScriptProcessStorage;
static const char* const GlobalNamespace;

private:
Expand Down
31 changes: 6 additions & 25 deletions src/xrScriptEngine/script_profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void CScriptProfiler::startSamplingMode(u32 sampling_interval)

if (!luaIsJitProfilerDefined(lua()))
{
Msg("[P] Cannot start scripts sampling profiler, jit.profiler module is not defined");
Msg("[P] Cannot start scripts sampling profiler, jit module is not defined");
return;
}

Expand Down Expand Up @@ -499,33 +499,14 @@ int CScriptProfiler::luaMemoryUsed(lua_State* L)
}

/*
* @returns whether jit.profile module is defined in lua state
* @returns whether jit is enabled
*/
bool CScriptProfiler::luaIsJitProfilerDefined(lua_State* L)
{
if (L == nullptr)
{
return false;
}

auto top = lua_gettop(L);

lua_getglobal(L, "require");
lua_pushstring(L, "jit.profile");

if (lua_pcall(L, 1, 0, 0) == LUA_OK)
{
R_ASSERT2(top == lua_gettop(L), "Lua VM stack should not be affected by jit.profile check");
return true;
}
else
{
// Remove error message to keep stack in previous state.
lua_pop(L, 1);

R_ASSERT2(top == lua_gettop(L), "Lua VM stack should not be affected by jit.profile check");
return true;
}
// Safest and least invasive way to check it.
// Other methods affect lua stack and may add interfere with VS extensions / other hooks / error callbacks.
// We assume that we do not load JIT libs only if nojit parameter is provided.
return !strstr(Core.Params, CScriptEngine::ARGUMENT_ENGINE_NOJIT);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/xrScriptEngine/script_profiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class XRSCRIPTENGINE_API CScriptProfiler
using Duration = Clock::duration;

private:
// todo: Can we make some global module to store all the arguments as experessions?
// List of commnad line args for startup profuler attach:
constexpr static cpcstr ARGUMENT_PROFILER_DEFAULT = "-lua_profiler";
constexpr static cpcstr ARGUMENT_PROFILER_HOOK = "-lua_hook_profiler";
Expand Down

0 comments on commit d3d5aa3

Please sign in to comment.