JintObjectExpression.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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(Engine engine, ObjectExpression expression) : base(engine, expression)
  48. {
  49. _initialized = false;
  50. }
  51. protected override void Initialize()
  52. {
  53. _canBuildFast = true;
  54. var expression = (ObjectExpression) _expression;
  55. if (expression.Properties.Count == 0)
  56. {
  57. // empty object initializer
  58. return;
  59. }
  60. _valueExpressions = new JintExpression[expression.Properties.Count];
  61. _properties = new ObjectProperty[expression.Properties.Count];
  62. for (var i = 0; i < _properties.Length; i++)
  63. {
  64. string? propName = null;
  65. var property = expression.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. }
  76. _properties[i] = new ObjectProperty(propName, p);
  77. if (p.Kind == PropertyKind.Init || p.Kind == PropertyKind.Data)
  78. {
  79. var propertyValue = p.Value;
  80. _valueExpressions[i] = Build(_engine, 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] = Build(_engine, spreadElement.Argument);
  93. }
  94. else
  95. {
  96. ExceptionHelper.ThrowArgumentOutOfRangeException("property", "cannot handle property " + property);
  97. }
  98. _canBuildFast &= propName != null;
  99. }
  100. }
  101. protected override object EvaluateInternal()
  102. {
  103. return _canBuildFast
  104. ? BuildObjectFast()
  105. : BuildObjectNormal();
  106. }
  107. /// <summary>
  108. /// Version that can safely build plain object with only normal init/data fields fast.
  109. /// </summary>
  110. private object BuildObjectFast()
  111. {
  112. var obj = _engine.Realm.Intrinsics.Object.Construct(0);
  113. if (_properties.Length == 0)
  114. {
  115. return obj;
  116. }
  117. var properties = new PropertyDictionary(_properties.Length, checkExistingKeys: true);
  118. for (var i = 0; i < _properties.Length; i++)
  119. {
  120. var objectProperty = _properties[i];
  121. var valueExpression = _valueExpressions[i];
  122. var propValue = valueExpression.GetValue().Clone();
  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()
  132. {
  133. var obj = _engine.Realm.Intrinsics.Object.Construct(_properties.Length);
  134. for (var i = 0; i < _properties.Length; i++)
  135. {
  136. var objectProperty = _properties[i];
  137. if (objectProperty is null)
  138. {
  139. // spread
  140. if (_valueExpressions[i].GetValue() is ObjectInstance source)
  141. {
  142. source.CopyDataProperties(obj, null);
  143. }
  144. continue;
  145. }
  146. var property = objectProperty._value;
  147. if (property.Method)
  148. {
  149. var methodDef = property.DefineMethod(obj);
  150. methodDef.Closure.SetFunctionName(methodDef.Key);
  151. var desc = new PropertyDescriptor(methodDef.Closure, PropertyFlag.ConfigurableEnumerableWritable);
  152. obj.DefinePropertyOrThrow(methodDef.Key, desc);
  153. continue;
  154. }
  155. var propName = objectProperty.KeyJsString ?? property.GetKey(_engine);
  156. if (property.Kind == PropertyKind.Init || property.Kind == PropertyKind.Data)
  157. {
  158. var expr = _valueExpressions[i];
  159. JsValue propValue = expr.GetValue().Clone();
  160. if (expr._expression.IsFunctionDefinition())
  161. {
  162. var closure = (FunctionInstance) propValue;
  163. closure.SetFunctionName(propName);
  164. }
  165. var propDesc = new PropertyDescriptor(propValue, PropertyFlag.ConfigurableEnumerableWritable);
  166. obj.DefinePropertyOrThrow(propName, propDesc);
  167. }
  168. else if (property.Kind == PropertyKind.Get || property.Kind == PropertyKind.Set)
  169. {
  170. var function = objectProperty.GetFunctionDefinition(_engine);
  171. var closure = new ScriptFunctionInstance(
  172. _engine,
  173. function,
  174. _engine.ExecutionContext.LexicalEnvironment,
  175. function.ThisMode);
  176. closure.SetFunctionName(propName, property.Kind == PropertyKind.Get ? "get" : "set");
  177. closure.MakeMethod(obj);
  178. var propDesc = new GetSetPropertyDescriptor(
  179. get: property.Kind == PropertyKind.Get ? closure : null,
  180. set: property.Kind == PropertyKind.Set ? closure : null,
  181. PropertyFlag.Enumerable | PropertyFlag.Configurable);
  182. obj.DefinePropertyOrThrow(propName, propDesc);
  183. }
  184. }
  185. return obj;
  186. }
  187. }
  188. }