SmallTalk:关于方法“ withargs:executeMethod:; quot”
我正在尝试理解“ withargs:exputemethod:”在Smalltalk中,尖叫。
1。我试图了解该方法的作用是什么? 2。需要传递哪些论点才能进行?
I'm trying to understand the method "withArgs: executeMethod: " in smalltalk, squeak.
1. I am trying to understand what is the role of the method?
2. What arguments need to be passed to it for it to be carried out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
理解此方法的一种好方法是将其视为一般表达式的句法变体
,其中
object
是带有selectormsg的消息的接收者
其参数。当然有没有或多个参数的变体,但是这个想法是相同的。当
对象
接收此消息(*)虚拟机(VM)在object中查找
msg: 的层次结构,并将其转移到控件,绑定compilesmethod
带有selectorself
to对象
以及该方法的正式参数转换为arg arg 。
请注意,此调用由VM管理,由虚拟图像(VI)否。那么,我们如何在VI中反映同样的情况?好吧,此行为有两个步骤(1)找到该方法,(2)将其正式接收器和参数绑定到实际的接收器,并让其运行。
步骤(1)是所谓的查找算法。它很容易在SmallTalk中实现:只需询问接收器的类,检查类是否包括selector
#msg:
,如果不是,请转到超级类并重复。如果所有检查失败,请发出do notundectand:
消息。步骤(2)完全需要
#withargs:executemethod:
提供。它使我们可以说方法
是步骤(1)中找到的compilesmethod
。 [我们必须使用{arg}
而不是arg
,因为中的复数:
建议该方法期望array
要这个
我们为什么
更重要的是,使用此功能的相关示例是实现方法包装器。简而言之,给定任何特定方法,您可以在
包装器
方法中包装它(作为wrappee
),该方法还具有preblock
。如果您在MethodDictionary
中替换原始方法,则使用包装器
,您可以让wrapper
首先执行> preblock
,然后是预期的方法。第一个任务很容易:只需发送消息Preblock Value
即可。第二个我们有该方法(wrappee
),接收器和参数(如果有)。因此,要完成任务,您只需要发送到接收者withargs:executemethod:
带有实际参数(s)和crappee
。啊!我们不要忘记提到使用方法包装器的原因之一是测量测试覆盖范围。
还请注意,
withargs:executemethod:
不需要第二个参数,即执行,在任何类中的方法,更不用说接收者的类。特别是,您可以在任何给定的对象上飞行创建编译
。当然,如果接收器只有两个等,则可以确保执行不会使用接收器的第三个IVAR崩溃VM。代码>不在任何类中安装它的情况下是通过要求SmallTalk编译器来做的(寻找newCompiler
的发件人以了解如何做到这一点)。A good way to understand this method is by considering it as a syntactic variant of the general expression
where
object
is the receiver of the message with selectormsg:
andarg
its argument. There are of course variants with no or multiple arguments, but the idea is the same.When
object
receives this message (*) the Virtual Machine (VM) looks up for theCompiledMethod
with selectormsg:
in theobject
's hierarchy, and transfers it the control, bindingself
toobject
and the formal argument of the method toarg
.Notice that this invocation is managed by the VM, no by the Virtual Image (VI). So, how could we reflect the same in the VI? Well, there are two steps in this behavior (1) find the method and (2) bind its formal receiver and arguments to the actual ones and let it run.
Step (1) is the so called lookup algorithm. It is easily implemented in Smalltalk: just ask the receiver its class, check whether the class includes the selector
#msg:
and, if not, go to the superclass and repeat. If all checks fail, issue thedoesNotUnderstand:
message.Step (2) exactly requires what
#withArgs:executeMethod:
provides. It allows us to saywhere
method
is theCompiledMethod
found in step (1). [We have to use{arg}
rather thanarg
because the plural inwithArgs:
suggests that the method expects anArray
of arguments.]Why would we want this?
Generally speaking, giving the VI the capability to mimic behavior implemented in the VM is good because it makes metaprogramming easier (and more natural).
More practically, a relevant example of the use of this capability is the implementation of Method Wrappers. Briefly described, given any particular method, you can wrap it (as the
wrappee
) inside awrapper
method, which also has apreBlock
. If you then substitute the original method in theMethodDictionary
where it belongs, with thewrapper
, you can let thewrapper
first execute thepreBlock
and then the intended method. The first task is easy: just send the messagepreBlock value
. For the second we have the method (thewrappee
), the receiver and the arguments (if any). So, to complete the task you only need to send to the receiverwithArgs:executeMethod:
with the actual argument(s) and thewrappee
.Ah! Let's not forget to mention that one of the reasons for having Method Wrappers is to measure testing coverage.
Note also that
withArgs:executeMethod:
does not require the second argument, i.e., the method to execute, to be in any class, let alone the class of the receiver. In particular, you could create aCompiledMethod
on the fly and execute it on any given object. Of course, it is up to you to make sure that the execution will not crash the VM by, say, using the third ivar of the receiver if the receiver has only two etc. A simple way to create aCompiledMethod
without installing it in any class is by asking the Smalltalk compiler to do so (look for senders ofnewCompiler
to learn how to do that).