Exemple #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            NatashaController.Builder = (ApplicationBuilder)app;
            DomainManagement.RegisterDefault <AssemblyDomain>();
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseSwagger();

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
            // specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
                c.RoutePrefix = string.Empty;
            });

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                NatashaController.Endpoints = endpoints.DataSources;
            });
        }
Exemple #2
0
    static AssemblyDomain()
    {
        _defaultDomain  = DomainManagement.RegisterDefault <AssemblyDomain>();
        _shareLibraries = new ConcurrentQueue <string>();
        var assemblies = AppDomain.CurrentDomain.GetAssemblies();

        foreach (var asm in assemblies)
        {
            try
            {
                _shareLibraries.Enqueue(asm.Location);
            }
            catch (Exception)
            {
            }
        }
    }
Exemple #3
0
 public static void RegistDomain <TDomain>() where TDomain : DomainBase <TDomain>, new()
 {
     DomainManagement.RegisterDefault <TDomain>();
 }
Exemple #4
0
        public async Task Execute(string code, NScriptOptions scriptOptions)
        {
            if (code.Contains("static void Main(") || code.Contains("static async Task Main("))
            {
                // Program
            }
            else
            {
                // Statement
                if (code.Contains("await "))
                {
                    //async
                    code = $@"public static async Task MainAsync()
  {{
    {code}
  }}";
                }
                else
                {
                    // sync
                    code = $@"public static void Main(string[] args)
  {{
    {code}
  }}";
                }
            }

            if (!code.Contains("static void Main(") && code.Contains("static async Task Main("))
            {
                if (code.Contains("static async Task Main()"))
                {
                    code = $@"{code}
public static void Main() => MainAsync().Wait();
";
                }
                else
                {
                    code = $@"{code}
public static void Main() => MainAsync(null).Wait();
";
                }
            }

            if (!code.Contains("class Program"))
            {
                code = $@"public class Program
{{
  {code}
}}";
            }

            if (scriptOptions.UsingList.Count > 0)
            {
                code = $"{scriptOptions.UsingList.Select(ns => $"using {ns};").StringJoin(Environment.NewLine)}{Environment.NewLine}{code}";
            }

            DomainManagement.RegisterDefault <AssemblyDomain>();
            var assBuilder = new AssemblyCSharpBuilder(GuidIdGenerator.Instance.NewId());

            assBuilder.Add(code, scriptOptions.UsingList);

            // TODO: add references

            assBuilder.CompilerOption(compiler =>
            {
                compiler.AssemblyOutputKind = AssemblyBuildKind.File;
            });

            var assembly        = assBuilder.GetAssembly();
            var targetFramework = assembly.GetCustomAttribute <TargetFrameworkAttribute>();

            var entryPoint = assembly.GetType("Program")?.GetMethod("Main", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

            if (null != entryPoint)
            {
                entryPoint.Invoke(null, entryPoint.GetParameters().Select(p => p.ParameterType.GetDefaultValue()).ToArray());
            }
            else
            {
                throw new ArgumentException("can not find entry point");
            }

            //using (var executor = new DotNetExecutor(assembly.Location, _outputHelper))
            //{
            //    await executor.ExecuteAsync();
            //}
        }
Exemple #5
0
 static void Main(string[] args)
 {
     DomainManagement.RegisterDefault <AssemblyDomain>();
     BenchmarkRunner.Run <BenchmarkTest>();
     Console.ReadKey();
 }
Exemple #6
0
        static void Main(string[] args)
        {
            DomainManagement.RegisterDefault <AssemblyDomain>();
            //var @operator = FastMethodOperator.DefaultDomain();
            //var actionDelegate = @operator
            //    .Param(typeof(string), "parameter")
            //    .Body("Console.WriteLine(parameter);")
            //    .Compile();

            //actionDelegate.DynamicInvoke("HelloWorld!");
            //var action = (Action<string>)actionDelegate;
            //action("HelloWorld!");
            //actionDelegate.DisposeDomain();

            //起个类
            NClass nClass = NClass.DefaultDomain();

            nClass
            .Namespace("MyNamespace")
            .Public()
            .Name("MyClass")
            .Ctor(ctor => ctor.Public().Body("MyField=\"Hello\";"))
            .Property(prop => prop
                      .Type(typeof(string))
                      .Name("MyProperty")
                      .Public()
                      .OnlyGetter("return \"World!\";")
                      );


            //添加方法
            MethodBuilder mb = new MethodBuilder();

            mb
            .Public()
            .Override()
            .Name("ToString")
            .Body("return MyField+\" \"+MyProperty;")
            .Return(typeof(string));
            nClass.Method(mb);


            //添加字段
            FieldBuilder fb = nClass.GetFieldBuilder();

            fb.Public()
            .Name("MyField")
            .Type <string>();


            //动态调用动态创建的类
            var action = NDelegate
                         .RandomDomain()
                         .Action("Console.WriteLine((new MyClass()).ToString());", nClass.GetType());

            action();
            action.DisposeDomain();
            //Console.WriteLine(typeof(List<int>[]).GetRuntimeName());
            //Console.WriteLine(typeof(List<int>[,]).GetRuntimeName());
            //Console.WriteLine(typeof(int[,]).GetRuntimeName());
            //Console.WriteLine(typeof(int[][]).GetRuntimeName());
            //Console.WriteLine(typeof(int[][,,,]).GetRuntimeName());
            Console.ReadKey();
        }
Exemple #7
0
        static void Main(string[] args)
        {
            DomainManagement.RegisterDefault <AssemblyDomain>();

            NErrorLog.Enabled   = false;
            NSucceedLog.Enabled = false;
            NWarningLog.Enabled = false;
            Stopwatch watch = new Stopwatch();
            double    tempTotleTime;

            #region Natasha Preheating
            var preTime = Process.GetCurrentProcess().TotalProcessorTime;
            watch.Start();
            tempType = NClass.CreateDomain("tes1t")
                       .Namespace("Test")
                       .UseRandomName()
                       .PublicField <string>("Name")
                       .PublicField <string>("Age")
                       .PublicField <int[]>("Temp")
                       .Ctor(item => item.Body("Temp = new int[40960];"))
                       .GetType();
            watch.Stop();
            tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime);
            if (watch.Elapsed.Seconds > 0)
            {
                tempTotleTime = tempTotleTime / watch.Elapsed.Seconds;
            }
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine("Natasha预热:");
            Console.WriteLine("-----------------------------------------------------------------------------------------");
            Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|");
            Console.WriteLine("-----------------------------------------------------------------------------------------");
            #endregion

            Thread.Sleep(1000);

            #region Run Compiler
            preTime = Process.GetCurrentProcess().TotalProcessorTime;
            watch.Restart();
            Test();
            watch.Stop();
            tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime);
            if (watch.Elapsed.Seconds > 0)
            {
                tempTotleTime = tempTotleTime / watch.Elapsed.Seconds;
            }
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"{count}个独立域编译后:");
            Console.WriteLine("-----------------------------------------------------------------------------------------");
            Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|");
            Console.WriteLine("-----------------------------------------------------------------------------------------");
            #endregion

            Thread.Sleep(1000);

            #region Release Handler
            preTime = Process.GetCurrentProcess().TotalProcessorTime;
            watch.Restart();
            Release();
            watch.Stop();
            tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime);
            if (watch.Elapsed.Seconds > 0)
            {
                tempTotleTime = tempTotleTime / watch.Elapsed.Seconds;
            }
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("释放中:");
            Console.WriteLine("-----------------------------------------------------------------------------------------");
            Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|");
            Console.WriteLine("-----------------------------------------------------------------------------------------");
            #endregion

            Thread.Sleep(1000);
            #region Run GC
            preTime = Process.GetCurrentProcess().TotalProcessorTime;
            watch.Restart();
            RunGc();
            watch.Stop();
            tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime);
            if (watch.Elapsed.Seconds > 0)
            {
                tempTotleTime = tempTotleTime / watch.Elapsed.Seconds;
            }
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine();
            Console.WriteLine("回收后:");
            Console.WriteLine("-----------------------------------------------------------------------------------------");
            Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|");
            Console.WriteLine("-----------------------------------------------------------------------------------------");
            #endregion

            Thread.Sleep(1000);

            #region Check Alive
            preTime = Process.GetCurrentProcess().TotalProcessorTime;
            watch.Restart();
            var alive = CheckAlive();
            DomainManagement.Clear();
            watch.Stop();
            tempTotleTime = GetCpu(Process.GetCurrentProcess().TotalProcessorTime, preTime);
            if (watch.Elapsed.Seconds > 0)
            {
                tempTotleTime = tempTotleTime / watch.Elapsed.Seconds;
            }
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine();
            Console.WriteLine($"存活检测: {alive}");
            Console.WriteLine("-----------------------------------------------------------------------------------------");
            Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|");
            Console.WriteLine("-----------------------------------------------------------------------------------------");
            #endregion

            //for (int i = 0; i < 10; i++)
            //{
            //    Thread.Sleep(3000);
            //    preTime = Process.GetCurrentProcess().TotalProcessorTime;
            //    Console.WriteLine($"第{i}次静默检测:");
            //    Console.WriteLine("-----------------------------------------------------------------------------------------");
            //    Console.WriteLine($"|\tCPU:{tempTotleTime.ToString("f2")}%\t|\t内存:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M\t|\t执行耗时:{watch.Elapsed}\t|");
            //    Console.WriteLine("-----------------------------------------------------------------------------------------");
            //}

            Console.ReadKey();
        }
Exemple #8
0
 public static void RegistDomain <TDomain>(bool initializeReference = true) where TDomain : DomainBase
 {
     DomainManagement.RegisterDefault <TDomain>(initializeReference);
 }