TProcess failed running Swift compiler

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

TProcess failed running Swift compiler

Mr Bee
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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Bls: TProcess failed running Swift compiler

Mr Bee
I found the answer myself. It seems that I need to supply the full path to the swift REPL executable. I don't understand why it requires it for swift because it could call python executable just fine without using full path. Anyway, problem solved. :)
 

–Mr Bee



Pada Senin, 9 Januari 2017 12:51, Mr Bee <[hidden email]> menulis:


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




_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Bls: TProcess failed running Swift compiler

el_es
Hi Mr Bee,

On 09/01/17 13:34, Mr Bee wrote:
> I found the answer myself. It seems that I need to supply the full
> path to the swift REPL executable. I don't understand why it requires
> it for swift because it could call python executable just fine
> without using full path. Anyway, problem solved. :)
>
>
> –Mr Bee
>

Can you verify what your $PATH environment variable holds for either interpreter ? ;)
(just a thought)

-L.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal