From: Donal K. F. <don...@ma...> - 2010-11-10 09:16:48
|
On 08/11/2010 18:19, Tom Jackson wrote: > If there are three classes (http://rmadilo.com/files/tcloo/protected/): It's taken me a bit to work through this example. The notion of "protected" that we are using isn't one of guarding of the methods (for that, people should use code in C or multiple interpreters) so much as of restricting what methods can be called through the official entry point. (Since it's pretty easy to work out what [my] is called for a particular object, any method on that object can be invoked without too much difficulty if you really want; [info object namespace] is the core of that particular trick.) You *can* do the sort of protection by overriding the unknown method: method unknown {name args} { if { [catch {self caller} context] == 0 && [canAccess [self] $name $context] } then { tailcall my $name {*}$args } return [next $name {*}$args] } (I leave out the details of [canAccess] here.) The problem with this is that method calls where the method name is unknown are not cached, so the above mechanism is rather slow. I don't cache the mappings of unknown method names to implementations as there's an infinite number of such names, and adding caching of them would also make cache spoilage costs a bigger problem (right now, most code doesn't pay the cache spoilage costs at all; that's good because they're quite large). (Other variations on the theme of protection are possible through the use of filters - the [self] command provides most of the tools required, and any that are missing are things I should be told about so I can fix the lack - though I don't feel like going into depth now.) ---- A quick refresher on [self caller]: % oo::class create Foo { method a args {my {*}$args} method b {} {return [self caller]} } ::Foo % Foo create bar ::bar % bar a b ::Foo ::bar a % bar b caller is not an object As you can see, it reports information about how a method was called that is (in theory at least) enough to define access control rules. Donal. |