StringInstance.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using Jint.Native.Object;
  2. using Jint.Runtime;
  3. using Jint.Runtime.Descriptors;
  4. namespace Jint.Native.String;
  5. internal class StringInstance : ObjectInstance, IJsPrimitive
  6. {
  7. internal PropertyDescriptor? _length;
  8. public StringInstance(Engine engine, JsString value)
  9. : base(engine, ObjectClass.String)
  10. {
  11. StringData = value;
  12. _length = PropertyDescriptor.AllForbiddenDescriptor.ForNumber(value.Length);
  13. }
  14. Types IJsPrimitive.Type => Types.String;
  15. JsValue IJsPrimitive.PrimitiveValue => StringData;
  16. public JsString StringData { get; }
  17. private static bool IsInt32(double d, out int intValue)
  18. {
  19. if (d >= int.MinValue && d <= int.MaxValue)
  20. {
  21. intValue = (int) d;
  22. return intValue == d;
  23. }
  24. intValue = 0;
  25. return false;
  26. }
  27. public sealed override PropertyDescriptor GetOwnProperty(JsValue property)
  28. {
  29. if (CommonProperties.Infinity.Equals(property))
  30. {
  31. return PropertyDescriptor.Undefined;
  32. }
  33. if (CommonProperties.Length.Equals(property))
  34. {
  35. return _length ?? PropertyDescriptor.Undefined;
  36. }
  37. var desc = base.GetOwnProperty(property);
  38. if (desc != PropertyDescriptor.Undefined)
  39. {
  40. return desc;
  41. }
  42. if ((property._type & (InternalTypes.Number | InternalTypes.Integer | InternalTypes.String)) == InternalTypes.Empty)
  43. {
  44. return PropertyDescriptor.Undefined;
  45. }
  46. var str = StringData.ToString();
  47. var number = TypeConverter.ToNumber(property);
  48. if (!IsInt32(number, out var index) || index < 0 || index >= str.Length)
  49. {
  50. return PropertyDescriptor.Undefined;
  51. }
  52. return new PropertyDescriptor(str[index], PropertyFlag.OnlyEnumerable);
  53. }
  54. public sealed override IEnumerable<KeyValuePair<JsValue, PropertyDescriptor>> GetOwnProperties()
  55. {
  56. foreach (var entry in base.GetOwnProperties())
  57. {
  58. yield return entry;
  59. }
  60. if (_length != null)
  61. {
  62. yield return new KeyValuePair<JsValue, PropertyDescriptor>(CommonProperties.Length, _length);
  63. }
  64. }
  65. internal sealed override IEnumerable<JsValue> GetInitialOwnStringPropertyKeys()
  66. {
  67. yield return JsString.LengthString;
  68. }
  69. public sealed override List<JsValue> GetOwnPropertyKeys(Types types = Types.String | Types.Symbol)
  70. {
  71. var keys = new List<JsValue>(StringData.Length + 1);
  72. if ((types & Types.String) != Types.Empty)
  73. {
  74. for (uint i = 0; i < StringData.Length; ++i)
  75. {
  76. keys.Add(JsString.Create(i));
  77. }
  78. keys.AddRange(base.GetOwnPropertyKeys(Types.String));
  79. }
  80. if ((types & Types.Symbol) != Types.Empty)
  81. {
  82. keys.AddRange(base.GetOwnPropertyKeys(Types.Symbol));
  83. }
  84. return keys;
  85. }
  86. protected internal sealed override void SetOwnProperty(JsValue property, PropertyDescriptor desc)
  87. {
  88. if (CommonProperties.Length.Equals(property))
  89. {
  90. _length = desc;
  91. }
  92. else
  93. {
  94. base.SetOwnProperty(property, desc);
  95. }
  96. }
  97. public sealed override void RemoveOwnProperty(JsValue property)
  98. {
  99. if (CommonProperties.Length.Equals(property))
  100. {
  101. _length = null;
  102. }
  103. base.RemoveOwnProperty(property);
  104. }
  105. }