JintObjectExpression.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using Esprima.Ast;
  2. using Jint.Collections;
  3. using Jint.Native;
  4. using Jint.Native.Function;
  5. using Jint.Native.Object;
  6. using Jint.Runtime.Descriptors;
  7. namespace Jint.Runtime.Interpreter.Expressions
  8. {
  9. /// <summary>
  10. /// https://tc39.es/ecma262/#sec-object-initializer
  11. /// </summary>
  12. internal sealed class JintObjectExpression : JintExpression
  13. {
  14. private JintExpression[] _valueExpressions = Array.Empty<JintExpression>();
  15. private ObjectProperty?[] _properties = Array.Empty<ObjectProperty>();
  16. // check if we can do a shortcut when all are object properties
  17. // and don't require duplicate checking
  18. private bool _canBuildFast;
  19. private bool _initialized;
  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(function);
  44. return _functionDefinition;
  45. }
  46. }
  47. private JintObjectExpression(ObjectExpression expression) : base(expression)
  48. {
  49. }
  50. public static JintExpression Build(ObjectExpression expression)
  51. {
  52. return expression.Properties.Count == 0
  53. ? JintEmptyObjectExpression.Instance
  54. : new JintObjectExpression(expression);
  55. }
  56. private void Initialize()
  57. {
  58. _canBuildFast = true;
  59. var expression = (ObjectExpression) _expression;
  60. ref readonly var properties = ref expression.Properties;
  61. _valueExpressions = new JintExpression[properties.Count];
  62. _properties = new ObjectProperty[properties.Count];
  63. for (var i = 0; i < _properties.Length; i++)
  64. {
  65. string? propName = null;
  66. var property = 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 &= !string.Equals(propName, "__proto__", StringComparison.Ordinal);
  77. }
  78. _properties[i] = new ObjectProperty(propName, p);
  79. if (p.Kind is PropertyKind.Init)
  80. {
  81. var propertyValue = p.Value;
  82. _valueExpressions[i] = Build((Expression) 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(spreadElement.Argument);
  95. }
  96. else
  97. {
  98. ExceptionHelper.ThrowArgumentOutOfRangeException("property", "cannot handle property " + property);
  99. }
  100. _canBuildFast &= propName != null;
  101. }
  102. }
  103. protected override object EvaluateInternal(EvaluationContext context)
  104. {
  105. if (!_initialized)
  106. {
  107. Initialize();
  108. _initialized = true;
  109. }
  110. return _canBuildFast
  111. ? BuildObjectFast(context)
  112. : BuildObjectNormal(context);
  113. }
  114. /// <summary>
  115. /// Version that can safely build plain object with only normal init/data fields fast.
  116. /// </summary>
  117. private object BuildObjectFast(EvaluationContext context)
  118. {
  119. var obj = new JsObject(context.Engine);
  120. var properties = new PropertyDictionary(_properties.Length, checkExistingKeys: true);
  121. for (var i = 0; i < _properties.Length; i++)
  122. {
  123. var objectProperty = _properties[i];
  124. var valueExpression = _valueExpressions[i];
  125. var propValue = valueExpression.GetValue(context).Clone();
  126. properties[objectProperty!._key!] = new PropertyDescriptor(propValue, PropertyFlag.ConfigurableEnumerableWritable);
  127. }
  128. obj.SetProperties(properties);
  129. return obj;
  130. }
  131. /// <summary>
  132. /// https://tc39.es/ecma262/#sec-object-initializer-runtime-semantics-propertydefinitionevaluation
  133. /// </summary>
  134. private object BuildObjectNormal(EvaluationContext context)
  135. {
  136. var engine = context.Engine;
  137. var obj = engine.Realm.Intrinsics.Object.Construct(_properties.Length);
  138. for (var i = 0; i < _properties.Length; i++)
  139. {
  140. var objectProperty = _properties[i];
  141. if (objectProperty is null)
  142. {
  143. // spread
  144. if (_valueExpressions[i].GetValue(context) is ObjectInstance source)
  145. {
  146. source.CopyDataProperties(obj, null);
  147. }
  148. continue;
  149. }
  150. var property = objectProperty._value;
  151. if (property.Method)
  152. {
  153. var methodDef = property.DefineMethod(obj);
  154. methodDef.Closure.SetFunctionName(methodDef.Key);
  155. var desc = new PropertyDescriptor(methodDef.Closure, PropertyFlag.ConfigurableEnumerableWritable);
  156. obj.DefinePropertyOrThrow(methodDef.Key, desc);
  157. continue;
  158. }
  159. JsValue? propName = objectProperty.KeyJsString;
  160. if (propName is null)
  161. {
  162. var value = property.TryGetKey(engine);
  163. if (context.IsAbrupt())
  164. {
  165. return value;
  166. }
  167. propName = TypeConverter.ToPropertyKey(value);
  168. }
  169. if (property.Kind == PropertyKind.Init)
  170. {
  171. var expr = _valueExpressions[i];
  172. var completion = expr.GetValue(context);
  173. if (context.IsAbrupt())
  174. {
  175. return completion;
  176. }
  177. var propValue = completion.Clone();
  178. if (string.Equals(objectProperty._key, "__proto__", StringComparison.Ordinal) && !objectProperty._value.Computed && !objectProperty._value.Shorthand)
  179. {
  180. if (propValue.IsObject() || propValue.IsNull())
  181. {
  182. obj.SetPrototypeOf(propValue);
  183. }
  184. continue;
  185. }
  186. if (expr._expression.IsAnonymousFunctionDefinition())
  187. {
  188. var closure = (FunctionInstance) propValue;
  189. closure.SetFunctionName(propName);
  190. }
  191. var propDesc = new PropertyDescriptor(propValue, PropertyFlag.ConfigurableEnumerableWritable);
  192. obj.DefinePropertyOrThrow(propName, propDesc);
  193. }
  194. else if (property.Kind == PropertyKind.Get || property.Kind == PropertyKind.Set)
  195. {
  196. var function = objectProperty.GetFunctionDefinition(engine);
  197. var closure = engine.Realm.Intrinsics.Function.OrdinaryFunctionCreate(
  198. engine.Realm.Intrinsics.Function.PrototypeObject,
  199. function,
  200. function.ThisMode,
  201. engine.ExecutionContext.LexicalEnvironment,
  202. engine.ExecutionContext.PrivateEnvironment);
  203. closure.SetFunctionName(propName, property.Kind == PropertyKind.Get ? "get" : "set");
  204. closure.MakeMethod(obj);
  205. var propDesc = new GetSetPropertyDescriptor(
  206. get: property.Kind == PropertyKind.Get ? closure : null,
  207. set: property.Kind == PropertyKind.Set ? closure : null,
  208. PropertyFlag.Enumerable | PropertyFlag.Configurable);
  209. obj.DefinePropertyOrThrow(propName, propDesc);
  210. }
  211. }
  212. return obj;
  213. }
  214. internal sealed class JintEmptyObjectExpression : JintExpression
  215. {
  216. public static JintEmptyObjectExpression Instance = new(new ObjectExpression(NodeList.Create(System.Linq.Enumerable.Empty<Node>())));
  217. private JintEmptyObjectExpression(Expression expression) : base(expression)
  218. {
  219. }
  220. protected override object EvaluateInternal(EvaluationContext context)
  221. {
  222. return new JsObject(context.Engine);
  223. }
  224. public override JsValue GetValue(EvaluationContext context)
  225. {
  226. return new JsObject(context.Engine);
  227. }
  228. }
  229. }
  230. }