nielsAD / lape

Scripting engine with Pascal-like syntax for FPC and Delphi
118 stars 28 forks source link

failed to call records method for records with param type "const" or in "with do" construction #155

Closed Vizit0r closed 3 years ago

Vizit0r commented 3 years ago

Examples: 1)

var zzz : TStringList;
begin
zzz := TStringList.Create;
zzz.Add('test123');  //all ok
zzz.Free;

zzz := TStringList.Create;
with zzz do
try
  Add('test234'); //all ok.
finally
  free;
end;

with TStringList.Create do 
try
  Add('test345'); //Error: Variable expected
finally 
  free;  
end;

2) Error with same root cause

procedure CallGood( abc : TStringList );
begin
  abc.Add('test123');  //all ok
end; 

procedure CallBad(const abc : TStringList );
begin
  abc.Add('test123');  //Error: Variable expected
end; 
ollydev commented 3 years ago

How are you importing the methods and TStringList type?

The following works in Simba

procedure CallGood( abc : TStringList );
begin
  abc.Add('test123');  //all ok
end; 

procedure CallBad(const abc : TStringList);
begin
  abc.Add('test123');  //Error: Variable expected
end; 

var foo:TStringList;
begin
  foo.init();

  CallGood(foo);
  CallBad(foo);
end.
Vizit0r commented 3 years ago

@ollydev

//  LapeImport_TStringList(Compiler, 'TStringList');

procedure LapeImport_TStringList(Compiler : TLapeCompiler; ClassNamePrefix : String);
begin
//TStringList
    Compiler.addGlobalType('record f_f : Integer; end', ClassNamePrefix);
    Compiler.addGlobalMethod(Format('function %s.Create: TStringList;',[ClassNamePrefix]), @TStringList_Create, nil);
    Compiler.addGlobalMethod(Format('procedure %s.Free;',[ClassNamePrefix]), @TStringList_Free, nil);
    Compiler.addGlobalFunc(Format('function %s.Add(S: String): Integer;',[ClassNamePrefix]), @TStringList_Add);
....
end;

procedure TStringList_Create(const Params: PParamArray; const Result: Pointer);
begin
  PObject(Result)^ := TStringList.Create;
  PObject(Params^[0]) := PObject(Result);
end;

procedure TStringList_Free(const Params: PParamArray);
var _Self : TObject;
begin
  _Self := PObject(Params^[0])^;
  TStringList(_Self).Free;
  PObject(Params^[0])^ := nil;
end;

procedure TStringList_Add(const Params: PParamArray; const Result: Pointer);
var _Self : TObject;
begin
  _Self := PObject(Params^[0])^;
  PInteger(Result)^ := TStringList(_Self).Add(PlpString(Params^[1])^);
end;
Vizit0r commented 3 years ago

seems like my import is not correct, will try to use your importer. No documentation at all, even basic, searching a way blinded(

ollydev commented 3 years ago

Append constref like so: function %s.Add(S: String): Integer; constref;

This will tell Lape that Self is not being modifed and your script should work.

Lape should ideally handle this better, without needing a hint. But it is what it is.

ollydev commented 3 years ago

@Vizit0r ^

Vizit0r commented 3 years ago

update according to Simba import, works now.

thank you.