Skip to content

Commit

Permalink
Continued implementing framework functions (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Unknown6656 committed Jul 22, 2020
1 parent 81dea96 commit ac2e92e
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 10 deletions.
12 changes: 6 additions & 6 deletions new/AutoItInterpreter/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

//////////////////////////////////////////////////////////////////////////
// Auto-generated 2020-07-22 17:49:48.481 //
// Auto-generated 2020-07-22 18:43:38.353 //
// ANY CHANGES TO THIS DOCUMENT WILL BE LOST UPON RE-GENERATION //
//////////////////////////////////////////////////////////////////////////

using System.Reflection;
using System;

[assembly: AssemblyVersion("0.6.1209.7324")]
[assembly: AssemblyFileVersion("0.6.1209.7324")]
[assembly: AssemblyInformationalVersion("250dc1fb3b869eee7b10541de20b1dd5a608c56a")]
[assembly: AssemblyVersion("0.6.1210.7324")]
[assembly: AssemblyFileVersion("0.6.1210.7324")]
[assembly: AssemblyInformationalVersion("81dea96d7ef5ab47e64484df7b78269616e64bf7")]
[assembly: AssemblyCompany("Unknown6656")]
[assembly: AssemblyCopyright("Copyright © 2018 - 2020, Unknown6656")]
[assembly: AssemblyProduct("AutoIt3 Interpreter by Unknown6656")]
Expand All @@ -35,11 +35,11 @@ public static class __module__
/// <summary>
/// The interpreter's current version.
/// </summary>
public static Version? InterpreterVersion { get; } = Version.Parse("0.6.1209.7324");
public static Version? InterpreterVersion { get; } = Version.Parse("0.6.1210.7324");
/// <summary>
/// The Git hash associated with the current build.
/// </summary>
public static string GitHash { get; } = "250dc1fb3b869eee7b10541de20b1dd5a608c56a";
public static string GitHash { get; } = "81dea96d7ef5ab47e64484df7b78269616e64bf7";
/// <summary>
/// The URL of this project's Git(Hub) repository.
/// </summary>
Expand Down
126 changes: 126 additions & 0 deletions new/AutoItInterpreter/Extensibility/Plugins.Au3Framework.Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ public sealed class FrameworkFunctions
ProvidedNativeFunction.Create(nameof(ObjGet), 1, 3, ObjGet, Variant.Default, Variant.Default),
ProvidedNativeFunction.Create(nameof(ObjName), 1, 2, ObjName, 1),
ProvidedNativeFunction.Create(nameof(Ping), 1, 2, Ping, 4_000),
ProvidedNativeFunction.Create(nameof(ProcessClose), 1, ProcessClose),
ProvidedNativeFunction.Create(nameof(ProcessExists), 1, ProcessExists),
ProvidedNativeFunction.Create(nameof(ProcessGetStats), 1, 2, ProcessGetStats, Variant.Zero),
ProvidedNativeFunction.Create(nameof(ProcessList), 0, 1, ProcessList, Variant.Default),
ProvidedNativeFunction.Create(nameof(ProcessSetPriority), 2, ProcessSetPriority),
ProvidedNativeFunction.Create(nameof(ProcessWait), 1, 2, ProcessWait, Variant.Zero),
ProvidedNativeFunction.Create(nameof(ProcessWaitClose), 1, 2, ProcessWaitClose, Variant.Zero),
ProvidedNativeFunction.Create(nameof(Random), 0, 3, Random, Variant.Zero, 1, Variant.False),
//ProvidedNativeFunction.Create(nameof(RegDelete), 1, 2, RegDelete),
//ProvidedNativeFunction.Create(nameof(RegEnumKey), , RegEnumKey),
Expand Down Expand Up @@ -1953,6 +1960,125 @@ internal static unsafe FunctionReturnValue Ping(CallFrame frame, Variant[] args)
return FunctionReturnValue.Error(err);
}

private static Process? GetProcess(Variant variant)
{
try
{
return Process.GetProcessById((int)variant);
}
catch
{
return Process.GetProcessesByName(variant.ToString()).OrderByDescending(p => p.Id)?.FirstOrDefault();
}
}

internal static FunctionReturnValue ProcessClose(CallFrame frame, Variant[] args)
{
if (GetProcess(args[0]) is Process proc)
try
{
proc.Kill();

return Variant.True;
}
catch
{
return FunctionReturnValue.Error(3);
}
else
return FunctionReturnValue.Error(4);
}

internal static FunctionReturnValue ProcessExists(CallFrame frame, Variant[] args) => GetProcess(args[0]) is Process proc ? proc.Id : Variant.Zero;

internal static FunctionReturnValue ProcessGetStats(CallFrame frame, Variant[] args)
{
if (GetProcess(args[0]) is Process proc)
return (int)args[1] switch
{
0 => Variant.FromArray(frame.Interpreter, proc.WorkingSet64, proc.PeakWorkingSet64),
// TODO : 1 => Variant.FromArray(frame.Interpreter, ),
_ => FunctionReturnValue.Error(1),
};
else
return FunctionReturnValue.Error(1);
}

internal static FunctionReturnValue ProcessList(CallFrame frame, Variant[] args)
{
Variant[] items = Process.GetProcesses()
.Where(proc => args[0].IsDefault ? true : proc.ProcessName == args[0].ToString())
.ToArray(proc => Variant.FromArray(frame.Interpreter, proc.ProcessName, proc.Id));

return Variant.FromArray(frame.Interpreter, items.Prepend(Variant.FromArray(frame.Interpreter, items.Length)));
}

internal static FunctionReturnValue ProcessSetPriority(CallFrame frame, Variant[] args)
{
if (GetProcess(args[0]) is Process proc)
try
{
proc.PriorityClass = (int)args[1] switch
{
0 => ProcessPriorityClass.Idle,
1 => ProcessPriorityClass.BelowNormal,
2 => ProcessPriorityClass.Normal,
3 => ProcessPriorityClass.AboveNormal,
4 => ProcessPriorityClass.High,
5 => ProcessPriorityClass.RealTime,
_ => (ProcessPriorityClass)(-1)
};

return Variant.True;
}
catch
{
return FunctionReturnValue.Error(2);
}
else
return FunctionReturnValue.Error(1);
}

internal static FunctionReturnValue ProcessWait(CallFrame frame, Variant[] args)
{
string procname = args[0].ToString();
long timeout = (long)args[1] * 1000;
Stopwatch sw = new Stopwatch();

sw.Start();

while (timeout <= 0 || sw.ElapsedMilliseconds < timeout)
{
try
{
if (Process.GetProcessesByName(procname).FirstOrDefault()?.Id is int pid)
return (Variant)pid;
}
catch
{
}

Thread.Sleep(250);
}

return Variant.Zero;
}

internal static FunctionReturnValue ProcessWaitClose(CallFrame frame, Variant[] args)
{
if (GetProcess(args[0]) is Process proc)
{
if ((int)args[1] <= 0)
proc.WaitForExit();
else
proc.WaitForExit((int)args[0]);

return (Variant)proc.HasExited;
}
else
return Variant.False;
}

internal static unsafe FunctionReturnValue Random(CallFrame frame, Variant[] args)
{
decimal min = (decimal)args[0];
Expand Down
31 changes: 29 additions & 2 deletions new/AutoItInterpreter/Runtime/ScriptScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ namespace Unknown6656.AutoIt3.Runtime
{
using static AST;

/// <summary>
/// Represents an AutoIt3 script scanning module.
/// </summary>
public sealed class ScriptScanner
{
private const RegexOptions _REGEX_OPTIONS = RegexOptions.IgnoreCase | RegexOptions.Compiled;
Expand Down Expand Up @@ -385,6 +388,9 @@ private static (FileInfo physical, string content)? ResolveUNC(string path)
private static (FileInfo physical, string content)? ResolveSSH(string path) => (new FileInfo(path), From.SSH(path).To.String());
}

/// <summary>
/// Represents a scanned AutoIt3 script.
/// </summary>
public sealed class ScannedScript
: IEquatable<ScannedScript>
{
Expand Down Expand Up @@ -422,9 +428,15 @@ internal T AddFunction<T>(T function) where T : ScriptFunction
return function;
}

internal void AddStartupFunction(string name, SourceLocation decl) => _startup.Add((name.ToUpperInvariant(), decl));
public void AddStartupFunction(string name, SourceLocation decl) => AddFunction(name, decl, in _startup);

internal void AddExitFunction(string name, SourceLocation decl) => _exit.Add((name.ToUpperInvariant(), decl));
public void AddExitFunction(string name, SourceLocation decl) => AddFunction(name, decl, in _startup);

private void AddFunction(string name, SourceLocation decl, in List<(string, SourceLocation)> list)
{
if (!list.Any(t => string.Equals(t.Item1, name, StringComparison.InvariantCultureIgnoreCase)))
list.Add((name.ToUpperInvariant(), decl));
}

public InterpreterError? LoadScript(CallFrame frame) => HandleLoading(frame, false);

Expand All @@ -451,10 +463,13 @@ internal T AddFunction<T>(T function) where T : ScriptFunction
return result;
}

/// <inheritdoc/>
public override int GetHashCode() => Path.GetFullPath(Location.FullName).GetHashCode(StringComparison.CurrentCulture);

/// <inheritdoc/>
public override bool Equals(object? obj) => Equals(obj as ScannedScript);

/// <inheritdoc/>
public override string ToString() => Location.ToString();

public bool Equals(ScannedScript? other) => other is ScannedScript script && GetHashCode() == script.GetHashCode();
Expand All @@ -467,6 +482,9 @@ internal T AddFunction<T>(T function) where T : ScriptFunction
public static bool operator !=(ScannedScript? s1, ScannedScript? s2) => !(s1 == s2);
}

/// <summary>
/// Represents an abstract script function. This could be an AutoIt3 or a native function.
/// </summary>
public abstract class ScriptFunction
: IEquatable<ScriptFunction>
{
Expand Down Expand Up @@ -498,12 +516,16 @@ internal ScriptFunction(ScannedScript script, string name)
Script.AddFunction(this);
}

/// <inheritdoc/>
public override int GetHashCode() => HashCode.Combine(Name.ToUpperInvariant(), Script);

/// <inheritdoc/>
public override bool Equals(object? obj) => Equals(obj as ScriptFunction);

/// <inheritdoc/>
public bool Equals(ScriptFunction? other) => other is ScriptFunction f && f.GetHashCode() == GetHashCode();

/// <inheritdoc/>
public override string ToString() => $"[{Script}] Func {Name}";


Expand All @@ -529,10 +551,13 @@ internal JumpLabel(AU3Function function, SourceLocation location, string name)
Name = name;
}

/// <inheritdoc/>
public override bool Equals(object? obj) => Equals(obj as JumpLabel);

/// <inheritdoc/>
public bool Equals(JumpLabel? other) => other != null && EqualityComparer<AU3Function>.Default.Equals(Function, other.Function) && Name == other.Name;

/// <inheritdoc/>
public override int GetHashCode() => HashCode.Combine(Function, Name);

public static bool operator ==(JumpLabel? left, JumpLabel? right) => EqualityComparer<JumpLabel>.Default.Equals(left, right);
Expand Down Expand Up @@ -599,6 +624,7 @@ public JumpLabel AddJumpLabel(SourceLocation location, string name)
return l;
});

/// <inheritdoc/>
public override string ToString() => $"{base.ToString()}({string.Join<PARAMETER_DECLARATION>(", ", Parameters)}) [{LineCount} Lines]";
}

Expand All @@ -621,6 +647,7 @@ internal NativeFunction(ScannedScript script, string name, (int min, int max) pa

public FunctionReturnValue Execute(NativeCallFrame frame, Variant[] args) => _execute(frame, args);

/// <inheritdoc/>
public override string ToString() => "[native] " + base.ToString();
}

Expand Down
4 changes: 2 additions & 2 deletions new/AutoItInterpreter/version.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
0.6.1209.7324
250dc1fb3b869eee7b10541de20b1dd5a608c56a
0.6.1210.7324
81dea96d7ef5ab47e64484df7b78269616e64bf7

0 comments on commit ac2e92e

Please sign in to comment.