[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Call Lua functions without side effects
- From: Matthew Paul Del Buono <delbu9c1@...>
- Date: Thu, 12 Jun 2008 14:32:35 -0400 (EDT)
Hi Peter,
Assuming any external processing of the functions in those files use globals (i.e., your API being offered is in the global environment), you don't have to supply anything up front:
>
>function foo()
> print "foo"
>end
>
>print "I want to do whatever I want! Who cares about the side effects!"
Here, if you haven't yet opened the libraries (thus, print does not yet exist), you will have an error occur (attempt to call global 'print' a nil value). Yet, in the following:
>function foo()
> print "foo"
>end
This will compile successfully. print() doesn't yet exist, but since it will be a global lookup, all you need to do is ensure that print exists at the time you call foo(), and not before. Everything should still work just as well.
If you want more fine control (such as "I want to give access to these functions but not these other ones") then try taking a look at setfenv(). Similarly, if you are worried about code like this:
function foo()
print("bar");
end
a = 3;
And you don't want "a = 3" to harm other code, you can use setfenv() to ensure that any globals set by this chunk will only impact that chunk and nothing else.
Hope that helps,
-- Matthew P. Del Buono