public void TestSimpleGeneric()
        {
            BoundType type = new BoundType("module", "SimpleGeneric", "", typeof(SimpleGeneric <>),
                                           BuiltinType.NoOperators,
                                           BuiltinType.NoOperators,
                                           Enumerable.Empty <(string name, IMethodInvokeFactory invoker)>(),
                                           Enumerable.Empty <(string name, IFieldAccessFactory access)>()
                                           );
            Context       context       = new Context(KontrolRegistry.CreateCore());
            ModuleContext moduleContext = context.CreateModuleContext("Test");

            Assert.Equal("SimpleGeneric", type.LocalName);
            Assert.Equal("module::SimpleGeneric<T>", type.Name);
            Assert.Equal(new[] { "T" }, type.GenericParameters);
            Assert.False(type.IsValid(moduleContext));
            Assert.Equal(typeof(SimpleGeneric <>), type.GeneratedType(moduleContext));

            RealizedType filledType = type.FillGenerics(moduleContext, new Dictionary <string, RealizedType> {
                { "T", BuiltinType.Int }
            });

            Assert.Equal("SimpleGeneric", filledType.LocalName);
            Assert.Equal("module::SimpleGeneric<int>", filledType.Name);
            Assert.True(filledType.IsValid(moduleContext));
            Assert.Empty(filledType.GenericParameters);
            Assert.Equal(typeof(SimpleGeneric <long>), filledType.GeneratedType(moduleContext));

            RealizedType aliased = type.FillGenerics(moduleContext, new Dictionary <string, RealizedType> {
                { "T", new GenericParameter("U") }
            });

            Assert.Equal("SimpleGeneric", aliased.LocalName);
            Assert.Equal("module::SimpleGeneric<U>", aliased.Name);
            Assert.False(aliased.IsValid(moduleContext));
            Assert.Equal(new[] { "U" }, aliased.GenericParameters);
            Assert.Equal(typeof(SimpleGeneric <>), aliased.GeneratedType(moduleContext));

            RealizedType filledType2 = aliased.FillGenerics(moduleContext, new Dictionary <string, RealizedType> {
                { "U", BuiltinType.String }
            });

            Assert.Equal("SimpleGeneric", filledType2.LocalName);
            Assert.Equal("module::SimpleGeneric<string>", filledType2.Name);
            Assert.True(filledType2.IsValid(moduleContext));
            Assert.Empty(filledType2.GenericParameters);
            Assert.Equal(typeof(SimpleGeneric <string>), filledType2.GeneratedType(moduleContext));

            Assert.Equal(new Dictionary <string, RealizedType> {
                { "T", BuiltinType.Int }
            }, type.InferGenericArgument(moduleContext, filledType).ToDictionary(t => t.name, t => t.type));
            Assert.Equal(new Dictionary <string, RealizedType> {
                { "U", BuiltinType.Int }
            }, aliased.InferGenericArgument(moduleContext, filledType).ToDictionary(t => t.name, t => t.type));
            Assert.Equal(new Dictionary <string, RealizedType> {
                { "T", BuiltinType.String }
            }, type.InferGenericArgument(moduleContext, filledType2).ToDictionary(t => t.name, t => t.type));
            Assert.Equal(new Dictionary <string, RealizedType> {
                { "U", BuiltinType.String }
            }, aliased.InferGenericArgument(moduleContext, filledType2).ToDictionary(t => t.name, t => t.type));
        }
Exemple #2
0
        public void MakeLongTuple()
        {
            Context       context       = new Context(KontrolRegistry.CreateCore());
            ModuleContext moduleContext = context.CreateModuleContext("Test");

            Assert.Equal(typeof(ValueTuple <double, long, string, double, bool, string, long, ValueTuple <string> >),
                         new TupleType(new List <TO2Type> {
                BuiltinType.Float, BuiltinType.Int, BuiltinType.String, BuiltinType.Float, BuiltinType.Bool,
                BuiltinType.String, BuiltinType.Int, BuiltinType.String
            }).GeneratedType(moduleContext));
            Assert.Equal(
                typeof(ValueTuple <double, long, string, double, bool, string, long,
                                   ValueTuple <string, bool, long, double, bool, long, double> >),
                new TupleType(new List <TO2Type> {
                BuiltinType.Float, BuiltinType.Int, BuiltinType.String, BuiltinType.Float, BuiltinType.Bool,
                BuiltinType.String, BuiltinType.Int,
                BuiltinType.String, BuiltinType.Bool, BuiltinType.Int, BuiltinType.Float, BuiltinType.Bool,
                BuiltinType.Int, BuiltinType.Float
            }).GeneratedType(moduleContext));
            Assert.Equal(
                typeof(ValueTuple <double, long, string, double, bool, string, long, ValueTuple <string, bool, long,
                                                                                                 double, bool, long, double, ValueTuple <string, bool> > >),
                new TupleType(new List <TO2Type> {
                BuiltinType.Float, BuiltinType.Int, BuiltinType.String, BuiltinType.Float, BuiltinType.Bool,
                BuiltinType.String, BuiltinType.Int,
                BuiltinType.String, BuiltinType.Bool, BuiltinType.Int, BuiltinType.Float, BuiltinType.Bool,
                BuiltinType.Int, BuiltinType.Float,
                BuiltinType.String, BuiltinType.Bool
            }).GeneratedType(moduleContext));
        }
Exemple #3
0
        public void TestMakeAction()
        {
            Context       context       = new Context(KontrolRegistry.CreateCore());
            ModuleContext moduleContext = context.CreateModuleContext("Test");

            Assert.Equal(typeof(Func <object>),
                         new FunctionType(false, new List <TO2Type>(), BuiltinType.Unit).GeneratedType(moduleContext));
            Assert.Equal(typeof(Func <long, object>),
                         new FunctionType(false, new List <TO2Type> {
                BuiltinType.Int
            }, BuiltinType.Unit).GeneratedType(
                             moduleContext));
            Assert.Equal(typeof(Func <long, string, object>),
                         new FunctionType(false, new List <TO2Type> {
                BuiltinType.Int, BuiltinType.String
            }, BuiltinType.Unit)
                         .GeneratedType(moduleContext));
            Assert.Equal(typeof(Func <long, string, bool, object>),
                         new FunctionType(false, new List <TO2Type> {
                BuiltinType.Int, BuiltinType.String, BuiltinType.Bool
            },
                                          BuiltinType.Unit).GeneratedType(moduleContext));
            Assert.Equal(typeof(Func <long, string, bool, double, object>),
                         new FunctionType(false,
                                          new List <TO2Type> {
                BuiltinType.Int, BuiltinType.String, BuiltinType.Bool, BuiltinType.Float
            },
                                          BuiltinType.Unit).GeneratedType(moduleContext));
        }
Exemple #4
0
        public static KontrolRegistry CreateKSP()
        {
            KontrolRegistry registry = KontrolRegistry.CreateCore();

            registry.RegisterModule(KSPMathModule.Instance.module);

            KSPVessel.KSPVesselModule.DirectBindings();
            registry.RegisterModule(
                BindingGenerator.BindModule(typeof(KSPConsole.KSPConsoleModule)));
            registry.RegisterModule(
                BindingGenerator.BindModule(typeof(KSPGame.KSPGameModule)));
            registry.RegisterModule(
                BindingGenerator.BindModule(typeof(KSPGame.KSPGameWarpModule)));
            registry.RegisterModule(
                BindingGenerator.BindModule(typeof(KSPOrbit.KSPOrbitModule)));
            registry.RegisterModule(
                BindingGenerator.BindModule(typeof(KSPControl.KSPControlModule)));
            registry.RegisterModule(
                BindingGenerator.BindModule(typeof(KSPVessel.KSPVesselModule)));
            registry.RegisterModule(BindingGenerator.BindModule(typeof(KSPUI.KSPUIModule)));
            registry.RegisterModule(
                BindingGenerator.BindModule(typeof(KSPDebug.KSPDebugModule)));
            registry.RegisterModule(BindingGenerator.BindModule(typeof(KSPResource.KSPResourceModule)));
            registry.RegisterModule(BindingGenerator.BindModule(typeof(KSPAddons.KSPAddonsModule)));
            registry.RegisterModule(BindingGenerator.BindModule(typeof(Testing.KSPTesting)));

            return(registry);
        }
 public static void RunTests(KontrolRegistry registry, ITestReporter reporter,
                             TestContextFactory contextFactory)
 {
     foreach (IKontrolModule module in registry.modules.Values)
     {
         RunTests(module, reporter, contextFactory);
     }
 }
Exemple #6
0
 public static void GenerateDocs(ModuleContext moduleContext, KontrolRegistry registry)
 {
     foreach (IKontrolModule module in registry.modules.Values)
     {
         if (IsModuleEmpty(module))
         {
             continue;
         }
         using (StreamWriter fs = File.CreateText(Path.Combine(Directory.GetCurrentDirectory(), "docsSrc",
                                                               "content", "reference", module.Name.Replace("::", "_") + ".md"))) {
             GenerateDocs(moduleContext, module, fs);
             Console.Out.WriteLine($"Generated: {module.Name}");
         }
     }
 }
Exemple #7
0
        static MethodInfo GenerateMethod(Expression expression, TO2Type returnType)
        {
            Context       context       = new Context(KontrolRegistry.CreateCore());
            ModuleContext moduleContext = context.CreateModuleContext("DynamicExpression");
            IBlockContext methodContext = moduleContext.CreateMethodContext(FunctionModifier.Public, false, "Exec",
                                                                            returnType, Enumerable.Empty <FunctionParameter>());

            expression.EmitCode(methodContext, false);
            Assert.False(methodContext.HasErrors);
            methodContext.IL.EmitReturn(methodContext.MethodBuilder.ReturnType);

            Type dynamicType = moduleContext.CreateType();

            return(dynamicType.GetMethod("Exec"));
        }
Exemple #8
0
        public Context(KontrolRegistry registry)
        {
            string       id           = Guid.NewGuid().ToString("N");
            AppDomain    appDomain    = AppDomain.CurrentDomain;
            AssemblyName assemblyName = new AssemblyName("KontrolTO2Generated" + id);

            this.registry = registry;
#if DEBUG
            assemblyBuilder = appDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
            moduleBuilder   =
                assemblyBuilder.DefineDynamicModule("KontrolTO2.Generated" + id, "KontrolTO2" + id + ".dll");
#else
            assemblyBuilder = appDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndCollect);
            moduleBuilder   = assemblyBuilder.DefineDynamicModule("KontrolTO2.Generated" + id);
#endif
        }
        public void TestNonGeneric()
        {
            BoundType type = new BoundType("module", "NonGeneric", "", typeof(NonGeneric),
                                           BuiltinType.NoOperators,
                                           BuiltinType.NoOperators,
                                           Enumerable.Empty <(string name, IMethodInvokeFactory invoker)>(),
                                           Enumerable.Empty <(string name, IFieldAccessFactory access)>()
                                           );
            Context       context       = new Context(KontrolRegistry.CreateCore());
            ModuleContext moduleContext = context.CreateModuleContext("Test");

            Assert.Equal("NonGeneric", type.LocalName);
            Assert.Equal("module::NonGeneric", type.Name);
            Assert.True(type.IsValid(moduleContext));
            Assert.Empty(type.GenericParameters);

            Assert.Equal(typeof(NonGeneric), type.GeneratedType(moduleContext));
        }
Exemple #10
0
        private async void DoReboot(KontrolSystemConfig config)
        {
            rebooting = true;
            State nextState = await Task.Run(() => {
                Stopwatch stopwatch = new Stopwatch();
                try {
                    stopwatch.Start();
                    string registryPath = Path.GetFullPath(config.TO2BaseDir).TrimEnd(PathSeparator);

                    PluginLogger.Instance.Info($"Rebooting Mainframe on path {registryPath}");

                    Directory.CreateDirectory(registryPath);

                    KontrolRegistry nextRegistry = KontrolSystemKSPRegistry.CreateKSP();

                    if (KontrolSystemConfig.Instance.IncludeStdLib)
                    {
                        PluginLogger.Instance.Debug($"Add Directory: {KontrolSystemConfig.Instance.StdLibDir}");
                        nextRegistry.AddDirectory(KontrolSystemConfig.Instance.StdLibDir);
                    }

                    PluginLogger.Instance.Debug($"Add Directory: {registryPath}");
                    nextRegistry.AddDirectory(registryPath);
                    stopwatch.Stop();

                    return(new State(nextRegistry, stopwatch.Elapsed, new List <MainframeError>()));
                } catch (CompilationErrorException e) {
                    PluginLogger.Instance.Debug(e.ToString());

                    foreach (StructuralError error in e.errors)
                    {
                        PluginLogger.Instance.Info(error.ToString());
                    }

                    return(new State(state?.registry, stopwatch.Elapsed, e.errors.Select(error => new MainframeError(
                                                                                             error.start,
                                                                                             error.errorType.ToString(),
                                                                                             error.message
                                                                                             )).ToList()));
                } catch (ParseException e) {
                    PluginLogger.Instance.Debug(e.ToString());
                    PluginLogger.Instance.Info(e.Message);

                    return(new State(state?.registry, stopwatch.Elapsed, new List <MainframeError> {
                        new MainframeError(e.position, "Parsing", e.Message)
                    }));
                } catch (Exception e) {
                    PluginLogger.Instance.Error("Mainframe initialization error: " + e);

                    return(new State(state?.registry, stopwatch.Elapsed, new List <MainframeError> {
                        new MainframeError(new Position(), "Unknown error", e.Message)
                    }));
                }
            });

            if (nextState.errors.Count == 0)
            {
                processes = (processes ?? Enumerable.Empty <KontrolSystemProcess>())
                            .Where(p => p.State == KontrolSystemProcessState.Running ||
                                   p.State == KontrolSystemProcessState.Outdated).Select(p => p.MarkOutdated()).Concat(
                    nextState.registry.modules.Values
                    .Where(module =>
                           module.HasKSCEntrypoint() || module.HasEditorEntrypoint() ||
                           module.HasTrackingEntrypoint() ||
                           module.HasFlightEntrypoint())
                    .Select(module => new KontrolSystemProcess(module))).ToList();
            }

            state     = nextState;
            rebooting = false;
        }
Exemple #11
0
 internal State(KontrolRegistry registry, TimeSpan bootTime, List <MainframeError> errors)
 {
     this.registry = registry;
     this.bootTime = bootTime;
     this.errors   = errors;
 }