JintObjectExpression.cs 9.6 KB

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