Exemple #1
0
        public static DynValue ToChar(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectIntegerAtIndex(0);

            return(new DynValue(((char)args[0].Number).ToString()));
        }
        public DynValue AttachShellToDevice(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectNone();

            if (!Kernel.Instance.NetworkConnectionStack.TryPeek(out var entity))
            {
                return(new DynValue(SystemReturnCodes.Network.NotLinked));
            }

            if (!(entity is Device device))
            {
                return(new DynValue(SystemReturnCodes.Network.NotADevice));
            }

            if (!Kernel.Instance.CurrentSystemContext.IsLocal &&
                Kernel.Instance.CurrentSystemContext.RemoteDevice == device)
            {
                return(new DynValue(SystemReturnCodes.Network.ShellStillOpen));
            }

            if (!device.ShellEnabled)
            {
                return(new DynValue(SystemReturnCodes.Network.ShellDisabled));
            }

            Kernel.Instance.AttachShell(device);
            return(new DynValue(SystemReturnCodes.Success));
        }
Exemple #3
0
        public static DynValue Length(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            return(new DynValue(args[0].String.Length));
        }
        public DynValue LinkToDevice(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            var addr   = Address.Parse(args[0].String);
            var device = UserProfile.Instance.Internet.GetDevice(addr.ToStringWithoutNode());

            if (device == null)
            {
                return(new DynValue(SystemReturnCodes.Network.NoRouteToHost));
            }

            if (Kernel.Instance.NetworkConnectionStack.TryPeek(out var entity))
            {
                if (entity is Node)
                {
                    return(new DynValue(SystemReturnCodes.Network.BoundToNode));
                }
            }

            if (Kernel.Instance.NetworkConnectionStack.Contains(device))
            {
                return(new DynValue(SystemReturnCodes.Network.AlreadyLinked));
            }


            Kernel.Instance.LinkToDevice(device);
            return(new DynValue(SystemReturnCodes.Success));
        }
        public DynValue PingServiceNode(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectByteAtIndex(0);

            var port = (byte)args[0].Number;

            if (!Kernel.Instance.NetworkConnectionStack.TryPeek(out var entity))
            {
                return(new DynValue(SystemReturnCodes.Network.NotLinked));
            }

            if (!(entity is Device device))
            {
                return(new DynValue(SystemReturnCodes.Network.NotADevice));
            }

            var serviceNode = device.GetServiceNode(port);

            if (serviceNode != null)
            {
                if (serviceNode.IsActive)
                {
                    return(new DynValue(SystemReturnCodes.Network.ServiceNodeActive));
                }

                return(new DynValue(SystemReturnCodes.Network.ServiceNodeInactive));
            }

            return(new DynValue(SystemReturnCodes.Success));
        }
Exemple #6
0
        public DynValue GetAttributes(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            var path = args[0].String;

            if (!File.Exists(path))
            {
                return(new DynValue(-1));
            }

            var attrs = File.Get(path).Attributes;
            var ret   = string.Empty;

            if ((attrs & FileAttributes.Executable) != 0)
            {
                ret += "x";
            }
            else
            {
                ret += "-";
            }

            if ((attrs & FileAttributes.Hidden) != 0)
            {
                ret += "h";
            }
            else
            {
                ret += "-";
            }

            return(new DynValue(ret));
        }
Exemple #7
0
        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}'.");
            }
        }
Exemple #8
0
        public DynValue Move(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(2)
            .ExpectTypeAtIndex(0, DynValueType.String)
            .ExpectTypeAtIndex(1, DynValueType.String);

            var source = args[0].String;
            var target = args[1].String;

            try
            {
                if (Directory.Exists(source))
                {
                    Directory.Move(source, target);
                }
                else if (File.Exists(source))
                {
                    File.Move(source, target);
                }
                else
                {
                    return(new DynValue(-2));
                }

                return(DynValue.Zero);
            }
            catch
            {
                return(new DynValue(-1));
            }
        }
Exemple #9
0
        public DynValue Import(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            var libName = args[0].String;

            var path    = "/lib/" + libName + ".lib";
            var file    = File.Get(path);
            var absPath = file.GetAbsolutePath();

            var process = Kernel.Instance.ProcessManager.GetProcess(interpreter);

            if (process.ImportedLibraryPaths.Contains(absPath))
            {
                interpreter.TerminateWithKernelMessage($"{absPath} WAS ALREADY IMPORTED");
                return(new DynValue(-1));
            }

            try
            {
                var content = file.GetData();
                process.ImportedLibraryPaths.Add(absPath);

                return(interpreter.ExecuteAsync(content, Kernel.Instance.InteractionCancellation.Token).GetAwaiter().GetResult());
            }
            catch
            {
                interpreter.TerminateWithKernelMessage($"FAILED TO IMPORT {absPath}");
                return(new DynValue(-2));
            }
        }
        public DynValue ScanCurrentNeighborhood(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectNone();

            var tbl = new Table();

            var position = Vector2.Zero;

            if (Kernel.Instance.NetworkConnectionStack.TryPeek(out var entity))
            {
                if (entity is Device dev)
                {
                    position = dev.GetNetPositionVector();
                }
                else if (entity is Node node)
                {
                    position = node.Owner.GetNetPositionVector();
                }
            }

            var devices = UserProfile.Instance.Internet.GetDevicesInRange(position, 10);

            for (var i = 0; i < devices.Count; i++)
            {
                tbl[i] = new DynValue(devices[i].Address.Value.ToString());
            }

            return(new DynValue(tbl));
        }
Exemple #11
0
        private DynValue Abs(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.Number);

            return(new DynValue(Math.Abs(args[0].Number)));
        }
Exemple #12
0
        public DynValue SetXY(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(6)
            .ExpectTypeAtIndex(0, DynValueType.String)
            .ExpectIntegerAtIndex(1)
            .ExpectIntegerAtIndex(2)
            .ExpectByteAtIndex(3)
            .ExpectByteAtIndex(4)
            .ExpectByteAtIndex(5);

            var str = args[0].String;
            var x   = args[1].Number;
            var y   = args[2].Number;
            var r   = (byte)args[3].Number;
            var g   = (byte)args[4].Number;
            var b   = (byte)args[5].Number;

            if (x >= Kernel.Instance.Vga.TotalColumns)
            {
                return(new DynValue(-1));
            }

            if (y >= Kernel.Instance.Vga.TotalRows)
            {
                return(new DynValue(-1));
            }

            if (str.Length != 1)
            {
                throw new ClrFunctionException("Expected a single-character string.");
            }

            Kernel.Instance.Vga.PutCharAt(str[0], new Color(r, g, b), Color.Black, (int)x, (int)y);
            return(DynValue.Zero);
        }
Exemple #13
0
        public DynValue SetCursorPosition(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(2)
            .ExpectIntegerAtIndex(0)
            .ExpectIntegerAtIndex(1);

            var x = args[0].Number;
            var y = args[1].Number;

            if (x >= Kernel.Instance.Vga.TotalColumns - Kernel.Instance.Vga.Margins.Right)
            {
                x = Kernel.Instance.Vga.Margins.Right;
            }

            if (y >= Kernel.Instance.Vga.TotalRows - Kernel.Instance.Vga.Margins.Bottom)
            {
                y = Kernel.Instance.Vga.Margins.Bottom;
            }

            if (x < Kernel.Instance.Vga.Margins.Left)
            {
                x = Kernel.Instance.Vga.Margins.Left;
            }

            if (y < Kernel.Instance.Vga.Margins.Top)
            {
                y = Kernel.Instance.Vga.Margins.Top;
            }

            Kernel.Instance.Vga.CursorX = (int)x;
            Kernel.Instance.Vga.CursorY = (int)y;

            return(DynValue.Zero);
        }
Exemple #14
0
        public DynValue Margins(Interpreter interpreter, ClrFunctionArguments args)
        {
            if (args.Count == 0)
            {
                var tbl = new Table();

                tbl["left"]   = new DynValue(Kernel.Instance.Vga.Margins.Left);
                tbl["top"]    = new DynValue(Kernel.Instance.Vga.Margins.Top);
                tbl["right"]  = new DynValue(Kernel.Instance.Vga.Margins.Right);
                tbl["bottom"] = new DynValue(Kernel.Instance.Vga.Margins.Bottom);

                return(new DynValue(tbl));
            }
            else
            {
                args.ExpectExactly(4)
                .ExpectByteAtIndex(0)
                .ExpectByteAtIndex(1)
                .ExpectByteAtIndex(2)
                .ExpectByteAtIndex(3);

                Kernel.Instance.Vga.SetMargins(
                    (byte)args[0].Number,
                    (byte)args[1].Number,
                    (byte)args[2].Number,
                    (byte)args[3].Number
                    );

                return(DynValue.Zero);
            }
        }
Exemple #15
0
        public static DynValue PrintLine(Interpreter interpreter, ClrFunctionArguments args)
        {
            var output = Print(interpreter, args);

            Console.WriteLine();

            return(output);
        }
Exemple #16
0
        public static DynValue Print(Interpreter interpreter, ClrFunctionArguments args)
        {
            var output = string.Join(' ', args.Select(x => x.AsString().String));

            Console.Write(output);

            return(new DynValue(output.Length));
        }
Exemple #17
0
        public DynValue Clear(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectNone();

            Kernel.Instance.Vga.ClearScreen(false);

            return(DynValue.Zero);
        }
Exemple #18
0
        public DynValue ResetTextModeForeground(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectNone();

            Kernel.Instance.Vga.ActiveForegroundColor = Kernel.Instance.Vga.DefaultForegroundColor;

            return(DynValue.Zero);
        }
        public DynValue Stamp(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectNone();

            var stamp = (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds;

            return(new DynValue(stamp));
        }
Exemple #20
0
        public DynValue PrintLine(Interpreter interpreter, ClrFunctionArguments args)
        {
            foreach (var value in args)
            {
                Kernel.Instance.Terminal.WriteLine(value.AsString().String);
            }

            return(DynValue.Zero);
        }
Exemple #21
0
        public static DynValue LowerCase(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            var str = args[0].String;

            return(new DynValue(str.ToLowerInvariant()));
        }
Exemple #22
0
        public DynValue KeyUp(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.Number);

            var keyCode = args[0].Number;

            return(new DynValue(Keyboard.IsKeyUp((KeyCode)keyCode) ? 1 : 0));
        }
Exemple #23
0
        public DynValue LastReturnValue(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectNone();

            if (!Kernel.Instance.ProcessManager.ReturnValues.TryPeek(out var value))
            {
                return(DynValue.Zero);
            }

            return(value);
        }
Exemple #24
0
        public static DynValue ToNumber(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            if (!decimal.TryParse(args[0].String, out var result))
            {
                throw new ClrFunctionException("The number was in an invalid format.");
            }

            return(new DynValue(result));
        }
Exemple #25
0
        public DynValue Wait(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectIntegerAtIndex(0);

            var timeExpires = DateTime.Now + TimeSpan.FromMilliseconds(args[0].Number);

            while (!interpreter.BreakExecution && DateTime.Now < timeExpires)
            {
            }

            return(DynValue.Zero);
        }
Exemple #26
0
        public DynValue SetAttributes(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(2)
            .ExpectTypeAtIndex(0, DynValueType.String)
            .ExpectTypeAtIndex(1, DynValueType.String);

            var path  = args[0].String;
            var attrs = args[1].String.Distinct();

            if (!File.Exists(path))
            {
                return(new DynValue(-1));
            }

            FileAttributes fileAttrs = 0;

            foreach (var c in attrs)
            {
                if (!File.SupportedAttributeDescriptors.Contains(c) && !(c == '-'))
                {
                    return(new DynValue(-2));
                }

                if (c == 'x')
                {
                    fileAttrs |= FileAttributes.Executable;
                    continue;
                }
                else if (c == '-')
                {
                    fileAttrs &= ~(FileAttributes.Executable);
                    continue;
                }

                if (c == 'h')
                {
                    fileAttrs |= FileAttributes.Hidden;
                }
                else if (c == '-')
                {
                    fileAttrs &= ~(FileAttributes.Hidden);
                }
            }

            var file = File.Get(path);

            file.Attributes = fileAttrs;

            return(DynValue.Zero);
        }
Exemple #27
0
        public static DynValue ToCharCode(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            var str = args[0].String;

            if (str.Length != 1)
            {
                throw new ClrFunctionException("Expected a single character.");
            }

            return(new DynValue(str[0]));
        }
Exemple #28
0
        public static DynValue Strace(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectNone();

            var trace = interpreter.Environment.StackTrace();
            var tbl   = new Table();

            for (var i = 0; i < trace.Count; i++)
            {
                tbl[i] = new DynValue($"{trace.Count - i - 1}: {trace[i].FunctionName}");
            }

            return(new DynValue(tbl));
        }
Exemple #29
0
        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));
        }
Exemple #30
0
        public static DynValue IsGlobal(Interpreter interpreter, ClrFunctionArguments args)
        {
            args.ExpectExactly(1)
            .ExpectTypeAtIndex(0, DynValueType.String);

            var name = args[0].String;

            if (interpreter.Environment.GlobalScope.HasMember(name))
            {
                return(new DynValue(1));
            }

            return(new DynValue(0));
        }