[Cuis-dev] question regarding become: on self
Juan Vuletich
juan at cuis.st
Tue Feb 27 04:36:24 PST 2024
Hi Folks,
As Hernán pointed out, doing #become: sometimes is dangerous. For
instance, if you do `self become: xxx` in the middle of a method, like
in this case, the rest of the method will continue, but with 'self' now
being the new object. But the method on execution belongs to the old
class! Any possible instance variable access will result in undefined
behavior or a hard crash.
The system has no way to validate that the method in execution is
appropriate for the new instance. And the same would happen for any
method where the becomed object is the receiver in the call stack, as
soon execution will return there, and the same will happen.
There are two ways out of this:
1- #become: objects by 'remote control'. Ensure that the object to be
becomed is not in the call stack and have some other object handle this.
2- Take full responsibility for what you are doing, because you know
that no invalid ivar accesses will happen. Implement your own version of
#become:, maybe calling it #uncheckedBecome: or such.
BTW, David, please read the exception description again and try to make
sense of it. It it doesn't seem to make sense, please help us improve
the text to something easier to understand in your view.
Thanks,
On 2/26/2024 9:04 AM, Hernán Wilkinson via Cuis-dev wrote:
> Hi David,
> The exception that is signaled is MethodInCallStackToBecomeInvalid.
> It means that there is a method context in the stack that has a
> reference to the object you want to "become", in this case it is self.
> Doing a become in such a condition can produce problems (I do not
> remember the exact details of them, but something related to the
> method being jitted or something like that) and that is why the check
> and signaling the exception, but the exception is resumable as you
> saw. I have never had any problem resuming that exception under this
> circumstance.
>
> Cheers
> Hernan.
>
>
> On Mon, Feb 26, 2024 at 6:09 AM David Faitelson via Cuis-dev
> <cuis-dev at lists.cuis.st <mailto:cuis-dev at lists.cuis.st>> wrote:
>
> Hi dear all,
>
> I want to use become: to lazily load objects from a database.
> Something along the lines of:
>
> Stub >> doesNotUnderstand: aMessage
>
> | actual |
>
> actual := “fetch from db”.
>
> self become: actual.
>
> aMessage sendTo: self.
>
> When I run this code I get an exception, but if I continue despite
> the signal, the operations succeeds and the stub is correctly
> replaced by the actual object.
>
> Is this specific use of become: safe despite the warning? Or does
> it work by luck and I should seek a different solution? Or perhaps
> I can use become: in some other way to achieve this effect?
>
> Thanks,
>
> David
>
> --
> Cuis-dev mailing list
> Cuis-dev at lists.cuis.st <mailto:Cuis-dev at lists.cuis.st>
> https://lists.cuis.st/mailman/listinfo/cuis-dev
>
>
>
> --
> *Hernán Wilkinson
> Agile Software Development, Teaching & Coaching*
> *Phone: +54-011*-4893-2057
> *Twitter: @HernanWilkinson*
> *site: http://www.10Pines.com <http://www.10pines.com/>*
> Address: Alem 896, Floor 6, Buenos Aires, Argentina
--
Juan Vuletich
cuis.st
github.com/jvuletich
researchgate.net/profile/Juan-Vuletich
independent.academia.edu/JuanVuletich
patents.justia.com/inventor/juan-manuel-vuletich
linkedin.com/in/juan-vuletich-75611b3
twitter.com/JuanVuletich
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20240227/91d37196/attachment-0001.htm>
More information about the Cuis-dev
mailing list