Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
zedxxx opened this issue Dec 27, 2020 · 8 comments
Closed

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

zedxxx opened this issue Dec 27, 2020 · 8 comments

Comments

@zedxxx
Copy link
Contributor

zedxxx commented Dec 27, 2020

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)
@zedxxx
Copy link
Contributor Author

zedxxx commented Dec 27, 2020

I found that it stopped working after this commit: 86a057c

@Vizit0r can you look into this?

@zedxxx zedxxx changed the title Param with type "array of const" don't work Param with type "array of const" doesn't work Dec 27, 2020
@Vizit0r
Copy link
Contributor

Vizit0r commented Dec 28, 2020

@zedxxx on my latest connit, which i already push to my rep, and awaiting for @carlokok approval to merge with main code - all works fine, checked it right now.
a=1 b=2
Press ENTER to exit...

@carlokok
Copy link
Member

@Vizit0r which branch/pr?

@Vizit0r
Copy link
Contributor

Vizit0r commented Dec 28, 2020

@carlokok look at the "main" thread -
#225
or i miss something and it does not requested as merge?

@zedxxx
Copy link
Contributor Author

zedxxx commented Dec 29, 2020

@Vizit0r I tested your fork and can confirm that it fixes this bug.

@zedxxx
Copy link
Contributor Author

zedxxx commented Sep 25, 2024

This commit 2962298 (related issue #267) partly fix this issue. However, there are still some bugs and test code failures when param contains an AnsiString value.

const
  VScript = 'var s: AnsiString; begin s := ''ansistring''; Format_P(''a=%d b=%d s=%s'', [1, 2, s]); end.';

@L00pZBreak3r
Copy link

Array of const works.
Array of String (or array of any type) is broken for a very long time.
Try to use function FmtMessage.

@zedxxx
Copy link
Contributor Author

zedxxx commented Oct 11, 2024

@L00pZBreak3r I think you should report a new issue about this.

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

No branches or pull requests

5 participants