From bf4114ca8b869054a14374d8493d7024b7d75afb Mon Sep 17 00:00:00 2001
From: Benoit Germain <benoit.germain@ubisoft.com>
Date: Thu, 23 May 2024 17:07:46 +0200
Subject: Automatically initialize "jit" module in new lanes

---
 docs/index.html | 7 +++++--
 src/state.cpp   | 6 +++++-
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/docs/index.html b/docs/index.html
index 47dc47c..92cc38a 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -500,14 +500,16 @@
 	<table style="width:100%">
 		<tr>
 			<td style="width:5%"></td>
-			<td>(nothing)</td>
+			<td>
+				<tt>nil</tt>
+			</td>
 			<td style="width:5%"></td>
 			<td>no standard libraries (default)</td>
 		</tr>
 		<tr>
 			<td />
 			<td>
-				<tt>"base"</tt> or <tt>""</tt>
+				<tt>"base"</tt>
 			</td>
 			<td />
 			<td>
@@ -658,6 +660,7 @@
 </p>
 
 <p>
+	Any non-<tt>nil</tt> value causes initialization of <tt>"base"</tt> and <tt>"jit"</tt> (the latter only for LuaJIT-based builds).<br/>
 	Initializing the standard libs takes a bit of time at each lane invocation. This is the main reason why "no libraries" is the default.
 </p>
 
diff --git a/src/state.cpp b/src/state.cpp
index 83b9e85..08ed111 100644
--- a/src/state.cpp
+++ b/src/state.cpp
@@ -344,7 +344,7 @@ lua_State* luaG_newstate(Universe* U_, SourceState from_, char const* libs_)
     // 'lua.c' stops GC during initialization so perhaps it is a good idea. :)
     lua_gc(_L, LUA_GCSTOP, 0);
 
-    // Anything causes 'base' to be taken in
+    // Anything causes 'base' and 'jit' to be taken in
     if (libs_ != nullptr) {
         // special "*" case (mainly to help with LuaJIT compatibility)
         // as we are called from luaopen_lanes_core() already, and that would deadlock
@@ -355,6 +355,10 @@ lua_State* luaG_newstate(Universe* U_, SourceState from_, char const* libs_)
             open1lib(DEBUGSPEW_PARAM_COMMA(U_) _L, kLanesCoreLibName);
             libs_ = nullptr; // done with libs
         } else {
+#if LUAJIT_FLAVOR() != 0 // building against LuaJIT headers, always open jit
+            DEBUGSPEW_CODE(DebugSpew(U_) << "opening 'jit' library" << std::endl);
+            open1lib(DEBUGSPEW_PARAM_COMMA(U_) _L, LUA_JITLIBNAME);
+#endif // LUAJIT_FLAVOR()
             DEBUGSPEW_CODE(DebugSpew(U_) << "opening 'base' library" << std::endl);
 #if LUA_VERSION_NUM >= 502
             // open base library the same way as in luaL_openlibs()
-- 
cgit v1.2.3-55-g6feb