[fpc-pascal] TProcess failed running Swift compiler

Mr Bee pak.lebah at yahoo.com
Mon Jan 9 06:51:40 CET 2017


Hi,
I'm writing a simple editor for Swift language. I use TProcess to run the Swift REPL. Unfortunately, TProcess failed to execute the Swift REPL for no obvious reasons. The Swift compiler and REPL are installed just fine and able to execute any Swift codes. My exact same code has no problem to run other executable files, like bash or Python interpreter.
Here's the code:
_____ start code _____
program Execute;
uses  SysUtils, Process;   function ExecCmd(const ExeName: string; const Params: array of string;                  var OutText: string; InText: string = ''): boolean;const  READ_BYTES = 65536;var  proc: TProcess;  i, numBytes, bytesRead,   outputLength, available: integer;begin  proc := TProcess.Create(nil);  proc.Executable := ExeName;  proc.Options := [poUsePipes, poStderrToOutput];
  // set params  if high(Params) >= 0 then    for i := low(Params) to high(Params) do      proc.Parameters.Add(Params[i]);
  // execute  bytesRead := 0;  outputLength := 0;  try    try      proc.Execute;
      // write stdin data      if InText <> '' then begin        InText := InText + LineEnding;        proc.Input.Write(InText[1], Length(InText));      end;
      while proc.Running do      begin        // read stdout size        available := proc.Output.NumBytesAvailable;        if available > 0 then        begin          if (bytesRead + available) > outputLength then          begin            outputLength := bytesRead + READ_BYTES;            SetLength(OutText, outputLength);          end;
          numBytes := proc.Output.Read(OutText[1+bytesRead], available);          if numBytes > 0 then Inc(bytesRead, numBytes);        end        else          Sleep(10);      end;
      // read stdout data      available := proc.Output.NumBytesAvailable;      while available > 0 do      begin        if (bytesRead + available) > outputlength then        begin          outputLength := bytesRead + READ_BYTES;          SetLength(OutText, outputLength);        end;
        numBytes := proc.Output.Read(OutText[1+bytesRead], available);        if numBytes > 0 then Inc(bytesRead, numBytes);        available := proc.Output.NumBytesAvailable;      end;      SetLength(OutText, bytesRead);            Result := true;    except      // failed      on e : Exception do        begin          Result := false;          SetLength(OutText, bytesRead);        end;    end;  finally    proc.Free;  end;end;
var  s: string = '';begin  ExecCmd('/bin/ls',['-l'],s);        // <- OK
  //ExecCmd('swift',['test.swift'],s);  // <- FAILED!  //ExecCmd('python',['test.py'],s);    // <- OK  
  writeln(s);end.
_____ end code _____
The test code called by the REPL is just a simple hello world program, nothing fancy. Can anyone here enlighten me what did I do wrong in the code above?
I'm on FPC v.3.0, Linux Ubuntu 14.04, and Swift v.3.0.
Thank you.
Regards, 
–Mr Bee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20170109/4bb7489e/attachment.html>


More information about the fpc-pascal mailing list