EsprimaExtensions.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Runtime.CompilerServices;
  5. using Esprima.Ast;
  6. using Jint.Native;
  7. using Jint.Runtime;
  8. using Jint.Runtime.Interpreter.Expressions;
  9. namespace Jint
  10. {
  11. public static class EsprimaExtensions
  12. {
  13. public static JsValue GetKey(this Property property, Engine engine) => GetKey(property.Key, engine, property.Computed);
  14. public static JsValue GetKey<T>(this T expression, Engine engine, bool resolveComputed = false) where T : Expression
  15. {
  16. if (expression is Literal literal)
  17. {
  18. return LiteralKeyToString(literal);
  19. }
  20. if (!resolveComputed && expression is Identifier identifier)
  21. {
  22. return identifier.Name;
  23. }
  24. if (!resolveComputed || !TryGetComputedPropertyKey(expression, engine, out var propertyKey))
  25. {
  26. return ExceptionHelper.ThrowArgumentException<JsValue>("Unable to extract correct key, node type: " + expression.Type);
  27. }
  28. return propertyKey;
  29. }
  30. private static bool TryGetComputedPropertyKey<T>(T expression, Engine engine, out JsValue propertyKey)
  31. where T : Expression
  32. {
  33. if (expression.Type == Nodes.Identifier
  34. || expression.Type == Nodes.CallExpression
  35. || expression.Type == Nodes.BinaryExpression
  36. || expression.Type == Nodes.UpdateExpression
  37. || expression is StaticMemberExpression)
  38. {
  39. propertyKey = TypeConverter.ToPropertyKey(JintExpression.Build(engine, expression).GetValue());
  40. return true;
  41. }
  42. propertyKey = string.Empty;
  43. return false;
  44. }
  45. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  46. internal static bool IsFunctionWithName<T>(this T node) where T : Node
  47. {
  48. var type = node.Type;
  49. return type == Nodes.FunctionExpression || type == Nodes.ArrowFunctionExpression || type == Nodes.ArrowParameterPlaceHolder;
  50. }
  51. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  52. internal static string LiteralKeyToString(Literal literal)
  53. {
  54. // prevent conversion to scientific notation
  55. if (literal.Value is double d)
  56. {
  57. return DoubleToString(d);
  58. }
  59. return literal.Value as string ?? Convert.ToString(literal.Value, provider: null);
  60. }
  61. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  62. internal static string DoubleToString(double d)
  63. {
  64. return (d - (long) d) == 0 ? ((long) d).ToString() : d.ToString(CultureInfo.InvariantCulture);
  65. }
  66. internal static void GetBoundNames(this VariableDeclaration variableDeclaration, List<string> target)
  67. {
  68. ref readonly var declarations = ref variableDeclaration.Declarations;
  69. for (var i = 0; i < declarations.Count; i++)
  70. {
  71. var declaration = declarations[i];
  72. GetBoundNames(declaration.Id, target);
  73. }
  74. }
  75. internal static void GetBoundNames(this Node parameter, List<string> target)
  76. {
  77. if (parameter is null || parameter.Type == Nodes.Literal)
  78. {
  79. return;
  80. }
  81. // try to get away without a loop
  82. if (parameter is Identifier id)
  83. {
  84. target.Add(id.Name);
  85. return;
  86. }
  87. while (true)
  88. {
  89. if (parameter is Identifier identifier)
  90. {
  91. target.Add(identifier.Name);
  92. return;
  93. }
  94. if (parameter is RestElement restElement)
  95. {
  96. parameter = restElement.Argument;
  97. continue;
  98. }
  99. else if (parameter is ArrayPattern arrayPattern)
  100. {
  101. ref readonly var arrayPatternElements = ref arrayPattern.Elements;
  102. for (var i = 0; i < arrayPatternElements.Count; i++)
  103. {
  104. var expression = arrayPatternElements[i];
  105. GetBoundNames(expression, target);
  106. }
  107. }
  108. else if (parameter is ObjectPattern objectPattern)
  109. {
  110. ref readonly var objectPatternProperties = ref objectPattern.Properties;
  111. for (var i = 0; i < objectPatternProperties.Count; i++)
  112. {
  113. var property = objectPatternProperties[i];
  114. if (property is Property p)
  115. {
  116. GetBoundNames(p.Value, target);
  117. }
  118. else
  119. {
  120. GetBoundNames((RestElement) property, target);
  121. }
  122. }
  123. }
  124. else if (parameter is AssignmentPattern assignmentPattern)
  125. {
  126. parameter = assignmentPattern.Left;
  127. continue;
  128. }
  129. break;
  130. }
  131. }
  132. }
  133. }