2021-10-29 21:25:12 +01:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
2022-02-11 18:43:14 +00:00
|
|
|
#include "Repl.h"
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
#include "lua.h"
|
|
|
|
#include "lualib.h"
|
|
|
|
|
2022-10-13 23:59:53 +01:00
|
|
|
#include "Luau/CodeGen.h"
|
2021-10-29 21:25:12 +01:00
|
|
|
#include "Luau/Compiler.h"
|
|
|
|
#include "Luau/BytecodeBuilder.h"
|
|
|
|
#include "Luau/Parser.h"
|
|
|
|
|
2022-07-21 21:36:41 +01:00
|
|
|
#include "Coverage.h"
|
2021-10-29 21:25:12 +01:00
|
|
|
#include "FileUtils.h"
|
2022-07-21 21:36:41 +01:00
|
|
|
#include "Flags.h"
|
2021-10-29 21:25:12 +01:00
|
|
|
#include "Profiler.h"
|
|
|
|
|
2022-02-03 23:09:37 +00:00
|
|
|
#include "isocline.h"
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2021-11-12 02:12:39 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#include <fcntl.h>
|
2022-08-04 22:27:28 +01:00
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
2021-11-12 02:12:39 +00:00
|
|
|
#endif
|
|
|
|
|
2022-07-08 02:05:31 +01:00
|
|
|
#ifdef CALLGRIND
|
|
|
|
#include <valgrind/callgrind.h>
|
|
|
|
#endif
|
|
|
|
|
2022-05-06 00:52:48 +01:00
|
|
|
#include <locale.h>
|
2022-08-04 22:27:28 +01:00
|
|
|
#include <signal.h>
|
2022-05-06 00:52:48 +01:00
|
|
|
|
2022-01-14 16:06:31 +00:00
|
|
|
LUAU_FASTFLAG(DebugLuauTimeTracing)
|
|
|
|
|
|
|
|
enum class CliMode
|
|
|
|
{
|
|
|
|
Unknown,
|
|
|
|
Repl,
|
|
|
|
Compile,
|
|
|
|
RunSourceFiles
|
|
|
|
};
|
|
|
|
|
2021-11-12 02:12:39 +00:00
|
|
|
enum class CompileFormat
|
|
|
|
{
|
2021-11-18 22:21:07 +00:00
|
|
|
Text,
|
2022-04-29 02:04:52 +01:00
|
|
|
Binary,
|
2022-10-13 23:59:53 +01:00
|
|
|
Remarks,
|
2023-01-27 21:28:45 +00:00
|
|
|
Codegen, // Prints annotated native code including IR and assembly
|
|
|
|
CodegenAsm, // Prints annotated native code assembly
|
|
|
|
CodegenIr, // Prints annotated native code IR
|
|
|
|
CodegenVerbose, // Prints annotated native code including IR, assembly and outlined code
|
2022-10-21 18:33:43 +01:00
|
|
|
CodegenNull,
|
2022-04-29 02:04:52 +01:00
|
|
|
Null
|
2021-11-12 02:12:39 +00:00
|
|
|
};
|
|
|
|
|
2022-02-18 00:41:20 +00:00
|
|
|
constexpr int MaxTraversalLimit = 50;
|
|
|
|
|
2022-10-13 23:59:53 +01:00
|
|
|
static bool codegen = false;
|
|
|
|
|
2022-08-04 22:27:28 +01:00
|
|
|
// Ctrl-C handling
|
|
|
|
static void sigintCallback(lua_State* L, int gc)
|
|
|
|
{
|
|
|
|
if (gc >= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
lua_callbacks(L)->interrupt = NULL;
|
|
|
|
|
|
|
|
lua_rawcheckstack(L, 1); // reserve space for error string
|
|
|
|
luaL_error(L, "Execution interrupted");
|
|
|
|
}
|
|
|
|
|
|
|
|
static lua_State* replState = NULL;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
BOOL WINAPI sigintHandler(DWORD signal)
|
|
|
|
{
|
|
|
|
if (signal == CTRL_C_EVENT && replState)
|
|
|
|
lua_callbacks(replState)->interrupt = &sigintCallback;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static void sigintHandler(int signum)
|
|
|
|
{
|
|
|
|
if (signum == SIGINT && replState)
|
|
|
|
lua_callbacks(replState)->interrupt = &sigintCallback;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-01-21 16:23:02 +00:00
|
|
|
struct GlobalOptions
|
|
|
|
{
|
|
|
|
int optimizationLevel = 1;
|
2022-02-11 18:43:14 +00:00
|
|
|
int debugLevel = 1;
|
2022-01-21 16:23:02 +00:00
|
|
|
} globalOptions;
|
|
|
|
|
2021-12-10 21:17:10 +00:00
|
|
|
static Luau::CompileOptions copts()
|
|
|
|
{
|
|
|
|
Luau::CompileOptions result = {};
|
2022-01-21 16:23:02 +00:00
|
|
|
result.optimizationLevel = globalOptions.optimizationLevel;
|
2022-02-11 18:43:14 +00:00
|
|
|
result.debugLevel = globalOptions.debugLevel;
|
2021-12-10 21:17:10 +00:00
|
|
|
result.coverageLevel = coverageActive() ? 2 : 0;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
static int lua_loadstring(lua_State* L)
|
|
|
|
{
|
|
|
|
size_t l = 0;
|
|
|
|
const char* s = luaL_checklstring(L, 1, &l);
|
|
|
|
const char* chunkname = luaL_optstring(L, 2, s);
|
|
|
|
|
|
|
|
lua_setsafeenv(L, LUA_ENVIRONINDEX, false);
|
|
|
|
|
2021-12-10 21:17:10 +00:00
|
|
|
std::string bytecode = Luau::compile(std::string(s, l), copts());
|
2021-11-18 22:21:07 +00:00
|
|
|
if (luau_load(L, chunkname, bytecode.data(), bytecode.size(), 0) == 0)
|
2021-10-29 21:25:12 +01:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
lua_pushnil(L);
|
2022-08-04 22:27:28 +01:00
|
|
|
lua_insert(L, -2); // put before error message
|
|
|
|
return 2; // return nil plus error message
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int finishrequire(lua_State* L)
|
|
|
|
{
|
|
|
|
if (lua_isstring(L, -1))
|
|
|
|
lua_error(L);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lua_require(lua_State* L)
|
|
|
|
{
|
|
|
|
std::string name = luaL_checkstring(L, 1);
|
|
|
|
std::string chunkname = "=" + name;
|
|
|
|
|
|
|
|
luaL_findtable(L, LUA_REGISTRYINDEX, "_MODULES", 1);
|
|
|
|
|
|
|
|
// return the module from the cache
|
|
|
|
lua_getfield(L, -1, name.c_str());
|
|
|
|
if (!lua_isnil(L, -1))
|
2022-07-21 21:36:41 +01:00
|
|
|
{
|
|
|
|
// L stack: _MODULES result
|
2021-10-29 21:25:12 +01:00
|
|
|
return finishrequire(L);
|
2022-07-21 21:36:41 +01:00
|
|
|
}
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
lua_pop(L, 1);
|
|
|
|
|
2021-11-12 02:12:39 +00:00
|
|
|
std::optional<std::string> source = readFile(name + ".luau");
|
2021-10-29 21:25:12 +01:00
|
|
|
if (!source)
|
2021-11-12 02:12:39 +00:00
|
|
|
{
|
|
|
|
source = readFile(name + ".lua"); // try .lua if .luau doesn't exist
|
|
|
|
if (!source)
|
|
|
|
luaL_argerrorL(L, 1, ("error loading " + name).c_str()); // if neither .luau nor .lua exist, we have an error
|
|
|
|
}
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
// module needs to run in a new thread, isolated from the rest
|
2022-07-21 21:36:41 +01:00
|
|
|
// note: we create ML on main thread so that it doesn't inherit environment of L
|
2021-10-29 21:25:12 +01:00
|
|
|
lua_State* GL = lua_mainthread(L);
|
|
|
|
lua_State* ML = lua_newthread(GL);
|
|
|
|
lua_xmove(GL, L, 1);
|
|
|
|
|
|
|
|
// new thread needs to have the globals sandboxed
|
|
|
|
luaL_sandboxthread(ML);
|
|
|
|
|
|
|
|
// now we can compile & run module on the new thread
|
2021-12-10 21:17:10 +00:00
|
|
|
std::string bytecode = Luau::compile(*source, copts());
|
2021-11-18 22:21:07 +00:00
|
|
|
if (luau_load(ML, chunkname.c_str(), bytecode.data(), bytecode.size(), 0) == 0)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-10-13 23:59:53 +01:00
|
|
|
if (codegen)
|
|
|
|
Luau::CodeGen::compile(ML, -1);
|
|
|
|
|
2021-12-10 21:17:10 +00:00
|
|
|
if (coverageActive())
|
|
|
|
coverageTrack(ML, -1);
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
int status = lua_resume(ML, L, 0);
|
|
|
|
|
|
|
|
if (status == 0)
|
|
|
|
{
|
|
|
|
if (lua_gettop(ML) == 0)
|
|
|
|
lua_pushstring(ML, "module must return a value");
|
|
|
|
else if (!lua_istable(ML, -1) && !lua_isfunction(ML, -1))
|
|
|
|
lua_pushstring(ML, "module must return a table or function");
|
|
|
|
}
|
|
|
|
else if (status == LUA_YIELD)
|
|
|
|
{
|
|
|
|
lua_pushstring(ML, "module can not yield");
|
|
|
|
}
|
|
|
|
else if (!lua_isstring(ML, -1))
|
|
|
|
{
|
|
|
|
lua_pushstring(ML, "unknown error while running module");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 21:36:41 +01:00
|
|
|
// there's now a return value on top of ML; L stack: _MODULES ML
|
2021-10-29 21:25:12 +01:00
|
|
|
lua_xmove(ML, L, 1);
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_setfield(L, -4, name.c_str());
|
|
|
|
|
2022-07-21 21:36:41 +01:00
|
|
|
// L stack: _MODULES ML result
|
2021-10-29 21:25:12 +01:00
|
|
|
return finishrequire(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lua_collectgarbage(lua_State* L)
|
|
|
|
{
|
|
|
|
const char* option = luaL_optstring(L, 1, "collect");
|
|
|
|
|
|
|
|
if (strcmp(option, "collect") == 0)
|
|
|
|
{
|
|
|
|
lua_gc(L, LUA_GCCOLLECT, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(option, "count") == 0)
|
|
|
|
{
|
|
|
|
int c = lua_gc(L, LUA_GCCOUNT, 0);
|
|
|
|
lua_pushnumber(L, c);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
luaL_error(L, "collectgarbage must be called with 'count' or 'collect'");
|
|
|
|
}
|
|
|
|
|
2022-07-08 02:05:31 +01:00
|
|
|
#ifdef CALLGRIND
|
|
|
|
static int lua_callgrind(lua_State* L)
|
|
|
|
{
|
|
|
|
const char* option = luaL_checkstring(L, 1);
|
|
|
|
|
|
|
|
if (strcmp(option, "running") == 0)
|
|
|
|
{
|
|
|
|
int r = RUNNING_ON_VALGRIND;
|
|
|
|
lua_pushboolean(L, r);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(option, "zero") == 0)
|
|
|
|
{
|
|
|
|
CALLGRIND_ZERO_STATS;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(option, "dump") == 0)
|
|
|
|
{
|
|
|
|
const char* name = luaL_checkstring(L, 2);
|
|
|
|
|
|
|
|
CALLGRIND_DUMP_STATS_AT(name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
luaL_error(L, "callgrind must be called with one of 'running', 'zero', 'dump'");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-01-27 21:29:34 +00:00
|
|
|
void setupState(lua_State* L)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-10-13 23:59:53 +01:00
|
|
|
if (codegen)
|
|
|
|
Luau::CodeGen::create(L);
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
luaL_openlibs(L);
|
|
|
|
|
|
|
|
static const luaL_Reg funcs[] = {
|
|
|
|
{"loadstring", lua_loadstring},
|
|
|
|
{"require", lua_require},
|
|
|
|
{"collectgarbage", lua_collectgarbage},
|
2022-07-08 02:05:31 +01:00
|
|
|
#ifdef CALLGRIND
|
|
|
|
{"callgrind", lua_callgrind},
|
|
|
|
#endif
|
2021-10-29 21:25:12 +01:00
|
|
|
{NULL, NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
|
|
|
luaL_register(L, NULL, funcs);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
luaL_sandbox(L);
|
|
|
|
}
|
|
|
|
|
2022-01-27 21:29:34 +00:00
|
|
|
std::string runCode(lua_State* L, const std::string& source)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2021-12-10 21:17:10 +00:00
|
|
|
std::string bytecode = Luau::compile(source, copts());
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2021-11-18 22:21:07 +00:00
|
|
|
if (luau_load(L, "=stdin", bytecode.data(), bytecode.size(), 0) != 0)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
const char* msg = lua_tolstring(L, -1, &len);
|
|
|
|
|
|
|
|
std::string error(msg, len);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2022-10-13 23:59:53 +01:00
|
|
|
if (codegen)
|
|
|
|
Luau::CodeGen::compile(L, -1);
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
lua_State* T = lua_newthread(L);
|
|
|
|
|
|
|
|
lua_pushvalue(L, -2);
|
|
|
|
lua_remove(L, -3);
|
|
|
|
lua_xmove(L, T, 1);
|
|
|
|
|
|
|
|
int status = lua_resume(T, NULL, 0);
|
|
|
|
|
|
|
|
if (status == 0)
|
|
|
|
{
|
|
|
|
int n = lua_gettop(T);
|
|
|
|
|
|
|
|
if (n)
|
|
|
|
{
|
|
|
|
luaL_checkstack(T, LUA_MINSTACK, "too many results to print");
|
2022-01-27 21:29:34 +00:00
|
|
|
lua_getglobal(T, "_PRETTYPRINT");
|
|
|
|
// If _PRETTYPRINT is nil, then use the standard print function instead
|
|
|
|
if (lua_isnil(T, -1))
|
|
|
|
{
|
|
|
|
lua_pop(T, 1);
|
|
|
|
lua_getglobal(T, "print");
|
|
|
|
}
|
2021-10-29 21:25:12 +01:00
|
|
|
lua_insert(T, 1);
|
|
|
|
lua_pcall(T, n, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-05 02:42:00 +00:00
|
|
|
std::string error;
|
|
|
|
|
|
|
|
if (status == LUA_YIELD)
|
|
|
|
{
|
|
|
|
error = "thread yielded unexpectedly";
|
|
|
|
}
|
|
|
|
else if (const char* str = lua_tostring(T, -1))
|
|
|
|
{
|
|
|
|
error = str;
|
|
|
|
}
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
error += "\nstack backtrace:\n";
|
|
|
|
error += lua_debugtrace(T);
|
|
|
|
|
|
|
|
fprintf(stdout, "%s", error.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
2022-02-18 00:41:20 +00:00
|
|
|
// Replaces the top of the lua stack with the metatable __index for the value
|
|
|
|
// if it exists. Returns true iff __index exists.
|
|
|
|
static bool tryReplaceTopWithIndex(lua_State* L)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-02-18 00:41:20 +00:00
|
|
|
if (luaL_getmetafield(L, -1, "__index"))
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-02-18 00:41:20 +00:00
|
|
|
// Remove the table leaving __index on the top of stack
|
|
|
|
lua_remove(L, -2);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2022-02-18 00:41:20 +00:00
|
|
|
|
|
|
|
// This function is similar to lua_gettable, but it avoids calling any
|
|
|
|
// lua callback functions (e.g. __index) which might modify the Lua VM state.
|
|
|
|
static void safeGetTable(lua_State* L, int tableIndex)
|
|
|
|
{
|
|
|
|
lua_pushvalue(L, tableIndex); // Duplicate the table
|
|
|
|
|
|
|
|
// The loop invariant is that the table to search is at -1
|
|
|
|
// and the key is at -2.
|
|
|
|
for (int loopCount = 0;; loopCount++)
|
|
|
|
{
|
|
|
|
lua_pushvalue(L, -2); // Duplicate the key
|
|
|
|
lua_rawget(L, -2); // Try to find the key
|
|
|
|
if (!lua_isnil(L, -1) || loopCount >= MaxTraversalLimit)
|
|
|
|
{
|
|
|
|
// Either the key has been found, and/or we have reached the max traversal limit
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-02-18 00:41:20 +00:00
|
|
|
lua_pop(L, 1); // Pop the nil result
|
|
|
|
if (!luaL_getmetafield(L, -1, "__index"))
|
|
|
|
{
|
|
|
|
lua_pushnil(L);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (lua_istable(L, -1))
|
|
|
|
{
|
|
|
|
// Replace the current table being searched with __index table
|
|
|
|
lua_replace(L, -2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lua_pop(L, 1); // Pop the value
|
|
|
|
lua_pushnil(L);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_remove(L, -2); // Remove the table
|
|
|
|
lua_remove(L, -2); // Remove the original key
|
|
|
|
}
|
|
|
|
|
|
|
|
// completePartialMatches finds keys that match the specified 'prefix'
|
|
|
|
// Note: the table/object to be searched must be on the top of the Lua stack
|
|
|
|
static void completePartialMatches(lua_State* L, bool completeOnlyFunctions, const std::string& editBuffer, std::string_view prefix,
|
|
|
|
const AddCompletionCallback& addCompletionCallback)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < MaxTraversalLimit && lua_istable(L, -1); i++)
|
|
|
|
{
|
|
|
|
// table, key
|
|
|
|
lua_pushnil(L);
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2022-02-18 00:41:20 +00:00
|
|
|
// Loop over all the keys in the current table
|
|
|
|
while (lua_next(L, -2) != 0)
|
|
|
|
{
|
|
|
|
if (lua_type(L, -2) == LUA_TSTRING)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-02-18 00:41:20 +00:00
|
|
|
// table, key, value
|
|
|
|
std::string_view key = lua_tostring(L, -2);
|
|
|
|
int valueType = lua_type(L, -1);
|
2022-01-21 16:23:02 +00:00
|
|
|
|
2022-02-18 00:41:20 +00:00
|
|
|
// If the last separator was a ':' (i.e. a method call) then only functions should be completed.
|
|
|
|
bool requiredValueType = (!completeOnlyFunctions || valueType == LUA_TFUNCTION);
|
2022-01-21 16:23:02 +00:00
|
|
|
|
2022-02-18 00:41:20 +00:00
|
|
|
if (!key.empty() && requiredValueType && Luau::startsWith(key, prefix))
|
|
|
|
{
|
|
|
|
std::string completedComponent(key.substr(prefix.size()));
|
|
|
|
std::string completion(editBuffer + completedComponent);
|
|
|
|
if (valueType == LUA_TFUNCTION)
|
2022-01-21 16:23:02 +00:00
|
|
|
{
|
2022-02-18 00:41:20 +00:00
|
|
|
// Add an opening paren for function calls by default.
|
|
|
|
completion += "(";
|
2022-01-21 16:23:02 +00:00
|
|
|
}
|
2022-02-18 00:41:20 +00:00
|
|
|
addCompletionCallback(completion, std::string(key));
|
2022-01-06 22:10:07 +00:00
|
|
|
}
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
2022-02-18 00:41:20 +00:00
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the current table being searched with an __index table if one exists
|
|
|
|
if (!tryReplaceTopWithIndex(L))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void completeIndexer(lua_State* L, const std::string& editBuffer, const AddCompletionCallback& addCompletionCallback)
|
|
|
|
{
|
|
|
|
std::string_view lookup = editBuffer;
|
|
|
|
bool completeOnlyFunctions = false;
|
|
|
|
|
|
|
|
// Push the global variable table to begin the search
|
|
|
|
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
size_t sep = lookup.find_first_of(".:");
|
|
|
|
std::string_view prefix = lookup.substr(0, sep);
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2022-02-18 00:41:20 +00:00
|
|
|
if (sep == std::string_view::npos)
|
|
|
|
{
|
|
|
|
completePartialMatches(L, completeOnlyFunctions, editBuffer, prefix, addCompletionCallback);
|
2021-10-29 21:25:12 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// find the key in the table
|
|
|
|
lua_pushlstring(L, prefix.data(), prefix.size());
|
2022-02-18 00:41:20 +00:00
|
|
|
safeGetTable(L, -2);
|
2021-10-29 21:25:12 +01:00
|
|
|
lua_remove(L, -2);
|
|
|
|
|
2022-02-18 00:41:20 +00:00
|
|
|
if (lua_istable(L, -1) || tryReplaceTopWithIndex(L))
|
2022-01-21 16:23:02 +00:00
|
|
|
{
|
2022-02-18 00:41:20 +00:00
|
|
|
completeOnlyFunctions = lookup[sep] == ':';
|
|
|
|
lookup.remove_prefix(sep + 1);
|
2022-01-21 16:23:02 +00:00
|
|
|
}
|
2022-02-18 00:41:20 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Unable to search for keys, so stop searching
|
2021-10-29 21:25:12 +01:00
|
|
|
break;
|
2022-02-18 00:41:20 +00:00
|
|
|
}
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
|
2022-02-11 18:43:14 +00:00
|
|
|
void getCompletions(lua_State* L, const std::string& editBuffer, const AddCompletionCallback& addCompletionCallback)
|
|
|
|
{
|
|
|
|
completeIndexer(L, editBuffer, addCompletionCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void icGetCompletions(ic_completion_env_t* cenv, const char* editBuffer)
|
|
|
|
{
|
|
|
|
auto* L = reinterpret_cast<lua_State*>(ic_completion_arg(cenv));
|
|
|
|
|
|
|
|
getCompletions(L, std::string(editBuffer), [cenv](const std::string& completion, const std::string& display) {
|
|
|
|
ic_add_completion_ex(cenv, completion.data(), display.data(), nullptr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-03 23:09:37 +00:00
|
|
|
static bool isMethodOrFunctionChar(const char* s, long len)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-02-03 23:09:37 +00:00
|
|
|
char c = *s;
|
|
|
|
return len == 1 && (isalnum(c) || c == '.' || c == ':' || c == '_');
|
|
|
|
}
|
|
|
|
|
|
|
|
static void completeRepl(ic_completion_env_t* cenv, const char* editBuffer)
|
|
|
|
{
|
2022-02-11 18:43:14 +00:00
|
|
|
ic_complete_word(cenv, editBuffer, icGetCompletions, isMethodOrFunctionChar);
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
2022-03-04 16:19:20 +00:00
|
|
|
static void loadHistory(const char* name)
|
2022-01-06 22:10:07 +00:00
|
|
|
{
|
2022-03-04 16:19:20 +00:00
|
|
|
std::string path;
|
2022-01-06 22:10:07 +00:00
|
|
|
|
2022-03-04 16:19:20 +00:00
|
|
|
if (const char* home = getenv("HOME"))
|
|
|
|
{
|
|
|
|
path = joinPaths(home, name);
|
|
|
|
}
|
|
|
|
else if (const char* userProfile = getenv("USERPROFILE"))
|
|
|
|
{
|
|
|
|
path = joinPaths(userProfile, name);
|
2022-01-06 22:10:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 16:19:20 +00:00
|
|
|
if (!path.empty())
|
|
|
|
ic_set_history(path.c_str(), -1 /* default entries (= 200) */);
|
|
|
|
}
|
2022-01-06 22:10:07 +00:00
|
|
|
|
2022-01-21 16:23:02 +00:00
|
|
|
static void runReplImpl(lua_State* L)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-02-03 23:09:37 +00:00
|
|
|
ic_set_default_completer(completeRepl, L);
|
|
|
|
|
2022-05-06 00:52:48 +01:00
|
|
|
// Reset the locale to C
|
|
|
|
setlocale(LC_ALL, "C");
|
|
|
|
|
2022-02-03 23:09:37 +00:00
|
|
|
// Make brace matching easier to see
|
|
|
|
ic_style_def("ic-bracematch", "teal");
|
|
|
|
|
|
|
|
// Prevent auto insertion of braces
|
|
|
|
ic_enable_brace_insertion(false);
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2022-03-04 16:19:20 +00:00
|
|
|
// Loads history from the given file; isocline automatically saves the history on process exit
|
|
|
|
loadHistory(".luau_history");
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
std::string buffer;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2022-02-11 18:43:14 +00:00
|
|
|
const char* prompt = buffer.empty() ? "" : ">";
|
|
|
|
std::unique_ptr<char, void (*)(void*)> line(ic_readline(prompt), free);
|
2022-02-03 23:09:37 +00:00
|
|
|
if (!line)
|
2021-10-29 21:25:12 +01:00
|
|
|
break;
|
|
|
|
|
2022-02-11 18:43:14 +00:00
|
|
|
if (buffer.empty() && runCode(L, std::string("return ") + line.get()) == std::string())
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-02-11 18:43:14 +00:00
|
|
|
ic_history_add(line.get());
|
2021-10-29 21:25:12 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-03 23:09:37 +00:00
|
|
|
if (!buffer.empty())
|
|
|
|
buffer += "\n";
|
2022-02-11 18:43:14 +00:00
|
|
|
buffer += line.get();
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
std::string error = runCode(L, buffer);
|
|
|
|
|
|
|
|
if (error.length() >= 5 && error.compare(error.length() - 5, 5, "<eof>") == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error.length())
|
|
|
|
{
|
|
|
|
fprintf(stdout, "%s\n", error.c_str());
|
|
|
|
}
|
|
|
|
|
2022-02-03 23:09:37 +00:00
|
|
|
ic_history_add(buffer.c_str());
|
2021-10-29 21:25:12 +01:00
|
|
|
buffer.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-21 16:23:02 +00:00
|
|
|
static void runRepl()
|
|
|
|
{
|
|
|
|
std::unique_ptr<lua_State, void (*)(lua_State*)> globalState(luaL_newstate(), lua_close);
|
|
|
|
lua_State* L = globalState.get();
|
|
|
|
|
|
|
|
setupState(L);
|
2022-08-04 22:27:28 +01:00
|
|
|
|
|
|
|
// setup Ctrl+C handling
|
|
|
|
replState = L;
|
|
|
|
#ifdef _WIN32
|
|
|
|
SetConsoleCtrlHandler(sigintHandler, TRUE);
|
|
|
|
#else
|
|
|
|
signal(SIGINT, sigintHandler);
|
|
|
|
#endif
|
|
|
|
|
2022-01-21 16:23:02 +00:00
|
|
|
luaL_sandboxthread(L);
|
|
|
|
runReplImpl(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
// `repl` is used it indicate if a repl should be started after executing the file.
|
|
|
|
static bool runFile(const char* name, lua_State* GL, bool repl)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
std::optional<std::string> source = readFile(name);
|
|
|
|
if (!source)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error opening %s\n", name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// module needs to run in a new thread, isolated from the rest
|
|
|
|
lua_State* L = lua_newthread(GL);
|
|
|
|
|
|
|
|
// new thread needs to have the globals sandboxed
|
|
|
|
luaL_sandboxthread(L);
|
|
|
|
|
|
|
|
std::string chunkname = "=" + std::string(name);
|
|
|
|
|
2021-12-10 21:17:10 +00:00
|
|
|
std::string bytecode = Luau::compile(*source, copts());
|
2021-10-29 21:25:12 +01:00
|
|
|
int status = 0;
|
|
|
|
|
2021-11-18 22:21:07 +00:00
|
|
|
if (luau_load(L, chunkname.c_str(), bytecode.data(), bytecode.size(), 0) == 0)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-10-13 23:59:53 +01:00
|
|
|
if (codegen)
|
|
|
|
Luau::CodeGen::compile(L, -1);
|
|
|
|
|
2021-12-10 21:17:10 +00:00
|
|
|
if (coverageActive())
|
|
|
|
coverageTrack(L, -1);
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
status = lua_resume(L, NULL, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = LUA_ERRSYNTAX;
|
|
|
|
}
|
|
|
|
|
2021-11-18 22:21:07 +00:00
|
|
|
if (status != 0)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2021-11-05 02:42:00 +00:00
|
|
|
std::string error;
|
|
|
|
|
|
|
|
if (status == LUA_YIELD)
|
|
|
|
{
|
|
|
|
error = "thread yielded unexpectedly";
|
|
|
|
}
|
|
|
|
else if (const char* str = lua_tostring(L, -1))
|
|
|
|
{
|
|
|
|
error = str;
|
|
|
|
}
|
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
error += "\nstacktrace:\n";
|
|
|
|
error += lua_debugtrace(L);
|
|
|
|
|
|
|
|
fprintf(stderr, "%s", error.c_str());
|
|
|
|
}
|
2021-11-18 22:21:07 +00:00
|
|
|
|
2022-01-21 16:23:02 +00:00
|
|
|
if (repl)
|
|
|
|
{
|
|
|
|
runReplImpl(L);
|
|
|
|
}
|
2021-11-18 22:21:07 +00:00
|
|
|
lua_pop(GL, 1);
|
|
|
|
return status == 0;
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void report(const char* name, const Luau::Location& location, const char* type, const char* message)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s(%d,%d): %s: %s\n", name, location.begin.line + 1, location.begin.column + 1, type, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reportError(const char* name, const Luau::ParseError& error)
|
|
|
|
{
|
|
|
|
report(name, error.getLocation(), "SyntaxError", error.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reportError(const char* name, const Luau::CompileError& error)
|
|
|
|
{
|
|
|
|
report(name, error.getLocation(), "CompileError", error.what());
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:33:43 +01:00
|
|
|
static std::string getCodegenAssembly(const char* name, const std::string& bytecode, Luau::CodeGen::AssemblyOptions options)
|
2022-10-13 23:59:53 +01:00
|
|
|
{
|
|
|
|
std::unique_ptr<lua_State, void (*)(lua_State*)> globalState(luaL_newstate(), lua_close);
|
|
|
|
lua_State* L = globalState.get();
|
|
|
|
|
|
|
|
if (luau_load(L, name, bytecode.data(), bytecode.size(), 0) == 0)
|
2022-10-21 18:33:43 +01:00
|
|
|
return Luau::CodeGen::getAssembly(L, -1, options);
|
2022-10-13 23:59:53 +01:00
|
|
|
|
|
|
|
fprintf(stderr, "Error loading bytecode %s\n", name);
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
static void annotateInstruction(void* context, std::string& text, int fid, int instpos)
|
2022-10-21 18:33:43 +01:00
|
|
|
{
|
|
|
|
Luau::BytecodeBuilder& bcb = *(Luau::BytecodeBuilder*)context;
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
bcb.annotateInstruction(text, fid, instpos);
|
2022-10-21 18:33:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct CompileStats
|
|
|
|
{
|
|
|
|
size_t lines;
|
|
|
|
size_t bytecode;
|
|
|
|
size_t codegen;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool compileFile(const char* name, CompileFormat format, CompileStats& stats)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
std::optional<std::string> source = readFile(name);
|
|
|
|
if (!source)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error opening %s\n", name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
// NOTE: Normally, you should use Luau::compile or luau_compile (see lua_require as an example)
|
|
|
|
// This function is much more complicated because it supports many output human-readable formats through internal interfaces
|
2022-10-21 18:33:43 +01:00
|
|
|
|
2021-10-29 21:25:12 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Luau::BytecodeBuilder bcb;
|
2023-01-27 21:28:45 +00:00
|
|
|
|
|
|
|
Luau::CodeGen::AssemblyOptions options;
|
|
|
|
options.outputBinary = format == CompileFormat::CodegenNull;
|
|
|
|
|
|
|
|
if (!options.outputBinary)
|
|
|
|
{
|
|
|
|
options.includeAssembly = format != CompileFormat::CodegenIr;
|
|
|
|
options.includeIr = format != CompileFormat::CodegenAsm;
|
|
|
|
options.includeOutlinedCode = format == CompileFormat::CodegenVerbose;
|
|
|
|
}
|
|
|
|
|
|
|
|
options.annotator = annotateInstruction;
|
|
|
|
options.annotatorContext = &bcb;
|
2021-11-18 22:21:07 +00:00
|
|
|
|
|
|
|
if (format == CompileFormat::Text)
|
|
|
|
{
|
2022-04-21 22:04:22 +01:00
|
|
|
bcb.setDumpFlags(Luau::BytecodeBuilder::Dump_Code | Luau::BytecodeBuilder::Dump_Source | Luau::BytecodeBuilder::Dump_Locals |
|
|
|
|
Luau::BytecodeBuilder::Dump_Remarks);
|
2021-11-18 22:21:07 +00:00
|
|
|
bcb.setDumpSource(*source);
|
|
|
|
}
|
2022-10-13 23:59:53 +01:00
|
|
|
else if (format == CompileFormat::Remarks)
|
|
|
|
{
|
|
|
|
bcb.setDumpFlags(Luau::BytecodeBuilder::Dump_Source | Luau::BytecodeBuilder::Dump_Remarks);
|
|
|
|
bcb.setDumpSource(*source);
|
|
|
|
}
|
2023-01-27 21:28:45 +00:00
|
|
|
else if (format == CompileFormat::Codegen || format == CompileFormat::CodegenAsm || format == CompileFormat::CodegenIr ||
|
|
|
|
format == CompileFormat::CodegenVerbose)
|
2022-10-21 18:33:43 +01:00
|
|
|
{
|
|
|
|
bcb.setDumpFlags(Luau::BytecodeBuilder::Dump_Code | Luau::BytecodeBuilder::Dump_Source | Luau::BytecodeBuilder::Dump_Locals |
|
|
|
|
Luau::BytecodeBuilder::Dump_Remarks);
|
|
|
|
bcb.setDumpSource(*source);
|
|
|
|
}
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2022-10-27 23:22:49 +01:00
|
|
|
Luau::Allocator allocator;
|
|
|
|
Luau::AstNameTable names(allocator);
|
|
|
|
Luau::ParseResult result = Luau::Parser::parse(source->c_str(), source->size(), names, allocator);
|
|
|
|
|
|
|
|
if (!result.errors.empty())
|
|
|
|
throw Luau::ParseErrors(result.errors);
|
|
|
|
|
|
|
|
stats.lines += result.lines;
|
|
|
|
|
|
|
|
Luau::compileOrThrow(bcb, result, names, copts());
|
2022-10-21 18:33:43 +01:00
|
|
|
stats.bytecode += bcb.getBytecode().size();
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2021-11-12 02:12:39 +00:00
|
|
|
switch (format)
|
|
|
|
{
|
2021-11-18 22:21:07 +00:00
|
|
|
case CompileFormat::Text:
|
2021-11-12 02:12:39 +00:00
|
|
|
printf("%s", bcb.dumpEverything().c_str());
|
|
|
|
break;
|
2022-10-13 23:59:53 +01:00
|
|
|
case CompileFormat::Remarks:
|
|
|
|
printf("%s", bcb.dumpSourceRemarks().c_str());
|
|
|
|
break;
|
2021-11-12 02:12:39 +00:00
|
|
|
case CompileFormat::Binary:
|
|
|
|
fwrite(bcb.getBytecode().data(), 1, bcb.getBytecode().size(), stdout);
|
|
|
|
break;
|
2022-10-13 23:59:53 +01:00
|
|
|
case CompileFormat::Codegen:
|
2023-01-27 21:28:45 +00:00
|
|
|
case CompileFormat::CodegenAsm:
|
|
|
|
case CompileFormat::CodegenIr:
|
2022-10-21 18:33:43 +01:00
|
|
|
case CompileFormat::CodegenVerbose:
|
|
|
|
printf("%s", getCodegenAssembly(name, bcb.getBytecode(), options).c_str());
|
|
|
|
break;
|
|
|
|
case CompileFormat::CodegenNull:
|
|
|
|
stats.codegen += getCodegenAssembly(name, bcb.getBytecode(), options).size();
|
2022-10-13 23:59:53 +01:00
|
|
|
break;
|
2022-04-29 02:04:52 +01:00
|
|
|
case CompileFormat::Null:
|
|
|
|
break;
|
2021-11-12 02:12:39 +00:00
|
|
|
}
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (Luau::ParseErrors& e)
|
|
|
|
{
|
|
|
|
for (auto& error : e.getErrors())
|
|
|
|
reportError(name, error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch (Luau::CompileError& e)
|
|
|
|
{
|
|
|
|
reportError(name, e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void displayHelp(const char* argv0)
|
|
|
|
{
|
|
|
|
printf("Usage: %s [--mode] [options] [file list]\n", argv0);
|
|
|
|
printf("\n");
|
|
|
|
printf("When mode and file list are omitted, an interactive REPL is started instead.\n");
|
|
|
|
printf("\n");
|
|
|
|
printf("Available modes:\n");
|
|
|
|
printf(" omitted: compile and run input files one by one\n");
|
2022-10-21 18:33:43 +01:00
|
|
|
printf(" --compile[=format]: compile input files and output resulting bytecode/assembly (binary, text, remarks, codegen)\n");
|
2021-10-29 21:25:12 +01:00
|
|
|
printf("\n");
|
|
|
|
printf("Available options:\n");
|
2022-01-21 16:23:02 +00:00
|
|
|
printf(" --coverage: collect code coverage while running the code and output results to coverage.out\n");
|
2022-01-14 16:06:31 +00:00
|
|
|
printf(" -h, --help: Display this usage message.\n");
|
2022-01-21 16:23:02 +00:00
|
|
|
printf(" -i, --interactive: Run an interactive REPL after executing the last script specified.\n");
|
2022-02-11 18:43:14 +00:00
|
|
|
printf(" -O<n>: compile with optimization level n (default 1, n should be between 0 and 2).\n");
|
|
|
|
printf(" -g<n>: compile with debug level n (default 1, n should be between 0 and 2).\n");
|
2021-10-29 21:25:12 +01:00
|
|
|
printf(" --profile[=N]: profile the code using N Hz sampling (default 10000) and output results to profile.out\n");
|
2022-01-14 16:06:31 +00:00
|
|
|
printf(" --timetrace: record compiler time tracing information into trace.json\n");
|
2022-10-13 23:59:53 +01:00
|
|
|
printf(" --codegen: execute code using native code generation\n");
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
2022-01-06 22:10:07 +00:00
|
|
|
static int assertionHandler(const char* expr, const char* file, int line, const char* function)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
printf("%s(%d): ASSERTION FAILED: %s\n", file, line, expr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-01-27 21:29:34 +00:00
|
|
|
int replMain(int argc, char** argv)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
Luau::assertHandler() = assertionHandler;
|
|
|
|
|
2022-07-21 21:36:41 +01:00
|
|
|
setLuauFlagsDefault();
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2022-01-14 16:06:31 +00:00
|
|
|
CliMode mode = CliMode::Unknown;
|
|
|
|
CompileFormat compileFormat{};
|
|
|
|
int profile = 0;
|
|
|
|
bool coverage = false;
|
2022-01-21 16:23:02 +00:00
|
|
|
bool interactive = false;
|
2022-01-14 16:06:31 +00:00
|
|
|
|
|
|
|
// Set the mode if the user has explicitly specified one.
|
|
|
|
int argStart = 1;
|
|
|
|
if (argc >= 2 && strncmp(argv[1], "--compile", strlen("--compile")) == 0)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-01-14 16:06:31 +00:00
|
|
|
argStart++;
|
|
|
|
mode = CliMode::Compile;
|
|
|
|
if (strcmp(argv[1], "--compile") == 0)
|
|
|
|
{
|
|
|
|
compileFormat = CompileFormat::Text;
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[1], "--compile=binary") == 0)
|
|
|
|
{
|
|
|
|
compileFormat = CompileFormat::Binary;
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[1], "--compile=text") == 0)
|
|
|
|
{
|
|
|
|
compileFormat = CompileFormat::Text;
|
|
|
|
}
|
2022-10-13 23:59:53 +01:00
|
|
|
else if (strcmp(argv[1], "--compile=remarks") == 0)
|
|
|
|
{
|
|
|
|
compileFormat = CompileFormat::Remarks;
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[1], "--compile=codegen") == 0)
|
|
|
|
{
|
|
|
|
compileFormat = CompileFormat::Codegen;
|
|
|
|
}
|
2023-01-27 21:28:45 +00:00
|
|
|
else if (strcmp(argv[1], "--compile=codegenasm") == 0)
|
|
|
|
{
|
|
|
|
compileFormat = CompileFormat::CodegenAsm;
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[1], "--compile=codegenir") == 0)
|
|
|
|
{
|
|
|
|
compileFormat = CompileFormat::CodegenIr;
|
|
|
|
}
|
2022-10-21 18:33:43 +01:00
|
|
|
else if (strcmp(argv[1], "--compile=codegenverbose") == 0)
|
|
|
|
{
|
|
|
|
compileFormat = CompileFormat::CodegenVerbose;
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[1], "--compile=codegennull") == 0)
|
|
|
|
{
|
|
|
|
compileFormat = CompileFormat::CodegenNull;
|
|
|
|
}
|
2022-04-29 02:04:52 +01:00
|
|
|
else if (strcmp(argv[1], "--compile=null") == 0)
|
|
|
|
{
|
|
|
|
compileFormat = CompileFormat::Null;
|
|
|
|
}
|
2022-01-14 16:06:31 +00:00
|
|
|
else
|
|
|
|
{
|
2022-01-21 16:23:02 +00:00
|
|
|
fprintf(stderr, "Error: Unrecognized value for '--compile' specified.\n");
|
|
|
|
return 1;
|
2022-01-14 16:06:31 +00:00
|
|
|
}
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
|
2022-01-14 16:06:31 +00:00
|
|
|
for (int i = argStart; i < argc; i++)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-01-14 16:06:31 +00:00
|
|
|
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
|
|
|
|
{
|
|
|
|
displayHelp(argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-01-21 16:23:02 +00:00
|
|
|
else if (strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--interactive") == 0)
|
|
|
|
{
|
|
|
|
interactive = true;
|
|
|
|
}
|
|
|
|
else if (strncmp(argv[i], "-O", 2) == 0)
|
|
|
|
{
|
|
|
|
int level = atoi(argv[i] + 2);
|
|
|
|
if (level < 0 || level > 2)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error: Optimization level must be between 0 and 2 inclusive.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
globalOptions.optimizationLevel = level;
|
|
|
|
}
|
2022-02-11 18:43:14 +00:00
|
|
|
else if (strncmp(argv[i], "-g", 2) == 0)
|
|
|
|
{
|
|
|
|
int level = atoi(argv[i] + 2);
|
|
|
|
if (level < 0 || level > 2)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error: Debug level must be between 0 and 2 inclusive.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
globalOptions.debugLevel = level;
|
|
|
|
}
|
2022-01-14 16:06:31 +00:00
|
|
|
else if (strcmp(argv[i], "--profile") == 0)
|
|
|
|
{
|
|
|
|
profile = 10000; // default to 10 KHz
|
|
|
|
}
|
|
|
|
else if (strncmp(argv[i], "--profile=", 10) == 0)
|
|
|
|
{
|
|
|
|
profile = atoi(argv[i] + 10);
|
|
|
|
}
|
2022-10-13 23:59:53 +01:00
|
|
|
else if (strcmp(argv[i], "--codegen") == 0)
|
|
|
|
{
|
|
|
|
codegen = true;
|
|
|
|
}
|
2022-01-14 16:06:31 +00:00
|
|
|
else if (strcmp(argv[i], "--coverage") == 0)
|
|
|
|
{
|
|
|
|
coverage = true;
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[i], "--timetrace") == 0)
|
|
|
|
{
|
|
|
|
FFlag::DebugLuauTimeTracing.value = true;
|
|
|
|
}
|
2022-04-21 22:04:22 +01:00
|
|
|
else if (strncmp(argv[i], "--fflags=", 9) == 0)
|
|
|
|
{
|
2022-07-21 21:36:41 +01:00
|
|
|
setLuauFlags(argv[i] + 9);
|
2022-04-21 22:04:22 +01:00
|
|
|
}
|
2022-01-14 16:06:31 +00:00
|
|
|
else if (argv[i][0] == '-')
|
|
|
|
{
|
2022-01-21 16:23:02 +00:00
|
|
|
fprintf(stderr, "Error: Unrecognized option '%s'.\n\n", argv[i]);
|
2022-01-14 16:06:31 +00:00
|
|
|
displayHelp(argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2021-11-12 02:12:39 +00:00
|
|
|
|
2022-07-21 21:36:41 +01:00
|
|
|
#if !defined(LUAU_ENABLE_TIME_TRACE)
|
|
|
|
if (FFlag::DebugLuauTimeTracing)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "To run with --timetrace, Luau has to be built with LUAU_ENABLE_TIME_TRACE enabled\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-10-13 23:59:53 +01:00
|
|
|
#if !LUA_CUSTOM_EXECUTION
|
|
|
|
if (codegen)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "To run with --codegen, Luau has to be built with LUA_CUSTOM_EXECUTION enabled\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-01-14 16:06:31 +00:00
|
|
|
const std::vector<std::string> files = getSourceFiles(argc, argv);
|
|
|
|
if (mode == CliMode::Unknown)
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
2022-01-14 16:06:31 +00:00
|
|
|
mode = files.empty() ? CliMode::Repl : CliMode::RunSourceFiles;
|
|
|
|
}
|
2021-11-12 02:12:39 +00:00
|
|
|
|
2022-10-13 23:59:53 +01:00
|
|
|
if (mode != CliMode::Compile && codegen && !Luau::CodeGen::isSupported())
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Cannot enable --codegen, native code generation is not supported in current configuration\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-01-14 16:06:31 +00:00
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case CliMode::Compile:
|
|
|
|
{
|
2021-11-12 02:12:39 +00:00
|
|
|
#ifdef _WIN32
|
2022-01-14 16:06:31 +00:00
|
|
|
if (compileFormat == CompileFormat::Binary)
|
2021-11-12 02:12:39 +00:00
|
|
|
_setmode(_fileno(stdout), _O_BINARY);
|
|
|
|
#endif
|
|
|
|
|
2022-10-21 18:33:43 +01:00
|
|
|
CompileStats stats = {};
|
2021-10-29 21:25:12 +01:00
|
|
|
int failed = 0;
|
|
|
|
|
2021-11-18 22:21:07 +00:00
|
|
|
for (const std::string& path : files)
|
2022-10-21 18:33:43 +01:00
|
|
|
failed += !compileFile(path.c_str(), compileFormat, stats);
|
|
|
|
|
|
|
|
if (compileFormat == CompileFormat::Null)
|
|
|
|
printf("Compiled %d KLOC into %d KB bytecode\n", int(stats.lines / 1000), int(stats.bytecode / 1024));
|
|
|
|
else if (compileFormat == CompileFormat::CodegenNull)
|
2022-11-04 17:02:37 +00:00
|
|
|
printf("Compiled %d KLOC into %d KB bytecode => %d KB native code\n", int(stats.lines / 1000), int(stats.bytecode / 1024),
|
|
|
|
int(stats.codegen / 1024));
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2022-01-14 16:06:31 +00:00
|
|
|
return failed ? 1 : 0;
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
2022-01-14 16:06:31 +00:00
|
|
|
case CliMode::Repl:
|
|
|
|
{
|
|
|
|
runRepl();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case CliMode::RunSourceFiles:
|
2021-10-29 21:25:12 +01:00
|
|
|
{
|
|
|
|
std::unique_ptr<lua_State, void (*)(lua_State*)> globalState(luaL_newstate(), lua_close);
|
|
|
|
lua_State* L = globalState.get();
|
|
|
|
|
|
|
|
setupState(L);
|
|
|
|
|
|
|
|
if (profile)
|
|
|
|
profilerStart(L, profile);
|
|
|
|
|
2021-12-10 21:17:10 +00:00
|
|
|
if (coverage)
|
|
|
|
coverageInit(L);
|
|
|
|
|
2021-11-18 22:21:07 +00:00
|
|
|
int failed = 0;
|
2021-10-29 21:25:12 +01:00
|
|
|
|
2022-01-21 16:23:02 +00:00
|
|
|
for (size_t i = 0; i < files.size(); ++i)
|
|
|
|
{
|
|
|
|
bool isLastFile = i == files.size() - 1;
|
|
|
|
failed += !runFile(files[i].c_str(), L, interactive && isLastFile);
|
|
|
|
}
|
2021-10-29 21:25:12 +01:00
|
|
|
|
|
|
|
if (profile)
|
|
|
|
{
|
|
|
|
profilerStop();
|
|
|
|
profilerDump("profile.out");
|
|
|
|
}
|
|
|
|
|
2021-12-10 21:17:10 +00:00
|
|
|
if (coverage)
|
|
|
|
coverageDump("coverage.out");
|
|
|
|
|
2022-01-14 16:06:31 +00:00
|
|
|
return failed ? 1 : 0;
|
|
|
|
}
|
|
|
|
case CliMode::Unknown:
|
|
|
|
default:
|
|
|
|
LUAU_ASSERT(!"Unhandled cli mode.");
|
2022-01-27 21:29:34 +00:00
|
|
|
return 1;
|
2021-10-29 21:25:12 +01:00
|
|
|
}
|
|
|
|
}
|