[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: lua_xmove
- From: Nevo Hed <nhed@...>
- Date: Thu, 28 Jul 2005 14:41:02 -0400
Hi
Pretty new to Lua myself - so maybe I am reading this all wrong - but ....
I looked at the test directory in Lua 5.0.2 at luac.lua
--8<------
-- bare-bones luac in Lua
-- usage: lua luac.lua file.lua
assert(arg[1]~=nil and arg[2]==nil,"usage: lua luac.lua file.lua")
f=assert(io.open("luac.out","wb"))
f:write(string.dump(assert(loadfile(arg[1]))))
io.close(f)
--8<------
It looks to me like you would be able to serialize whatever code you want to
run in
the other environment just with string.dump()
Pluto looks great - but might be an overkill for simple uses (i.e. sending
''events'' to other lua environments)
That said, we heard here that lua_xmove is intended for threads of
the same parent environment, ok, is there anything specific about xmove's
implementation that prevents use by unrelated environments?
Is the only thing I am risking that there will be an invalid object
reference?
If so - what if I promise that I only pass simple types (numbers, strings,
booleans,
and tables of those simple types), see below ... isn't this good enough for
what Jeff was looking to do with lua_xmove()?
where is the risk?
Thanks
-Nevo
i.e.
function serialize(d, l)
local ret=""
if(l==nil) then l=0 end
if (type(d)=="function" or type(d)=="thread" or type(d)=="userdata") then
error("Type "..type(d).." is not allowed in serialize", 2+l)
elseif (type(d)=="table") then
ret=ret.."{"
for i,v in pairs(d) do
if(type(i)=="number") then
ret=ret.."["..i.."]"
else
ret=ret..i
end
ret=ret.."="..serialize(v,l+1)..";"
end
ret=ret.."}"
elseif (type(d)=="string") then
ret=ret..string.format("%q", d)
else
ret=ret..tostring(d)
end
return ret
end
function readonly(table, key, value)
error("Attempt to modify read-only table", 2)
end
-----Original Message-----
From: Ben Sunshine-Hill [mailto:sneftel@gmail.com]
Sent: Tuesday, July 26, 2005 12:13 PM
To: Lua list
Subject: Re: lua_xmove
Not to blow my own horn, but if you want to do this for arbitrary
datatypes (and you're currently using lua 5.0) you can use Pluto to
serialize the data to a string, then deserialize it into the other
state. http://luaforge.net/projects/pluto/ .
Ben
On 7/26/05, Jeffrey Williams <jwilliams@mfchelp.com> wrote:
> I've been using lua_xmove to allow one lua_State to call functions in
> another lua_State, but I am concerned that what I am doing is not a
> legitimate usage of lua.
>
> Here are some more specifics... the two lua_State's involved are not lua
> "threads", they are two separate states created by two calls to
> lua_Open/lua_dofile
>
> I do something like this:
>
> lua_getglobal(L, "myfunc"); // on top of stack now resides a function
> lua_xmove(L, L2, 1);
> lua_pushnumber(L2, 23);
> lua_pcall(L2, 0, 0, 0); // call myfunc(2) in L from state L2
>
> It seems to work, but I am concerned that perhaps I am misusing the api
and
> that it only works in certain cases. So my question is: Is this proper
> usage or is it just luck that it doesnt crash on me?
>
> Jeff
>
>
>