#!/usr/bin/perl
#use Mooix::Thing;
run sub {
	my $this=shift;

	# Test to see if the caller can destroy this object.
	# This method is not stackless, so the easiest test is to attempt
	# to write to a field of the object; if the write succeeds then the
	# caller can destroy it.
	if (! open (OUT, ">.mooix")) {
		# The other possibility is if this object is being
		# destroyed by the object that encapsulates it. This leaves
		# it up to the encapsulator to check its caller.
		require Mooix::CallStack;
		import Mooix::CalStack;
		my $stack=Mooix::CallStack::get();
		while ($stack) {
			if (length $stack->method) {
				if ($stack->index eq $this->encapsulator->index) {
					last; # success
				}

				if ($stack->index ne $this->index ||
			     	    ($stack->basemethod ne 'destroy' && $stack->basemethod ne 'candestroy')) {
			 		$this->croak("invalid caller");
				}
			}

			$stack=$stack->next;
		}
	}
	close OUT;
	
	return 1;
}
