Search code examples
delphidwscript

Delphi Web Script: Accessing a Variable after the Script is Executed


Imagine this Script:

var s = TStrings.Create;
s.Add('Line 1');
s.Add('Line 2');

procedure MyProc;
begin
  if s.count = 2 then
  // ...
end;

When the Script runs it creates the variable "s". Now I'd like to call "MyProc" after the script is done:

...
Exec := Program.CreateNewExecution;
Exec.BeginProgram;
Exec.RunProgram(0);
if Exec.ProgramState in [psRunning, psRunningStopped] then
begin
  Func := Exec.Info.Func['MyProc']; 
  Func.Call([]);
  Exec.EndProgram; 
end;

I get an error accessing "s" from MyProc. I assume that the garbage collector of DWS already freed the stringlist. Is this right? Can I do something to keep "s" alive?


Solution

  • That variable should not be cleaned up before the call to EndProgram.

    One thing that could explain your issue is if your script didn't compile without errors in the first place (check Program.Msgs, it should then contain the errors).