Make fixes and improvements

- Fix handling of precision in hex float formatting
- Enhance the cocmd interpreter for system() and popen()
- Manually ran the Lua unit tests, which are now passing
- Let stdio i/o operations happen when file is in error state
- We're now saving and restoring xmm in ftrace out of paranoia
This commit is contained in:
Justine Tunney 2023-07-09 05:11:25 -07:00
parent 95fbdb4f76
commit 41396ff48a
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
43 changed files with 495 additions and 261 deletions

View file

@ -26,6 +26,7 @@
*/
#define lua_c
#include "third_party/lua/lrepl.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/errno.h"
@ -44,7 +45,6 @@
#include "third_party/lua/cosmo.h"
#include "third_party/lua/lauxlib.h"
#include "third_party/lua/lprefix.h"
#include "third_party/lua/lrepl.h"
#include "third_party/lua/lua.h"
#include "third_party/lua/lualib.h"
// clang-format off
@ -70,8 +70,8 @@ bool lua_repl_blocking;
bool lua_repl_isterminal;
linenoiseCompletionCallback *lua_repl_completions_callback;
struct linenoiseState *lua_repl_linenoise;
const char *lua_progname;
static lua_State *globalL;
static const char *g_progname;
static const char *g_historypath;
/*
@ -253,6 +253,7 @@ static ssize_t pushline (lua_State *L, int firstline) {
} else {
lua_repl_unlock();
fputs(prmt, stdout);
free(prmt);
fflush(stdout);
b = linenoiseGetLine(stdin);
if (b) {
@ -328,16 +329,15 @@ static int multiline (lua_State *L) {
}
void lua_initrepl(lua_State *L, const char *progname) {
void lua_initrepl(lua_State *L) {
const char *prompt;
lua_repl_lock();
g_progname = progname;
if ((lua_repl_isterminal = linenoiseIsTerminal())) {
linenoiseSetCompletionCallback(lua_readline_completions);
linenoiseSetHintsCallback(lua_readline_hint);
linenoiseSetFreeHintsCallback(free);
prompt = get_prompt(L, 1);
if ((g_historypath = linenoiseGetHistoryPath(progname))) {
if ((g_historypath = linenoiseGetHistoryPath(lua_progname))) {
if (linenoiseHistoryLoad(g_historypath) == -1) {
fprintf(stderr, "%r%s: failed to load history: %m%n", g_historypath);
free(g_historypath);
@ -469,7 +469,7 @@ void lua_l_print (lua_State *L) {
lua_getglobal(L, "print");
lua_insert(L, 1);
if (lua_pcall(L, n, 0, 0) != LUA_OK)
lua_l_message(g_progname, lua_pushfstring(L, "error calling 'print' (%s)",
lua_l_message(lua_progname, lua_pushfstring(L, "error calling 'print' (%s)",
lua_tostring(L, -1)));
}
}
@ -483,7 +483,7 @@ void lua_l_print (lua_State *L) {
int lua_report (lua_State *L, int status) {
if (status != LUA_OK) {
const char *msg = lua_tostring(L, -1);
lua_l_message(g_progname, msg);
lua_l_message(lua_progname, msg);
lua_pop(L, 1); /* remove message */
}
return status;

View file

@ -8,6 +8,7 @@ COSMOPOLITAN_C_START_
extern bool lua_repl_blocking;
extern bool lua_repl_isterminal;
extern const char *lua_progname;
extern struct linenoiseState *lua_repl_linenoise;
extern linenoiseCompletionCallback *lua_repl_completions_callback;
@ -16,8 +17,8 @@ void lua_repl_lock(void);
void lua_repl_unlock(void);
int lua_loadline(lua_State *);
void lua_l_print(lua_State *);
void lua_initrepl(lua_State *);
void lua_sigint(lua_State *, int);
void lua_initrepl(lua_State *, const char *);
int lua_report(lua_State *, int);
int lua_runchunk(lua_State *, int, int);
void lua_l_message(const char *, const char *);

View file

@ -57,7 +57,7 @@ Lua 5.4.3 (MIT License)\\n\
Copyright 19942021 Lua.org, PUC-Rio.\"");
asm(".include \"libc/disclaimer.inc\"");
STATIC_STACK_SIZE(0x40000);
STATIC_STACK_SIZE(0x80000);
#if !defined(LUA_PROGNAME)
#define LUA_PROGNAME "lua"
@ -71,7 +71,6 @@ STATIC_STACK_SIZE(0x40000);
static lua_State *globalL = NULL;
static const char *progname = LUA_PROGNAME;
static bool lua_stdin_is_tty(void) {
@ -80,7 +79,7 @@ static bool lua_stdin_is_tty(void) {
static void print_usage (const char *badoption) {
lua_writestringerror("%s: ", progname);
lua_writestringerror("%s: ", lua_progname);
if (badoption[1] == 'e' || badoption[1] == 'l')
lua_writestringerror("'%s' needs argument\n", badoption);
else
@ -97,7 +96,7 @@ static void print_usage (const char *badoption) {
" -- stop handling options\n"
" - stop handling options and execute stdin\n"
,
progname);
lua_progname);
}
@ -304,9 +303,9 @@ static int handle_luainit (lua_State *L) {
*/
static void doREPL (lua_State *L) {
int status;
const char *oldprogname = progname;
progname = NULL; /* no 'progname' on errors in interactive mode */
lua_initrepl(L, LUA_PROGNAME);
const char *oldprogname = lua_progname;
lua_progname = NULL; /* no 'progname' on errors in interactive mode */
lua_initrepl(L);
for (;;) {
if (lua_repl_isterminal)
linenoiseEnableRawMode(0);
@ -325,7 +324,7 @@ static void doREPL (lua_State *L) {
lua_pushfstring(L, "read error: %s", strerror(errno));
lua_report(L, status);
lua_freerepl();
progname = oldprogname;
lua_progname = oldprogname;
return;
}
if (status == LUA_OK)
@ -338,7 +337,7 @@ static void doREPL (lua_State *L) {
}
lua_freerepl();
lua_settop(L, 0); /* clear stack */
progname = oldprogname;
lua_progname = oldprogname;
}
/* }================================================================== */
@ -354,7 +353,8 @@ static int pmain (lua_State *L) {
int script;
int args = collectargs(argv, &script);
luaL_checkversion(L); /* check that interpreter has correct version */
if (argv[0] && argv[0][0]) progname = argv[0];
lua_progname = LUA_PROGNAME;
if (argv[0] && argv[0][0]) lua_progname = argv[0];
if (args == has_error) { /* bad arg? */
print_usage(argv[script]); /* 'script' has index of bad arg. */
return 0;

View file

@ -792,9 +792,18 @@ assert(os.date(string.rep("%", 200)) == string.rep("%", 100))
local function checkDateTable (t)
_G.D = os.date("*t", t)
assert(os.time(D) == t)
load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and
D.hour==%H and D.min==%M and D.sec==%S and
D.wday==%w+1 and D.yday==%j)]], t))()
-- [jart] rewrote test due to octal
assert(string.format('%d', D.year) == os.date('%Y', t))
assert(string.format('%02d', D.month) == os.date('%m', t))
assert(string.format('%02d', D.day) == os.date('%d', t))
assert(string.format('%02d', D.hour) == os.date('%H', t))
assert(string.format('%02d', D.min) == os.date('%M', t))
assert(string.format('%02d', D.sec) == os.date('%S', t))
assert(string.format('%d', D.wday - 1) == os.date('%w', t))
assert(string.format('%03d', D.yday) == os.date('%j', t))
-- load(os.date([[assert(D.year==%Y and D.month==%m and D.day==%d and
-- D.hour==%H and D.min==%M and D.sec==%S and
-- D.wday==%w+1 and D.yday==%j)]], t))()
_G.D = nil
end

View file

@ -25,7 +25,7 @@ assert("\099" == '\99')
assert("\099\n" == 'c\10')
assert('\0\0\0alo' == '\0' .. '\0\0' .. 'alo')
assert(010 .. 020 .. -030 == "1020-30")
assert(10 .. 20 .. -30 == "1020-30") -- [jart] octal extension
-- hexadecimal escapes
assert("\x00\x05\x10\x1f\x3C\xfF\xe8" == "\0\5\16\31\60\255\232")

View file

@ -401,7 +401,7 @@ assert(tonumber("-0x"..string.rep("f", (intbits//4))) == 1)
-- testing 'tonumber' with base
assert(tonumber(' 001010 ', 2) == 10)
assert(tonumber(' 001010 ', 10) == 001010)
assert(tonumber(' 001010 ', 10) == 1010) -- [jart] octal extension fix
assert(tonumber(' -1010 ', 2) == -10)
assert(tonumber('10', 36) == 36)
assert(tonumber(' -10 ', 36) == -36)