StringInstance.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.Collections.Generic;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. namespace Jint.Native.String
  6. {
  7. public class StringInstance : ObjectInstance, IPrimitiveInstance
  8. {
  9. internal PropertyDescriptor _length;
  10. public StringInstance(Engine engine)
  11. : base(engine, ObjectClass.String)
  12. {
  13. }
  14. Types IPrimitiveInstance.Type => Types.String;
  15. JsValue IPrimitiveInstance.PrimitiveValue => StringData;
  16. public JsString StringData { get; internal init; }
  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 override PropertyDescriptor GetOwnProperty(JsValue property)
  28. {
  29. if (property == CommonProperties.Infinity)
  30. {
  31. return PropertyDescriptor.Undefined;
  32. }
  33. if (property == CommonProperties.Length)
  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)) == 0)
  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 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. public override List<JsValue> GetOwnPropertyKeys(Types types)
  66. {
  67. var keys = new List<JsValue>(StringData.Length + 1);
  68. for (uint i = 0; i < StringData.Length; ++i)
  69. {
  70. keys.Add(JsString.Create(i));
  71. }
  72. keys.AddRange(base.GetOwnPropertyKeys(Types.String));
  73. keys.AddRange(base.GetOwnPropertyKeys(Types.Symbol));
  74. keys.Add(JsString.LengthString);
  75. return keys;
  76. }
  77. protected internal override void SetOwnProperty(JsValue property, PropertyDescriptor desc)
  78. {
  79. if (property == CommonProperties.Length)
  80. {
  81. _length = desc;
  82. }
  83. else
  84. {
  85. base.SetOwnProperty(property, desc);
  86. }
  87. }
  88. public override bool HasOwnProperty(JsValue property)
  89. {
  90. if (property == CommonProperties.Length)
  91. {
  92. return _length != null;
  93. }
  94. return base.HasOwnProperty(property);
  95. }
  96. public override void RemoveOwnProperty(JsValue property)
  97. {
  98. if (property == CommonProperties.Length)
  99. {
  100. _length = null;
  101. }
  102. base.RemoveOwnProperty(property);
  103. }
  104. }
  105. }