Example #1
0
        //
        // The main entry point for the host.
        //
        public static int Main(string[] arguments)
        {
            int returnValue = 1;
            CommandLineArguments commandLineArguments = ProcessArguments(arguments);

            if (arguments.Length - commandLineArguments.ArgumentsStart < 0)
            {
                Console.Error.WriteLine("usage: chakrahost [-debug] [-profile] <script name> <arguments>");
                return(returnValue);
            }

            try
            {
                //
                // Create the runtime. We're only going to use one runtime for this host.
                //

                using (JavaScriptRuntime runtime = JavaScriptRuntime.Create())
                {
                    //
                    // Similarly, create a single execution context. Note that we're putting it on the stack here,
                    // so it will stay alive through the entire run.
                    //

                    JavaScriptContext context = CreateHostContext(runtime, arguments, commandLineArguments.ArgumentsStart);

                    //
                    // Now set the execution context as being the current one on this thread.
                    //

                    using (new JavaScriptContext.Scope(context))
                    {
                        //
                        // Start debugging if requested.
                        //

                        if (commandLineArguments.Debug)
                        {
                            StartDebugging();
                        }

                        //
                        // Start profiling if requested.
                        //

                        if (commandLineArguments.Profile)
                        {
                            var profiler = new Profiler();
                            JavaScriptContext.StartProfiling(profiler, Native.ProfilerEventMask.TraceAll, 0);
                        }

                        //
                        // Load the script from the disk.
                        //

                        string script = File.ReadAllText(arguments[commandLineArguments.ArgumentsStart]);

                        //
                        // Run the script.
                        //

                        JavaScriptValue result;
                        try
                        {
                            result = JavaScriptContext.RunScript(script, currentSourceContext++, arguments[commandLineArguments.ArgumentsStart]);
                        }
                        catch (JavaScriptScriptException e)
                        {
                            PrintScriptException(e.Error);
                            return(1);
                        }
                        catch (Exception e)
                        {
                            Console.Error.WriteLine("chakrahost: failed to run script: {0}", e.Message);
                            return(1);
                        }

                        //
                        // Convert the return value.
                        //

                        JavaScriptValue numberResult = result.ConvertToNumber();
                        double          doubleResult = numberResult.ToDouble();
                        returnValue = (int)doubleResult;

                        //
                        // Stop profiling.
                        //

                        if (commandLineArguments.Profile)
                        {
                            JavaScriptContext.StopProfiling(0);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("chakrahost: fatal error: internal error: {0}.", e.Message);
            }

            return(returnValue);
        }