mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 11:37:35 +00:00
0dbf01bf1d
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.
233 lines
8.7 KiB
C
233 lines
8.7 KiB
C
/*-*- 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 │
|
|
│ Copyright © 2004-2023 Lua.org, PUC-Rio. │
|
|
│ │
|
|
│ 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. │
|
|
│ │
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#define lmem_c
|
|
#define LUA_CORE
|
|
|
|
#include "libc/log/log.h"
|
|
#include "third_party/lua/ldebug.h"
|
|
#include "third_party/lua/ldo.h"
|
|
#include "third_party/lua/lgc.h"
|
|
#include "third_party/lua/lmem.h"
|
|
#include "third_party/lua/lobject.h"
|
|
#include "third_party/lua/lprefix.h"
|
|
#include "third_party/lua/lstate.h"
|
|
#include "third_party/lua/lua.h"
|
|
__static_yoink("lua_notice");
|
|
|
|
|
|
/*
|
|
** About the realloc function:
|
|
** void *frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
|
|
** ('osize' is the old size, 'nsize' is the new size)
|
|
**
|
|
** - frealloc(ud, p, x, 0) frees the block 'p' and returns NULL.
|
|
** Particularly, frealloc(ud, NULL, 0, 0) does nothing,
|
|
** which is equivalent to free(NULL) in ISO C.
|
|
**
|
|
** - frealloc(ud, NULL, x, s) creates a new block of size 's'
|
|
** (no matter 'x'). Returns NULL if it cannot create the new block.
|
|
**
|
|
** - otherwise, frealloc(ud, b, x, y) reallocates the block 'b' from
|
|
** size 'x' to size 'y'. Returns NULL if it cannot reallocate the
|
|
** block to the new size.
|
|
*/
|
|
|
|
|
|
/*
|
|
** Macro to call the allocation function.
|
|
*/
|
|
#define callfrealloc(g,block,os,ns) ((*g->frealloc)(g->ud, block, os, ns))
|
|
|
|
|
|
/*
|
|
** When an allocation fails, it will try again after an emergency
|
|
** collection, except when it cannot run a collection. The GC should
|
|
** not be called while the state is not fully built, as the collector
|
|
** is not yet fully initialized. Also, it should not be called when
|
|
** 'gcstopem' is true, because then the interpreter is in the middle of
|
|
** a collection step.
|
|
*/
|
|
#define cantryagain(g) (completestate(g) && !g->gcstopem)
|
|
|
|
|
|
|
|
|
|
#if defined(EMERGENCYGCTESTS)
|
|
/*
|
|
** First allocation will fail except when freeing a block (frees never
|
|
** fail) and when it cannot try again; this fail will trigger 'tryagain'
|
|
** and a full GC cycle at every allocation.
|
|
*/
|
|
static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {
|
|
if (ns > 0 && cantryagain(g))
|
|
return NULL; /* fail */
|
|
else /* normal allocation */
|
|
return callfrealloc(g, block, os, ns);
|
|
}
|
|
#else
|
|
#define firsttry(g,block,os,ns) callfrealloc(g, block, os, ns)
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
** {==================================================================
|
|
** Functions to allocate/deallocate arrays for the Parser
|
|
** ===================================================================
|
|
*/
|
|
|
|
/*
|
|
** Minimum size for arrays during parsing, to avoid overhead of
|
|
** reallocating to size 1, then 2, and then 4. All these arrays
|
|
** will be reallocated to exact sizes or erased when parsing ends.
|
|
*/
|
|
#define MINSIZEARRAY 4
|
|
|
|
|
|
void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
|
|
int size_elems, int limit, const char *what) {
|
|
void *newblock;
|
|
int size = *psize;
|
|
if (nelems + 1 <= size) /* does one extra element still fit? */
|
|
return block; /* nothing to be done */
|
|
if (size >= limit / 2) { /* cannot double it? */
|
|
if (l_unlikely(size >= limit)) /* cannot grow even a little? */
|
|
luaG_runerror(L, "too many %s (limit is %d)", what, limit);
|
|
size = limit; /* still have at least one free place */
|
|
}
|
|
else {
|
|
size *= 2;
|
|
if (size < MINSIZEARRAY)
|
|
size = MINSIZEARRAY; /* minimum size */
|
|
}
|
|
lua_assert(nelems + 1 <= size && size <= limit);
|
|
/* 'limit' ensures that multiplication will not overflow */
|
|
newblock = luaM_saferealloc_(L, block, cast_sizet(*psize) * size_elems,
|
|
cast_sizet(size) * size_elems);
|
|
*psize = size; /* update only when everything else is OK */
|
|
return newblock;
|
|
}
|
|
|
|
|
|
/*
|
|
** In prototypes, the size of the array is also its number of
|
|
** elements (to save memory). So, if it cannot shrink an array
|
|
** to its number of elements, the only option is to raise an
|
|
** error.
|
|
*/
|
|
void *luaM_shrinkvector_ (lua_State *L, void *block, int *size,
|
|
int final_n, int size_elem) {
|
|
void *newblock;
|
|
size_t oldsize = cast_sizet((*size) * size_elem);
|
|
size_t newsize = cast_sizet(final_n * size_elem);
|
|
lua_assert(newsize <= oldsize);
|
|
newblock = luaM_saferealloc_(L, block, oldsize, newsize);
|
|
*size = final_n;
|
|
return newblock;
|
|
}
|
|
|
|
/* }================================================================== */
|
|
|
|
|
|
l_noret luaM_toobig (lua_State *L) {
|
|
luaG_runerror(L, "memory allocation error: block too big");
|
|
}
|
|
|
|
|
|
/*
|
|
** Free memory
|
|
*/
|
|
void luaM_free_ (lua_State *L, void *block, size_t osize) {
|
|
global_State *g = G(L);
|
|
lua_assert((osize == 0) == (block == NULL));
|
|
callfrealloc(g, block, osize, 0);
|
|
g->GCdebt -= osize;
|
|
}
|
|
|
|
|
|
/*
|
|
** In case of allocation fail, this function will do an emergency
|
|
** collection to free some memory and then try the allocation again.
|
|
*/
|
|
static void *tryagain (lua_State *L, void *block,
|
|
size_t osize, size_t nsize) {
|
|
global_State *g = G(L);
|
|
if (cantryagain(g)) {
|
|
WARNF("reacting to malloc() failure by running lua garbage collector..."); // [jart]
|
|
luaC_fullgc(L, 1); /* try to free some memory... */
|
|
return callfrealloc(g, block, osize, nsize); /* try again */
|
|
}
|
|
else return NULL; /* cannot run an emergency collection */
|
|
}
|
|
|
|
|
|
/*
|
|
** Generic allocation routine.
|
|
*/
|
|
void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
|
|
void *newblock;
|
|
global_State *g = G(L);
|
|
lua_assert((osize == 0) == (block == NULL));
|
|
newblock = firsttry(g, block, osize, nsize);
|
|
if (l_unlikely(newblock == NULL && nsize > 0)) {
|
|
newblock = tryagain(L, block, osize, nsize);
|
|
if (newblock == NULL) /* still no memory? */
|
|
return NULL; /* do not update 'GCdebt' */
|
|
}
|
|
lua_assert((nsize == 0) == (newblock == NULL));
|
|
g->GCdebt = (g->GCdebt + nsize) - osize;
|
|
return newblock;
|
|
}
|
|
|
|
|
|
void *luaM_saferealloc_ (lua_State *L, void *block, size_t osize,
|
|
size_t nsize) {
|
|
void *newblock = luaM_realloc_(L, block, osize, nsize);
|
|
if (l_unlikely(newblock == NULL && nsize > 0)) /* allocation failed? */
|
|
luaM_error(L);
|
|
return newblock;
|
|
}
|
|
|
|
|
|
void *luaM_malloc_ (lua_State *L, size_t size, int tag) {
|
|
if (size == 0)
|
|
return NULL; /* that's all */
|
|
else {
|
|
global_State *g = G(L);
|
|
void *newblock = firsttry(g, NULL, tag, size);
|
|
if (l_unlikely(newblock == NULL)) {
|
|
newblock = tryagain(L, NULL, tag, size);
|
|
if (newblock == NULL)
|
|
luaM_error(L);
|
|
}
|
|
g->GCdebt += size;
|
|
return newblock;
|
|
}
|
|
}
|