JintObjectExpression.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using Jint.Collections;
  2. using Jint.Native;
  3. using Jint.Native.Function;
  4. using Jint.Native.Object;
  5. using Jint.Runtime.Descriptors;
  6. namespace Jint.Runtime.Interpreter.Expressions;
  7. /// <summary>
  8. /// https://tc39.es/ecma262/#sec-object-initializer
  9. /// </summary>
  10. internal sealed class JintObjectExpression : JintExpression
  11. {
  12. private readonly ExpressionCache _valueExpressions = new();
  13. private ObjectProperty?[] _properties = [];
  14. // check if we can do a shortcut when all are object properties
  15. // and don't require duplicate checking
  16. private bool _canBuildFast;
  17. private bool _initialized;
  18. private sealed class ObjectProperty
  19. {
  20. internal readonly string? _key;
  21. private JsString? _keyJsString;
  22. internal readonly Property _value;
  23. private JintFunctionDefinition? _functionDefinition;
  24. public ObjectProperty(string? key, Property property)
  25. {
  26. _key = key;
  27. _value = property;
  28. }
  29. public JsString? KeyJsString => _keyJsString ??= _key != null ? JsString.Create(_key) : null;
  30. public JintFunctionDefinition GetFunctionDefinition(Engine engine)
  31. {
  32. if (_functionDefinition is not null)
  33. {
  34. return _functionDefinition;
  35. }
  36. var function = _value.Value as IFunction;
  37. if (function is null)
  38. {
  39. ExceptionHelper.ThrowSyntaxError(engine.Realm);
  40. }
  41. _functionDefinition = new JintFunctionDefinition(function);
  42. return _functionDefinition;
  43. }
  44. }
  45. private JintObjectExpression(ObjectExpression expression) : base(expression)
  46. {
  47. }
  48. public static JintExpression Build(ObjectExpression expression)
  49. {
  50. return expression.Properties.Count == 0
  51. ? JintEmptyObjectExpression.Instance
  52. : new JintObjectExpression(expression);
  53. }
  54. private void Initialize(EvaluationContext context)
  55. {
  56. _canBuildFast = true;
  57. var expression = (ObjectExpression) _expression;
  58. ref readonly var properties = ref expression.Properties;
  59. var valueExpressions = new Expression[properties.Count];
  60. _properties = new ObjectProperty[properties.Count];
  61. for (var i = 0; i < _properties.Length; i++)
  62. {
  63. string? propName = null;
  64. var property = properties[i];
  65. if (property is Acornima.Ast.ObjectProperty p)
  66. {
  67. if (p.Key is Literal literal)
  68. {
  69. propName = AstExtensions.LiteralKeyToString(literal);
  70. }
  71. if (!p.Computed && p.Key is Identifier identifier)
  72. {
  73. propName = identifier.Name;
  74. _canBuildFast &= !string.Equals(propName, "__proto__", StringComparison.Ordinal);
  75. }
  76. _properties[i] = new ObjectProperty(propName, p);
  77. if (p.Kind is PropertyKind.Init)
  78. {
  79. var propertyValue = p.Value;
  80. valueExpressions[i] = (Expression) propertyValue;
  81. _canBuildFast &= !propertyValue.IsFunctionDefinition();
  82. }
  83. else
  84. {
  85. _canBuildFast = false;
  86. }
  87. }
  88. else if (property is SpreadElement spreadElement)
  89. {
  90. _canBuildFast = false;
  91. _properties[i] = null;
  92. valueExpressions[i] = spreadElement.Argument;
  93. }
  94. else
  95. {
  96. ExceptionHelper.ThrowArgumentOutOfRangeException("property", "cannot handle property " + property);
  97. }
  98. _canBuildFast &= propName != null;
  99. }
  100. _valueExpressions.Initialize(context, valueExpressions.AsSpan());
  101. }
  102. protected override object EvaluateInternal(EvaluationContext context)
  103. {
  104. if (!_initialized)
  105. {
  106. Initialize(context);
  107. _initialized = true;
  108. }
  109. return _canBuildFast
  110. ? BuildObjectFast(context)
  111. : BuildObjectNormal(context);
  112. }
  113. /// <summary>
  114. /// Version that can safely build plain object with only normal init/data fields fast.
  115. /// </summary>
  116. private JsObject BuildObjectFast(EvaluationContext context)
  117. {
  118. var obj = new JsObject(context.Engine);
  119. var properties = new PropertyDictionary(_properties.Length, checkExistingKeys: true);
  120. for (var i = 0; i < _properties.Length; i++)
  121. {
  122. var objectProperty = _properties[i];
  123. var propValue = _valueExpressions.GetValue(context, i);
  124. properties[objectProperty!._key!] = new PropertyDescriptor(propValue, PropertyFlag.ConfigurableEnumerableWritable);
  125. }
  126. obj.SetProperties(properties);
  127. return obj;
  128. }
  129. /// <summary>
  130. /// https://tc39.es/ecma262/#sec-object-initializer-runtime-semantics-propertydefinitionevaluation
  131. /// </summary>
  132. private object BuildObjectNormal(EvaluationContext context)
  133. {
  134. var engine = context.Engine;
  135. var obj = engine.Realm.Intrinsics.Object.Construct(_properties.Length);
  136. for (var i = 0; i < _properties.Length; i++)
  137. {
  138. var objectProperty = _properties[i];
  139. if (objectProperty is null)
  140. {
  141. // spread
  142. if (_valueExpressions.GetValue(context, i) is ObjectInstance source)
  143. {
  144. source.CopyDataProperties(obj, excludedItems: null);
  145. }
  146. continue;
  147. }
  148. var property = objectProperty._value;
  149. if (property.Method)
  150. {
  151. ClassDefinition.MethodDefinitionEvaluation(engine, obj, property, enumerable: true);
  152. continue;
  153. }
  154. JsValue? propName = objectProperty.KeyJsString;
  155. if (propName is null)
  156. {
  157. var value = property.TryGetKey(engine);
  158. if (context.IsAbrupt())
  159. {
  160. return value;
  161. }
  162. propName = TypeConverter.ToPropertyKey(value);
  163. }
  164. if (property.Kind == PropertyKind.Init)
  165. {
  166. var propValue = _valueExpressions.GetValue(context, i)!;
  167. if (string.Equals(objectProperty._key, "__proto__", StringComparison.Ordinal) && !objectProperty._value.Computed && !objectProperty._value.Shorthand)
  168. {
  169. if (propValue.IsObject() || propValue.IsNull())
  170. {
  171. obj.SetPrototypeOf(propValue);
  172. }
  173. continue;
  174. }
  175. if (_valueExpressions.IsAnonymousFunctionDefinition(i))
  176. {
  177. var closure = (Function) propValue;
  178. closure.SetFunctionName(propName);
  179. }
  180. obj.CreateDataPropertyOrThrow(propName, propValue);
  181. }
  182. else if (property.Kind is PropertyKind.Get or PropertyKind.Set)
  183. {
  184. var function = objectProperty.GetFunctionDefinition(engine);
  185. var closure = engine.Realm.Intrinsics.Function.OrdinaryFunctionCreate(
  186. engine.Realm.Intrinsics.Function.PrototypeObject,
  187. function,
  188. function.ThisMode,
  189. engine.ExecutionContext.LexicalEnvironment,
  190. engine.ExecutionContext.PrivateEnvironment);
  191. closure.SetFunctionName(propName, property.Kind == PropertyKind.Get ? "get" : "set");
  192. closure.MakeMethod(obj);
  193. var propDesc = new GetSetPropertyDescriptor(
  194. get: property.Kind == PropertyKind.Get ? closure : null,
  195. set: property.Kind == PropertyKind.Set ? closure : null,
  196. PropertyFlag.Enumerable | PropertyFlag.Configurable);
  197. obj.DefinePropertyOrThrow(propName, propDesc);
  198. }
  199. }
  200. return obj;
  201. }
  202. internal sealed class JintEmptyObjectExpression : JintExpression
  203. {
  204. public static JintEmptyObjectExpression Instance = new(new ObjectExpression(NodeList.From(Array.Empty<Node>())));
  205. private JintEmptyObjectExpression(Expression expression) : base(expression)
  206. {
  207. }
  208. protected override object EvaluateInternal(EvaluationContext context)
  209. {
  210. return new JsObject(context.Engine);
  211. }
  212. public override JsValue GetValue(EvaluationContext context)
  213. {
  214. return new JsObject(context.Engine);
  215. }
  216. }
  217. }