Search code examples
delphiscriptingpascalscript

How can I get Pascal Script to recognize the 'create' and 'free' functions when importing a custom class?


I am having a problem with the example from this article. The article explains how to import your own classes so they can be called from a Pascal Script. I am importing my custom class but cannot get Pascal Script to recognize the 'Create' and 'Free' functions.

My plugin:

TMyPsPlugin = class
  public
    procedure PrintMessage(const AMessage: String);
end;

procedure TMyPsPlugin.PrintMessage(const AMessage: String);
begin
  ShowMessage(AMessage);
end;

My app:

procedure TForm1.FormCreate(Sender: TObject);
var
  Plugin: TPSPlugin;
begin
  Plugin := TPSImport_MyPsPlugin.Create(Self);
  TPSPluginItem(ps.Plugins.Add).Plugin := Plugin;
end;

procedure TForm1.bCompileClick(Sender: TObject);
begin
  ps.Script.Text := mScript.Text;
  if ps.Compile then
    begin
      if ps.Execute then
        ShowMessage('Done.')
      else
        ShowMessage('Execution Error: ' + Ps.ExecErrorToString);
    end
  else
    HandleError;
end;

My Script:

program test;
var
  Plugin: TMyPsPlugin;
begin
  Plugin := TMyPsPlugin.Create;
  Plugin.PrintMessage('Hello');
  Plugin.Free;
end.

Error Messages:

[Error] (5:25): Unknown identifier 'Create'
[Error] (7:10): Unknown identifier 'FREE'

Solution

  • Apparently your plugin class descends directly from TObject. Add uPSC_std and uPSR_std to your project and run SIRegisterTObject and RIRegisterTObject (C and R being the Compile-time and Runtime versions) before registering your plugin. That'll set up the default constructor and the Free method. If that doesn't work, make sure the unit importer specifically states that you're descending from TObject.