StringInstance.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  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, IPrimitiveType
  8. {
  9. private readonly Engine _engine;
  10. public StringInstance(Engine engine)
  11. : base(engine)
  12. {
  13. _engine = engine;
  14. }
  15. public override string Class
  16. {
  17. get
  18. {
  19. return "String";
  20. }
  21. }
  22. Types IPrimitiveType.Type
  23. {
  24. get { return Types.Boolean; }
  25. }
  26. object IPrimitiveType.PrimitiveValue
  27. {
  28. get { return PrimitiveValue; }
  29. }
  30. public string PrimitiveValue { get; set; }
  31. public override PropertyDescriptor GetOwnProperty(string propertyName)
  32. {
  33. var desc = base.GetOwnProperty(propertyName);
  34. if (desc != PropertyDescriptor.Undefined)
  35. {
  36. return desc;
  37. }
  38. if (propertyName != System.Math.Abs(TypeConverter.ToInteger(propertyName)).ToString())
  39. {
  40. return PropertyDescriptor.Undefined;
  41. }
  42. var str = PrimitiveValue;
  43. var index = (int)TypeConverter.ToInteger(propertyName);
  44. var len = str.Length;
  45. if (len <= index)
  46. {
  47. return PropertyDescriptor.Undefined;
  48. }
  49. var resultStr = str[index].ToString();
  50. return new DataDescriptor(resultStr) {Enumerable = true, Writable = false, Configurable = false};
  51. }
  52. }
  53. }