public DynValue Directories(Interpreter interpreter, ClrFunctionArguments args) { args.ExpectAtLeast(1) .ExpectAtMost(2) .ExpectTypeAtIndex(0, DynValueType.String); var abs = false; if (args.Count == 2) { args.ExpectTypeAtIndex(1, DynValueType.Number); abs = args[1].Number != 0; } var path = args[0].String; try { var table = new Table(); var dirs = Directory.GetDirectories(path, abs); for (var i = 0; i < dirs.Count; i++) { table[i] = new DynValue(dirs[i]); } return(new DynValue(table)); } catch { throw new ClrFunctionException($"Couldn't retrieve directory list for path '{path}'."); } }
public DynValue SpawnProcess(Interpreter interpreter, ClrFunctionArguments args) { args.ExpectAtLeast(1) .ExpectTypeAtIndex(0, DynValueType.String); var path = args[0].String; if (!File.Exists(path, true)) { return(new DynValue(SystemReturnCodes.FileSystem.FileDoesNotExist)); } var file = File.Get(path, true); if ((file.Attributes & FileAttributes.Executable) == 0) { return(new DynValue(SystemReturnCodes.FileSystem.AccessDenied)); } var processArgs = new List <string>(); if (args.Count > 1) { for (var i = 1; i < args.Count; i++) { args.ExpectTypeAtIndex(i, DynValueType.String); processArgs.Add(args[i].String); } } var pid = Kernel.Instance.ProcessManager.ExecuteProgram( file.GetData(), path, Kernel.Instance.ProcessManager.GetProcess(interpreter).Pid, Kernel.Instance.InteractionCancellation.Token, processArgs.ToArray() ); if (pid < 0) { return(new DynValue(SystemReturnCodes.Kernel.ProcessSpaceExhausted)); } var tbl = new Table(); tbl["pid"] = new DynValue(pid); tbl["path"] = new DynValue(path); return(new DynValue(tbl)); }
public static DynValue NumberToString(Interpreter interpreter, ClrFunctionArguments args) { args.ExpectAtLeast(1) .ExpectIntegerAtIndex(0); var number = (int)args[0].Number; var toBase = 10; if (args.Count == 2) { args.ExpectIntegerAtIndex(1); toBase = (int)args[1].Number; } return(new DynValue(Convert.ToString(number, toBase))); }
public static DynValue Substring(Interpreter interpreter, ClrFunctionArguments args) { args.ExpectAtLeast(2) .ExpectAtMost(3); if (args.Count == 2) { args.ExpectTypeAtIndex(0, DynValueType.String) .ExpectIntegerAtIndex(1); var str = args[0].String; var startIndex = (int)args[1].Number; if (startIndex < 0 || startIndex >= str.Length) { throw new ClrFunctionException("The starting index is outside the provided string."); } return(new DynValue(str.Substring(startIndex))); } else { args.ExpectTypeAtIndex(0, DynValueType.String) .ExpectIntegerAtIndex(1) .ExpectIntegerAtIndex(2); var str = args[0].String; var startIndex = (int)args[1].Number; if (startIndex < 0 || startIndex >= str.Length) { throw new ClrFunctionException("The starting index is outside the provided string."); } var length = (int)args[2].Number; if (length < 0 || length + startIndex > str.Length) { throw new ClrFunctionException("The provided length of substring exceeds the base string bounds."); } return(new DynValue(str.Substring(startIndex, length))); } }
private DynValue Rnd(Interpreter interpreter, ClrFunctionArguments args) { args.ExpectAtLeast(1) .ExpectAtMost(2) .ExpectTypeAtIndex(0, DynValueType.Number); var max = args[0].Number; if (args.Count == 2) { args.ExpectTypeAtIndex(1, DynValueType.Number); var min = args[1].Number; return(new DynValue(Random.Next((int)min, (int)max))); } else { return(new DynValue(Random.Next((int)max))); } }
public DynValue Remove(Interpreter interpreter, ClrFunctionArguments args) { args.ExpectAtLeast(1) .ExpectTypeAtIndex(0, DynValueType.String); var forceRemoveDirectory = false; if (args.Count == 2) { args.ExpectTypeAtIndex(1, DynValueType.Number); if (args[1].Number != 0) { forceRemoveDirectory = true; } } var path = args[0].String; if (Directory.Exists(path)) { var dir = Directory.GetDirectory(path); if (dir.Children.Count > 0) { if (!forceRemoveDirectory) { return(new DynValue(-1)); } } Directory.RemoveDirectory(path); return(DynValue.Zero); } else if (File.Exists(path)) { File.Remove(path); return(DynValue.Zero); } return(new DynValue(-2)); }