Island-Window DllMain question

In Island RTL, we have the following method:

    method DllMainCRTStartup(aModule: rtl.HMODULE; aReason: rtl.DWORD; aReserved: ^Void): Boolean;
    begin
    	var lMain: ^DllMainType := @_dllmain;
    	ExternalCalls.fModuleHandle := aModule;
    	if lMain^ = nil then exit true;
    	exit lMain^(aModule, aReason, aReserved);
    end;

I am curious - is there any specific reason to use lMain: ^DllMainType?

Wouldn’t the following accomplish the same, with fewer lines?

method DllMainCRTStartup(aModule: rtl.HMODULE; aReason: rtl.DWORD; aReserved: ^Void): Boolean;
begin
	ExternalCalls.fModuleHandle := aModule;
	if _dllMain = nil then exit true;
	exit _dllMain(aModule, aReason, aReserved);
end;

The problem is that the adres of the dllmain function is nil, and you can’t really compare that to nil directly, as it’s typeless

would’t assigned(_dllMain) work there?

No because the normal way is that it would try to invoke that function. So the issue really is a rare compiler limitation, you normally never check this. What I did is work around the issue instead of changing the compiler fo just this.

Does that mean, for a similar situation, we must use the same trick? For example,
type
MyFuncType = method();

var
myFunc: ^MyFuncType;


if myFunc^ <> nil then …

For Block/Delegate, this won’t be an issue, though, correct?

If the compiler enforces the rule (like C) that

  • invoking a method call must have parenthesis (even for parameter-less methods)

or a rule like Delphi

  • a function pointer used as L-value, will be treated as function invocation

Then we won’t have the issue, correct?

No you won’t need this trick in 99% of the cases because we rarely check if a method itself is nil(opposed to a function pointer).

Sort of; In delphi a function: Boolean would invoke and require @. In Elements all function → function pointer assignments require @. If implemented the compiler would need to support if @_dllMain = nil then … But this code is so rare that I didn’t want to turn it into a special compiler feature.

In short and simply put, all can be summarized in the following:

  • A variable of Method type, when assigned must use the address operator @. It stores the entry address of the method
  • Method type itself is not nullable, hence can not be compared to nil
  • Event/Block type, on the other hand, is nullable type thus can be compared to nil

Are these accurate? @ck

Pretty much yes.