views:

176

answers:

2

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'
A: 

You didn't follow directions correctly from the article you cited.

It specifically says to run the unit importer, which generates two additional files (from MyClass.pas it creates MyClass.int and uPSI_MyClass.pas). You need to use the uPSI_MyClass.pas (using, of course, the proper filename for your unit), and use the proper methods from that unit.

Assuming that your source for TMyPSPlugin is in MyPSPlugin.pas, the unit importer would create MyPSPlugin.int and uPSI_MyPSPlugin.pas. You'd need to add uPSI_MyPSPlugin to your uses clause, and then use TPSImport_MyPSPlugin.Create and the additional code to register the plugin. (See the fourth image from the web page you linked - the image has a caption bar reading "ide_editor.pas".) At that point, Pascal Script is aware of your class and will recognize it's Create and Free methods.

Ken White
Sorry if my code was confusing. I am using the generated class. I added the additional code to show this.
Lawrence Barsanti
A: 

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.

Mason Wheeler
Thank you. I added these two functions to the generated code and it worked. Note: I had to add this BEFORE the generated functions SIRegister_TMyPsPlugin and RIRegister_TMyPsPlugin were called.
Lawrence Barsanti