开发者

Internal (memory) representation of TProc and references at all

开发者 https://www.devze.com 2023-03-16 23:50 出处:网络
Does anyone here know how Delphi represents a reference to procedure? for example var proc: TProc; ... proc = procedure begin beep end;

Does anyone here know how Delphi represents a reference to procedure?

for example

var
  proc: TProc;
...
proc = procedure begin beep end;

What do we got in "proc"?

I know that for "method variable" the memory representation is 4 bytes for the "procedure address" followed by 4 bytes for the "object address", but for "reference to procedure" is somewhat different and I cannot quite figure it out.

The reason I want this is because I have some 开发者_如何学编程legacy code that I want to make it work with references.

Does anyone know something about it?


Method references are implemented as a COM-style interface with a single method called Invoke, which has the same signature as the method reference.

So TProc looks like this:

type
  TProc = interface(IInterface) // so inherits QI, AddRef, Release
     procedure Invoke;
  end;

It's a valid question to ask, as Delphi has interoperability with the C++ product. By using a pre-existing reference-counted type and idiom (COM lifetime rules), interop with C++ at the method reference level is possible.

Anonymous methods generate a hidden class which implements an interface isomorphic to the method reference interface, i.e. exactly the same shape, but not with the same symbolic identity. The hidden class doesn't implement the method reference interface directly because it may need to implement the interface multiple times (a single block may contain multiple anonymous methods all assigned to locations of the same method reference type).

0

精彩评论

暂无评论...
验证码 换一张
取 消