lua-users home
lua-l archive

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

在 2017/5/13 13:36, Xianfu Pan 写道:
> I wanna achieve a function that mark value as a watch point, when the value changes, it triggers an event LUA_HOOKWATCH. My intention is to findat witch point and who stealthily changed the value of a specific address(TValue).
> I redefine TValuefiled as "Value value_, int watch_:1, int tt_:31". So when TValue, the variant,  who stores the value executes assignment operation, it notifies, if watch_ flag == 1;
> I also redefine setobj "...; if (o1->watch_) luaD_hook(LUA_HOOKWATCH); o1->value_ = o2->value_; o1->tt_ = o2->tt_";
> My issue is how to mark all Tvaluesborn with watch_ = 0(not to be watched by default);  and how to locate the Tvalue I hope to watch so that Ican mark its watch_ = 1.
> More detail, what assignment macros such as setobj, setobj2s, should I redefine?

I'd rather suggest another solution: add a new special type tag instead of changing the existing tag's meaning.
then add a function to create a value with this special type. then you just need a simple check.

you may fine tune your policies like on what occasion should Lua trigger the hook, whether overwriting a guard
value with another guard value triggers the hook, whether the guard valuecan be used as table keys, etc.

the mechanism is simple and flexible.

e.g. the new tag could be called LUA_TGUARD, a function lua_pushguard(L) to create a guard value. depending on
your requirement, you might or might not expose the function to Lua side,possibly inside the debug.* namespace.

the nerdy Peng / 书呆彭 /