JintObjectExpression.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 sealed 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. _canBuildFast &= propName != "__proto__";
  77. }
  78. _properties[i] = new ObjectProperty(propName, p);
  79. if (p.Kind == PropertyKind.Init || p.Kind == PropertyKind.Data)
  80. {
  81. var propertyValue = p.Value;
  82. _valueExpressions[i] = Build(engine, propertyValue);
  83. _canBuildFast &= !propertyValue.IsFunctionDefinition();
  84. }
  85. else
  86. {
  87. _canBuildFast = false;
  88. }
  89. }
  90. else if (property is SpreadElement spreadElement)
  91. {
  92. _canBuildFast = false;
  93. _properties[i] = null;
  94. _valueExpressions[i] = Build(engine, spreadElement.Argument);
  95. }
  96. else
  97. {
  98. ExceptionHelper.ThrowArgumentOutOfRangeException("property", "cannot handle property " + property);
  99. }
  100. _canBuildFast &= propName != null;
  101. }
  102. }
  103. protected override ExpressionResult EvaluateInternal(EvaluationContext context)
  104. {
  105. return _canBuildFast
  106. ? BuildObjectFast(context)
  107. : BuildObjectNormal(context);
  108. }
  109. /// <summary>
  110. /// Version that can safely build plain object with only normal init/data fields fast.
  111. /// </summary>
  112. private ExpressionResult BuildObjectFast(EvaluationContext context)
  113. {
  114. var obj = context.Engine.Realm.Intrinsics.Object.Construct(0);
  115. if (_properties.Length == 0)
  116. {
  117. return NormalCompletion(obj);
  118. }
  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).Value!.Clone();
  125. properties[objectProperty!._key] = new PropertyDescriptor(propValue, PropertyFlag.ConfigurableEnumerableWritable);
  126. }
  127. obj.SetProperties(properties);
  128. return NormalCompletion(obj);
  129. }
  130. /// <summary>
  131. /// https://tc39.es/ecma262/#sec-object-initializer-runtime-semantics-propertydefinitionevaluation
  132. /// </summary>
  133. private ExpressionResult 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).Value is ObjectInstance source)
  144. {
  145. source.CopyDataProperties(obj, null);
  146. }
  147. continue;
  148. }
  149. var property = objectProperty._value;
  150. if (property.Method)
  151. {
  152. var methodDef = property.DefineMethod(obj);
  153. methodDef.Closure.SetFunctionName(methodDef.Key);
  154. var desc = new PropertyDescriptor(methodDef.Closure, PropertyFlag.ConfigurableEnumerableWritable);
  155. obj.DefinePropertyOrThrow(methodDef.Key, desc);
  156. continue;
  157. }
  158. JsValue? propName = objectProperty.KeyJsString;
  159. if (propName is null)
  160. {
  161. var completion = property.TryGetKey(engine);
  162. if (completion.IsAbrupt())
  163. {
  164. return completion;
  165. }
  166. propName = TypeConverter.ToPropertyKey(completion.Value);
  167. }
  168. if (property.Kind == PropertyKind.Init || property.Kind == PropertyKind.Data)
  169. {
  170. var expr = _valueExpressions[i];
  171. var completion = expr.GetValue(context);
  172. if (completion.IsAbrupt())
  173. {
  174. return completion;
  175. }
  176. var propValue = completion.Value!.Clone();
  177. if (expr._expression.IsFunctionDefinition())
  178. {
  179. var closure = (FunctionInstance) propValue;
  180. closure.SetFunctionName(propName);
  181. }
  182. if (objectProperty._key == "__proto__")
  183. {
  184. if (propValue.IsObject() || propValue.IsNull())
  185. {
  186. obj.SetPrototypeOf(propValue);
  187. continue;
  188. }
  189. }
  190. var propDesc = new PropertyDescriptor(propValue, PropertyFlag.ConfigurableEnumerableWritable);
  191. obj.DefinePropertyOrThrow(propName, propDesc);
  192. }
  193. else if (property.Kind == PropertyKind.Get || property.Kind == PropertyKind.Set)
  194. {
  195. var function = objectProperty.GetFunctionDefinition(engine);
  196. var closure = new ScriptFunctionInstance(
  197. engine,
  198. function,
  199. engine.ExecutionContext.LexicalEnvironment,
  200. function.ThisMode);
  201. closure.SetFunctionName(propName, property.Kind == PropertyKind.Get ? "get" : "set");
  202. closure.MakeMethod(obj);
  203. var propDesc = new GetSetPropertyDescriptor(
  204. get: property.Kind == PropertyKind.Get ? closure : null,
  205. set: property.Kind == PropertyKind.Set ? closure : null,
  206. PropertyFlag.Enumerable | PropertyFlag.Configurable);
  207. obj.DefinePropertyOrThrow(propName, propDesc);
  208. }
  209. }
  210. return NormalCompletion(obj);
  211. }
  212. }
  213. }