Is there a way to keep variable values when executing a DWScript twice?
Asked Answered
B

2

9

The application I am working on allows embedding script sinppets into a document. For example:

SomeText
<* PrintLn("This line is generated by a script"); *>
Some other text
<* PrintLn("This line is generated by a script, too"); *>
Some more lines

Results

SomeText
This line is generated by a script
Some other text
This line is generated by a script, too
Some more lines

I am using DWScript. Internally the first script snippet is compiled & executed. Than the next is RecompiledInContext and executed and so on. A function/variable/etc declared in a snippet becames available in all later snippets. However the variable values are lost between snippets. For example:

SomeText
<* var x: Integer = 5; *>
Some other text
<* PrintLn(x); *>
Some more lines

After generating the document:

SomeText
Some other text
0  <-- I would like this to be 5
Some more lines

Here is a sample application which illustrates the problem:

program POC.Variable;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  dwsExprs,
  dwsComp,
  dwsCompiler; 

var
  FDelphiWebScript: TDelphiWebScript;
  FProgram: IdwsProgram;
  FExecutionResult: IdwsProgramExecution;

begin
  FDelphiWebScript := TDelphiWebScript.Create(nil);
  try
    FProgram := FDelphiWebScript.Compile('var x: Integer = 2;');
    FProgram.Execute;

    FDelphiWebScript.RecompileInContext(FProgram, 'PrintLn(x);');

    FExecutionResult := FProgram.Execute;
    // The next line fails, Result[1] is '0'
    Assert(FExecutionResult.Result.ToString[1] = '2');
  finally
    FDelphiWebScript.Free;
  end
end.

Is there a way to "transfer" or "keep" the variable values between executions?

Here is an updated code of Andrew's answer which does not work:

begin
  FDelphiWebScript := TDelphiWebScript.Create(nil);
  try
    FProgram := FDelphiWebScript.Compile('PrintLn("Hello");');

    FExecution:= FProgram.BeginNewExecution();

    FDelphiWebScript.RecompileInContext(FProgram, 'var x: Integer;');
    FExecution.RunProgram(0);
    WriteLn('Compile Result:');
    WriteLn(FExecution.Result.ToString);

    FDelphiWebScript.RecompileInContext(FProgram, 'x := 2; PrintLn(x);');
    FExecution.RunProgram(0); // <-- Access violation
    WriteLn('Compile Result:');
    WriteLn(FExecution.Result.ToString);

    FExecution.EndProgram();
    ReadLn;
  finally
    FDelphiWebScript.Free;
  end
end;
Burlie answered 9/6, 2012 at 3:19 Comment(1)
Urgh... Swapping script commands breaks execution. Seems the only way for you to make this feature work is to dig deeply into library with the debugger. Good luck for you with DWSCript! :)Cashandcarry
C
1

The issue is that when RecompileInContext() adds new global variables, they don't have space allocated, as space allocation is performed by BeginNewExecution, but it should work if the variables pre-exist, or if new variables are added within a function so are local vars, rather than global ones).

So if you change the "updated code" to something like this, it will work

FProgram := DelphiWebScript1.Compile( 'PrintLn("Hello");'
                                     +'var x: Integer;');

FExecution:= FProgram.BeginNewExecution();

FExecution.RunProgram(0);
SynEdit1.Lines.Add('Compile Result:');
SynEdit1.Lines.Add(FExecution.Result.ToString);

DelphiWebScript1.RecompileInContext(FProgram, 'x := 2; PrintLn(x);');
FExecution.RunProgram(0); // <-- Access violation
SynEdit1.Lines.Add('Compile Result:');
SynEdit1.Lines.Add(FExecution.Result.ToString);

FExecution.EndProgram();

Edit: this as now been fixed by r1513 in the DWScript SVN.

Capita answered 20/6, 2012 at 10:9 Comment(1)
Hi Eric, works as expected with the latest source. This is awesome! Thanks for fixing this.Burlie
C
5

You can try to use BeginNewExecution / RunProgram / EndProgram block instead (tested on DWScript 2.2):

begin
  FDelphiWebScript := TDelphiWebScript.Create(nil);
  try
    FProgram := FDelphiWebScript.Compile('var x: Integer;');

    FExecution:= FProgram.BeginNewExecution();

    FDelphiWebScript.RecompileInContext(FProgram, 'x := 2; PrintLn(x);');
    FExecution.RunProgram(0);
    WriteLn('Compile Result:');
    WriteLn(FExecution.Result.ToString);

    FDelphiWebScript.RecompileInContext(FProgram, 'x := x + 3; PrintLn(x);');
    FExecution.RunProgram(0);
    WriteLn('Recompile Result: ');
    WriteLn(FExecution.Result.ToString);

    FExecution.EndProgram();

    ReadLn;
  finally
    FDelphiWebScript.Free;
  end
end.
Cashandcarry answered 9/6, 2012 at 7:7 Comment(4)
This is not reliable. I just tried the following code and it results in an AV error. (Have to post it as an answere since does not fit as a comment)Burlie
@RM which Delphi version you have?Cashandcarry
Delphi XE2 Update 4 (including hot fix) + latest DWScript sourceBurlie
@Burlie Latest DWScript means 2.3 (unstable)?Cashandcarry
C
1

The issue is that when RecompileInContext() adds new global variables, they don't have space allocated, as space allocation is performed by BeginNewExecution, but it should work if the variables pre-exist, or if new variables are added within a function so are local vars, rather than global ones).

So if you change the "updated code" to something like this, it will work

FProgram := DelphiWebScript1.Compile( 'PrintLn("Hello");'
                                     +'var x: Integer;');

FExecution:= FProgram.BeginNewExecution();

FExecution.RunProgram(0);
SynEdit1.Lines.Add('Compile Result:');
SynEdit1.Lines.Add(FExecution.Result.ToString);

DelphiWebScript1.RecompileInContext(FProgram, 'x := 2; PrintLn(x);');
FExecution.RunProgram(0); // <-- Access violation
SynEdit1.Lines.Add('Compile Result:');
SynEdit1.Lines.Add(FExecution.Result.ToString);

FExecution.EndProgram();

Edit: this as now been fixed by r1513 in the DWScript SVN.

Capita answered 20/6, 2012 at 10:9 Comment(1)
Hi Eric, works as expected with the latest source. This is awesome! Thanks for fixing this.Burlie

© 2022 - 2024 — McMap. All rights reserved.