lua-users home
lua-l archive

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


OK, figured out why it doesn't work. Basically, the token filter as below can only be applied once in a lua state; after the first file, the token filter must be reset before every loadstring, loadfile or dofile call. I'm trying to figure out why - the coroutines never return so they should always be resumable.

function FILTER(get,source)
	FILTER=coroutine.wrap(INC)
	FILTER(get,coroutine.yield)
end

Here's a nasty workaround:

_G.FILTERMAKER = function FILTER(get,source)
	FILTER=coroutine.wrap(INC)
	FILTER(get,coroutine.yield)
end
FILTER = _G.FILTERMAKER

Then in the main script before any loadstring, dofile etc., add:
FILTER = FILTERMAKER

Of course I want to avoid this if I can. For now, I've added an element in each filter function that detects <eof> and resets the FILTER function accordingly.


On Apr 22, 2007, at 1:51 AM, Graham Wakefield wrote:

Summary:

lua_resume() on the main Lua state (instead of e.g. pcall) works.

This also works after loading a different script, e.g. a token filter defined in a lua script.

However, after this, calls to loadstring(), dofile() etc. in Lua do not work (but neither do they throw an error). Somehow it appears as if the token filter is trashing/zeroing the loaded chunks.

Not sure why - is it because the FILTER definition also employs coroutines?

On Apr 21, 2007, at 6:39 PM, lists@grahamwakefield.net wrote:



extern "C" {
	#include "lua.h"
	#include "lualib.h"
	#include "lauxlib.h"
}

int main (int argc, char ** argv)
{
	char * filename = "/test1.lua";
	int result;

	lua_State * L = luaL_newstate(); //lua_newstate(Lua :: alloc, 0);
	if (L) {
		luaL_openlibs(L);

		// luaL_dofile(L, "/preload1.lua"); // *1*

		if (luaL_loadfile (L, filename))
		{
			printf("luaL_loadfile error: %s", lua_tostring(L, -1));
			printf("luaL_loadfile failed");
			lua_pop(L, 1);
		}
		else
		{
			result = lua_resume(L, lua_gettop(L) - 1);
			printf("lua_resume result %i\n", result);
		}

	} else {
		printf("luaL_newstate failed\n");
	}
	return 0;
}



-- debug filter
local function IN(get,put)
	put("IN init")
	while true do
		local line,token,value=get()
		print(line,token,value)
		put(line,token,value)
	end
end

local function OUT(get,put)
	put("OUT init")
	while true do
		local line,token,value=get()
		print("",line,token,value)
		put(line,token,value)
	end
end

local function pipe(f,get,put)
	put = put or coroutine.yield
	local F=coroutine.wrap(f)
	F(get,put)
	return F
end

function FILTER(get,source) FILTER=pipe(OUT,pipe(IN,get)) end





grrr waaa
www.grahamwakefield.net