Program.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Reflection;
  5. using Jint.Runtime;
  6. namespace Jint.Repl
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. var engine = new Engine()
  13. .SetValue("print", new Action<object>(Console.WriteLine))
  14. ;
  15. var filename = args.Length > 0 ? args[0] : "";
  16. if (!String.IsNullOrEmpty(filename))
  17. {
  18. if (!File.Exists(filename))
  19. {
  20. Console.WriteLine("Could not find file: {0}", filename);
  21. }
  22. var script = File.ReadAllText(filename);
  23. var result = engine.GetValue(engine.Execute(script).GetCompletionValue());
  24. return;
  25. }
  26. Assembly assembly = Assembly.GetExecutingAssembly();
  27. FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
  28. string version = fvi.FileVersion;
  29. Console.WriteLine("Welcome to Jint ({0})", version);
  30. Console.WriteLine("Type 'exit' to leave, 'print()' to write on the console.");
  31. Console.WriteLine();
  32. var defaultColor = Console.ForegroundColor;
  33. while (true)
  34. {
  35. Console.ForegroundColor = defaultColor;
  36. Console.Write("jint> ");
  37. var input = Console.ReadLine();
  38. if (input == "exit")
  39. {
  40. return;
  41. }
  42. try
  43. {
  44. var result = engine.GetValue(engine.Execute(input).GetCompletionValue());
  45. //var str = TypeConverter.ToString(engine.Json.Stringify(engine.Json, Arguments.From(result, Undefined.Instance, " ")));
  46. //Console.ForegroundColor = ConsoleColor.Magenta;
  47. //Console.WriteLine("=> {0}", str);
  48. }
  49. catch (JavaScriptException je)
  50. {
  51. Console.ForegroundColor = ConsoleColor.Red;
  52. Console.WriteLine(je.ToString());
  53. }
  54. catch (Exception e)
  55. {
  56. Console.ForegroundColor = ConsoleColor.Red;
  57. Console.WriteLine(e.Message);
  58. }
  59. }
  60. }
  61. }
  62. }