JintObjectExpression.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. using Jint.Runtime.Descriptors.Specialized;
  8. namespace Jint.Runtime.Interpreter.Expressions
  9. {
  10. /// <summary>
  11. /// http://www.ecma-international.org/ecma-262/5.1/#sec-11.1.5
  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 = (Expression) 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. private object BuildObjectNormal()
  114. {
  115. var obj = _engine.Object.Construct(_properties.Length);
  116. bool isStrictModeCode = _engine._isStrict || StrictModeScope.IsStrictModeCode;
  117. for (var i = 0; i < _properties.Length; i++)
  118. {
  119. var objectProperty = _properties[i];
  120. if (objectProperty is null)
  121. {
  122. // spread
  123. if (_valueExpressions[i].GetValue() is ObjectInstance source)
  124. {
  125. source.CopyDataProperties(obj, null);
  126. }
  127. continue;
  128. }
  129. var property = objectProperty._value;
  130. var propName = objectProperty.KeyJsString ?? property.GetKey(_engine);
  131. PropertyDescriptor propDesc;
  132. if (property.Kind == PropertyKind.Init || property.Kind == PropertyKind.Data)
  133. {
  134. var expr = _valueExpressions[i];
  135. var propValue = expr.GetValue().Clone();
  136. if (expr._expression.IsFunctionWithName())
  137. {
  138. var functionInstance = (FunctionInstance) propValue;
  139. functionInstance.SetFunctionName(propName);
  140. }
  141. propDesc = new PropertyDescriptor(propValue, PropertyFlag.ConfigurableEnumerableWritable);
  142. }
  143. else if (property.Kind == PropertyKind.Get || property.Kind == PropertyKind.Set)
  144. {
  145. var function = property.Value as IFunction ?? ExceptionHelper.ThrowSyntaxError<IFunction>(_engine);
  146. var functionInstance = new ScriptFunctionInstance(
  147. _engine,
  148. function,
  149. _engine.ExecutionContext.LexicalEnvironment,
  150. isStrictModeCode
  151. );
  152. functionInstance.SetFunctionName(propName);
  153. functionInstance._prototypeDescriptor = null;
  154. propDesc = new GetSetPropertyDescriptor(
  155. get: property.Kind == PropertyKind.Get ? functionInstance : null,
  156. set: property.Kind == PropertyKind.Set ? functionInstance : null,
  157. PropertyFlag.Enumerable | PropertyFlag.Configurable);
  158. }
  159. else
  160. {
  161. return ExceptionHelper.ThrowArgumentOutOfRangeException<object>();
  162. }
  163. obj.DefineOwnProperty(propName, propDesc);
  164. }
  165. return obj;
  166. }
  167. }
  168. }