JintObjectExpression.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 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. _initialized = false;
  49. }
  50. public static JintExpression Build(ObjectExpression expression)
  51. {
  52. return expression.Properties.Count == 0
  53. ? JintEmptyObjectExpression.Instance
  54. : new JintObjectExpression(expression);
  55. }
  56. protected override void Initialize(EvaluationContext context)
  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 &= propName != "__proto__";
  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. 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 object BuildObjectFast(EvaluationContext context)
  113. {
  114. var obj = new JsObject(context.Engine);
  115. var properties = new PropertyDictionary(_properties.Length, checkExistingKeys: true);
  116. for (var i = 0; i < _properties.Length; i++)
  117. {
  118. var objectProperty = _properties[i];
  119. var valueExpression = _valueExpressions[i];
  120. var propValue = valueExpression.GetValue(context).Clone();
  121. properties[objectProperty!._key!] = new PropertyDescriptor(propValue, PropertyFlag.ConfigurableEnumerableWritable);
  122. }
  123. obj.SetProperties(properties);
  124. return obj;
  125. }
  126. /// <summary>
  127. /// https://tc39.es/ecma262/#sec-object-initializer-runtime-semantics-propertydefinitionevaluation
  128. /// </summary>
  129. private object BuildObjectNormal(EvaluationContext context)
  130. {
  131. var engine = context.Engine;
  132. var obj = engine.Realm.Intrinsics.Object.Construct(_properties.Length);
  133. for (var i = 0; i < _properties.Length; i++)
  134. {
  135. var objectProperty = _properties[i];
  136. if (objectProperty is null)
  137. {
  138. // spread
  139. if (_valueExpressions[i].GetValue(context) is ObjectInstance source)
  140. {
  141. source.CopyDataProperties(obj, null);
  142. }
  143. continue;
  144. }
  145. var property = objectProperty._value;
  146. if (property.Method)
  147. {
  148. var methodDef = property.DefineMethod(obj);
  149. methodDef.Closure.SetFunctionName(methodDef.Key);
  150. var desc = new PropertyDescriptor(methodDef.Closure, PropertyFlag.ConfigurableEnumerableWritable);
  151. obj.DefinePropertyOrThrow(methodDef.Key, desc);
  152. continue;
  153. }
  154. JsValue? propName = objectProperty.KeyJsString;
  155. if (propName is null)
  156. {
  157. var value = property.TryGetKey(engine);
  158. if (context.IsAbrupt())
  159. {
  160. return value;
  161. }
  162. propName = TypeConverter.ToPropertyKey(value);
  163. }
  164. if (property.Kind == PropertyKind.Init)
  165. {
  166. var expr = _valueExpressions[i];
  167. var completion = expr.GetValue(context);
  168. if (context.IsAbrupt())
  169. {
  170. return completion;
  171. }
  172. var propValue = completion.Clone();
  173. if (expr._expression.IsFunctionDefinition())
  174. {
  175. var closure = (FunctionInstance) propValue;
  176. closure.SetFunctionName(propName);
  177. }
  178. if (objectProperty._key == "__proto__")
  179. {
  180. if (propValue.IsObject() || propValue.IsNull())
  181. {
  182. obj.SetPrototypeOf(propValue);
  183. continue;
  184. }
  185. }
  186. var propDesc = new PropertyDescriptor(propValue, PropertyFlag.ConfigurableEnumerableWritable);
  187. obj.DefinePropertyOrThrow(propName, propDesc);
  188. }
  189. else if (property.Kind == PropertyKind.Get || property.Kind == PropertyKind.Set)
  190. {
  191. var function = objectProperty.GetFunctionDefinition(engine);
  192. var closure = new ScriptFunctionInstance(
  193. engine,
  194. function,
  195. engine.ExecutionContext.LexicalEnvironment,
  196. function.ThisMode);
  197. closure.SetFunctionName(propName, property.Kind == PropertyKind.Get ? "get" : "set");
  198. closure.MakeMethod(obj);
  199. var propDesc = new GetSetPropertyDescriptor(
  200. get: property.Kind == PropertyKind.Get ? closure : null,
  201. set: property.Kind == PropertyKind.Set ? closure : null,
  202. PropertyFlag.Enumerable | PropertyFlag.Configurable);
  203. obj.DefinePropertyOrThrow(propName, propDesc);
  204. }
  205. }
  206. return obj;
  207. }
  208. internal sealed class JintEmptyObjectExpression : JintExpression
  209. {
  210. public static JintEmptyObjectExpression Instance = new(new ObjectExpression(NodeList.Create(System.Linq.Enumerable.Empty<Node>())));
  211. private JintEmptyObjectExpression(Expression expression) : base(expression)
  212. {
  213. }
  214. protected override object EvaluateInternal(EvaluationContext context)
  215. {
  216. return new JsObject(context.Engine);
  217. }
  218. public override JsValue GetValue(EvaluationContext context)
  219. {
  220. return new JsObject(context.Engine);
  221. }
  222. }
  223. }
  224. }