Skip to content

Commit 9902f58

Browse files
committed
Saver checking for luaIsJitProfilerDefined.
1 parent a7d4c4b commit 9902f58

File tree

4 files changed

+12
-27
lines changed

4 files changed

+12
-27
lines changed

src/xrScriptEngine/script_engine.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,9 @@ void CScriptEngine::init(ExporterFunc exporterFunc, bool loadGlobalNamespace)
10461046
// if (jit == nil) then
10471047
// profiler.setup_hook()
10481048
// end
1049-
if (!strstr(Core.Params, "-nojit"))
1049+
//
1050+
// Update: '-nojit' option adds garbage to stack and luabind calls fail
1051+
if (!strstr(Core.Params, ARGUMENT_ENGINE_NOJIT))
10501052
{
10511053
luajit::open_lib(lua(), LUA_JITLIBNAME, luaopen_jit);
10521054
// Xottab_DUTY: commented this. Let's use default opt level, which is 3

src/xrScriptEngine/script_engine.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ extern Flags32 XRSCRIPTENGINE_API g_LuaDebug;
7171
class XRSCRIPTENGINE_API CScriptEngine
7272
{
7373
public:
74-
typedef AssociativeVector<ScriptProcessor, CScriptProcess*> CScriptProcessStorage;
74+
constexpr static cpcstr ARGUMENT_ENGINE_NOJIT = "-nojit";
75+
typedef AssociativeVector<ScriptProcessor, CScriptProcess*> CScriptProcessStorage;
7576
static const char* const GlobalNamespace;
7677

7778
private:

src/xrScriptEngine/script_profiler.cpp

+6-25
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void CScriptProfiler::startSamplingMode(u32 sampling_interval)
9696

9797
if (!luaIsJitProfilerDefined(lua()))
9898
{
99-
Msg("[P] Cannot start scripts sampling profiler, jit.profiler module is not defined");
99+
Msg("[P] Cannot start scripts sampling profiler, jit module is not defined");
100100
return;
101101
}
102102

@@ -499,33 +499,14 @@ int CScriptProfiler::luaMemoryUsed(lua_State* L)
499499
}
500500

501501
/*
502-
* @returns whether jit.profile module is defined in lua state
502+
* @returns whether jit is enabled
503503
*/
504504
bool CScriptProfiler::luaIsJitProfilerDefined(lua_State* L)
505505
{
506-
if (L == nullptr)
507-
{
508-
return false;
509-
}
510-
511-
auto top = lua_gettop(L);
512-
513-
lua_getglobal(L, "require");
514-
lua_pushstring(L, "jit.profile");
515-
516-
if (lua_pcall(L, 1, 0, 0) == LUA_OK)
517-
{
518-
R_ASSERT2(top == lua_gettop(L), "Lua VM stack should not be affected by jit.profile check");
519-
return true;
520-
}
521-
else
522-
{
523-
// Remove error message to keep stack in previous state.
524-
lua_pop(L, 1);
525-
526-
R_ASSERT2(top == lua_gettop(L), "Lua VM stack should not be affected by jit.profile check");
527-
return true;
528-
}
506+
// Safest and least invasive way to check it.
507+
// Other methods affect lua stack and may add interfere with VS extensions / other hooks / error callbacks.
508+
// We assume that we do not load JIT libs only if nojit parameter is provided.
509+
return !strstr(Core.Params, CScriptEngine::ARGUMENT_ENGINE_NOJIT);
529510
}
530511

531512
/**

src/xrScriptEngine/script_profiler.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class XRSCRIPTENGINE_API CScriptProfiler
7676
using Duration = Clock::duration;
7777

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

0 commit comments

Comments
 (0)