lua-users home
lua-l archive

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


Hi,

I'm trying to run the attached program in Lua 5.2.2 and 5.4.3 but I'm getting different number of arguments in Lua stack when I check in hook function. Is anything changed related to Lua hook function call?.

I compiled the attached program as:
In 5.4: 
cc -o test_set_hook test_set_hook.c -Wall -I /var/views/lua/lua-5.4.3/src/ -L/var/views/lua/lua-5.4.3/src/ -lm -llua
In 5.2:
cc -o test_set_hook test_set_hook.c -Wall -I /var/views/lua/lua-5.2.2/src/ -L/var/views/lua/lua-5.2.2/src/ -lm -llua

I'm doing this compilation in FeeBSD -11.4 (AMD 64).

Here is the output of the program:
In Lua 5.4:

[Aman_freebsd114:/var/views/lua/lua-5.4.3/src]# ./test_set_hook
In C, calling Lua->test_sethook_1("abcdef")

top : 2
Checking top element value : (null)
Back in C again
Returned string=abcdef

---------------------------------
In C, calling Lua->test_sethook_2("abcdef")

top : 17
Checking top element value :
top : 17
Checking top element value :
top : 17
Checking top element value :  !"#$%&'()*+,-./
top : 17
Checking top element value : 0123456789:;<=>?
top : 17
Checking top element value : @ABCDEFGHIJKLMNO
top : 17
Checking top element value : PQRSTUVWXYZ[\]^_
top : 17
Checking top element value : `abcdefghijklmno
top : 17
Checking top element value : pqrstuvwxyz{|}~
top : 17
Checking top element value :

top : 17
Checking top element value :  
top : 17
Checking top element value :  ¡¢£¤¥¦§¨©ª«¬­®¯
top : 17
Checking top element value : °±²³´µ¶·¸¹º»¼½¾¿
top : 17
Checking top element value : AÃŅLJɉˋ͍Ϗ
top : 17
Checking top element value : ёӓՕחٙۛݝߟ
top : 17
Checking top element value : ᢢ䥥稨ꫫ󻠍
top : 17
Checking top element value : 񲳳񶷷򻼼
top : 24
Checking top element value : 255
Back in C again
Returned string=abcdef





Lua 5.2.2:

[Aman_freebsd114:/var/views/lua/lua-5.2.2/src]# cc -o test_set_hook test_set_hook.c -Wall -I /var/views/lua/lua-5.2.2/src/ -L/var/views/lua/lua-5.2.2/src/ -lm -llua
[Aman_freebsd114:/var/views/lua/lua-5.2.2/src]# ./test_set_hook
In C, calling Lua->test_sethook_1("abcdef")

top : 1
Checking top element value : abcdef
Back in C again
Returned string=abcdef

---------------------------------
In C, calling Lua->test_sethook_2("abcdef")

top : 17
Checking top element value :
top : 17
Checking top element value :
top : 17
Checking top element value :  !"#$%&'()*+,-./  
top : 17
Checking top element value : 0123456789:;<=>?  
top : 17
Checking top element value : @ABCDEFGHIJKLMNO  
top : 17
Checking top element value : PQRSTUVWXYZ[\]^_  
top : 17
Checking top element value : `abcdefghijklmno  
top : 17
Checking top element value : pqrstuvwxyz{|}~    
top : 17
Checking top element value :

top : 17
Checking top element value :
top : 17
Checking top element value :  ¡¢£¤¥¦§¨©ª«¬­®¯  
top : 17
Checking top element value : °±²³´µ¶·¸¹º»¼½¾¿  
top : 17
Checking top element value : AÃŅLJɉˋ͍Ϗ  
top : 17
Checking top element value : ёӓՕחٙۛݝߟ  
top : 17
Checking top element value : ᢢ䥥稨ꫫ󻮠
top : 17
Checking top element value : 񲳳񶷷򻼼񞞠
top : 1
Checking top element value : abcdef
Back in C again
Returned string=abcdef
[Aman_freebsd114:/var/views/lua/lua-5.2.2/src]# 
--
Best Regards
Aman Agrawal
#include <lua.h>                                /* Always include this when calling Lua */
#include <lauxlib.h>                            /* Always include this when calling Lua */
#include <lualib.h>                             /* Prototype for luaL_openlibs(), */
/*   always include
 *   this when calling
 *   Lua */

#include <stdlib.h>                             /* For function exit() */
#include <stdio.h>                              /* For input/output */

void bail(lua_State *L, char *msg){
	fprintf(stderr, "\nFATAL ERROR:\n  %s: %s\n\n",
			msg, lua_tostring(L, -1));
	exit(1);
}

void ns_lua_trace_hook(lua_State *L, lua_Debug *ar)
{
	int top ;
	switch (ar->event) {
		case LUA_HOOKRET:
			top = lua_gettop(L);
			printf("\ntop : %d", lua_gettop(L));
			printf("\nChecking top element value : %s", lua_tostring(L, -1));
			break;
	}
}

int main(void)
{
	lua_State *L;

	L = luaL_newstate();                        /* Create Lua state variable */
	luaL_openlibs(L);                           /* Load Lua libraries */

	if (luaL_loadfile(L, "callfuncscript.lua")) /* Load but don't run the Lua script */
		bail(L, "luaL_loadfile() failed");      /* Error out if file can't be read */

	if (lua_pcall(L, 0, 0, 0))                  /* PRIMING RUN. FORGET THIS AND YOU'RE TOAST */
		bail(L, "lua_pcall() failed");          /* Error out if Lua file has an error */

	printf("In C, calling Lua->test_sethook_1(\"abcdef\")\n");

	lua_sethook(L, ns_lua_trace_hook, LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE, 0);

	lua_getglobal(L, "test_sethook_1");                 /* Tell it to run callfuncscript.lua->test_sethook_1() */
	lua_pushstring(L, "abcdef");                       /* Submit 6 as the argument to square() */
	if (lua_pcall(L, 1, 1, 0))                  /* Run function, !!! NRETURN=1 !!! */
		bail(L, "lua_pcall() failed"); 

	printf("\nBack in C again\n");
	const char *mystring = lua_tostring(L, -1);
	printf("Returned string=%s\n", mystring);


	printf("\n---------------------------------\n");
	printf("In C, calling Lua->test_sethook_2(\"abcdef\")\n");

	lua_sethook(L, ns_lua_trace_hook, LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE, 0);

	lua_getglobal(L, "test_sethook_2");                 /* Tell it to run callfuncscript.lua->test_sethook_2() */
	lua_pushstring(L, "abcdef");                       /* Submit 6 as the argument to square() */
	if (lua_pcall(L, 1, 1, 0))                  /* Run function, !!! NRETURN=1 !!! */
		bail(L, "lua_pcall() failed"); 

	printf("\nBack in C again\n");
	mystring = lua_tostring(L, -1);
	printf("Returned string=%s\n", mystring);

	lua_close(L);                               /* Clean up, free the Lua state var */
	return 0;
}

Attachment: callfuncscript.lua
Description: Binary data