Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            JavaScriptHostingConfig config = new JavaScriptHostingConfig();

            config.AddModuleFolder("Scripts");
            config.AddPlugin(echo);

            InterruptableHost host = new InterruptableHost();
            var app            = host.GetModuleClass("app", "app", config);
            var runtimeService = app.ServiceNode.GetService <IRuntimeService>();

            StopEngine(runtimeService);
            var switchService = app.ServiceNode.GetService <IContextSwitchService>();

            try
            {
                app.CallMethod("loop");
            }
            catch (Exception ex) when(ex.Message == "Script was terminated.")
            {
                //switchService.Leave();
                Console.WriteLine("Script terminated");
                runtimeService.Disabled = false;
                Console.WriteLine("Engine Enabled");
            }
            catch (Exception)
            {
                throw;
            }
            //GC.KeepAlive(host);
            Console.Read();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Dictionary <string, string> dict = new Dictionary <string, string>();

            using (StreamReader reader = new StreamReader("Data.txt"))
            {
                var hosting = JavaScriptHosting.Default;
                var config  = new JavaScriptHostingConfig();


                config.AddModuleFolder("Scripts");                                  //set script folder

                var app = hosting.GetModuleClass("SimpleProcessor", "App", config); //get javascript class



                var runtimeService = app.ServiceNode.GetService <IRuntimeService>();

                using (var debugger = new ChakraCore.NET.DebugAdapter2.VSCode.Managed.VSCodeChakraDebugger2Managed(runtimeService.Runtime))
                {
                    debugger.Start("test", true, 5000);
                    debugger.WaitForDebugger();



                    InjectDictionaryConverter(app.ServiceNode.GetService <IJSValueConverterService>());

                    int line = 0;
                    dict.Add("Line", null);

                    app.CallMethod <IDictionary <string, string> >("Init", dict);

                    string data = reader.ReadLine();
                    string tmp;
                    while (data != null)
                    {
                        line++;
                        dict["Line"] = line.ToString();
                        tmp          = app.CallFunction <string, string>("Process", data);
                        Console.WriteLine(tmp);
                        data = reader.ReadLine();
                        dumpDict(dict);
                        Console.WriteLine();
                    }
                }
            }

            Console.WriteLine("Processing done, press enter to exit");
            Console.ReadLine();
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                showUsage();
            }
            else
            {
                ScriptConfig config = null;
                try
                {
                    config = ScriptConfig.Parse(args);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("invalid parameter");
                    Console.WriteLine(ex.Message);
                    showUsage();
                    Console.WriteLine("Press any key to exit");
                    Console.Read();
                    return;
                }
                JavaScriptHostingConfig hostingConfig = new JavaScriptHostingConfig(false);
                hostingConfig
                .AddPlugin <SysInfoPluginInstaller>("SysInfo")
                .AddModuleFolder(config.RootFolder)
                .AddModuleFolderFromCurrentAssembly()
                .EnableHosting((moduleName) => { return(hostingConfig); })
                .AddPluginLoader(JavaScriptHostingConfig.DefaultPluginInstaller)
                ;
                ChakraCore.NET.Plugin.Common.EchoProvider.OnEcho = (msg) => { Console.WriteLine(msg); };

                string script = File.ReadAllText(config.File);
                Console.WriteLine("---Script Start---");
                if (config.IsModule)
                {
                    var app = JavaScriptHosting.Default.GetModuleClass <JSApp>(config.FileName, config.ModuleClass, hostingConfig);
                    app.EntryPoint = config.ModuleEntryPoint;
                    app.Run();
                }
                else
                {
                    JavaScriptHosting.Default.RunScript(script, hostingConfig);
                }
            }

            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
Ejemplo n.º 4
0
        private Tuple <ChakraContext, JSValue> CreateChakraContext()
        {
            JavaScriptHosting       hosting = JavaScriptHosting.Default; //get default host instantce
            JavaScriptHostingConfig config  = new JavaScriptHostingConfig();

            config.AddModuleFolder("wwwroot/dist/js"); //set script folder

            var chakraContext = hosting.CreateContext(config);

            // Function Converter
            var valueServices = chakraContext.ServiceNode.GetService <IJSValueConverterService>();

            valueServices.RegisterFunctionConverter <string>();
            valueServices.RegisterProxyConverter <EvConsole>((binding, instance, serviceNode) =>
            {
                binding.SetMethod <string>("log", instance.Log);
            });

            // EvContext Converter
            valueServices.RegisterStructConverter <EvContext>((to, from) => { to.WriteProperty("title", from.Title); },
                                                              (from) => new EvContext
            {
                Title = from.ReadProperty <string>("title")
            });

            var console = new EvConsole();

            chakraContext.GlobalObject.WriteProperty("console", console);

            chakraContext.Enter();
            // https://ssr.vuejs.org/guide/non-node.html
            var obj = JavaScriptValue.CreateObject();

            obj.SetProperty(JavaScriptPropertyId.FromString("VUE_ENV"), JavaScriptValue.FromString("server"), false);
            obj.SetProperty(JavaScriptPropertyId.FromString("NODE_ENV"), JavaScriptValue.FromString("production"),
                            false);

            var process = JavaScriptValue.CreateObject();

            process.SetProperty(JavaScriptPropertyId.FromString("env"), obj, false);

            chakraContext.GlobalObject.WriteProperty(JavaScriptPropertyId.FromString("process"), process);
            chakraContext.Leave();

            var appClass = chakraContext.ProjectModuleClass("entrypoint", "App", config.LoadModule);

            return(Tuple.Create(chakraContext, appClass));
        }
Ejemplo n.º 5
0
        public override void Init()
        {
            base.Init();
            engine = new ImageSharpDrawingInstaller();
            JavaScriptHostingConfig config = new JavaScriptHostingConfig();

            config.AddPlugin(engine);
            foreach (var item in ScriptFolders)
            {
                config.AddModuleFolder(item);
            }
            engine.SetTextureRoot(ResourceFolder);
            engine.SetFontRoot(ResourceFolder);
            config.AddPlugin(new ClockPluginInstaller(manager));
            app = JavaScriptHosting.Default.GetModuleClass <ClockApp>("app", "App", config);
            app.Init();
        }
Ejemplo n.º 6
0
        public JSEngine(IConsole console, BaseOption option, IRandomGenerator randomGenerator, IDebugAdapter debugAdapter)
        {
            DefaultScriptsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Scripts");
            _console           = console;
            var config = new JavaScriptHostingConfig();

            _context = JavaScriptHosting.Default.CreateContext(config);
            _context.RegisterEvalService();
            _prettyPrint      = new PrettyPrint(this);
            _timerCallsHelper = new TimerCallsHelper(this);
            _option           = option;
            _randomGenerator  = randomGenerator;
            ExposeConsoleToContext();
            ExposeCryptoHelpers();
            ExposeAElfOption();
            LoadCryptoJS();
            LoadEncodingJS();
            LoadAelfJs();
            LoadHelpersJs();
            ExposeHttpRequestorToContext();
            ExposeTimerCallsHelper();
            ExposeAccountSaver();
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            CancellationTokenSource debugCTS = null;
            ScriptConfig            config   = null;
            JSApp app = null;

            if (args.Length == 0)
            {
                showUsage();
                return;
            }
            else
            {
                try
                {
                    config = ScriptConfig.Parse(args);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("invalid parameter");
                    Console.WriteLine(ex.Message);
                    showUsage();
                    Console.WriteLine("Press any key to exit");
                    Console.Read();
                    return;
                }
                JavaScriptHostingConfig hostingConfig = new JavaScriptHostingConfig();

                hostingConfig
                .AddPlugin <SysInfoPluginInstaller>()
                .AddModuleFolder(config.RootFolder)
                .AddPlugin(new EchoProvider(new Echo()))
                .AddModuleFolderFromCurrentAssembly()
                .EnableHosting((moduleName) => { return(hostingConfig); })
                ;
                if (config.DebugMode)
                {
                    debugCTS = new CancellationTokenSource();
                    var adapter = new VSCodeDebugAdapter(true);//wait for launch command from VSCode, user can reconnect with attach command after launch debug is done
                    //var adapter = new VSCodeDebugAdapter(false);//start program, wait for attach command from VSCode
                    adapter.OnLaunch         += (sender, arguments) => { Console.WriteLine($"Launch requested,arguments={arguments}"); };
                    adapter.OnAttach         += (sender, arguments) => { Console.WriteLine($"Attach requested,arguments={arguments}"); };
                    adapter.OnAdapterMessage += (sender, msg) => { Console.WriteLine(msg); };
                    adapter.OnStatusChang    += (sender, e) => { Console.WriteLine(e); };
                    adapter.RunServer(3515, debugCTS.Token);
                    //adapter.RunClient(3515, debugCTS.Token);
                    hostingConfig.DebugAdapter = adapter;
                }

                if (config.IsModule)
                {
                    app            = JavaScriptHosting.Default.GetModuleClass <JSApp>(config.FileName, config.ModuleClass, hostingConfig);
                    app.EntryPoint = config.ModuleEntryPoint;
                    app.Run();
                }
                else
                {
                    string script = File.ReadAllText(config.File);
                    Console.WriteLine("---Script Start---");
                    JavaScriptHosting.Default.RunScript(script, hostingConfig);
                }
            }
            if (config.IsModule)
            {
                while (true)
                {
                    Console.WriteLine("input \"exit\" to exit, anything else to run the module again");
                    string command = Console.ReadLine();
                    if (command.ToLower() == "exit")
                    {
                        break;
                    }
                    app.Run();
                }
            }
            else
            {
                Console.WriteLine("Press Enter to exit");
                Console.Read();
            }



            debugCTS?.Cancel();
        }