Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8113227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:45:41+00:00 2026-06-06T02:45:41+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:45:44+00:00Added an answer on June 6, 2026 at 2:45 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an Android application that allows the user to speak into
I'm working on an application that allows users to edit invoices generated in other
I'm working with an application that allows the customer to customize what shortcut keys
I'm working on an application that allows users to add their own RSS feeds
I'm working on a multiplayer iPhone application that allows up to 6 users to
I'm working on an application that allows the user to send a file using
I am working on an application that allows users to manipulate multiple images by
I'm currently working on an application that allows people to schedule Shows for an
I have been working on an application which allows the user to make a
I am currently working on an application that allows reverse geocoding using silverlight +

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.