JintObjectExpression.cs 8.5 KB

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