[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: lua_register() while running script
- From: Luiz Henrique de Figueiredo <lhf@...>
- Date: Sat, 9 Dec 2000 10:41:40 -0200 (EDT)
> lua_pop(p_pCLuaState, l_nArguments);
There is no need for this: when a C function returns, Lua runtime handles this.
Normally, you only need to call lua_pop in more complicated situations,
such as loops in C functions that call Lua and leave garbage behind (ie, where
in 3.2 you'd use begin/endblock).
> pCLuaState = lua_open(LUA_MINSTACK);
Try lua_open(0) instead of lua_open(LUA_MINSTACK). LUA_MINSTACK is NOT the
default stack size, it's the minimum stack space guaranteed to be available
for a C function. Perhaps you're overflowing the stack.
>I thought the first call to ImportSystem() while the script runs could register
>the functions the are used below.
It should. See below for a simple example that works.
--lhf
/*
* min.c
* a minimal Lua interpreter. loads stdin only.
* no standard library, only a "print" function.
*/
#include <stdio.h>
#include "lua.h"
/* a simple "print". based on the code in lbaselib.c */
static int print(lua_State *L)
{
int n=lua_gettop(L);
int i;
for (i=1; i<=n; i++)
{
if (i>1) printf("\t");
if (lua_isstring(L,i))
printf("%s",lua_tostring(L,i));
else
printf("%s:%p",lua_typename(L,lua_type(L,i)),lua_topointer(L,i));
}
printf("\n");
return 0;
}
static int load(lua_State *L)
{
lua_register(L,"print",print);
return 0;
}
int main(void)
{
lua_State *L=lua_open(0);
lua_register(L,"load",load);
lua_dostring(L,"load();print(load,print,'lua',{})");
return 0;
}