Skip to content

InvokeCall.inc: store a btProcPtr result in the proc-ptr variable layout - #321

Open
Vizit0r wants to merge 1 commit into
remobjects:masterfrom
Vizit0r:fix/procptr-result-layout
Open

Vizit0r wants to merge 1 commit into
remobjects:masterfrom
Vizit0r:fix/procptr-result-layout

Conversation

@Vizit0r

@Vizit0r Vizit0r commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Problem

A script proc-ptr variable is three pointer-sized slots (ProcNo, Self, Ptr), and the runtime treats Self/Ptr as a native method only when ProcNo = 0 (the CM_PV case in TPSExec.RunLine). In InvokeCall.inc the btProcPtr result of an invoked function is written as a bare TMethod, which puts Code into ProcNo. Calling the returned procedure from the script then fails with "Out of Proc Range".

Repro

type
  TCB = procedure(var x, y: Integer);   // registered with AddTypeS as
                                        // 'procedure(var x : Integer; var y : Integer)'

procedure DelphiCB(var x, y: Integer);
begin
  x := 600;
  y := 800;
end;

function GetCB: TMethod;
begin
  Result.Code := @DelphiCB;
  Result.Data := nil;
end;

// OnCompile: Sender.AddFunction(@GetCB, 'function GetCB : TCB;');

Script:

var CB: TCB; X, Y: Integer;
begin
  X := 1; Y := 2;
  CB := GetCB;
  CB(X, Y);        // runtime error: Out of Proc Range
end.

Change

The result is written in the proc-ptr layout: ProcNo := 0, then Self := Data, Ptr := Code, i.e. the same layout the runtime builds for a native method elsewhere.

Testing

Delphi 13 (InvokeCall path, the default since Delphi 10.1), Win32 and Win64, current master:

  • Before: Out of Proc Range on the CB(X, Y) line.
  • After: the Delphi procedure runs, the script gets X = 600, Y = 800.

A script proc-ptr variable is three pointer-sized slots (ProcNo, Self,
Ptr), and the runtime treats Self/Ptr as a native method only when
ProcNo = 0 (CM_PV in TPSExec.RunLine). The btProcPtr result of an
invoked function was written as a bare TMethod, which put Code into
ProcNo, so calling the returned procedure from the script failed with
"Out of Proc Range".

Repro: register a Delphi function that returns a TMethod (Data = nil,
Code = a plain procedure) as "function GetCB : TCB;" with
TCB = procedure(var x : Integer; var y : Integer), then in the script
"CB := GetCB; CB(X, Y);". Before: runtime error "Out of Proc Range" on
the call, Win32 and Win64 (Delphi 13, InvokeCall path). After: the
Delphi procedure runs and writes X and Y back.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant