[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: How to change a module's upvalue?
- From: Rena <hyperhacker@...>
- Date: Sat, 19 Jul 2014 10:53:49 -0400
On Sat, Jul 19, 2014 at 10:46 AM, 俞江 <firstname.lastname@example.org> wrote:
> Hi all!
> There is a module 'a.lua':
> local num = 1
> function print_num()
> And used as:
> A = require('a')
> I want to change the 'num' to 10 in the runtime, but
> load("local num=100",nil,nil,A)()
> does not work, how to do it?
I'm not sure what you're trying to do with load(). It looks like
you've given A as the environment rather than the function. Also,
since the module doesn't return anything, require('a') will return
true. (and will set the global print_num, which may or may not be your
intention; it's considered bad practice for modules to set globals.)
There is debug.setupvalue() to change upvalues of a function. However,
it takes an upvalue index, not a name, so you'd need to loop through
the function's upvalues with debug.getupvalue(), find the one named
'num', and change it. (I'm not sure if upvalue indexes start at 0 or
Sent from my Game Boy.