JintObjectExpression.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. public JintObjectExpression(ObjectExpression expression) : base(expression)
  47. {
  48. _initialized = false;
  49. }
  50. protected override void Initialize(EvaluationContext context)
  51. {
  52. _canBuildFast = true;
  53. var expression = (ObjectExpression) _expression;
  54. if (expression.Properties.Count == 0)
  55. {
  56. // empty object initializer
  57. return;
  58. }
  59. var engine = context.Engine;
  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. _canBuildFast &= propName != "__proto__";
  76. }
  77. _properties[i] = new ObjectProperty(propName, p);
  78. if (p.Kind is PropertyKind.Init)
  79. {
  80. var propertyValue = p.Value;
  81. _valueExpressions[i] = Build(engine, (Expression) 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 object 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 object BuildObjectFast(EvaluationContext context)
  112. {
  113. var obj = context.Engine.Realm.Intrinsics.Object.Construct(0);
  114. if (_properties.Length == 0)
  115. {
  116. return 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).Clone();
  124. properties[objectProperty!._key!] = new PropertyDescriptor(propValue, PropertyFlag.ConfigurableEnumerableWritable);
  125. }
  126. obj.SetProperties(properties);
  127. return obj;
  128. }
  129. /// <summary>
  130. /// https://tc39.es/ecma262/#sec-object-initializer-runtime-semantics-propertydefinitionevaluation
  131. /// </summary>
  132. private object 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) 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. var value = property.TryGetKey(engine);
  161. if (context.IsAbrupt())
  162. {
  163. return value;
  164. }
  165. propName = TypeConverter.ToPropertyKey(value);
  166. }
  167. if (property.Kind == PropertyKind.Init)
  168. {
  169. var expr = _valueExpressions[i];
  170. var completion = expr.GetValue(context);
  171. if (context.IsAbrupt())
  172. {
  173. return completion;
  174. }
  175. var propValue = completion.Clone();
  176. if (expr._expression.IsFunctionDefinition())
  177. {
  178. var closure = (FunctionInstance) propValue;
  179. closure.SetFunctionName(propName);
  180. }
  181. if (objectProperty._key == "__proto__")
  182. {
  183. if (propValue.IsObject() || propValue.IsNull())
  184. {
  185. obj.SetPrototypeOf(propValue);
  186. continue;
  187. }
  188. }
  189. var propDesc = new PropertyDescriptor(propValue, PropertyFlag.ConfigurableEnumerableWritable);
  190. obj.DefinePropertyOrThrow(propName, propDesc);
  191. }
  192. else if (property.Kind == PropertyKind.Get || property.Kind == PropertyKind.Set)
  193. {
  194. var function = objectProperty.GetFunctionDefinition(engine);
  195. var closure = new ScriptFunctionInstance(
  196. engine,
  197. function,
  198. engine.ExecutionContext.LexicalEnvironment,
  199. function.ThisMode);
  200. closure.SetFunctionName(propName, property.Kind == PropertyKind.Get ? "get" : "set");
  201. closure.MakeMethod(obj);
  202. var propDesc = new GetSetPropertyDescriptor(
  203. get: property.Kind == PropertyKind.Get ? closure : null,
  204. set: property.Kind == PropertyKind.Set ? closure : null,
  205. PropertyFlag.Enumerable | PropertyFlag.Configurable);
  206. obj.DefinePropertyOrThrow(propName, propDesc);
  207. }
  208. }
  209. return obj;
  210. }
  211. }
  212. }