La aplicación en la que estoy trabajando permite incrustar secuencias de comandos en un documento. Por ejemplo:¿Hay alguna manera de mantener los valores de las variables al ejecutar un DWScript dos veces?
SomeText
<* PrintLn("This line is generated by a script"); *>
Some other text
<* PrintLn("This line is generated by a script, too"); *>
Some more lines
Resultados
SomeText
This line is generated by a script
Some other text
This line is generated by a script, too
Some more lines
estoy usando DWScript. Internamente se compila el primer fragmento de script &. Que el siguiente es RecompiledInContext y se ejecuta, y así sucesivamente. Una función/variable/etc. declarada en un fragmento se vuelve disponible en todos los fragmentos posteriores. Sin embargo, los valores de las variables se pierden entre los fragmentos. Por ejemplo:
SomeText
<* var x: Integer = 5; *>
Some other text
<* PrintLn(x); *>
Some more lines
Después de generar el documento:
SomeText
Some other text
0 <-- I would like this to be 5
Some more lines
Aquí es una aplicación de ejemplo que ilustra el problema:
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.
¿Hay una manera de "transferencia" o "mantener "los valores variables entre ejecuciones?
Aquí es un código actualizado de la respuesta de Andrew que no funciona:
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;
Urgh ... El comando de secuencia de comandos de intercambio interrumpe la ejecución. Parece que la única forma de que haga funcionar esta característica es profundizando en la biblioteca con el depurador. ¡Buena suerte para ti con DWSCript! :) – Andrew