Skip to content

Commit

Permalink
std: Add 'getError'
Browse files Browse the repository at this point in the history
  • Loading branch information
PaddiM8 committed Dec 22, 2024
1 parent 4e59eee commit d85535b
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 24 deletions.
21 changes: 21 additions & 0 deletions cli/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,27 @@
"Wcwidth": "[1.0.0, )"
}
}
},
"net9.0/linux-arm64": {
"Microsoft.DotNet.ILCompiler": {
"type": "Direct",
"requested": "[9.0.0, )",
"resolved": "9.0.0",
"contentHash": "bbnlV2PbUmCQ8Ndpx0kJaicLyV28IU+4IzyctQLL57+DxrHurYr2qsJrC8+yD44Q0DyPfv2oM168c1Tk6Bxbmg==",
"dependencies": {
"runtime.linux-arm64.Microsoft.DotNet.ILCompiler": "9.0.0"
}
},
"runtime.linux-arm64.Microsoft.DotNet.ILCompiler": {
"type": "Transitive",
"resolved": "9.0.0",
"contentHash": "7RVXw+bLJK+7wMeAXUQoAWj/V9ucm5ry3dop2DHjA/7w/mzP96KEjy7y1eM4WxhHU4xJPZftGHY6Ude3Jh+kOg=="
},
"SQLitePCLRaw.lib.e_sqlite3": {
"type": "Transitive",
"resolved": "2.1.10",
"contentHash": "mAr69tDbnf3QJpRy2nJz8Qdpebdil00fvycyByR58Cn9eARvR+UiG2Vzsp+4q1tV3ikwiYIjlXCQFc12GfebbA=="
}
}
}
}
3 changes: 2 additions & 1 deletion generators/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"resolved": "6.0.0",
"contentHash": "/iUeP3tq1S0XdNNoMz5C9twLSrM/TH+qElHkXWaPvuNOt+99G75NrV0OS2EqHx5wMN7popYjpc8oTjC1y16DLg=="
}
}
},
"net9.0/linux-arm64": {}
}
}
3 changes: 2 additions & 1 deletion language-server/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@
"Wcwidth": "[1.0.0, )"
}
}
}
},
"net9.0/linux-arm64": {}
}
}
3 changes: 2 additions & 1 deletion readline/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"resolved": "1.0.0",
"contentHash": "aekut5BeF4c1Jr2qab3aXcttcgPEfPb2ok2L8911EIzn3RnDBYnUWgx/1OVlqrSXCdwAkBpNgYWXb6b5t4cBng=="
}
}
},
"net9.0/linux-arm64": {}
}
}
12 changes: 2 additions & 10 deletions src/Std/Environment/Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Elk.Std.Attributes;
using Elk.Std.DataTypes;
using Elk.Vm;
using Microsoft.VisualBasic.FileIO;

#endregion

Expand Down Expand Up @@ -127,15 +128,6 @@ public static RuntimeObject ExitCode()
: new RuntimeInteger(int.Parse(exitCode));
}

/// <param name="pattern">The glob pattern to expand</param>
/// <returns>Expands a glob string, eg. "**/*.elk"</returns>
[ElkFunction("expand")]
public static RuntimeGenerator Expand(RuntimeString pattern)
=> new(
Globbing.Glob(ShellEnvironment.WorkingDirectory, pattern.Value)
.Select(x => new RuntimeString(x))
);

/// <param name="index">The index of the argument to get</param>
/// <returns>The list of command line arguments passed to the program, or a specific one if an index was specified</returns>
[ElkFunction("getArgv", Reachability.Everywhere)]
Expand Down Expand Up @@ -343,4 +335,4 @@ public static List<string> GetDirectoryNames(string path)

return directoryNames;
}
}
}
26 changes: 23 additions & 3 deletions src/Std/Pipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static RuntimePipe DisposeAll(RuntimePipe pipe)
{
pipe.EnableDisposeOutput();
pipe.EnableDisposeError();
pipe.AllowNonZeroExit();
pipe.Start();

return pipe;
Expand All @@ -78,15 +79,34 @@ public static RuntimePipe DisposeErr(RuntimePipe pipe)
return pipe;
}

/// <summary>
/// Configures the given pipe to only redirect stderr.
/// </summary>
/// <returns>The given pipe.</returns>
[ElkFunction("getErr", Reachability.Everywhere, StartsPipeManually = true)]
public static RuntimePipe Err(RuntimePipe pipe)
{
pipe.EnableDisposeOutput();
pipe.AllowNonZeroExit();
pipe.Start();

return pipe;
}

/// <returns>
/// The exit code of the process belonging to the given pipe, or nil if it has not terminated yet.
/// Note: Only works on pipes that run in the background (eg. where the `background` function has been used).
/// </returns>
[ElkFunction("exitCode")]
[ElkFunction("exitCode", Reachability.Everywhere, StartsPipeManually = true)]
public static RuntimeObject ExitCode(RuntimePipe pipe)
=> pipe.ExitCode == null
{
pipe.AllowNonZeroExit();
pipe.Start();

return pipe.ExitCode == null
? RuntimeNil.Value
: new RuntimeInteger(pipe.ExitCode.Value);
}

/// <summary>
/// Waits for a pipe that was started in the background.
Expand All @@ -106,4 +126,4 @@ public static void WaitAll(RuntimeList pipes)
foreach (var pipe in pipes)
pipe.As<RuntimePipe>().Wait();
}
}
}
16 changes: 9 additions & 7 deletions src/Vm/ProcessContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ public void StartWithRedirect()
if (_disposeError)
_process!.StartInfo.RedirectStandardError = true;

_allowNonZeroExit = _process!.StartInfo.RedirectStandardError;
_process.EnableRaisingEvents = true;
_allowNonZeroExit = _allowNonZeroExit || _process!.StartInfo.RedirectStandardError;
_process!.EnableRaisingEvents = true;

try
{
Expand Down Expand Up @@ -180,7 +180,11 @@ private void Read(RuntimeObject value)
while (runtimePipe.StreamEnumerator.MoveNext())
streamWriter.WriteLine(runtimePipe.StreamEnumerator.Current);
}
else if (value is RuntimeList runtimeList)
else if (value is RuntimeString)
{
streamWriter.Write(value);
}
else if (value is IEnumerable<RuntimeObject> runtimeList)
{
foreach (var item in runtimeList)
streamWriter.WriteLine(item);
Expand Down Expand Up @@ -210,9 +214,6 @@ private void CloseProcess(bool messageOnError)

_process.WaitForExit();

//if (_process == null)
//return;

ExitCode = _process.ExitCode;
_process.Dispose();
_process = null;
Expand All @@ -225,6 +226,7 @@ private void CloseProcess(bool messageOnError)
RuntimeObject message = messageOnError
? new RuntimeString("Program returned a non-zero exit code.")
: RuntimeNil.Value;

// TODO: Somehow get the actual signal rather than relying on exit codes
if (ExitCode >= 128 && ExitCode <= 128 + SignalHelper.SignalNames.Length - 1)
{
Expand All @@ -236,4 +238,4 @@ private void CloseProcess(bool messageOnError)
throw new RuntimeUserException(new RuntimeError(message));
}
}
}
}
3 changes: 2 additions & 1 deletion src/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"Wcwidth": "[1.0.0, )"
}
}
}
},
"net9.0/linux-arm64": {}
}
}

0 comments on commit d85535b

Please sign in to comment.