22#include " lua.h"
33#include " lualib.h"
44#include " luacode.h"
5+ #include " llsl.h"
56
67#include " Luau/Common.h"
78#include " Luau/Frontend.h"
89#include " Luau/BuiltinDefinitions.h"
10+ #include " Luau/LSLBuiltins.h"
911
1012#include < string>
1113#include < memory>
@@ -62,17 +64,48 @@ struct DemoConfigResolver : Luau::ConfigResolver
6264 Luau::Config defaultConfig;
6365};
6466
67+ // SL mode state
68+ static lua_SLRuntimeState sl_state;
69+
70+ static void userthread_callback (lua_State* LP, lua_State* L)
71+ {
72+ if (LP == nullptr )
73+ return ;
74+ lua_setthreaddata (L, lua_getthreaddata (LP));
75+ }
76+
6577static void setupState (lua_State* L)
6678{
6779 luaL_openlibs (L);
6880
81+ // SL mode setup
82+ lua_setthreaddata (L, &sl_state);
83+ sl_state.slIdentifier = LUA_SL_IDENTIFIER;
84+ lua_callbacks (L)->userthread = userthread_callback;
85+
86+ // SL libraries
87+ luaopen_sl (L, true );
88+ luaopen_ll (L, true );
89+ lua_pop (L, 1 );
90+
91+ // JSON and Base64
92+ luaopen_cjson (L);
93+ lua_pop (L, 1 );
94+ luaopen_llbase64 (L);
95+ lua_pop (L, 1 );
96+
97+ // Set SL constants on _G
98+ luaSL_set_constant_globals (L);
99+
69100 luaL_sandbox (L);
101+ lua_fixallcollectable (L);
70102}
71103
72104static std::string runCode (lua_State* L, const std::string& source)
73105{
74106 size_t bytecodeSize = 0 ;
75107 char * bytecode = luau_compile (source.data (), source.length (), nullptr , &bytecodeSize);
108+ lua_setmemcat (L, 0 );
76109 int result = luau_load (L, " =stdin" , bytecode, bytecodeSize, 0 );
77110 free (bytecode);
78111
@@ -87,7 +120,9 @@ static std::string runCode(lua_State* L, const std::string& source)
87120 return error;
88121 }
89122
123+ lua_setmemcat (L, 2 );
90124 lua_State* T = lua_newthread (L);
125+ lua_setmemcat (L, 0 );
91126
92127 lua_pushvalue (L, -2 );
93128 lua_remove (L, -3 );
@@ -189,16 +224,34 @@ extern "C" const char* executeScript(const char* source)
189224 if (strncmp (flag->name , " Luau" , 4 ) == 0 )
190225 flag->value = true ;
191226
227+ // Initialize SL builtins once from embedded file
228+ static bool builtins_initialized = false ;
229+ if (!builtins_initialized)
230+ {
231+ luauSL_init_global_builtins (" /builtins.txt" );
232+ builtins_initialized = true ;
233+ }
234+
192235 // create new state
193236 std::unique_ptr<lua_State, void (*)(lua_State*)> globalState (luaL_newstate (), lua_close);
194237 lua_State* L = globalState.get ();
195238
196239 // setup state
197240 setupState (L);
198241
199- // sandbox thread
242+ // sandbox thread first (like REPL does)
200243 luaL_sandboxthread (L);
201244
245+ // Event and timer managers (after sandbox)
246+ luaSL_createeventmanager (L);
247+ lua_ref (L, -1 );
248+ lua_pushvalue (L, -1 ); // Duplicate for timer manager (it expects LLEvents on stack)
249+ lua_setglobal (L, " LLEvents" );
250+
251+ luaSL_createtimermanager (L);
252+ lua_ref (L, -1 );
253+ lua_setglobal (L, " LLTimers" );
254+
202255 // static string for caching result (prevents dangling ptr on function exit)
203256 static std::string result;
204257
0 commit comments