Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2013-09

PascalScript and Self instance

10 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

silvioprog

Hello,

I'm trying to set an instance to after to call a method (called as WriteLn) of it, but the result is:

[Error] (1:7): Unknown identifier 'WriteLn'

In attached the small project showing my implementation.

So, how to I set an instance to be used as default in my scripts?

Thank you!

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130916/bd083000/attachment-0001.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: test.zip Type: application/zip Size: 2572 bytes Desc: not available URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130916/bd083000/attachment-0001.zip>

Reply by email

Martin
to silvioprog
silvioprog wrote 13 lines
Hello, I'm trying to set an instance to after to call a method (called as WriteLn) of it, but the result is: [Error] (1:7): Unknown identifier 'WriteLn' In attached the small project showing my implementation. So, how to I set an instance to be used as default in my scripts? Thank you!

MAybe later more.

Have you looked at the editormacroscript package?

IT does exactly that

Reply by email

silvioprog
to Martin

2013/9/16 Martin

Show quoted text 21 lines
On 16/09/2013 18:03, silvioprog wrote: > Hello, > > I'm trying to set an instance to after to call a method (called as > WriteLn) of it, but the result is: > > [Error] (1:7): Unknown identifier 'WriteLn' > > In attached the small project showing my implementation. > > So, how to I set an instance to be used as default in my scripts? > > Thank you! > > Maybe later more. Have you looked at the editormacroscript package? IT does exactly that

Hm... I saw all units of editormacroscript, but I still don't understand how the Lazarus team pass the SELF instance as default. :(

Thank you anyway!

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130916/097568d3/attachment.html>

Reply by email

Martin
to silvioprog
silvioprog wrote 30 lines
2013/9/16 Martin < <mailto:>> On 16/09/2013 18:03, silvioprog wrote: Hello, I'm trying to set an instance to after to call a method (called as WriteLn) of it, but the result is: [Error] (1:7): Unknown identifier 'WriteLn' In attached the small project showing my implementation. So, how to I set an instance to be used as default in my scripts? Thank you! Maybe later more. Have you looked at the editormacroscript package? IT does exactly that Hm... I saw all units of editormacroscript, but I still don't understand how the Lazarus team pass the SELF instance as default. :( Thank you anyway!

What do you mean "passing the self const as default"? -------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130916/831c888e/attachment.html>

Reply by email

Martin
to Martin
Martin wrote 6 lines
Hm... I saw all units of editormacroscript, but I still don't understand how the Lazarus team pass the SELF instance as default. :( Thank you anyway! What do you mean "passing the self const as default"?

Ok, found the answer in the lpr.

I would not expect that to work, after all you cant do it in normal pascal either?

the main begin end is a program does not belong to a class. -------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130916/170afedd/attachment.html>

Reply by email

silvioprog
to Martin: What do you mean "passing the self const as default"? -------------- next part…

2013/9/16 Martin

What do you mean "passing the self const as default"?

I'm trying it:

  TMyClass = class
  private
    FData: string;
  public
    procedure WriteLn(const S: string);
...
var
  obj: TMyClass;
  ps.SetVarToInstance('SELF', obj);

So when I call "WriteLn", it must call WriteLn of my "obj" instance.

Please see the full code in attached in previous e-mail.

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130916/9989ec61/attachment.html>

Reply by email

Martin
to silvioprog
silvioprog wrote 23 lines
2013/9/16 Martin < <mailto:>> What do you mean "passing the self const as default"? I'm trying it: TMyClass = class private FData: string; public procedure WriteLn(const S: string); ... var obj: TMyClass; ps.SetVarToInstance('SELF', obj); So when I call "WriteLn", it must call WriteLn of my "obj" instance. Please see the full code in attached in previous e-mail.

yes found it.

Never used SetVarToInstance, dont know if it does what you want. -------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130916/8383778b/attachment-0001.html>

Reply by email

silvioprog
to silvioprog: 2013/9/16 Martin I'm trying it: TMyClass = class private FData: string; public procedure…

2013/9/16 silvioprog

Show quoted text 23 lines
2013/9/16 Martin > What do you mean "passing the self const as default"? > I'm trying it: TMyClass = class private FData: string; public procedure WriteLn(const S: string); ... var obj: TMyClass; ps.SetVarToInstance('SELF', obj); So when I call "WriteLn", it must call WriteLn of my "obj" instance. Please see the full code in attached in previous e-mail.

OMG, I think that I found an useful sample(*). I'll try it. :)

(*) http://forum.lazarus.freepascal.org/index.php?topic=15563.0
Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130916/37549b03/attachment.html>

Reply by email

silvioprog
to Martin: Ok, found the answer in the lpr. I would not expect that to work, after all you cant do…

2013/9/16 Martin

Show quoted text 7 lines
Ok, found the answer in the lpr. I would not expect that to work, after all you cant do it in normal pascal either? the main begin end is a program does not belong to a class.

Writeln was only by way of illustration, it could be anything else. :)

Problem solved with:

program project1;
{$mode objfpc}{$H+}
uses
  uPSComponent, myclass, uPSI_myclass;
type
  TApp = class
  private
    Fobj: TMyClass;
    Fps: TPSScript;
  public
    constructor Create;
    destructor Destroy; override;
    procedure OnCompile(Sender: TPSScript);
    procedure OnExecute(Sender: TPSScript);
    property ps: TPSScript read Fps;
    property obj: TMyClass read Fobj write Fobj;
  end;
  constructor TApp.Create;
  begin
    Fps := TPSScript.Create(nil);
    Fps.OnCompile := @OnCompile;
    Fps.OnExecute := @OnExecute;
  end;
  destructor TApp.Destroy;
  begin
    Fps.Free;
    inherited Destroy;
  end;
  procedure TApp.OnCompile(Sender: TPSScript);
  begin
    Sender.AddRegisteredVariable('Self', 'TMyClass');
  end;
  procedure TApp.OnExecute(Sender: TPSScript);
  begin
    Sender.SetVarToInstance('SELF', Fobj);
  end;
var
  app: TApp;
  i: integer;
  obj: TMyClass;
  pl: TPSImport_myclass;
begin
  app := TApp.Create;
  pl := TPSImport_myclass.Create(nil);
  obj := TMyClass.Create;
  try
    app.obj := obj;
    TPSPluginItem(app.ps.Plugins.Add).Plugin := pl;
    app.ps.Script.Text := 'begin Self.WriteLn(''OK''); end.';
    if app.ps.Compile then
      app.ps.Execute;
    WriteLn(obj.Data);
    WriteLn('--');
    for i := 0 to Pred(app.ps.Comp.MsgCount) do
      WriteLn(app.ps.Comp.Msg[I].MessageToString);
  finally
    obj.Free;
    pl.Free;
    app.ps.Free;
  end;
  ReadLn;
end.

Thank you very much! (y)

Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130916/d1657b43/attachment.html>

Reply by email

silvioprog
to silvioprog

2013/9/16 silvioprog

Show quoted text 87 lines
2013/9/16 Martin > Ok, found the answer in the lpr. > > I would not expect that to work, after all you cant do it in normal > pascal either? > > the main begin end is a program does not belong to a class. > Writeln was only by way of illustration, it could be anything else. :) Problem solved with: program project1; {$mode objfpc}{$H+} uses uPSComponent, myclass, uPSI_myclass; type TApp = class private Fobj: TMyClass; Fps: TPSScript; public constructor Create; destructor Destroy; override; procedure OnCompile(Sender: TPSScript); procedure OnExecute(Sender: TPSScript); property ps: TPSScript read Fps; property obj: TMyClass read Fobj write Fobj; end; constructor TApp.Create; begin Fps := TPSScript.Create(nil); Fps.OnCompile := @OnCompile; Fps.OnExecute := @OnExecute; end; destructor TApp.Destroy; begin Fps.Free; inherited Destroy; end; procedure TApp.OnCompile(Sender: TPSScript); begin Sender.AddRegisteredVariable('Self', 'TMyClass'); end; procedure TApp.OnExecute(Sender: TPSScript); begin Sender.SetVarToInstance('SELF', Fobj); end; var app: TApp; i: integer; obj: TMyClass; pl: TPSImport_myclass; begin app := TApp.Create; pl := TPSImport_myclass.Create(nil); obj := TMyClass.Create; try app.obj := obj; TPSPluginItem(app.ps.Plugins.Add).Plugin := pl; app.ps.Script.Text := 'begin Self.WriteLn(''OK''); end.'; if app.ps.Compile then app.ps.Execute; WriteLn(obj.Data); WriteLn('--'); for i := 0 to Pred(app.ps.Comp.MsgCount) do WriteLn(app.ps.Comp.Msg[I].MessageToString); finally obj.Free; pl.Free; app.ps.Free; end; ReadLn; end. Thank you very much! (y)

Or:

program project1;
{$mode objfpc}{$H+}
uses
  uPSCompiler, uPSRuntime, uPSC_std, uPSR_std, myclass, uPSI_myclass;
  function ScriptOnUses(Sender: TPSPascalCompiler; const Name: string):
boolean;
  begin
    if Name = 'SYSTEM' then
    begin
      SIRegister_Std(Sender);
      SIRegister_myclass(Sender);
      AddImportedClassVariable(Sender, 'Self', 'TMyClass');
      Result := True;
    end
    else
      Result := False;
  end;
  procedure ExecuteScript(const Script: string);
  var
    Compiler: TPSPascalCompiler;
    Exec: TPSExec;
    Data: string;
    CI: TPSRuntimeClassImporter;
    obj: TMyClass;
  begin
    Compiler := TPSPascalCompiler.Create;
    Compiler.OnUses := @ScriptOnUses;
    if not Compiler.Compile(Script) then
    begin
      Compiler.Free;
      Exit;
    end;
    Compiler.GetOutput(Data);
    Compiler.Free;
    CI := TPSRuntimeClassImporter.Create;
    RIRegister_Std(CI);
    RIRegister_myclass(CI);
    Exec := TPSExec.Create;
    RegisterClassLibraryRuntime(Exec, CI);
    if not Exec.LoadData(Data) then
    begin
      Exec.Free;
      Exit;
    end;
    obj := TMyClass.Create;
    SetVariantToClass(Exec.GetVarNo(Exec.GetVar('SELF')), obj);
    Exec.RunScript;
    WriteLn(obj.Data);
    Exec.Free;
    obj.Free;
    CI.Free;
  end;
begin
  ExecuteScript('begin Self.WriteLn(''OK'') end.');
  ReadLn;
end.
Silvio Cl?cio My public projects - github.com/silvioprog -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130916/55b74223/attachment-0001.html>

Reply by email