JintObjectExpression.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using Esprima.Ast;
  3. using Jint.Collections;
  4. using Jint.Native;
  5. using Jint.Native.Function;
  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 = ArrayExt.Empty<JintExpression>();
  16. private ObjectProperty[] _properties = ArrayExt.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. var property = (Property) expression.Properties[i];
  50. string propName = null;
  51. if (property.Key is Literal literal)
  52. {
  53. propName = EsprimaExtensions.LiteralKeyToString(literal);
  54. }
  55. if (!property.Computed && property.Key is Identifier identifier)
  56. {
  57. propName = identifier.Name;
  58. }
  59. _properties[i] = new ObjectProperty(propName, property);
  60. if (property.Kind == PropertyKind.Init || property.Kind == PropertyKind.Data)
  61. {
  62. var propertyValue = (Expression) property.Value;
  63. _valueExpressions[i] = Build(_engine, propertyValue);
  64. _canBuildFast &= !propertyValue.IsFunctionWithName();
  65. }
  66. else
  67. {
  68. _canBuildFast = false;
  69. }
  70. _canBuildFast &= propName != null;
  71. }
  72. }
  73. protected override object EvaluateInternal()
  74. {
  75. return _canBuildFast
  76. ? BuildObjectFast()
  77. : BuildObjectNormal();
  78. }
  79. /// <summary>
  80. /// Version that can safely build plain object with only normal init/data fields fast.
  81. /// </summary>
  82. private object BuildObjectFast()
  83. {
  84. var obj = _engine.Object.Construct(0);
  85. if (_properties.Length == 0)
  86. {
  87. return obj;
  88. }
  89. var properties = new PropertyDictionary(_properties.Length, checkExistingKeys: true);
  90. for (var i = 0; i < _properties.Length; i++)
  91. {
  92. var objectProperty = _properties[i];
  93. var valueExpression = _valueExpressions[i];
  94. var propValue = valueExpression.GetValue().Clone();
  95. properties[objectProperty._key] = new PropertyDescriptor(propValue, PropertyFlag.ConfigurableEnumerableWritable);
  96. }
  97. obj.SetProperties(properties);
  98. return obj;
  99. }
  100. private object BuildObjectNormal()
  101. {
  102. var obj = _engine.Object.Construct(_properties.Length);
  103. bool isStrictModeCode = StrictModeScope.IsStrictModeCode;
  104. for (var i = 0; i < _properties.Length; i++)
  105. {
  106. var objectProperty = _properties[i];
  107. var property = objectProperty._value;
  108. var propName = objectProperty.KeyJsString ?? property.GetKey(_engine);
  109. PropertyDescriptor propDesc;
  110. if (property.Kind == PropertyKind.Init || property.Kind == PropertyKind.Data)
  111. {
  112. var expr = _valueExpressions[i];
  113. var propValue = expr.GetValue().Clone();
  114. if (expr._expression.IsFunctionWithName())
  115. {
  116. var functionInstance = (FunctionInstance) propValue;
  117. functionInstance.SetFunctionName(propName);
  118. }
  119. propDesc = new PropertyDescriptor(propValue, PropertyFlag.ConfigurableEnumerableWritable);
  120. }
  121. else if (property.Kind == PropertyKind.Get || property.Kind == PropertyKind.Set)
  122. {
  123. var function = property.Value as IFunction ?? ExceptionHelper.ThrowSyntaxError<IFunction>(_engine);
  124. var functionInstance = new ScriptFunctionInstance(
  125. _engine,
  126. function,
  127. _engine.ExecutionContext.LexicalEnvironment,
  128. isStrictModeCode
  129. );
  130. functionInstance.SetFunctionName(propName);
  131. functionInstance._prototypeDescriptor = null;
  132. propDesc = new GetSetPropertyDescriptor(
  133. get: property.Kind == PropertyKind.Get ? functionInstance : null,
  134. set: property.Kind == PropertyKind.Set ? functionInstance : null,
  135. PropertyFlag.Enumerable | PropertyFlag.Configurable);
  136. }
  137. else
  138. {
  139. return ExceptionHelper.ThrowArgumentOutOfRangeException<object>();
  140. }
  141. obj.DefineOwnProperty(propName, propDesc);
  142. }
  143. return obj;
  144. }
  145. }
  146. }