[fpc-pascal] run pascal programs as scripts

Michael Van Canneyt michael at freepascal.org
Sun Mar 27 23:59:11 CEST 2011



On Sun, 27 Mar 2011, Mattias Gaertner wrote:

> On Sun, 27 Mar 2011 21:16:58 +0200 (CEST)
> Michael Van Canneyt <michael at freepascal.org> wrote:
>
>>
>>
>> On Sun, 27 Mar 2011, Mattias Gaertner wrote:
>>
>>> On Sun, 27 Mar 2011 19:05:12 +0200 (CEST)
>>> Michael Van Canneyt <michael at freepascal.org> wrote:
>>>
>>>>
>>>>
>>>> On Sun, 27 Mar 2011, Mattias Gaertner wrote:
>>>>
>>>>> I wrote a little tool:
>>>>>
>>>>> http://wiki.lazarus.freepascal.org/InstantFPC
>>>>
>>>> Nice job. OK to include in FPC as one of the utils ?
>>>
>>> Yes.
>>
>> Thank you. But please, fix a couple of bugs first:
>>
>> 1. I get a stream read error each time I execute a script the first time (i.e. it's not yet in the cache) :
>> home: >./helloworld.pas
>> An unhandled exception occurred at $0000000000425556 :
>> EReadError : Stream read error
>>    $0000000000425556
>>    $00000000004256FA
>>    $0000000000469E8A
>>    $0000000000400A25
>
> It works here.

I assume you use FPC 2.4.x. In FPC 2.5.1 your code

   repeat
     Count:=ss.CopyFrom(Proc.Output,4096);
   until Count=0;

Fails.

This is because CopyFrom assumes that at least 4096 bytes are present. 
If it doesn't get 4096 bytes, an exception is thrown.

(see recent change in trunk:
r16992 | sergei | 2011-02-24 04:25:40 +0100 (Thu, 24 Feb 2011) | 5 lines

* Rework TStream.CopyFrom (see Mantis #17980):
a) Use significantly larger buffer (128k instead of 1k)
b) When Count=0, do not try to determine the source size. Just copy until the source can be read. This should improve performance with limited seek capability sources (e.g. a decompression stream will be decompressed once rather than twice).
c) Use ReadBuffer/WriteBuffer, so an exception is raised when something goes wrong. This conforms to Delphi behavior.
)

My FPC outputs nothing, so CopyFrom does not get 4096 bytes and therefor 
raises an exception:

Breakpoint 1, 0x000000000041396c in fpc_raiseexception ()
(gdb) bt
#0  0x000000000041396c in fpc_raiseexception ()
#1  0x0000000000425565 in CLASSES_TSTREAM_$__READBUFFER$formal$LONGINT ()
#2  0x00000000004256fa in CLASSES_TSTREAM_$__COPYFROM$TSTREAM$INT64$$INT64 ()
#3  0x0000000000469e8a in INSTANTFPTOOLS_COMPILE$ANSISTRING$ANSISTRING ()
#4  0x0000000000400a25 in main ()

since the compilation is already done, the binary is in the cache, and the second run works.

Changing the code to

   buf : Array[1..4096] of byte;

begin
   // snip
   ss:=TStringStream.Create('');
   repeat
     Count:=Proc.Output.Read(Buf,4096);
     if Count>0 then
       ss.write(buf,count);
   until Count=0;

it works always. Will you change it, or do I change it once it is in FPC ?

This change may affect other things in Lazarus. 
Sergei (hope you are reading this), I think you should put this in the user_changes wiki page.

Michael.



More information about the fpc-pascal mailing list