JintObjectExpression.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. 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. }
  32. public JintObjectExpression(Engine engine, ObjectExpression expression) : base(engine, expression)
  33. {
  34. _initialized = false;
  35. }
  36. protected override void Initialize()
  37. {
  38. _canBuildFast = true;
  39. var expression = (ObjectExpression) _expression;
  40. if (expression.Properties.Count == 0)
  41. {
  42. // empty object initializer
  43. return;
  44. }
  45. _valueExpressions = new JintExpression[expression.Properties.Count];
  46. _properties = new ObjectProperty[expression.Properties.Count];
  47. for (var i = 0; i < _properties.Length; i++)
  48. {
  49. string? propName = null;
  50. var property = expression.Properties[i];
  51. if (property is Property p)
  52. {
  53. if (p.Key is Literal literal)
  54. {
  55. propName = EsprimaExtensions.LiteralKeyToString(literal);
  56. }
  57. if (!p.Computed && p.Key is Identifier identifier)
  58. {
  59. propName = identifier.Name;
  60. }
  61. _properties[i] = new ObjectProperty(propName, p);
  62. if (p.Kind == PropertyKind.Init || p.Kind == PropertyKind.Data)
  63. {
  64. var propertyValue = p.Value;
  65. _valueExpressions[i] = Build(_engine, propertyValue);
  66. _canBuildFast &= !propertyValue.IsFunctionWithName();
  67. }
  68. else
  69. {
  70. _canBuildFast = false;
  71. }
  72. }
  73. else if (property is SpreadElement spreadElement)
  74. {
  75. _canBuildFast = false;
  76. _properties[i] = null;
  77. _valueExpressions[i] = Build(_engine, spreadElement.Argument);
  78. }
  79. else
  80. {
  81. ExceptionHelper.ThrowArgumentOutOfRangeException("property", "cannot handle property " + property);
  82. }
  83. _canBuildFast &= propName != null;
  84. }
  85. }
  86. protected override object EvaluateInternal()
  87. {
  88. return _canBuildFast
  89. ? BuildObjectFast()
  90. : BuildObjectNormal();
  91. }
  92. /// <summary>
  93. /// Version that can safely build plain object with only normal init/data fields fast.
  94. /// </summary>
  95. private object BuildObjectFast()
  96. {
  97. var obj = _engine.Object.Construct(0);
  98. if (_properties.Length == 0)
  99. {
  100. return obj;
  101. }
  102. var properties = new PropertyDictionary(_properties.Length, checkExistingKeys: true);
  103. for (var i = 0; i < _properties.Length; i++)
  104. {
  105. var objectProperty = _properties[i];
  106. var valueExpression = _valueExpressions[i];
  107. var propValue = valueExpression.GetValue().Clone();
  108. properties[objectProperty!._key] = new PropertyDescriptor(propValue, PropertyFlag.ConfigurableEnumerableWritable);
  109. }
  110. obj.SetProperties(properties);
  111. return obj;
  112. }
  113. /// <summary>
  114. /// https://tc39.es/ecma262/#sec-object-initializer-runtime-semantics-propertydefinitionevaluation
  115. /// </summary>
  116. private object BuildObjectNormal()
  117. {
  118. var obj = _engine.Object.Construct(_properties.Length);
  119. bool isStrictModeCode = _engine._isStrict || StrictModeScope.IsStrictModeCode;
  120. for (var i = 0; i < _properties.Length; i++)
  121. {
  122. var objectProperty = _properties[i];
  123. if (objectProperty is null)
  124. {
  125. // spread
  126. if (_valueExpressions[i].GetValue() is ObjectInstance source)
  127. {
  128. source.CopyDataProperties(obj, null);
  129. }
  130. continue;
  131. }
  132. var property = objectProperty._value;
  133. if (property.Method)
  134. {
  135. var methodDef = property.DefineMethod(obj);
  136. methodDef.Closure.SetFunctionName(methodDef.Key);
  137. var desc = new PropertyDescriptor(methodDef.Closure, PropertyFlag.ConfigurableEnumerableWritable);
  138. obj.DefinePropertyOrThrow(methodDef.Key, desc);
  139. continue;
  140. }
  141. var propName = objectProperty.KeyJsString ?? property.GetKey(_engine);
  142. if (property.Kind == PropertyKind.Init || property.Kind == PropertyKind.Data)
  143. {
  144. var expr = _valueExpressions[i];
  145. JsValue propValue = expr.GetValue().Clone();
  146. if (expr._expression.IsFunctionWithName())
  147. {
  148. var closure = (FunctionInstance) propValue;
  149. closure.SetFunctionName(propName);
  150. }
  151. var propDesc = new PropertyDescriptor(propValue, PropertyFlag.ConfigurableEnumerableWritable);
  152. obj.DefinePropertyOrThrow(propName, propDesc);
  153. }
  154. else if (property.Kind == PropertyKind.Get || property.Kind == PropertyKind.Set)
  155. {
  156. var function = property.Value as IFunction ?? ExceptionHelper.ThrowSyntaxError<IFunction>(_engine);
  157. var closure = new ScriptFunctionInstance(
  158. _engine,
  159. function,
  160. _engine.ExecutionContext.LexicalEnvironment,
  161. isStrictModeCode);
  162. closure.SetFunctionName(propName, property.Kind == PropertyKind.Get ? "get" : "set");
  163. var propDesc = new GetSetPropertyDescriptor(
  164. get: property.Kind == PropertyKind.Get ? closure : null,
  165. set: property.Kind == PropertyKind.Set ? closure : null,
  166. PropertyFlag.Enumerable | PropertyFlag.Configurable);
  167. obj.DefinePropertyOrThrow(propName, propDesc);
  168. }
  169. }
  170. return obj;
  171. }
  172. }
  173. }