Program.cs 2.5 KB

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