PascalScript can't use classes like TStringList lazarus / 5-Lazarus Extra Components ---------------------------------------------------------------------- lainz · 2016-07-09 23:07 Hi, when I try to use a TStringList for example, the application crashes and gives me a SIGSEV error. I've already attached the Plugin to the Script, and also it compiles, so is not a compilation problem of the script but a problem executing it. Attached an example. Just uncomment the lines in the SynEdit and try. ---------------------------------------------------------------------- derek.john.evans · 2016-07-10 02:16 Ok, Ive had a look for what its worth. It seems all constructors are broken. I had a look in uPSRuntime & x86.inc which includes a lot of FPC specific edits to handle FPC constructor calling conventions, all of which are beyond my knowledge. So, I came up with two work arounds: 1) Create an external function which creates the TStringList. ie: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function CreateStringList: TStringList;begin  Result := TStringList.Create;end; procedure TForm1.PSScript1Compile(Sender: TPSScript);begin  PSScript1.AddFunction(@ShowMessage, 'procedure ShowMessage(m: string);');  PSScript1.AddFunction(@CreateStringList, 'function CreateStringList: TStringList;');end;   Then this code works: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  s: TStringList;begin  ShowMessage('This line works.');  s := CreateStringList;  s.Add('Test');  ShowMessage(s[0]);  s.Free;end.    But, it still will crash if you call the constructor. So: 2) Register a constructor for TStringList which links to the CreateStringList function: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.PSScript1CompImport(Sender: TObject; x: TPSPascalCompiler);begin  x.FindClass('TStringList').RegisterMethod('constructor Create');end; procedure TForm1.PSScript1ExecImport(Sender: TObject; se: TPSExec; x: TPSRuntimeClassImporter);begin  x.FindClass('TStringList').RegisterConstructor(@CreateStringList, 'Create');end;   This "shouldn't" work but it does for some reason. Im guessing someone with more knowledge of FPC constructor calling needs to look at this issue. But, there are 2 workarounds. ---------------------------------------------------------------------- fpcoder · 2016-07-10 06:34 EDIT: My suggestion doesn't work anymore. I use Geepsters workaround now. If I remember right, you should edit uPSC_classes.pas so that the constructor becomes available. There's a {$IFDEF DELPHI2005UP} that prevents the constructor from being registered. Dont forget to do the same for the TStrings class! Its in the same file, a bit up. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure SIRegisterTSTRINGLIST(Cl: TPSPascalCompiler);begin  with Cl.AddClassN(cl.FindClass('TStrings'), 'TStringList') do  begin {$IFDEF DELPHI2005UP}    RegisterMethod('constructor Create;');{$ENDIF}     RegisterMethod('function Find(S:String;var Index:Integer):Boolean');    RegisterMethod('procedure Sort');    RegisterProperty('Duplicates', 'TDuplicates', iptrw);    RegisterProperty('Sorted', 'Boolean', iptrw);    RegisterProperty('OnChange', 'TNotifyEvent', iptrw);    RegisterProperty('OnChanging', 'TNotifyEvent', iptrw);  end;end; ---------------------------------------------------------------------- lainz · 2016-07-10 13:39 Thanks Geepster, both workarounds are working. Kapibara, commenting the ifdef does not works for me, you've tested it? Edit: Reported here http://bugs.freepascal.org/view.php?id=30363 ---------------------------------------------------------------------- Martin_fr · 2016-07-10 16:03 please test with the upstream/original source. ---------------------------------------------------------------------- lainz · 2016-07-10 16:46 Quote from: Martin_fr on July 10, 2016, 06:03:59 pmplease test with the upstream/original source. Yes, it works with the PascalScript downloaded from GitHub. ---------------------------------------------------------------------- fpcoder · 2016-07-11 05:17 @Lainz Yes, a year ago I managed to import the TStringList and use from inside script, but it involved modifying a line or two of the source. Unfortunately I cant remember the exact steps. Looking at the source for the app I wrote, this is how the stringlist was made available. But it required the modified pascalscript source which later got accidentally replaced by a fresh unmodified version. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TDataParser.OnCompImport(Sender: TObject; ACompiler: TPSPascalCompiler);begin  SIRegister_Std(ACompiler);  SIRegisterTObject(ACompiler);  SIRegisterTStrings(ACompiler, True); //Both are  SIRegisterTStringList(ACompiler);    //needed!!end;  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TDataParser.OnExecImport(Sender: TObject; APSExec: TPSExec;  AClassImporter: TPSRuntimeClassImporter);begin  RIRegister_Std(AClassImporter);  RIRegisterTObject(AClassImporter);  RIRegisterTStrings(AClassImporter, True);  //Both are  RIRegisterTStringList(AClassImporter);     //Needed !!end; ---------------------------------------------------------------------- lainz · 2016-07-12 17:07 I'm now using the GitHub version, I need only to comment one line of code that don't want to be compiled and that's all. My code works with no modifications and no need to do OnCompImport and OnExecImport. ---------------------------------------------------------------------- fpcoder · 2016-07-13 01:35 @lainz - Thats great! I want it up and running too and removed lazarus preinstalled pascalscript and installed the GIT version. For me there was no error on install except lazarus package EditorMacroScript needed something from the removed sources. Anyway, I just wanted to see if it worked, but got SIGSEG in the script on TStringList.Create even after adding the PSImport_classes plugin to PSScript. Could you describe more closely what you did, or attach a small sample that you know works. Maybe its my installation that is wrong. ---------------------------------------------------------------------- lainz · 2016-07-13 01:47 I commented a line when compiling it and also the ide was using the old package. I deleted the folder under components and replaced with the git one i downloaded from github. Then i rebuild lazarus. The first post here contains an attachment that works just uncomment the lines and run it. ---------------------------------------------------------------------- lainz · 2016-07-13 01:49 Also im using lazarus release not trunk...