2022-04-24 16:59:22 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
|
|
|
╚──────────────────────────────────────────────────────────────────────────────╝
|
|
|
|
│ │
|
|
|
|
│ Lua │
|
Bring Lua to 5.4.6. (#1214)
This essentially re-does the work of #875 on top of master.
This is what I did to check that Cosmo's Lua extensions still worked:
```
$ build/bootstrap/make MODE=aarch64 o/aarch64/third_party/lua/lua
$ ape o/aarch64/third_party/lua/lua
>: 10
10
>: 010
8
>: 0b10
2
>: string.byte("\e")
27
>: "Hello, %s" % {"world"}
Hello, world
>: "*" * 3
***
```
`luaL_traceback2` was used to show the stack trace with parameter
values; it's used in `LuaCallWithTrace`, which is used in Redbean to run
Lua code. You should be able to see the extended stack trace by running
something like this: `redbean -e "function a(b)c()end a(2)"` (with
"params" indicating the extended stack trace):
```
stack traceback:
[string "function a(b)c()end a(2)"]:1: in function 'a', params: b = 2;
[string "function a(b)c()end a(2)"]:1: in main chunk
```
@pkulchenko confirmed that I get the expected result with the updated
code.
This is what I did to check that Lua itself still worked:
```
$ cd third_party/lua/test/
$ ape ../../../o/aarch64/third_party/lua/lua all.lua
```
There's one test failure, in `files.lua`:
```
***** FILE 'files.lua'*****
testing i/o
../../../o/aarch64/third_party/lua/lua: files.lua:84: assertion failed!
stack traceback:
[C]: in function 'assert'
files.lua:84: in main chunk
(...tail calls...)
all.lua:195: in main chunk
[C]: in ?
.>>> closing state <<<
```
That isn't a result of these changes; the same test is failing in
master.
The failure is here:
```lua
if not _port then -- invalid seek
local status, msg, code = io.stdin:seek("set", 1000)
assert(not status and type(msg) == "string" and type(code) == "number")
end
```
The test expects a seek to offset 1,000 on stdin to fail — but it
doesn't. `status` ends up being the new offset rather than `nil`.
If I comment out that one test, the remaining tests succeed.
2024-06-16 00:13:08 +00:00
|
|
|
│ Copyright © 2004-2023 Lua.org, PUC-Rio. │
|
2022-04-24 16:59:22 +00:00
|
|
|
│ │
|
|
|
|
│ Permission is hereby granted, free of charge, to any person obtaining │
|
|
|
|
│ a copy of this software and associated documentation files (the │
|
|
|
|
│ "Software"), to deal in the Software without restriction, including │
|
|
|
|
│ without limitation the rights to use, copy, modify, merge, publish, │
|
|
|
|
│ distribute, sublicense, and/or sell copies of the Software, and to │
|
|
|
|
│ permit persons to whom the Software is furnished to do so, subject to │
|
|
|
|
│ the following conditions: │
|
|
|
|
│ │
|
|
|
|
│ The above copyright notice and this permission notice shall be │
|
|
|
|
│ included in all copies or substantial portions of the Software. │
|
|
|
|
│ │
|
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │
|
|
|
|
│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │
|
|
|
|
│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │
|
|
|
|
│ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │
|
|
|
|
│ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │
|
|
|
|
│ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │
|
|
|
|
│ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │
|
|
|
|
│ │
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-04-17 03:29:08 +00:00
|
|
|
#define lua_c
|
2023-07-09 12:11:25 +00:00
|
|
|
#include "third_party/lua/lrepl.h"
|
2022-04-17 03:29:08 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2022-06-09 03:01:28 +00:00
|
|
|
#include "libc/calls/struct/sigaction.h"
|
2022-04-27 12:39:39 +00:00
|
|
|
#include "libc/errno.h"
|
2022-04-23 01:55:28 +00:00
|
|
|
#include "libc/intrin/nomultics.h"
|
2022-04-17 03:29:08 +00:00
|
|
|
#include "libc/log/check.h"
|
2022-04-25 15:30:14 +00:00
|
|
|
#include "libc/macros.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/mem/alg.h"
|
|
|
|
#include "libc/mem/gc.h"
|
2022-04-28 16:42:36 +00:00
|
|
|
#include "libc/mem/mem.h"
|
2022-04-17 03:29:08 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2022-04-29 13:06:23 +00:00
|
|
|
#include "libc/stdio/stdio.h"
|
2022-04-27 12:39:39 +00:00
|
|
|
#include "libc/str/str.h"
|
2022-04-17 03:29:08 +00:00
|
|
|
#include "libc/sysv/consts/sa.h"
|
2023-07-10 11:29:46 +00:00
|
|
|
#include "libc/sysv/consts/sig.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/thread/thread.h"
|
2022-04-17 03:29:08 +00:00
|
|
|
#include "third_party/linenoise/linenoise.h"
|
2022-04-27 12:39:39 +00:00
|
|
|
#include "third_party/lua/cosmo.h"
|
2022-04-17 03:29:08 +00:00
|
|
|
#include "third_party/lua/lauxlib.h"
|
|
|
|
#include "third_party/lua/lprefix.h"
|
|
|
|
#include "third_party/lua/lua.h"
|
|
|
|
#include "third_party/lua/lualib.h"
|
Release Cosmopolitan v3.3
This change upgrades to GCC 12.3 and GNU binutils 2.42. The GNU linker
appears to have changed things so that only a single de-duplicated str
table is present in the binary, and it gets placed wherever the linker
wants, regardless of what the linker script says. To cope with that we
need to stop using .ident to embed licenses. As such, this change does
significant work to revamp how third party licenses are defined in the
codebase, using `.section .notice,"aR",@progbits`.
This new GCC 12.3 toolchain has support for GNU indirect functions. It
lets us support __target_clones__ for the first time. This is used for
optimizing the performance of libc string functions such as strlen and
friends so far on x86, by ensuring AVX systems favor a second codepath
that uses VEX encoding. It shaves some latency off certain operations.
It's a useful feature to have for scientific computing for the reasons
explained by the test/libcxx/openmp_test.cc example which compiles for
fifteen different microarchitectures. Thanks to the upgrades, it's now
also possible to use newer instruction sets, such as AVX512FP16, VNNI.
Cosmo now uses the %gs register on x86 by default for TLS. Doing it is
helpful for any program that links `cosmo_dlopen()`. Such programs had
to recompile their binaries at startup to change the TLS instructions.
That's not great, since it means every page in the executable needs to
be faulted. The work of rewriting TLS-related x86 opcodes, is moved to
fixupobj.com instead. This is great news for MacOS x86 users, since we
previously needed to morph the binary every time for that platform but
now that's no longer necessary. The only platforms where we need fixup
of TLS x86 opcodes at runtime are now Windows, OpenBSD, and NetBSD. On
Windows we morph TLS to point deeper into the TIB, based on a TlsAlloc
assignment, and on OpenBSD/NetBSD we morph %gs back into %fs since the
kernels do not allow us to specify a value for the %gs register.
OpenBSD users are now required to use APE Loader to run Cosmo binaries
and assimilation is no longer possible. OpenBSD kernel needs to change
to allow programs to specify a value for the %gs register, or it needs
to stop marking executable pages loaded by the kernel as mimmutable().
This release fixes __constructor__, .ctor, .init_array, and lastly the
.preinit_array so they behave the exact same way as glibc.
We no longer use hex constants to define math.h symbols like M_PI.
2024-02-20 19:12:09 +00:00
|
|
|
__static_yoink("lua_notice");
|
2022-04-24 16:59:22 +00:00
|
|
|
|
|
|
|
|
2022-04-25 15:30:14 +00:00
|
|
|
static const char *const kKeywordHints[] = {
|
|
|
|
"else", //
|
|
|
|
"elseif", //
|
|
|
|
"function", //
|
|
|
|
"function", //
|
|
|
|
"repeat", //
|
|
|
|
"then", //
|
|
|
|
"until", //
|
|
|
|
"while", //
|
|
|
|
};
|
|
|
|
|
2022-04-23 01:55:28 +00:00
|
|
|
bool lua_repl_blocking;
|
|
|
|
bool lua_repl_isterminal;
|
2022-04-24 16:59:22 +00:00
|
|
|
linenoiseCompletionCallback *lua_repl_completions_callback;
|
2022-04-23 01:55:28 +00:00
|
|
|
struct linenoiseState *lua_repl_linenoise;
|
2023-07-09 12:11:25 +00:00
|
|
|
const char *lua_progname;
|
2022-04-17 03:29:08 +00:00
|
|
|
static lua_State *globalL;
|
2023-09-02 03:49:13 +00:00
|
|
|
static char *g_historypath;
|
2022-04-17 03:29:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
** {==================================================================
|
|
|
|
** Read-Eval-Print Loop (REPL)
|
|
|
|
** ===================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if !defined(LUA_PROMPT)
|
|
|
|
#define LUA_PROMPT ">: "
|
|
|
|
#define LUA_PROMPT2 ">>: "
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(LUA_MAXINPUT)
|
|
|
|
#define LUA_MAXINPUT 512
|
|
|
|
#endif
|
|
|
|
|
2022-04-25 15:30:14 +00:00
|
|
|
|
2022-04-27 12:39:39 +00:00
|
|
|
static void lua_readline_addcompletion (linenoiseCompletions *c, char *s) {
|
|
|
|
char **p;
|
2022-04-25 15:30:14 +00:00
|
|
|
if ((p = realloc(c->cvec, (c->len + 1) * sizeof(*p)))) {
|
2022-04-17 03:29:08 +00:00
|
|
|
c->cvec = p;
|
2022-04-27 12:39:39 +00:00
|
|
|
c->cvec[c->len++] = s;
|
2022-04-17 03:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 15:30:14 +00:00
|
|
|
|
|
|
|
void lua_readline_completions (const char *p, linenoiseCompletions *c) {
|
|
|
|
int i;
|
2022-04-28 16:42:36 +00:00
|
|
|
size_t n;
|
2022-04-27 12:39:39 +00:00
|
|
|
bool found;
|
2022-04-17 03:29:08 +00:00
|
|
|
lua_State *L;
|
2023-09-02 03:49:13 +00:00
|
|
|
const char *a;
|
2022-04-17 03:29:08 +00:00
|
|
|
const char *name;
|
2023-09-02 03:49:13 +00:00
|
|
|
char *b, *s, *component;
|
2022-04-27 12:39:39 +00:00
|
|
|
|
|
|
|
// start searching globals
|
2022-04-17 03:29:08 +00:00
|
|
|
L = globalL;
|
|
|
|
lua_pushglobaltable(L);
|
2022-04-27 12:39:39 +00:00
|
|
|
|
|
|
|
// traverse parent objects
|
|
|
|
// split foo.bar and foo:bar
|
|
|
|
a = p;
|
|
|
|
b = strpbrk(a, ".:");
|
|
|
|
while (b) {
|
|
|
|
found = false;
|
2022-06-26 10:11:56 +00:00
|
|
|
if (lua_istable(L, -1)) {
|
|
|
|
component = strndup(a, b - a);
|
|
|
|
lua_pushnil(L); // search key
|
|
|
|
while (lua_next(L, -2)) {
|
|
|
|
if (lua_type(L, -2) == LUA_TSTRING) {
|
|
|
|
name = lua_tostring(L, -2);
|
|
|
|
if (!strcmp(name, component)) {
|
|
|
|
lua_remove(L, -3); // remove table
|
|
|
|
lua_remove(L, -2); // remove key
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
}
|
2022-06-26 10:11:56 +00:00
|
|
|
lua_pop(L, 1); // pop value
|
2022-04-27 12:39:39 +00:00
|
|
|
}
|
2022-06-26 10:11:56 +00:00
|
|
|
free(component);
|
2022-04-27 12:39:39 +00:00
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
lua_pop(L, 1); // pop table
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
a = b + 1;
|
|
|
|
b = strpbrk(a, ".:");
|
|
|
|
}
|
|
|
|
|
|
|
|
// search final object
|
2022-04-28 03:18:34 +00:00
|
|
|
if (lua_type(L, -1) == LUA_TTABLE) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
while (lua_next(L, -2)) {
|
|
|
|
if (lua_type(L, -2) == LUA_TSTRING) {
|
2022-04-28 16:42:36 +00:00
|
|
|
name = lua_tolstring(L, -2, &n);
|
2023-08-14 03:31:27 +00:00
|
|
|
if (startswithi(name, a) && (s = malloc(a - p + n + 1))) {
|
2022-04-28 16:42:36 +00:00
|
|
|
memcpy(s, p, a - p);
|
|
|
|
memcpy(s + (a - p), name, n + 1);
|
|
|
|
lua_readline_addcompletion(c, s);
|
2022-04-28 03:18:34 +00:00
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
}
|
2022-04-28 03:18:34 +00:00
|
|
|
lua_pop(L, 1);
|
2022-04-17 03:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
|
2022-04-28 03:18:34 +00:00
|
|
|
lua_pop(L, 1); // pop table
|
2022-04-27 12:39:39 +00:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAYLEN(kKeywordHints); ++i) {
|
2023-08-14 03:31:27 +00:00
|
|
|
if (startswithi(kKeywordHints[i], p)) {
|
2022-04-28 16:42:36 +00:00
|
|
|
if ((s = strdup(kKeywordHints[i]))) {
|
|
|
|
lua_readline_addcompletion(c, s);
|
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-25 15:30:14 +00:00
|
|
|
if (lua_repl_completions_callback) {
|
|
|
|
lua_repl_completions_callback(p, c);
|
|
|
|
}
|
2022-04-17 03:29:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 15:30:14 +00:00
|
|
|
|
|
|
|
char *lua_readline_hint (const char *p, const char **ansi1, const char **ansi2) {
|
|
|
|
char *h;
|
2022-04-17 03:29:08 +00:00
|
|
|
linenoiseCompletions c = {0};
|
|
|
|
lua_readline_completions(p, &c);
|
2022-04-25 15:30:14 +00:00
|
|
|
h = c.len == 1 ? strdup(c.cvec[0] + strlen(p)) : 0;
|
2022-04-17 03:29:08 +00:00
|
|
|
linenoiseFreeCompletions(&c);
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2022-04-25 15:30:14 +00:00
|
|
|
|
2022-04-17 03:29:08 +00:00
|
|
|
static void lua_freeline (lua_State *L, char *b) {
|
|
|
|
free(b);
|
|
|
|
}
|
|
|
|
|
2022-04-25 15:30:14 +00:00
|
|
|
|
2022-04-17 03:29:08 +00:00
|
|
|
/*
|
|
|
|
** Return the string to be used as a prompt by the interpreter. Leave
|
|
|
|
** the string (or nil, if using the default value) on the stack, to keep
|
|
|
|
** it anchored.
|
|
|
|
*/
|
|
|
|
static const char *get_prompt (lua_State *L, int firstline) {
|
|
|
|
if (lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2") == LUA_TNIL)
|
|
|
|
return (firstline ? LUA_PROMPT : LUA_PROMPT2); /* use the default */
|
|
|
|
else { /* apply 'tostring' over the value */
|
|
|
|
const char *p = luaL_tolstring(L, -1, NULL);
|
|
|
|
lua_remove(L, -2); /* remove original value */
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 15:30:14 +00:00
|
|
|
|
2022-04-17 03:29:08 +00:00
|
|
|
/* mark in error messages for incomplete statements */
|
|
|
|
#define EOFMARK "<eof>"
|
|
|
|
#define marklen (sizeof(EOFMARK)/sizeof(char) - 1)
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Check whether 'status' signals a syntax error and the error
|
|
|
|
** message at the top of the stack ends with the above mark for
|
|
|
|
** incomplete statements.
|
|
|
|
*/
|
|
|
|
static int incomplete (lua_State *L, int status) {
|
|
|
|
if (status == LUA_ERRSYNTAX) {
|
|
|
|
size_t lmsg;
|
|
|
|
const char *msg = lua_tolstring(L, -1, &lmsg);
|
|
|
|
if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0; /* else... */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Prompt the user, read a line, and push it into the Lua stack.
|
|
|
|
*/
|
2022-04-23 01:55:28 +00:00
|
|
|
static ssize_t pushline (lua_State *L, int firstline) {
|
2022-04-17 03:29:08 +00:00
|
|
|
char *b;
|
|
|
|
size_t l;
|
2022-04-23 01:55:28 +00:00
|
|
|
ssize_t rc;
|
|
|
|
char *prmt;
|
2022-04-17 03:29:08 +00:00
|
|
|
globalL = L;
|
2022-05-23 17:15:53 +00:00
|
|
|
prmt = strdup(get_prompt(L, firstline));
|
|
|
|
lua_pop(L, 1); /* remove prompt */
|
2022-04-23 01:55:28 +00:00
|
|
|
if (lua_repl_isterminal) {
|
2022-06-27 20:01:58 +00:00
|
|
|
lua_repl_unlock();
|
2022-04-27 12:39:39 +00:00
|
|
|
rc = linenoiseEdit(lua_repl_linenoise, prmt, &b, !firstline || lua_repl_blocking);
|
2022-04-23 01:55:28 +00:00
|
|
|
free(prmt);
|
|
|
|
if (rc != -1) {
|
|
|
|
if (b && *b) {
|
|
|
|
linenoiseHistoryLoad(g_historypath);
|
|
|
|
linenoiseHistoryAdd(b);
|
|
|
|
linenoiseHistorySave(g_historypath);
|
|
|
|
}
|
|
|
|
}
|
2022-06-27 20:01:58 +00:00
|
|
|
lua_repl_lock();
|
2022-04-23 01:55:28 +00:00
|
|
|
} else {
|
2022-06-27 20:01:58 +00:00
|
|
|
lua_repl_unlock();
|
2022-05-23 17:15:53 +00:00
|
|
|
fputs(prmt, stdout);
|
2023-07-09 12:11:25 +00:00
|
|
|
free(prmt);
|
2022-05-23 17:15:53 +00:00
|
|
|
fflush(stdout);
|
2022-04-23 01:55:28 +00:00
|
|
|
b = linenoiseGetLine(stdin);
|
2022-05-23 17:15:53 +00:00
|
|
|
if (b) {
|
|
|
|
rc = 1;
|
|
|
|
} else if (ferror(stdin)) {
|
|
|
|
rc = -1;
|
|
|
|
} else {
|
|
|
|
rc = 0;
|
|
|
|
}
|
2022-06-27 20:01:58 +00:00
|
|
|
lua_repl_lock();
|
2022-04-23 01:55:28 +00:00
|
|
|
}
|
2022-04-27 12:39:39 +00:00
|
|
|
if (!(rc == -1 && errno == EAGAIN)) {
|
2022-05-19 23:57:49 +00:00
|
|
|
write(1, "\n", 1);
|
2022-04-27 12:39:39 +00:00
|
|
|
}
|
2022-04-23 01:55:28 +00:00
|
|
|
if (rc == -1 || (!rc && !b)) {
|
|
|
|
return rc;
|
|
|
|
}
|
2022-04-17 03:29:08 +00:00
|
|
|
l = strlen(b);
|
|
|
|
if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
|
|
|
|
b[--l] = '\0'; /* remove it */
|
2022-04-25 15:30:14 +00:00
|
|
|
if (firstline && b[0] == '=') { /* for compatibility with 5.2, ... */
|
2022-04-17 03:29:08 +00:00
|
|
|
lua_pushfstring(L, "return %s", b + 1); /* change '=' to 'return' */
|
2022-04-25 15:30:14 +00:00
|
|
|
} else {
|
2022-04-17 03:29:08 +00:00
|
|
|
lua_pushlstring(L, b, l);
|
2022-04-25 15:30:14 +00:00
|
|
|
}
|
2022-04-17 03:29:08 +00:00
|
|
|
lua_freeline(L, b);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Try to compile line on the stack as 'return <line>;'; on return, stack
|
|
|
|
** has either compiled chunk or original line (if compilation failed).
|
|
|
|
*/
|
|
|
|
static int addreturn (lua_State *L) {
|
|
|
|
const char *line = lua_tostring(L, -1); /* original line */
|
|
|
|
const char *retline = lua_pushfstring(L, "return %s;", line);
|
|
|
|
int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin");
|
|
|
|
if (status == LUA_OK) {
|
|
|
|
lua_remove(L, -2); /* remove modified line */
|
|
|
|
} else {
|
|
|
|
lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Hook set by signal function to stop the interpreter.
|
|
|
|
*/
|
|
|
|
static void lstop (lua_State *L, lua_Debug *ar) {
|
|
|
|
(void)ar; /* unused arg. */
|
|
|
|
lua_sethook(L, NULL, 0, 0); /* reset hook */
|
|
|
|
luaL_error(L, "interrupted!");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Read multiple lines until a complete Lua statement
|
|
|
|
*/
|
|
|
|
static int multiline (lua_State *L) {
|
|
|
|
for (;;) { /* repeat until gets a complete statement */
|
|
|
|
size_t len;
|
|
|
|
const char *line = lua_tolstring(L, 1, &len); /* get what it has */
|
|
|
|
int status = luaL_loadbuffer(L, line, len, "=stdin"); /* try it */
|
2022-05-27 20:25:46 +00:00
|
|
|
if (!incomplete(L, status) || pushline(L, 0) != 1)
|
2022-04-17 03:29:08 +00:00
|
|
|
return status; /* cannot or should not try to add continuation line */
|
|
|
|
lua_pushliteral(L, "\n"); /* add newline... */
|
|
|
|
lua_insert(L, -2); /* ...between the two lines */
|
|
|
|
lua_concat(L, 3); /* join them */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-09 12:11:25 +00:00
|
|
|
void lua_initrepl(lua_State *L) {
|
2022-04-23 01:55:28 +00:00
|
|
|
const char *prompt;
|
2022-06-27 20:01:58 +00:00
|
|
|
lua_repl_lock();
|
2022-04-23 01:55:28 +00:00
|
|
|
if ((lua_repl_isterminal = linenoiseIsTerminal())) {
|
|
|
|
linenoiseSetCompletionCallback(lua_readline_completions);
|
|
|
|
linenoiseSetHintsCallback(lua_readline_hint);
|
|
|
|
linenoiseSetFreeHintsCallback(free);
|
|
|
|
prompt = get_prompt(L, 1);
|
2023-07-09 12:11:25 +00:00
|
|
|
if ((g_historypath = linenoiseGetHistoryPath(lua_progname))) {
|
2022-04-23 01:55:28 +00:00
|
|
|
if (linenoiseHistoryLoad(g_historypath) == -1) {
|
2024-07-29 08:39:36 +00:00
|
|
|
fprintf(stderr, "%r%s: failed to load history: %m\n", g_historypath);
|
2022-04-23 01:55:28 +00:00
|
|
|
free(g_historypath);
|
|
|
|
g_historypath = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lua_repl_linenoise = linenoiseBegin(prompt, 0, 1);
|
|
|
|
lua_pop(L, 1); /* remove prompt */
|
2023-10-04 05:34:45 +00:00
|
|
|
__ttyconf.replmode = true;
|
|
|
|
if (isatty(2)) {
|
|
|
|
__ttyconf.replstderr = true;
|
|
|
|
}
|
2022-04-23 01:55:28 +00:00
|
|
|
}
|
2022-06-27 20:01:58 +00:00
|
|
|
lua_repl_unlock();
|
2022-04-23 01:55:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void lua_freerepl(void) {
|
2022-06-27 20:01:58 +00:00
|
|
|
lua_repl_lock();
|
2023-10-04 05:34:45 +00:00
|
|
|
__ttyconf.replmode = false;
|
2022-04-23 01:55:28 +00:00
|
|
|
linenoiseEnd(lua_repl_linenoise);
|
|
|
|
free(g_historypath);
|
2022-06-27 20:01:58 +00:00
|
|
|
lua_repl_unlock();
|
2022-04-17 03:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Read a line and try to load (compile) it first as an expression (by
|
|
|
|
** adding "return " in front of it) and second as a statement. Return
|
|
|
|
** the final status of load/call with the resulting function (if any)
|
|
|
|
** in the top of the stack.
|
2022-04-23 01:55:28 +00:00
|
|
|
**
|
|
|
|
** returns -1 on eof
|
|
|
|
** returns -2 on error
|
2022-04-17 03:29:08 +00:00
|
|
|
*/
|
|
|
|
int lua_loadline (lua_State *L) {
|
2022-04-23 01:55:28 +00:00
|
|
|
ssize_t rc;
|
2022-04-17 03:29:08 +00:00
|
|
|
int status;
|
|
|
|
lua_settop(L, 0);
|
2022-06-27 20:01:58 +00:00
|
|
|
lua_repl_lock();
|
2022-04-23 01:55:28 +00:00
|
|
|
if ((rc = pushline(L, 1)) != 1) {
|
2022-06-27 20:01:58 +00:00
|
|
|
lua_repl_unlock();
|
2022-04-23 01:55:28 +00:00
|
|
|
return rc - 1; /* eof or error */
|
|
|
|
}
|
2022-05-27 20:25:46 +00:00
|
|
|
if ((status = addreturn(L)) != LUA_OK) /* 'return ...' did not work? */
|
2022-04-17 03:29:08 +00:00
|
|
|
status = multiline(L); /* try as command, maybe with continuation lines */
|
|
|
|
lua_remove(L, 1); /* remove line from the stack */
|
|
|
|
lua_assert(lua_gettop(L) == 1);
|
2022-06-27 20:01:58 +00:00
|
|
|
lua_repl_unlock();
|
2022-04-17 03:29:08 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-14 18:47:16 +00:00
|
|
|
void lua_sigint (lua_State *L, int sig) {
|
|
|
|
int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT;
|
|
|
|
lua_sethook(L, lstop, flag, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-04-17 03:29:08 +00:00
|
|
|
/*
|
|
|
|
** Function to be called at a C signal. Because a C signal cannot
|
|
|
|
** just change a Lua state (as there is no proper synchronization),
|
|
|
|
** this function only sets a hook that, when called, will stop the
|
|
|
|
** interpreter.
|
|
|
|
*/
|
|
|
|
static void laction (int i) {
|
2022-05-14 18:47:16 +00:00
|
|
|
lua_sigint(globalL, i);
|
2022-04-17 03:29:08 +00:00
|
|
|
int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT;
|
|
|
|
lua_sethook(globalL, lstop, flag, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Message handler used to run all chunks
|
|
|
|
*/
|
|
|
|
static int msghandler (lua_State *L) {
|
|
|
|
const char *msg = lua_tostring(L, 1);
|
|
|
|
if (msg == NULL) { /* is error object not a string? */
|
|
|
|
if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */
|
|
|
|
lua_type(L, -1) == LUA_TSTRING) /* that produces a string? */
|
|
|
|
return 1; /* that is the message */
|
|
|
|
else
|
|
|
|
msg = lua_pushfstring(L, "(error object is a %s value)",
|
|
|
|
luaL_typename(L, 1));
|
|
|
|
}
|
|
|
|
luaL_traceback(L, L, msg, 1); /* append a standard traceback */
|
|
|
|
return 1; /* return the traceback */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Interface to 'lua_pcall', which sets appropriate message function
|
|
|
|
** and C-signal handler. Used to run all chunks.
|
|
|
|
*/
|
|
|
|
int lua_runchunk (lua_State *L, int narg, int nres) {
|
|
|
|
struct sigaction sa, saold;
|
|
|
|
int status;
|
|
|
|
int base = lua_gettop(L) - narg; /* function index */
|
|
|
|
lua_pushcfunction(L, msghandler); /* push message handler */
|
|
|
|
lua_insert(L, base); /* put it under function and args */
|
|
|
|
globalL = L; /* to be available to 'laction' */
|
|
|
|
sa.sa_flags = SA_RESETHAND; /* if another int happens, terminate */
|
|
|
|
sa.sa_handler = laction;
|
|
|
|
sigemptyset(&sa.sa_mask); /* do not mask any signal */
|
|
|
|
sigaction(SIGINT, &sa, &saold);
|
|
|
|
status = lua_pcall(L, narg, nres, base);
|
|
|
|
sigaction(SIGINT, &saold, 0); /* restore C-signal handler */
|
|
|
|
lua_remove(L, base); /* remove message handler from the stack */
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Prints an error message, adding the program name in front of it
|
|
|
|
** (if present)
|
|
|
|
*/
|
|
|
|
void lua_l_message (const char *pname, const char *msg) {
|
|
|
|
if (pname) lua_writestringerror("%s: ", pname);
|
|
|
|
lua_writestringerror("%s\n", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Prints (calling the Lua 'print' function) any values on the stack
|
|
|
|
*/
|
|
|
|
void lua_l_print (lua_State *L) {
|
|
|
|
int n = lua_gettop(L);
|
|
|
|
if (n > 0) { /* any result to be printed? */
|
|
|
|
luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
|
|
|
|
lua_getglobal(L, "print");
|
|
|
|
lua_insert(L, 1);
|
|
|
|
if (lua_pcall(L, n, 0, 0) != LUA_OK)
|
2023-07-09 12:11:25 +00:00
|
|
|
lua_l_message(lua_progname, lua_pushfstring(L, "error calling 'print' (%s)",
|
2022-04-17 03:29:08 +00:00
|
|
|
lua_tostring(L, -1)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Check whether 'status' is not OK and, if so, prints the error
|
|
|
|
** message on the top of the stack. It assumes that the error object
|
|
|
|
** is a string, as it was either generated by Lua or by 'msghandler'.
|
|
|
|
*/
|
|
|
|
int lua_report (lua_State *L, int status) {
|
|
|
|
if (status != LUA_OK) {
|
|
|
|
const char *msg = lua_tostring(L, -1);
|
2023-07-09 12:11:25 +00:00
|
|
|
lua_l_message(lua_progname, msg);
|
2022-04-17 03:29:08 +00:00
|
|
|
lua_pop(L, 1); /* remove message */
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|