99#include " lute/packagerun.h"
1010#include " lute/process.h"
1111#include " lute/profiler.h"
12- #include " lute/ref.h"
1312#include " lute/reporter.h"
1413#include " lute/requiresetup.h"
1514#include " lute/runtime.h"
@@ -116,84 +115,45 @@ Compile Options:
116115 -h, --help Display this usage message.
117116)" ;
118117
119- static bool setupArguments (lua_State* L, int argc, char ** argv)
120- {
121- if (!lua_checkstack (L, argc))
122- return false ;
123-
124- for (int i = 0 ; i < argc; ++i)
125- lua_pushstring (L, argv[i]);
126-
127- return true ;
128- }
129-
130- bool runBytecode (
118+ static bool runBytecode (
131119 Runtime& runtime,
132120 const std::string& bytecode,
133121 const std::string& chunkname,
134- lua_State* GL,
135122 int program_argc,
136123 char ** program_argv,
137124 LuteReporter& reporter,
138- std::optional<ProfileOptions> profileOptions
125+ std::optional<ProfileOptions> profileOptions = std:: nullopt
139126)
140127{
141- // module needs to run in a new thread, isolated from the rest
142- lua_State* L = lua_newthread (GL);
143-
144- if (profileOptions)
145- profilerStart (L, profileOptions->frequency );
146-
147- // new thread needs to have the globals sandboxed
148- luaL_sandboxthread (L);
149-
150- if (luau_load (L, chunkname.c_str (), bytecode.data (), bytecode.size (), 0 ) != 0 )
151- {
152- if (const char * str = lua_tostring (L, -1 ))
153- reporter.reportError (str);
154- else
155- reporter.reportError (" Failed to load bytecode" );
156-
157- lua_pop (GL, 1 );
158- return false ;
159- }
160-
161- if (getCodegenEnabled ())
162- {
163- Luau::CodeGen::CompilationOptions nativeOptions;
164- Luau::CodeGen::compile (L, -1 , nativeOptions);
165- }
166-
167- if (!setupArguments (L, program_argc, program_argv))
168- {
169- reporter.reportError (" Failed to pass arguments to Luau" );
170- lua_pop (GL, 1 );
171- return false ;
172- }
173-
174- runtime.args .clear ();
175- for (int i = 0 ; i < program_argc; ++i)
176- runtime.args .emplace_back (program_argv[i]);
177-
178- runtime.GL = GL;
179- runtime.runningThreads .push_back ({true , getRefForThread (L), program_argc});
180-
181- lua_pop (GL, 1 );
128+ bool success = runtime.runBytecode (
129+ bytecode,
130+ chunkname,
131+ program_argc,
132+ program_argv,
133+ [&](lua_State* L)
134+ {
135+ if (getCodegenEnabled ())
136+ {
137+ Luau::CodeGen::CompilationOptions nativeOptions;
138+ Luau::CodeGen::compile (L, -1 , nativeOptions);
139+ }
140+ if (profileOptions)
141+ profilerStart (L, profileOptions->frequency );
142+ }
143+ );
182144
183- bool b = runtime.runToCompletion ();
184145 if (profileOptions)
185146 {
186147 profilerStop ();
187148 profilerDump (profileOptions->filename .c_str (), reporter);
188149 }
189150
190- return b ;
151+ return success ;
191152}
192153
193154static bool runFile (
194155 Runtime& runtime,
195156 const char * name,
196- lua_State* GL,
197157 int program_argc,
198158 char ** program_argv,
199159 LuteReporter& reporter,
@@ -217,7 +177,7 @@ static bool runFile(
217177
218178 std::string bytecode = Luau::compile (*source, copts ());
219179
220- return runBytecode (runtime, bytecode, chunkname, GL, program_argc, program_argv, reporter, profileOptions);
180+ return runBytecode (runtime, bytecode, chunkname, program_argc, program_argv, reporter, profileOptions);
221181}
222182
223183static int assertionHandler (const char * expr, const char * file, int line, const char * function)
@@ -396,7 +356,6 @@ int handleRunCommand(int argc, char** argv, int argOffset, bool packageAwareness
396356 }
397357
398358 Runtime runtime{reporter};
399- lua_State* L;
400359
401360 if (packageAwareness)
402361 {
@@ -419,14 +378,14 @@ int handleRunCommand(int argc, char** argv, int argOffset, bool packageAwareness
419378 }
420379
421380 auto [directDependencies, allDependencies] = getDependenciesFromLockfile (*lockfile);
422- L = setupPkgRunState (runtime, std::move (directDependencies), std::move (allDependencies));
381+ setupPkgRunState (runtime, std::move (directDependencies), std::move (allDependencies));
423382 }
424383 else
425384 {
426- L = setupRunState (runtime);
385+ setupRunState (runtime);
427386 }
428387
429- bool success = runFile (runtime, validPath.c_str (), L, program_argc, program_argv, reporter, profileOptions);
388+ bool success = runFile (runtime, validPath.c_str (), program_argc, program_argv, reporter, profileOptions);
430389 return success ? 0 : 1 ;
431390}
432391
@@ -650,10 +609,9 @@ void setupVersionLibrary(lua_State* L)
650609int handleCliCommand (CliCommandResult result, int program_argc, char ** program_argv, LuteReporter& reporter)
651610{
652611 Runtime runtime{reporter};
653- lua_State* L = setupCliCommandState (runtime, setupVersionLibrary);
612+ setupCliCommandState (runtime, setupVersionLibrary);
654613
655- std::string bytecode = Luau::compile (std::string (result.contents ), copts ());
656- return runBytecode (runtime, bytecode, " @" + result.path , L, program_argc, program_argv, reporter) ? 0 : 1 ;
614+ return runtime.runSource (std::string (result.contents ), copts (), " @" + result.path , program_argc, program_argv) ? 0 : 1 ;
657615}
658616
659617int cliMain (int argc, char ** argv, LuteReporter& reporter)
@@ -674,12 +632,12 @@ int cliMain(int argc, char** argv, LuteReporter& reporter)
674632 {
675633 Runtime runtime{reporter};
676634
677- lua_State* GL = setupBundleState (runtime, payload->luauConfigFiles , payload->filePathToBytecode );
635+ setupBundleState (runtime, payload->luauConfigFiles , payload->filePathToBytecode );
678636 std::string entryPoint = payload->entryPointPath ;
679637 auto entryModule = payload->filePathToBytecode .find (entryPoint);
680638 if (entryModule != nullptr )
681639 {
682- bool success = runBytecode (runtime, *entryModule, " @@bundle/" + entryPoint, GL, argc, argv, reporter);
640+ bool success = runBytecode (runtime, *entryModule, " @@bundle/" + entryPoint, argc, argv, reporter);
683641 return success ? 0 : 1 ;
684642 }
685643 }
0 commit comments