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

徘徊边缘 提交于 2019-12-11 09:29:15

问题


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'

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/1743643/how-can-i-get-pascal-script-to-recognize-the-create-and-free-functions-when

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!