StringInstance.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 => PrimitiveValue;
  16. public JsString PrimitiveValue { get; set; }
  17. private static bool IsInt(double d)
  18. {
  19. if (d >= long.MinValue && d <= long.MaxValue)
  20. {
  21. var l = (long) d;
  22. return l >= int.MinValue && l <= int.MaxValue;
  23. }
  24. return false;
  25. }
  26. public override PropertyDescriptor GetOwnProperty(in Key propertyName)
  27. {
  28. if (propertyName == KnownKeys.Infinity)
  29. {
  30. return PropertyDescriptor.Undefined;
  31. }
  32. if (propertyName == KnownKeys.Length)
  33. {
  34. return _length ?? PropertyDescriptor.Undefined;
  35. }
  36. var desc = base.GetOwnProperty(propertyName);
  37. if (desc != PropertyDescriptor.Undefined)
  38. {
  39. return desc;
  40. }
  41. if (!TypeConverter.CanBeIndex(propertyName))
  42. {
  43. return PropertyDescriptor.Undefined;
  44. }
  45. var integer = TypeConverter.ToInteger(propertyName);
  46. var str = PrimitiveValue;
  47. var dIndex = integer;
  48. if (!IsInt(dIndex))
  49. return PropertyDescriptor.Undefined;
  50. var index = (int) dIndex;
  51. var len = str.AsStringWithoutTypeCheck().Length;
  52. if (len <= index || index < 0)
  53. {
  54. return PropertyDescriptor.Undefined;
  55. }
  56. var resultStr = TypeConverter.ToString(str.AsStringWithoutTypeCheck()[index]);
  57. return new PropertyDescriptor(resultStr, PropertyFlag.OnlyEnumerable);
  58. }
  59. public override IEnumerable<KeyValuePair<Key, PropertyDescriptor>> GetOwnProperties()
  60. {
  61. if (_length != null)
  62. {
  63. yield return new KeyValuePair<Key, PropertyDescriptor>(KnownKeys.Length, _length);
  64. }
  65. foreach (var entry in base.GetOwnProperties())
  66. {
  67. yield return entry;
  68. }
  69. }
  70. protected internal override void SetOwnProperty(in Key propertyName, PropertyDescriptor desc)
  71. {
  72. if (propertyName == KnownKeys.Length)
  73. {
  74. _length = desc;
  75. }
  76. else
  77. {
  78. base.SetOwnProperty(propertyName, desc);
  79. }
  80. }
  81. public override bool HasOwnProperty(in Key propertyName)
  82. {
  83. if (propertyName == KnownKeys.Length)
  84. {
  85. return _length != null;
  86. }
  87. return base.HasOwnProperty(propertyName);
  88. }
  89. public override void RemoveOwnProperty(in Key propertyName)
  90. {
  91. if (propertyName == KnownKeys.Length)
  92. {
  93. _length = null;
  94. }
  95. base.RemoveOwnProperty(propertyName);
  96. }
  97. }
  98. }