2022-03-21 19:31:03 -07:00
|
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
|
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
|
|
|
|
│ │
|
|
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
|
|
|
|
│ │
|
|
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-04-27 05:39:39 -07:00
|
|
|
|
#include "libc/assert.h"
|
2022-04-25 08:30:14 -07:00
|
|
|
|
#include "libc/bits/bits.h"
|
|
|
|
|
#include "libc/fmt/itoa.h"
|
2022-07-09 04:09:51 -07:00
|
|
|
|
#include "libc/log/rop.h"
|
2022-03-21 19:31:03 -07:00
|
|
|
|
#include "libc/math.h"
|
2022-04-27 05:39:39 -07:00
|
|
|
|
#include "libc/mem/mem.h"
|
2022-03-21 19:31:03 -07:00
|
|
|
|
#include "libc/stdio/append.internal.h"
|
2022-07-09 04:09:51 -07:00
|
|
|
|
#include "libc/stdio/strlist.internal.h"
|
2022-04-27 05:39:39 -07:00
|
|
|
|
#include "libc/x/x.h"
|
2022-07-09 16:27:26 -07:00
|
|
|
|
#include "third_party/double-conversion/wrapper.h"
|
2022-03-21 19:31:03 -07:00
|
|
|
|
#include "third_party/lua/cosmo.h"
|
|
|
|
|
#include "third_party/lua/lauxlib.h"
|
2022-04-29 06:06:23 -07:00
|
|
|
|
#include "third_party/lua/lctype.h"
|
2022-03-21 19:31:03 -07:00
|
|
|
|
#include "third_party/lua/lua.h"
|
2022-04-29 06:06:23 -07:00
|
|
|
|
#include "third_party/lua/visitor.h"
|
2022-03-21 19:31:03 -07:00
|
|
|
|
|
2022-04-29 06:06:23 -07:00
|
|
|
|
static bool IsLuaIdentifier(lua_State *L, int idx) {
|
|
|
|
|
size_t i, n;
|
|
|
|
|
const char *p;
|
|
|
|
|
p = luaL_checklstring(L, idx, &n);
|
|
|
|
|
if (!lislalpha(p[0])) return false;
|
|
|
|
|
for (i = 1; i < n; ++i) {
|
|
|
|
|
if (!lislalnum(p[i])) return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-04-27 05:39:39 -07:00
|
|
|
|
|
2022-04-29 06:06:23 -07:00
|
|
|
|
// TODO: Can we be smarter with lua_rawlen?
|
|
|
|
|
static bool IsLuaArray(lua_State *L) {
|
2022-04-27 05:39:39 -07:00
|
|
|
|
int i;
|
2022-04-29 06:06:23 -07:00
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
for (i = 1; lua_next(L, -2); ++i) {
|
|
|
|
|
if (!lua_isinteger(L, -2) || lua_tointeger(L, -2) != i) {
|
|
|
|
|
lua_pop(L, 2);
|
2022-04-27 05:39:39 -07:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-04-29 06:06:23 -07:00
|
|
|
|
lua_pop(L, 1);
|
2022-04-27 05:39:39 -07:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int LuaEncodeLuaDataImpl(lua_State *L, char **buf, int level,
|
|
|
|
|
char *numformat, int idx,
|
2022-04-29 06:06:23 -07:00
|
|
|
|
struct LuaVisited *visited) {
|
2022-03-21 19:31:03 -07:00
|
|
|
|
char *s;
|
2022-04-29 06:06:23 -07:00
|
|
|
|
bool isarray;
|
2022-04-25 08:30:14 -07:00
|
|
|
|
lua_Integer i;
|
2022-07-09 04:09:51 -07:00
|
|
|
|
struct StrList sl = {0};
|
|
|
|
|
int ktype, vtype, sli, rc;
|
2022-04-25 08:30:14 -07:00
|
|
|
|
size_t tbllen, buflen, slen;
|
2022-04-29 06:06:23 -07:00
|
|
|
|
char ibuf[24], fmt[] = "%.14g";
|
2022-04-25 08:30:14 -07:00
|
|
|
|
if (level > 0) {
|
|
|
|
|
switch (lua_type(L, idx)) {
|
2022-04-27 05:39:39 -07:00
|
|
|
|
|
2022-04-25 08:30:14 -07:00
|
|
|
|
case LUA_TNIL:
|
2022-07-09 04:09:51 -07:00
|
|
|
|
RETURN_ON_ERROR(appendw(buf, READ32LE("nil")));
|
2022-04-25 08:30:14 -07:00
|
|
|
|
return 0;
|
2022-04-27 05:39:39 -07:00
|
|
|
|
|
2022-04-25 08:30:14 -07:00
|
|
|
|
case LUA_TSTRING:
|
|
|
|
|
s = lua_tolstring(L, idx, &slen);
|
2022-07-09 04:09:51 -07:00
|
|
|
|
RETURN_ON_ERROR(EscapeLuaString(s, slen, buf));
|
2022-04-25 08:30:14 -07:00
|
|
|
|
return 0;
|
2022-04-27 05:39:39 -07:00
|
|
|
|
|
2022-04-25 08:30:14 -07:00
|
|
|
|
case LUA_TFUNCTION:
|
2022-07-09 04:09:51 -07:00
|
|
|
|
RETURN_ON_ERROR(
|
|
|
|
|
appendf(buf, "\"%s@%p\"", "func", lua_topointer(L, idx)));
|
2022-04-25 08:30:14 -07:00
|
|
|
|
return 0;
|
2022-04-27 05:39:39 -07:00
|
|
|
|
|
2022-04-25 08:30:14 -07:00
|
|
|
|
case LUA_TLIGHTUSERDATA:
|
2022-07-09 04:09:51 -07:00
|
|
|
|
RETURN_ON_ERROR(
|
|
|
|
|
appendf(buf, "\"%s@%p\"", "light", lua_topointer(L, idx)));
|
2022-04-25 08:30:14 -07:00
|
|
|
|
return 0;
|
2022-04-27 05:39:39 -07:00
|
|
|
|
|
2022-04-25 08:30:14 -07:00
|
|
|
|
case LUA_TTHREAD:
|
2022-07-09 04:09:51 -07:00
|
|
|
|
RETURN_ON_ERROR(
|
|
|
|
|
appendf(buf, "\"%s@%p\"", "thread", lua_topointer(L, idx)));
|
2022-04-25 08:30:14 -07:00
|
|
|
|
return 0;
|
2022-04-27 05:39:39 -07:00
|
|
|
|
|
|
|
|
|
case LUA_TUSERDATA:
|
|
|
|
|
if (luaL_callmeta(L, idx, "__repr")) {
|
|
|
|
|
if (lua_type(L, -1) == LUA_TSTRING) {
|
|
|
|
|
s = lua_tolstring(L, -1, &slen);
|
2022-07-09 04:09:51 -07:00
|
|
|
|
RETURN_ON_ERROR(appendd(buf, s, slen));
|
2022-04-27 05:39:39 -07:00
|
|
|
|
} else {
|
2022-07-09 04:09:51 -07:00
|
|
|
|
RETURN_ON_ERROR(appendf(buf, "[[error %s returned a %s value]]",
|
|
|
|
|
"__repr", luaL_typename(L, -1)));
|
2022-04-27 05:39:39 -07:00
|
|
|
|
}
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (luaL_callmeta(L, idx, "__tostring")) {
|
|
|
|
|
if (lua_type(L, -1) == LUA_TSTRING) {
|
|
|
|
|
s = lua_tolstring(L, -1, &slen);
|
2022-07-09 04:09:51 -07:00
|
|
|
|
RETURN_ON_ERROR(EscapeLuaString(s, slen, buf));
|
2022-04-27 05:39:39 -07:00
|
|
|
|
} else {
|
2022-07-09 04:09:51 -07:00
|
|
|
|
RETURN_ON_ERROR(appendf(buf, "[[error %s returned a %s value]]",
|
|
|
|
|
"__tostring", luaL_typename(L, -1)));
|
2022-04-27 05:39:39 -07:00
|
|
|
|
}
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2022-07-09 04:09:51 -07:00
|
|
|
|
RETURN_ON_ERROR(
|
|
|
|
|
appendf(buf, "\"%s@%p\"", "udata", lua_touserdata(L, idx)));
|
2022-04-27 05:39:39 -07:00
|
|
|
|
return 0;
|
|
|
|
|
|
2022-04-25 08:30:14 -07:00
|
|
|
|
case LUA_TNUMBER:
|
|
|
|
|
if (lua_isinteger(L, idx)) {
|
2022-07-09 04:09:51 -07:00
|
|
|
|
RETURN_ON_ERROR(
|
|
|
|
|
appendd(buf, ibuf,
|
|
|
|
|
FormatFlex64(ibuf, luaL_checkinteger(L, idx), 2) - ibuf));
|
2022-04-25 08:30:14 -07:00
|
|
|
|
} else {
|
2022-07-09 16:27:26 -07:00
|
|
|
|
RETURN_ON_ERROR(
|
|
|
|
|
appends(buf, DoubleToLua(ibuf, lua_tonumber(L, idx))));
|
2022-04-25 08:30:14 -07:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
2022-04-27 05:39:39 -07:00
|
|
|
|
|
2022-04-25 08:30:14 -07:00
|
|
|
|
case LUA_TBOOLEAN:
|
2022-07-09 04:09:51 -07:00
|
|
|
|
RETURN_ON_ERROR(appendw(buf, lua_toboolean(L, idx)
|
|
|
|
|
? READ32LE("true")
|
|
|
|
|
: READ64LE("false\0\0")));
|
2022-04-25 08:30:14 -07:00
|
|
|
|
return 0;
|
2022-04-27 05:39:39 -07:00
|
|
|
|
|
2022-04-25 08:30:14 -07:00
|
|
|
|
case LUA_TTABLE:
|
|
|
|
|
i = 0;
|
2022-07-09 04:09:51 -07:00
|
|
|
|
RETURN_ON_ERROR(rc = LuaPushVisit(visited, lua_topointer(L, idx)));
|
|
|
|
|
if (!rc) {
|
2022-04-29 06:06:23 -07:00
|
|
|
|
lua_pushvalue(L, idx); // idx becomes invalid once we change stack
|
|
|
|
|
isarray = IsLuaArray(L);
|
|
|
|
|
lua_pushnil(L); // push the first key
|
|
|
|
|
while (lua_next(L, -2)) {
|
|
|
|
|
ktype = lua_type(L, -2);
|
|
|
|
|
vtype = lua_type(L, -1);
|
2022-07-09 04:09:51 -07:00
|
|
|
|
RETURN_ON_ERROR(sli = AppendStrList(&sl));
|
2022-04-29 06:06:23 -07:00
|
|
|
|
if (isarray) {
|
|
|
|
|
// use {v₁′,v₂′,...} for lua-normal integer keys
|
|
|
|
|
} else if (ktype == LUA_TSTRING && IsLuaIdentifier(L, -2)) {
|
|
|
|
|
// use {𝑘=𝑣′} syntax when 𝑘 is legal as a lua identifier
|
|
|
|
|
s = lua_tolstring(L, -2, &slen);
|
2022-07-09 04:09:51 -07:00
|
|
|
|
RETURN_ON_ERROR(appendd(&sl.p[sli], s, slen));
|
|
|
|
|
RETURN_ON_ERROR(appendw(&sl.p[sli], '='));
|
2022-04-29 06:06:23 -07:00
|
|
|
|
} else {
|
|
|
|
|
// use {[𝑘′]=𝑣′} otherwise
|
2022-07-09 04:09:51 -07:00
|
|
|
|
RETURN_ON_ERROR(appendw(&sl.p[sli], '['));
|
|
|
|
|
RETURN_ON_ERROR(LuaEncodeLuaDataImpl(L, &sl.p[sli], level - 1,
|
|
|
|
|
numformat, -2, visited));
|
|
|
|
|
RETURN_ON_ERROR(appendw(&sl.p[sli], ']' | '=' << 010));
|
2022-04-27 05:39:39 -07:00
|
|
|
|
}
|
2022-07-09 04:09:51 -07:00
|
|
|
|
RETURN_ON_ERROR(LuaEncodeLuaDataImpl(L, &sl.p[sli], level - 1,
|
|
|
|
|
numformat, -1, visited));
|
2022-04-29 06:06:23 -07:00
|
|
|
|
lua_pop(L, 1); // table/-2, key/-1
|
2022-04-25 08:30:14 -07:00
|
|
|
|
}
|
2022-04-29 06:06:23 -07:00
|
|
|
|
lua_pop(L, 1); // table ref
|
2022-07-09 10:28:14 -07:00
|
|
|
|
if (!isarray) SortStrList(&sl);
|
2022-07-09 04:09:51 -07:00
|
|
|
|
RETURN_ON_ERROR(appendw(buf, '{'));
|
|
|
|
|
RETURN_ON_ERROR(JoinStrList(&sl, buf, READ16LE(", ")));
|
|
|
|
|
RETURN_ON_ERROR(appendw(buf, '}'));
|
|
|
|
|
FreeStrList(&sl);
|
2022-04-29 06:06:23 -07:00
|
|
|
|
LuaPopVisit(visited);
|
|
|
|
|
} else {
|
2022-07-09 04:09:51 -07:00
|
|
|
|
RETURN_ON_ERROR(
|
|
|
|
|
appendf(buf, "\"%s@%p\"", "cyclic", lua_topointer(L, idx)));
|
2022-04-25 08:30:14 -07:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
2022-04-27 05:39:39 -07:00
|
|
|
|
|
2022-04-25 08:30:14 -07:00
|
|
|
|
default:
|
2022-07-09 04:09:51 -07:00
|
|
|
|
// unsupported lua type
|
|
|
|
|
goto OnError;
|
2022-03-21 19:31:03 -07:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-07-09 04:09:51 -07:00
|
|
|
|
// too much depth
|
|
|
|
|
goto OnError;
|
2022-03-21 19:31:03 -07:00
|
|
|
|
}
|
2022-07-09 04:09:51 -07:00
|
|
|
|
OnError:
|
|
|
|
|
FreeStrList(&sl);
|
|
|
|
|
return -1;
|
2022-03-21 19:31:03 -07:00
|
|
|
|
}
|
2022-04-27 05:39:39 -07:00
|
|
|
|
|
2022-07-09 04:09:51 -07:00
|
|
|
|
/**
|
|
|
|
|
* Encodes Lua data structure as Lua code string.
|
|
|
|
|
*
|
|
|
|
|
* @param L is Lua interpreter state
|
|
|
|
|
* @param buf receives encoded output string
|
|
|
|
|
* @param numformat controls double formatting
|
|
|
|
|
* @param idx is index of item on Lua stack
|
|
|
|
|
* @return 0 on success, or -1 on error
|
|
|
|
|
*/
|
2022-04-27 05:39:39 -07:00
|
|
|
|
int LuaEncodeLuaData(lua_State *L, char **buf, char *numformat, int idx) {
|
|
|
|
|
int rc;
|
2022-04-29 06:06:23 -07:00
|
|
|
|
struct LuaVisited visited = {0};
|
2022-04-27 05:39:39 -07:00
|
|
|
|
rc = LuaEncodeLuaDataImpl(L, buf, 64, numformat, idx, &visited);
|
|
|
|
|
free(visited.p);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|