lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Here is a module that might be of some use when testing these things.
Compile as usual for a loadable C module and:

  lua -l stackchk -e 'print(stackchk())'

should print the size of the stack frame for lua->lua calls in bytes. On
versions other than 5.4 this should return 0, since previously the C
stack wasn't used for such calls; on amd64 with clang -O2 I get a value
of 224.

I'm curious to know what kind of values people get on other

/* stackchk.c */

#include <stddef.h>
#include <string.h>
#include <math.h>

#include <lua.h>
#include <lauxlib.h>

/* note, must avoid tail calls in r() */
static const char *stack_code =
    "local _ENV, f = nil, ...\n"
    "local function r(d)\n"
    "  if d >= 0 then return r(d-1),d else return f(),0 end\n"
    "return r\n";

static int getdepth(lua_State *L)
    volatile int t = 0;
    lua_pushlightuserdata(L, (void*) &t);
    return 1;

static int stackchk(lua_State *L)
    int i, n = luaL_optinteger(L, 1, 50);
    void *p[2];
    for (i = 0; i < 2; ++i) {
        lua_pushvalue(L, lua_upvalueindex(1));
        lua_pushinteger(L, (i*n));
        lua_call(L, 1, 1);
        p[i] = lua_touserdata(L, -1);
    lua_pushnumber(L, fabs((double)(p[0] - p[1])) / n);
    return 1;

int luaopen_stackchk(lua_State *L)
    if (luaL_loadbufferx(L, stack_code, strlen(stack_code), "stackchk", "t"))
    lua_pushcfunction(L, getdepth);
    lua_call(L, 1, 1);
    lua_pushcclosure(L, stackchk, 1);
    return 1;

/* end */