Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-07

What happens when assigning interfaces?

6 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ryan Joseph

Given the code below and the 2 cases can anyone explain what exactly the compiler does when the interfaces are assigned to? Does it copy a record like structure, call some internal functions, do runtime checks? I'm curious if they're doing more than I think they do.

============================

{$mode objfpc}
{$interfaces corba}
program unit_name;
type
  IFoo = interface ['IFoo']
  end;
type
  TMyClass = class (IFoo)
  end;
procedure DoSomething(i: IFoo);
begin
end;
var
  f: IFoo;
  c: TMyClass;
begin
  // case A
  f := c;
  // case B
  DoSomething(c);
end.

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Ryan Joseph
Ryan Joseph wrote 30 lines
Given the code below and the 2 cases can anyone explain what exactly the compiler does when the interfaces are assigned to? Does it copy a record like structure, call some internal functions, do runtime checks? I'm curious if they're doing more than I think they do. ============================ {$mode objfpc} {$interfaces corba} program unit_name; type IFoo = interface ['IFoo'] end; type TMyClass = class (IFoo) end; procedure DoSomething(i: IFoo); begin end; var f: IFoo; c: TMyClass; begin // case A f := c; // case B DoSomething(c); end.

As you're using CORBA or RAW interfaces a) will simply copy a shifted pointer to the 'c' class instance (so that it points to the VMT generated for the interface). And b) is a simple pointer passing.

Regards,
Sven

Reply by email

Ryan Joseph
to Sven Barth
On Jul 10, 2020, at 12:23 AM, Sven Barth via fpc-pascal wrote: (so that it points to the VMT generated for the interface)

So, it sounds like what the interface actually is, is a pointer to VMT table.

The VMT is generated with the class that implements the interface? Does it have to do a lookup to find the VMT table, like calling Supports() does?

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Ryan Joseph
Ryan Joseph wrote 5 lines
> On Jul 10, 2020, at 12:23 AM, Sven Barth via fpc-pascal wrote: > > (so that it points to the VMT generated for the interface) So, it sounds like what the interface actually is, is a pointer to VMT table.

Correct.

The VMT is generated with the class that implements the interface? Does it have to do a lookup to find the VMT table, like calling Supports() does?

Yes, the VMT belongs to the class. It contains interface thunks that adjust the Self pointer back to what the class expects before jumping(!) to the correct methods.

A lookup is only necessary if you use e.g. "SomeClass as SomeIntf". In the case of your example the compiler knows that TMyClass implements IFoo and thus can simply adjust the Self pointer.

Regards,
Sven

Reply by email

Ryan Joseph
to Sven Barth
On Jul 10, 2020, at 12:31 PM, Sven Barth wrote: A lookup is only necessary if you use e.g. "SomeClass as SomeIntf".

so if I do:

DoSomething(c as IFoo);

then Supports() is called at runtime to return the IFoo interface? That's the kind of thing I was really curious about, when are runtime checks performed vs compile time checks.

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Ryan Joseph

Ryan Joseph via fpc-pascal schrieb am
Fr., 10. Juli 2020, 12:04:

Show quoted text 15 lines
> On Jul 10, 2020, at 12:31 PM, Sven Barth wrote: > > A lookup is only necessary if you use e.g. "SomeClass as SomeIntf". so if I do: DoSomething(c as IFoo); then Supports() is called at runtime to return the IFoo interface? That's the kind of thing I was really curious about, when are runtime checks performed vs compile time checks.

If you use the "as" or "is" operators it will be runtime checks, otherwise it will be compile time checks.

Also to be pedantic: the operators use the lower level API GetInterfaceByStr not Supports (which uses another low level API).

Regards,
Sven

Reply by email