JintObjectExpression.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #nullable enable
  2. using Esprima.Ast;
  3. using Jint.Collections;
  4. using Jint.Native;
  5. using Jint.Native.Function;
  6. using Jint.Native.Object;
  7. using Jint.Runtime.Descriptors;
  8. namespace Jint.Runtime.Interpreter.Expressions
  9. {
  10. /// <summary>
  11. /// https://tc39.es/ecma262/#sec-object-initializer
  12. /// </summary>
  13. internal sealed class JintObjectExpression : JintExpression
  14. {
  15. private JintExpression[] _valueExpressions = System.Array.Empty<JintExpression>();
  16. private ObjectProperty?[] _properties = System.Array.Empty<ObjectProperty>();
  17. // check if we can do a shortcut when all are object properties
  18. // and don't require duplicate checking
  19. private bool _canBuildFast;
  20. private class ObjectProperty
  21. {
  22. internal readonly string? _key;
  23. private JsString? _keyJsString;
  24. internal readonly Property _value;
  25. private JintFunctionDefinition? _functionDefinition;
  26. public ObjectProperty(string? key, Property property)
  27. {
  28. _key = key;
  29. _value = property;
  30. }
  31. public JsString? KeyJsString => _keyJsString ??= _key != null ? JsString.Create(_key) : null;
  32. public JintFunctionDefinition GetFunctionDefinition(Engine engine)
  33. {
  34. if (_functionDefinition is not null)
  35. {
  36. return _functionDefinition;
  37. }
  38. var function = _value.Value as IFunction;
  39. if (function is null)
  40. {
  41. ExceptionHelper.ThrowSyntaxError(engine.Realm);
  42. }
  43. _functionDefinition = new JintFunctionDefinition(engine, function);
  44. return _functionDefinition;
  45. }
  46. }
  47. public JintObjectExpression(ObjectExpression expression) : base(expression)
  48. {
  49. _initialized = false;
  50. }
  51. protected override void Initialize(EvaluationContext context)
  52. {
  53. _canBuildFast = true;
  54. var expression = (ObjectExpression) _expression;
  55. if (expression.Properties.Count == 0)
  56. {
  57. // empty object initializer
  58. return;
  59. }
  60. var engine = context.Engine;
  61. _valueExpressions = new JintExpression[expression.Properties.Count];
  62. _properties = new ObjectProperty[expression.Properties.Count];
  63. for (var i = 0; i < _properties.Length; i++)
  64. {
  65. string? propName = null;
  66. var property = expression.Properties[i];
  67. if (property is Property p)
  68. {
  69. if (p.Key is Literal literal)
  70. {
  71. propName = EsprimaExtensions.LiteralKeyToString(literal);
  72. }
  73. if (!p.Computed && p.Key is Identifier identifier)
  74. {
  75. propName = identifier.Name;
  76. }
  77. _properties[i] = new ObjectProperty(propName, p);
  78. if (p.Kind == PropertyKind.Init || p.Kind == PropertyKind.Data)
  79. {
  80. var propertyValue = p.Value;
  81. _valueExpressions[i] = Build(engine, 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(engine, spreadElement.Argument);
  94. }
  95. else
  96. {
  97. ExceptionHelper.ThrowArgumentOutOfRangeException("property", "cannot handle property " + property);
  98. }
  99. _canBuildFast &= propName != null;
  100. }
  101. }
  102. protected override ExpressionResult EvaluateInternal(EvaluationContext context)
  103. {
  104. return _canBuildFast
  105. ? BuildObjectFast(context)
  106. : BuildObjectNormal(context);
  107. }
  108. /// <summary>
  109. /// Version that can safely build plain object with only normal init/data fields fast.
  110. /// </summary>
  111. private ExpressionResult BuildObjectFast(EvaluationContext context)
  112. {
  113. var obj = context.Engine.Realm.Intrinsics.Object.Construct(0);
  114. if (_properties.Length == 0)
  115. {
  116. return NormalCompletion(obj);
  117. }
  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 valueExpression = _valueExpressions[i];
  123. var propValue = valueExpression.GetValue(context).Value!.Clone();
  124. properties[objectProperty!._key] = new PropertyDescriptor(propValue, PropertyFlag.ConfigurableEnumerableWritable);
  125. }
  126. obj.SetProperties(properties);
  127. return NormalCompletion(obj);
  128. }
  129. /// <summary>
  130. /// https://tc39.es/ecma262/#sec-object-initializer-runtime-semantics-propertydefinitionevaluation
  131. /// </summary>
  132. private ExpressionResult 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[i].GetValue(context).Value is ObjectInstance source)
  143. {
  144. source.CopyDataProperties(obj, null);
  145. }
  146. continue;
  147. }
  148. var property = objectProperty._value;
  149. if (property.Method)
  150. {
  151. var methodDef = property.DefineMethod(obj);
  152. methodDef.Closure.SetFunctionName(methodDef.Key);
  153. var desc = new PropertyDescriptor(methodDef.Closure, PropertyFlag.ConfigurableEnumerableWritable);
  154. obj.DefinePropertyOrThrow(methodDef.Key, desc);
  155. continue;
  156. }
  157. JsValue? propName = objectProperty.KeyJsString;
  158. if (propName is null)
  159. {
  160. propName = TypeConverter.ToPropertyKey(property.GetKey(engine));
  161. }
  162. if (property.Kind == PropertyKind.Init || property.Kind == PropertyKind.Data)
  163. {
  164. var expr = _valueExpressions[i];
  165. var completion = expr.GetValue(context);
  166. if (completion.IsAbrupt())
  167. {
  168. return completion;
  169. }
  170. var propValue = completion.Value!.Clone();
  171. if (expr._expression.IsFunctionDefinition())
  172. {
  173. var closure = (FunctionInstance) propValue;
  174. closure.SetFunctionName(propName);
  175. }
  176. var propDesc = new PropertyDescriptor(propValue, PropertyFlag.ConfigurableEnumerableWritable);
  177. obj.DefinePropertyOrThrow(propName, propDesc);
  178. }
  179. else if (property.Kind == PropertyKind.Get || property.Kind == PropertyKind.Set)
  180. {
  181. var function = objectProperty.GetFunctionDefinition(engine);
  182. var closure = new ScriptFunctionInstance(
  183. engine,
  184. function,
  185. engine.ExecutionContext.LexicalEnvironment,
  186. function.ThisMode);
  187. closure.SetFunctionName(propName, property.Kind == PropertyKind.Get ? "get" : "set");
  188. closure.MakeMethod(obj);
  189. var propDesc = new GetSetPropertyDescriptor(
  190. get: property.Kind == PropertyKind.Get ? closure : null,
  191. set: property.Kind == PropertyKind.Set ? closure : null,
  192. PropertyFlag.Enumerable | PropertyFlag.Configurable);
  193. obj.DefinePropertyOrThrow(propName, propDesc);
  194. }
  195. }
  196. return NormalCompletion(obj);
  197. }
  198. }
  199. }