SmallTalk:关于方法“ withargs:executeMethod:; quot”

发布于 2025-02-07 05:54:54 字数 114 浏览 2 评论 0原文

我正在尝试理解“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吐个泡泡 2025-02-14 05:54:54

理解此方法的一种好方法是将其视为一般表达式的句法变体

  object msg: arg                      (*)

,其中object是带有selector msg的消息的接收者其参数。当然有没有或多个参数的变体,但是这个想法是相同的。

对象接收此消息(*)虚拟机(VM)在object中查找compilesmethod带有selector msg: 的层次结构,并将其转移到控件,绑定self to 对象以及该方法的正式参数转换为 arg arg 。

请注意,此调用由VM管理,由虚拟图像(VI)否。那么,我们如何在VI中反映同样的情况?好吧,此行为有两个步骤(1)找到该方法,(2)将其正式接收器和参数绑定到实际的接收器,并让其运行。

步骤(1)是所谓的查找算法。它很容易在SmallTalk中实现:只需询问接收器的类,检查类是否包括selector #msg:,如果不是,请转到超级类并重复。如果所有检查失败,请发出do notundectand:消息。

步骤(2)完全需要#withargs:executemethod:提供。它使我们可以说

   object withArgs: {arg} executeMethod: method

方法是步骤(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

  object msg: arg                      (*)

where object is the receiver of the message with selector msg: and arg 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 the CompiledMethod with selector msg: in the object's hierarchy, and transfers it the control, binding self to object and the formal argument of the method to arg.

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 the doesNotUnderstand: message.

Step (2) exactly requires what #withArgs:executeMethod: provides. It allows us to say

   object withArgs: {arg} executeMethod: method

where method is the CompiledMethod found in step (1). [We have to use {arg} rather than arg because the plural in withArgs: suggests that the method expects an Array 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 a wrapper method, which also has a preBlock. If you then substitute the original method in the MethodDictionary where it belongs, with the wrapper, you can let the wrapper first execute the preBlock and then the intended method. The first task is easy: just send the message preBlock value. For the second we have the method (the wrappee), the receiver and the arguments (if any). So, to complete the task you only need to send to the receiver withArgs:executeMethod: with the actual argument(s) and the wrappee.

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 a CompiledMethod 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 a CompiledMethod without installing it in any class is by asking the Smalltalk compiler to do so (look for senders of newCompiler to learn how to do that).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文