lua-users home
lua-l archive

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


I've done this for an internal debugger project.

* Use loadstring to turn the code string into a function 'f';
* Create an empty environment table 'e';
* Create a metatable 'm' with __index and __newindex metamethods that use debug.getinfo, debug.getlocal, debug.setlocal, etc to check locals, then upvalues, then the environment of the currently executing function and get/set the value as appropriate.
* Set 'm' as the metatable of 'e' and 'e' as the metatable of 'f'.
* Call f.

Now, when the code is run within your hook, any accesses to variables not defined in the debug string ("a = 100") will be trapped and modify the appropriate local/upvalue/global in the current execution context in which the debug hook was triggered.

Hope this helps!

----- Original Message ----- From: "cielacanth" <cielacanth@s60.xrea.com>
To: <lua@bazar2.conectiva.com.br>
Sent: Friday, February 08, 2008 11:25 AM
Subject: About lua_State's clone and dostring that can change a local variable

2, dostring that can change a local variable
In the hook function, I sometimes want to execute some lua program
in the context of the running function at that time.
(For instance it changes the value of local variable of that function.)
When lua_loadstring and lua_call is simply executed from the hook function,
it is considered that it is globally executed.
So it is not easy to change the value of the local variable unlike the
global variable.
I need the function like dostring that can execute programs in context
of a specified function.
(In the following, I call it 'special_dostring'.)

For instance,

function foo()
local a = 0
... -- (A)
end

If the hook function is called immediately before the execution of the
(A) point,
special_dostring("a=100", [infomation of the function foo]) changes the
value of the variable a
of the function foo.
Is it possible to write such a API ?

Thanks.