Skip to content

Param with type "array of const" doesn't work #241

@zedxxx

Description

@zedxxx

I want use in script this Delphi function: function Format_P(const Format: string; const Args: array of const): string;

If I use PascalScript sources from stable branch then all works fine, but when I switch to the master branch Args receives some garbage instead of actual values. My Delphi version is 10.3.3.

This bug can be easily reproduced with the test:

program FormatTest;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  SysUtils,
  uPSUtils,
  uPSRuntime,
  uPSCompiler;

function Format_P(const Format: string; const Args: array of const): string;
begin
  Result := SysUtils.Format(Format, Args);
  Writeln(Result); // for debug only
end;

function ScriptOnUses(Sender: TPSPascalCompiler; const AName: TbtString): Boolean;
begin
  if AName = 'SYSTEM' then begin
    Sender.AddDelphiFunction(
      'function Format_P(const Format: string; const Args: array of const): string'
    );
    Result := True;
  end else begin
    Result := False;
  end;
end;

procedure ExecuteScript(const AScript: AnsiString);
var
  I: Integer;
  VCompiler: TPSPascalCompiler;
  VExec: TPSExec;
  VData: AnsiString;
begin
  VCompiler := TPSPascalCompiler.Create;
  try
    VCompiler.OnUses := ScriptOnUses;
    if not VCompiler.Compile(AScript) then begin
      for I := 0 to VCompiler.MsgCount - 1 do begin
        Writeln(VCompiler.Msg[I].MessageToString);
      end;
      raise Exception.Create('Compile filed!');
    end;

    VCompiler.GetOutput(VData);
  finally
    VCompiler.Free;
  end;

  VExec := TPSExec.Create;
  try
    VExec.RegisterDelphiFunction(@Format_P, 'Format_P', cdRegister);

    if not VExec.LoadData(VData) then begin
      raise Exception.Create('Exec filed!');
    end;

    VExec.RunScript;
  finally
    VExec.Free;
  end;
end;

const
  VScript = 'begin Format_P(''a=%d b=%d'', [1, 2]); end.';
begin
  try
    ExecuteScript(AnsiString(VScript));
  except
    on E: Exception do begin
      Writeln(E.ClassName, ': ', E.Message);
    end;
  end;

  Writeln('Press ENTER to exit...');
  Readln;
end.

Test result:

  • stable branch outputs: a=1 b=2 (correct)
  • master branch outputs: a= b=-1 (incorrect)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions