Mac Local Procedure Parameters
(macOS only)
Note: This is a proposal
In procedure A, a reference to a local or global procedure C is to be passed as a parameter to a procedure B. Procedure B might be declared in another unit, unaware of possible type of callers. The body of B will in turn call procedure C. If procedure C is local, it can now access variables local to A, if global, it can't. Procedure B might also pass the parameter further, but is not allowed to store the parameter in a variable.
The procedure parameters can be implemented as a conceptual record with one procedure pointer and one local scope pointer.
When passing local procedures, the local scope pointer will point to the local variables, when passing a global procedure, the local scope pointer is set to nil.
The tricky problem is how B should call C. If the local scope pointer is nil, C should be called as a global procedure, if not nil it should be called as a local procedure, that is along with the local scope pointer.
By putting the local scope pointer last, the procedure calling code chunk can be the same for local and global calls. A runtime check checks whether the local scope pointer is not nil, and if so pushes it.
All three procedures A, B and C have to be declared in MacPas units for this to work, otherwise there is a compilation error.
In the future, for some register based calling conventions, if the local scope pointer is put in a register which otherwise would not be in use, the runtime check can be optimized away. (for PowerPC, if the local scope pointer happens to be allocated in one of r3 - r10)
Also, cdecl for local procedure calls should not be allowed.
From the Free Pascal wiki, page "Mac Local Procedure Parameters", as it stood on 2019-10-17 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.
Talk about this page
Nobody has opened a bench for this page yet. Sign in to open one.
