StringInstance.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using Jint.Native.Object;
  2. using Jint.Runtime;
  3. using Jint.Runtime.Descriptors;
  4. namespace Jint.Native.String
  5. {
  6. public class StringInstance : ObjectInstance, IPrimitiveType
  7. {
  8. public StringInstance(Engine engine)
  9. : base(engine)
  10. {
  11. }
  12. public override string Class
  13. {
  14. get
  15. {
  16. return "String";
  17. }
  18. }
  19. Types IPrimitiveType.Type
  20. {
  21. get { return Types.String; }
  22. }
  23. object IPrimitiveType.PrimitiveValue
  24. {
  25. get { return PrimitiveValue; }
  26. }
  27. public string PrimitiveValue { get; set; }
  28. public override PropertyDescriptor GetOwnProperty(string propertyName)
  29. {
  30. var desc = base.GetOwnProperty(propertyName);
  31. if (desc != PropertyDescriptor.Undefined)
  32. {
  33. return desc;
  34. }
  35. if (propertyName != System.Math.Abs(TypeConverter.ToInteger(propertyName)).ToString())
  36. {
  37. return PropertyDescriptor.Undefined;
  38. }
  39. var str = PrimitiveValue;
  40. var index = (int)TypeConverter.ToInteger(propertyName);
  41. var len = str.Length;
  42. if (len <= index)
  43. {
  44. return PropertyDescriptor.Undefined;
  45. }
  46. var resultStr = str[index].ToString();
  47. return new PropertyDescriptor(resultStr, false, true, false);
  48. }
  49. }
  50. }