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;
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! :) Commented Jun 9, 2012 at 22:27

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

4 Comments

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)
@RM which Delphi version you have?
Delphi XE2 Update 4 (including hot fix) + latest DWScript source
@RM. Latest DWScript means 2.3 (unstable)?
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.

1 Comment

Hi Eric, works as expected with the latest source. This is awesome! Thanks for fixing this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.