EsprimaExtensions.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using Esprima.Ast;
  3. using Jint.Native.Symbol;
  4. using Jint.Runtime;
  5. namespace Jint
  6. {
  7. internal static class EsprimaExtensions
  8. {
  9. public static string GetKey<T>(this T expression) where T : Expression
  10. {
  11. if (expression is Literal literal)
  12. {
  13. return literal.Value as string ?? Convert.ToString(literal.Value, provider: null);
  14. }
  15. if (expression is Identifier identifier)
  16. {
  17. return identifier.Name;
  18. }
  19. if (expression is StaticMemberExpression staticMemberExpression)
  20. {
  21. var obj = staticMemberExpression.Object.GetKey();
  22. var property = staticMemberExpression.Property.GetKey();
  23. if (obj == "Symbol" && property == "iterator")
  24. {
  25. return GlobalSymbolRegistry.Iterator._value;
  26. }
  27. }
  28. return ExceptionHelper.ThrowArgumentException<string>("Unable to extract correct key");
  29. }
  30. }
  31. }