lua-users home
lua-l archive

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


Hello,

I'm experiencing a strange problem. I'm testing a custom lua_gettop analogue:

> // test.c
> // build: gcc test.c -shared -I/usr/include/lua5.2 -o test.so
> #include "lua.h"
> #include "lapi.h"
> #include <stdio.h>
>
> int my_gettop(lua_State *L) {
>     return cast_int(L->top - (L->ci->func + 1)); // copied from lua_gettop source, lapi.c
> }
>
> static int test(lua_State *L) {
>     printf("lua_gettop() = %d\n", lua_gettop(L));
>     printf("my_gettop() = %d\n", my_gettop(L));
>     return 0;
> }
>
> int luaopen_test(lua_State *L) {
>     lua_pushcfunction(L, test);
>     return 1;
> }

And then I use it like this:

> Lua 5.2.1  Copyright (C) 1994-2012 Lua.org, PUC-Rio
>> test = require "test"
>> test()
> lua_gettop() = 0
> my_gettop() = 1431655765
>> test("foo")
> lua_gettop() = 1
> my_gettop() = -1431655765

This is strange considering my_gettop is a copy of lua_gettop.
I have noticed a pattern in seemingly random numbers produced by
my_gettop(i.e. 1431655765 = 0x55555555), but I have no idea why it
produces them instead of correct results.

I'm sure I've made a simple mistake somewhere, can anybody help me find it?

Thanks,

Peter