2020-11-12 14:24:33 -05:00
|
|
|
/*
|
|
|
|
|
** LuaJIT frontend. Runs commands, scripts, read-eval-print (REPL) etc.
|
|
|
|
|
** Copyright (C) 2005-2017 Mike Pall. See Copyright Notice in luajit.h
|
|
|
|
|
**
|
|
|
|
|
** Major portions taken verbatim or adapted from the Lua interpreter.
|
|
|
|
|
** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2020-11-13 15:18:09 -05:00
|
|
|
#include <signal.h>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <string>
|
2020-11-27 13:21:07 -05:00
|
|
|
#include "luastack.hpp"
|
2020-11-13 15:18:09 -05:00
|
|
|
#include "util.hpp"
|
2020-11-27 13:21:07 -05:00
|
|
|
#include "source.hpp"
|
2021-01-02 13:31:18 -05:00
|
|
|
#include "traceback.hpp"
|
2020-11-13 15:18:09 -05:00
|
|
|
|
2020-11-12 14:24:33 -05:00
|
|
|
|
|
|
|
|
// Add another error status.
|
|
|
|
|
#define LUA_PARTIAL (LUA_ERRERR + 10)
|
|
|
|
|
#define LUA_EOF (LUA_ERRERR + 11)
|
|
|
|
|
|
|
|
|
|
static lua_State *globalL = NULL;
|
|
|
|
|
|
2020-11-13 15:18:09 -05:00
|
|
|
|
2020-11-12 14:24:33 -05:00
|
|
|
static void lstop(lua_State *L, lua_Debug *ar)
|
|
|
|
|
{
|
|
|
|
|
(void)ar; /* unused arg. */
|
|
|
|
|
lua_sethook(L, NULL, 0, 0);
|
|
|
|
|
/* Avoid luaL_error -- a C hook doesn't add an extra frame. */
|
|
|
|
|
luaL_where(L, 0);
|
|
|
|
|
lua_pushfstring(L, "%sinterrupted!", lua_tostring(L, -1));
|
|
|
|
|
lua_error(L);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void laction(int i)
|
|
|
|
|
{
|
|
|
|
|
signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
|
|
|
|
|
terminate process (default action) */
|
|
|
|
|
lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void l_message(const char *msg)
|
|
|
|
|
{
|
|
|
|
|
fputs(msg, stderr);
|
|
|
|
|
fputc('\n', stderr);
|
|
|
|
|
fflush(stderr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int report(lua_State *L, int status)
|
|
|
|
|
{
|
|
|
|
|
if (status && !lua_isnil(L, -1))
|
|
|
|
|
{
|
|
|
|
|
const char *msg = lua_tostring(L, -1);
|
|
|
|
|
if (msg == NULL)
|
|
|
|
|
msg = "(error object is not a string)";
|
|
|
|
|
l_message(msg);
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
}
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 13:31:18 -05:00
|
|
|
static int docall(lua_State *L, int narg, int nret)
|
2020-11-12 14:24:33 -05:00
|
|
|
{
|
|
|
|
|
signal(SIGINT, laction);
|
2021-01-02 13:31:18 -05:00
|
|
|
int status = traceback_pcall(L, narg, nret);
|
2020-11-12 14:24:33 -05:00
|
|
|
signal(SIGINT, SIG_DFL);
|
|
|
|
|
/* force a complete garbage collection in case of errors */
|
|
|
|
|
if (status != LUA_OK)
|
|
|
|
|
lua_gc(L, LUA_GCCOLLECT, 0);
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Print a prompt, read a line of text, and push it on the stack.
|
|
|
|
|
// If it's the first line, translate "=" to "return"
|
|
|
|
|
//
|
|
|
|
|
static int pushline(lua_State *L, int firstline)
|
|
|
|
|
{
|
|
|
|
|
const int MAXINPUT = 1000;
|
|
|
|
|
char buf[MAXINPUT];
|
|
|
|
|
fputs(firstline ? "> " : ">> ", stdout);
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
if (fgets(buf, MAXINPUT, stdin))
|
|
|
|
|
{
|
|
|
|
|
size_t len = strlen(buf);
|
|
|
|
|
if (len > 0 && buf[len - 1] == '\n')
|
|
|
|
|
buf[len - 1] = '\0';
|
|
|
|
|
if (firstline && buf[0] == '=')
|
|
|
|
|
lua_pushfstring(L, "return %s", buf + 1);
|
|
|
|
|
else
|
|
|
|
|
lua_pushstring(L, buf);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Same as lua_loadbuffer, except: if the form passed in
|
|
|
|
|
// is a partial form, returns LUA_PARTIAL.
|
|
|
|
|
//
|
|
|
|
|
static int loadbuffer_partial(lua_State *L, const char *s, int len, const char *fn)
|
|
|
|
|
{
|
|
|
|
|
const char *eof = "'<eof>'";
|
|
|
|
|
int status = luaL_loadbuffer(L, s, len, fn);
|
|
|
|
|
if (status != LUA_ERRSYNTAX)
|
|
|
|
|
{
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
size_t lmsg;
|
|
|
|
|
const char *msg = lua_tolstring(L, -1, &lmsg);
|
|
|
|
|
const char *tp = msg + lmsg - (sizeof(eof) - 1);
|
|
|
|
|
if (strstr(msg, eof) == tp)
|
|
|
|
|
{
|
|
|
|
|
return LUA_PARTIAL;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read an entire form. On success, returns a closure. On EOF,
|
|
|
|
|
// returns just the LUA_EOF status code. On failure, returns an
|
|
|
|
|
// error message and a status code.
|
|
|
|
|
static int read_and_load(lua_State *L)
|
|
|
|
|
{
|
|
|
|
|
int status;
|
|
|
|
|
lua_settop(L, 0);
|
|
|
|
|
if (!pushline(L, 1))
|
|
|
|
|
return LUA_EOF; /* no input */
|
|
|
|
|
for (;;)
|
|
|
|
|
{ /* repeat until gets a complete line */
|
|
|
|
|
status = loadbuffer_partial(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
|
|
|
|
|
if (status != LUA_PARTIAL)
|
|
|
|
|
break;
|
|
|
|
|
lua_pop(L, 1); /* pop the error message */
|
|
|
|
|
if (!pushline(L, 0)) /* no more input? */
|
|
|
|
|
return LUA_EOF;
|
|
|
|
|
lua_pushliteral(L, "\n"); /* add a new line... */
|
|
|
|
|
lua_insert(L, -2); /* ...between the two lines */
|
|
|
|
|
lua_concat(L, 3); /* join them */
|
|
|
|
|
}
|
|
|
|
|
lua_remove(L, 1); /* remove line */
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void dotty(lua_State *L)
|
|
|
|
|
{
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
int status = read_and_load(L);
|
|
|
|
|
if (status == LUA_EOF) break;
|
2021-01-02 13:31:18 -05:00
|
|
|
if (status == LUA_OK) status = docall(L, 0, LUA_MULTRET);
|
2020-11-12 14:24:33 -05:00
|
|
|
report(L, status);
|
|
|
|
|
if (status == LUA_OK && lua_gettop(L) > 0)
|
|
|
|
|
{ /* any result to print? */
|
2020-11-27 13:21:07 -05:00
|
|
|
lua_getglobal(L, "pprint");
|
2020-11-15 16:49:42 -05:00
|
|
|
if (lua_isnil(L, -1)) {
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
lua_getglobal(L, "print");
|
|
|
|
|
}
|
2020-11-12 14:24:33 -05:00
|
|
|
lua_insert(L, 1);
|
|
|
|
|
if (lua_pcall(L, lua_gettop(L) - 1, 0, 0) != 0)
|
|
|
|
|
{
|
|
|
|
|
l_message(
|
|
|
|
|
lua_pushfstring(L, "error calling 'print' (%s)",
|
|
|
|
|
lua_tostring(L, -1)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
lua_settop(L, 0); /* clear stack */
|
|
|
|
|
fputs("\n", stdout);
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int pmain(lua_State *L)
|
|
|
|
|
{
|
|
|
|
|
globalL = L;
|
|
|
|
|
|
|
|
|
|
LUAJIT_VERSION_SYM(); /* Linker-enforced version check. */
|
|
|
|
|
|
2021-01-06 15:10:21 -05:00
|
|
|
LuaStack::register_all_userdata(L);
|
|
|
|
|
|
2021-01-02 13:31:18 -05:00
|
|
|
// Initialize the builtins, then copy a snapshot of the
|
|
|
|
|
// builtins to the registry. This will allow us to restore
|
|
|
|
|
// the builtins during source_rebuild operations.
|
|
|
|
|
source_load_builtins(L);
|
|
|
|
|
source_snapshot_builtins(L);
|
|
|
|
|
|
2020-12-05 18:57:53 -05:00
|
|
|
// Load the lua source.
|
|
|
|
|
source_update(L);
|
2021-01-02 13:31:18 -05:00
|
|
|
|
|
|
|
|
// Rebuild the global environment.
|
2020-12-05 18:57:53 -05:00
|
|
|
source_rebuild(L);
|
2020-11-12 14:24:33 -05:00
|
|
|
|
2021-01-02 13:31:18 -05:00
|
|
|
// Run all the autoinit functions.
|
|
|
|
|
source_autoinit(L);
|
|
|
|
|
|
2020-11-12 14:24:33 -05:00
|
|
|
dotty(L);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
int status;
|
|
|
|
|
lua_State *L = lua_open();
|
|
|
|
|
if (L == NULL)
|
|
|
|
|
{
|
|
|
|
|
l_message("cannot create state: not enough memory");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
status = lua_cpcall(L, pmain, NULL);
|
|
|
|
|
report(L, status);
|
|
|
|
|
lua_close(L);
|
|
|
|
|
return status ? -1 : 0;
|
|
|
|
|
}
|