EsprimaExtensions.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Runtime.CompilerServices;
  5. using Esprima.Ast;
  6. using Jint.Native;
  7. using Jint.Native.Function;
  8. using Jint.Native.Object;
  9. using Jint.Runtime;
  10. using Jint.Runtime.Environments;
  11. using Jint.Runtime.Interpreter;
  12. using Jint.Runtime.Interpreter.Expressions;
  13. namespace Jint
  14. {
  15. public static class EsprimaExtensions
  16. {
  17. public static JsValue GetKey(this ClassProperty property, Engine engine) => GetKey(property.Key, engine, property.Computed);
  18. public static JsValue GetKey(this Expression expression, Engine engine, bool resolveComputed = false)
  19. {
  20. if (expression is Literal literal)
  21. {
  22. return LiteralKeyToString(literal);
  23. }
  24. if (!resolveComputed && expression is Identifier identifier)
  25. {
  26. return identifier.Name;
  27. }
  28. if (!resolveComputed || !TryGetComputedPropertyKey(expression, engine, out var propertyKey))
  29. {
  30. ExceptionHelper.ThrowArgumentException("Unable to extract correct key, node type: " + expression.Type);
  31. return null;
  32. }
  33. return propertyKey;
  34. }
  35. private static bool TryGetComputedPropertyKey<T>(T expression, Engine engine, out JsValue propertyKey)
  36. where T : Expression
  37. {
  38. if (expression.Type is Nodes.Identifier
  39. or Nodes.CallExpression
  40. or Nodes.BinaryExpression
  41. or Nodes.UpdateExpression
  42. or Nodes.AssignmentExpression
  43. or Nodes.UnaryExpression
  44. || expression is StaticMemberExpression)
  45. {
  46. propertyKey = TypeConverter.ToPropertyKey(JintExpression.Build(engine, expression).GetValue());
  47. return true;
  48. }
  49. propertyKey = string.Empty;
  50. return false;
  51. }
  52. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  53. internal static bool IsFunctionWithName<T>(this T node) where T : Node
  54. {
  55. var type = node.Type;
  56. return type == Nodes.FunctionExpression
  57. || type == Nodes.ArrowFunctionExpression
  58. || type == Nodes.ArrowParameterPlaceHolder
  59. || type == Nodes.ClassExpression;
  60. }
  61. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  62. internal static bool IsOptional<T>(this T node) where T : Expression
  63. {
  64. switch (node)
  65. {
  66. case MemberExpression { Optional: true }:
  67. case CallExpression { Optional: true }:
  68. return true;
  69. default:
  70. return false;
  71. }
  72. }
  73. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  74. internal static string LiteralKeyToString(Literal literal)
  75. {
  76. // prevent conversion to scientific notation
  77. if (literal.Value is double d)
  78. {
  79. return TypeConverter.ToString(d);
  80. }
  81. return literal.Value as string ?? Convert.ToString(literal.Value, provider: null);
  82. }
  83. internal static void GetBoundNames(this VariableDeclaration variableDeclaration, List<string> target)
  84. {
  85. ref readonly var declarations = ref variableDeclaration.Declarations;
  86. for (var i = 0; i < declarations.Count; i++)
  87. {
  88. var declaration = declarations[i];
  89. GetBoundNames(declaration.Id, target);
  90. }
  91. }
  92. internal static void GetBoundNames(this Node? parameter, List<string> target)
  93. {
  94. if (parameter is null || parameter.Type == Nodes.Literal)
  95. {
  96. return;
  97. }
  98. // try to get away without a loop
  99. if (parameter is Identifier id)
  100. {
  101. target.Add(id.Name!);
  102. return;
  103. }
  104. if (parameter is VariableDeclaration variableDeclaration)
  105. {
  106. variableDeclaration.GetBoundNames(target);
  107. return;
  108. }
  109. while (true)
  110. {
  111. if (parameter is Identifier identifier)
  112. {
  113. target.Add(identifier.Name!);
  114. return;
  115. }
  116. if (parameter is RestElement restElement)
  117. {
  118. parameter = restElement.Argument;
  119. continue;
  120. }
  121. else if (parameter is ArrayPattern arrayPattern)
  122. {
  123. ref readonly var arrayPatternElements = ref arrayPattern.Elements;
  124. for (var i = 0; i < arrayPatternElements.Count; i++)
  125. {
  126. var expression = arrayPatternElements[i];
  127. GetBoundNames(expression, target);
  128. }
  129. }
  130. else if (parameter is ObjectPattern objectPattern)
  131. {
  132. ref readonly var objectPatternProperties = ref objectPattern.Properties;
  133. for (var i = 0; i < objectPatternProperties.Count; i++)
  134. {
  135. var property = objectPatternProperties[i];
  136. if (property is Property p)
  137. {
  138. GetBoundNames(p.Value, target);
  139. }
  140. else
  141. {
  142. GetBoundNames((RestElement) property, target);
  143. }
  144. }
  145. }
  146. else if (parameter is AssignmentPattern assignmentPattern)
  147. {
  148. parameter = assignmentPattern.Left;
  149. if (assignmentPattern.Right is ClassExpression classExpression)
  150. {
  151. // TODO check if there's more generic rule
  152. if (classExpression.Id is not null)
  153. {
  154. target.Add(classExpression.Id.Name!);
  155. }
  156. }
  157. continue;
  158. }
  159. break;
  160. }
  161. }
  162. internal static void BindingInitialization(
  163. this Expression? expression,
  164. Engine engine,
  165. JsValue value,
  166. EnvironmentRecord env)
  167. {
  168. if (expression is Identifier identifier)
  169. {
  170. var catchEnvRecord = (DeclarativeEnvironmentRecord) env;
  171. catchEnvRecord.CreateMutableBindingAndInitialize(identifier.Name, canBeDeleted: false, value);
  172. }
  173. else if (expression is BindingPattern bindingPattern)
  174. {
  175. BindingPatternAssignmentExpression.ProcessPatterns(
  176. engine,
  177. bindingPattern,
  178. value,
  179. env);
  180. }
  181. }
  182. /// <summary>
  183. /// https://tc39.es/ecma262/#sec-runtime-semantics-definemethod
  184. /// </summary>
  185. internal static Record DefineMethod(this ClassProperty m, ObjectInstance obj, ObjectInstance? functionPrototype = null)
  186. {
  187. var engine = obj.Engine;
  188. var property = TypeConverter.ToPropertyKey(m.GetKey(engine));
  189. var prototype = functionPrototype ?? engine.Realm.Intrinsics.Function.PrototypeObject;
  190. var function = m.Value as IFunction;
  191. if (function is null)
  192. {
  193. ExceptionHelper.ThrowSyntaxError(engine.Realm);
  194. }
  195. var functionDefinition = new JintFunctionDefinition(engine, function);
  196. var functionThisMode = functionDefinition.Strict || engine._isStrict
  197. ? FunctionThisMode.Strict
  198. : FunctionThisMode.Global;
  199. var closure = new ScriptFunctionInstance(
  200. engine,
  201. functionDefinition,
  202. engine.ExecutionContext.LexicalEnvironment,
  203. functionThisMode,
  204. prototype);
  205. closure.MakeMethod(obj);
  206. return new Record(property, closure);
  207. }
  208. internal readonly struct Record
  209. {
  210. public Record(JsValue key, ScriptFunctionInstance closure)
  211. {
  212. Key = key;
  213. Closure = closure;
  214. }
  215. public readonly JsValue Key;
  216. public readonly ScriptFunctionInstance Closure;
  217. }
  218. }
  219. }