ghkweon / dwscript

Automatically exported from code.google.com/p/dwscript
0 stars 0 forks source link

Recreating the TRect Structure - strange results #305

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I am working on some script in version 2.3 in Delphi XE2 which will be 
performing drawing on a canvas in the end delphi application. Right now, I'm 
trying to recreate the `TRect` record and the `Rect` function and test it, but 
although my script does not throw any errors, it is giving strange random 
results. When I execute the script below, most of the time the result is empty, 
but occasionally shows a random number and sometimes "50". None of these 
outputs are correct.

type
  TRect = record
    Left: Integer;
    Top: Integer;
    Right: Integer;
    Bottom: Integer;
  end;
function Rect(const Left, Top, Right, Bottom: Integer): TRect;
begin
  Result.Left:= Left;
  Result.Top:= Top;
  Result.Right:= Right;
  Result.Bottom:= Bottom;
end;
function RectToStr(const R: TRect): String;
var
  X: Integer;
begin
  X:= R.Left + R.Top + R.Right + R.Bottom;
  Result:= IntToStr(X);
  //Result:= IntToStr(R.Left) + ',' + IntToStr(R.Top) + 
    //',' + IntToStr(R.Right) + ',' + IntToStr(R.Bottom);
end;
PrintLn(RectToStr(Rect(50,40,30,20)));

As you can see above, I am calling the function `Rect` to create a `TRect` 
record, passing the 4 values to it. At the same time, this function is called 
within `RectToStr`, which is also called from within `PrintLn`. So there's 3 
functions called together, and the third one is expected to return a `TRect` 
record. But somehow, it is breaking. The above code should produce a result of 
"140" (and when uncommenting the 2 lines, "50,40,30,20") but instead, I'm 
getting results as listed below with the same script:

50
15063514
[BLANK]

Here's the simple code I'm using to execute the script:

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  prog : IdwsProgram;
  exec : IdwsProgramExecution;
begin
  prog := DelphiWebScript1.Compile(Memo1.Text);
  exec := prog.Execute;
  Memo2.Lines.Append(exec.Result.ToString);
end;

Original issue reported on code.google.com by djjd47...@gmail.com on 3 Nov 2012 at 5:54

GoogleCodeExporter commented 8 years ago
UPDATE: I created a simple console application and dropped my above script into 
the delphi app and it compiled/ran just fine (replacing "PrintLn" with 
"WriteLn" only). So my script is perfect when run in Delphi, but not in DWS.

Original comment by djjd47...@gmail.com on 3 Nov 2012 at 6:22

GoogleCodeExporter commented 8 years ago
UPDATE: After separating each function to be called separately, things are 
working now. It's something about embedding a function inside a function inside 
a procedure, specifically "PrintLn(RectToStr(Rect(50,40,30,20)));".

Original comment by djjd47...@gmail.com on 3 Nov 2012 at 8:58

GoogleCodeExporter commented 8 years ago
Can anyone confirm this issue? It appears that DWS doesn't accept calling a 
function which returns a record being called inside another function's 
parameter.

Original comment by djjd47...@gmail.com on 8 Nov 2012 at 12:20

GoogleCodeExporter commented 8 years ago
This issue was closed by revision r1615.

Original comment by zar...@gmail.com on 8 Nov 2012 at 10:26