StringPrototype.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using Jint.Runtime;
  3. using Jint.Runtime.Interop;
  4. namespace Jint.Native.String
  5. {
  6. /// <summary>
  7. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.5.4
  8. /// </summary>
  9. public sealed class StringPrototype : StringInstance
  10. {
  11. private StringPrototype(Engine engine)
  12. : base(engine)
  13. {
  14. }
  15. public static StringPrototype CreatePrototypeObject(Engine engine, StringConstructor stringConstructor)
  16. {
  17. var obj = new StringPrototype(engine);
  18. obj.Prototype = engine.Object.PrototypeObject;
  19. obj.PrimitiveValue = "";
  20. obj.FastAddProperty("constructor", stringConstructor, false, false, false);
  21. return obj;
  22. }
  23. public void Configure()
  24. {
  25. FastAddProperty("toString", new ClrFunctionInstance<object, object>(Engine, ToStringString), false, false, false);
  26. FastAddProperty("valueOf", new ClrFunctionInstance<StringInstance, string>(Engine, ValueOf), false, false, false);
  27. FastAddProperty("charAt", new ClrFunctionInstance<object, object>(Engine, CharAt), false, false, false);
  28. FastAddProperty("charCodeAt", new ClrFunctionInstance<object, object>(Engine, CharCodeAt), false, false, false);
  29. FastAddProperty("concat", new ClrFunctionInstance<object, object>(Engine, Concat), false, false, false);
  30. FastAddProperty("indexOf", new ClrFunctionInstance<object, object>(Engine, IndexOf), false, false, false);
  31. FastAddProperty("lastIndexOf", new ClrFunctionInstance<object, object>(Engine, LastIndexOf), false, false, false);
  32. FastAddProperty("localeCompare", new ClrFunctionInstance<object, object>(Engine, LocaleCompare), false, false, false);
  33. FastAddProperty("match", new ClrFunctionInstance<object, object>(Engine, Match), false, false, false);
  34. FastAddProperty("replace", new ClrFunctionInstance<object, object>(Engine, Replace), false, false, false);
  35. FastAddProperty("search", new ClrFunctionInstance<object, object>(Engine, Search), false, false, false);
  36. FastAddProperty("slice", new ClrFunctionInstance<object, object>(Engine, Slice), false, false, false);
  37. FastAddProperty("split", new ClrFunctionInstance<object, object>(Engine, Split), false, false, false);
  38. FastAddProperty("substring", new ClrFunctionInstance<object, object>(Engine, Substring), false, false, false);
  39. FastAddProperty("toLowerCase", new ClrFunctionInstance<object, object>(Engine, ToLowerCase), false, false, false);
  40. FastAddProperty("toLocaleLowerCase", new ClrFunctionInstance<object, object>(Engine, ToLocaleLowerCase), false, false, false);
  41. FastAddProperty("toUpperCase", new ClrFunctionInstance<object, object>(Engine, ToUpperCase), false, false, false);
  42. FastAddProperty("toLocaleUpperCase", new ClrFunctionInstance<object, object>(Engine, ToLocaleUpperCase), false, false, false);
  43. FastAddProperty("trim", new ClrFunctionInstance<object, object>(Engine, Trim), false, false, false);
  44. }
  45. private object Trim(object thisObj, object[] arguments)
  46. {
  47. throw new NotImplementedException();
  48. }
  49. private object ToLocaleUpperCase(object thisObj, object[] arguments)
  50. {
  51. throw new NotImplementedException();
  52. }
  53. private object ToUpperCase(object thisObj, object[] arguments)
  54. {
  55. throw new NotImplementedException();
  56. }
  57. private object ToLocaleLowerCase(object thisObj, object[] arguments)
  58. {
  59. throw new NotImplementedException();
  60. }
  61. private object ToLowerCase(object thisObj, object[] arguments)
  62. {
  63. throw new NotImplementedException();
  64. }
  65. private object Substring(object thisObj, object[] arguments)
  66. {
  67. throw new NotImplementedException();
  68. }
  69. private object Split(object thisObj, object[] arguments)
  70. {
  71. throw new NotImplementedException();
  72. }
  73. private object Slice(object thisObj, object[] arguments)
  74. {
  75. throw new NotImplementedException();
  76. }
  77. private object Search(object thisObj, object[] arguments)
  78. {
  79. throw new NotImplementedException();
  80. }
  81. private object Replace(object thisObj, object[] arguments)
  82. {
  83. throw new NotImplementedException();
  84. }
  85. private object Match(object thisObj, object[] arguments)
  86. {
  87. throw new NotImplementedException();
  88. }
  89. private object LocaleCompare(object thisObj, object[] arguments)
  90. {
  91. throw new NotImplementedException();
  92. }
  93. private object LastIndexOf(object thisObj, object[] arguments)
  94. {
  95. throw new NotImplementedException();
  96. }
  97. private object IndexOf(object thisObj, object[] arguments)
  98. {
  99. throw new NotImplementedException();
  100. }
  101. private object Concat(object thisObj, object[] arguments)
  102. {
  103. throw new NotImplementedException();
  104. }
  105. private object CharCodeAt(object thisObj, object[] arguments)
  106. {
  107. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  108. object pos = arguments.Length > 0 ? arguments[0] : 0;
  109. var s = TypeConverter.ToString(thisObj);
  110. var position = (int)TypeConverter.ToInteger(pos);
  111. if (position < 0 || position >= s.Length)
  112. {
  113. return double.NaN;
  114. }
  115. return (uint)s[position];
  116. }
  117. private object CharAt(object thisObj, object[] arguments)
  118. {
  119. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  120. var s = TypeConverter.ToString(thisObj);
  121. var position = TypeConverter.ToInteger(arguments.Length > 0 ? arguments[0] : Undefined.Instance);
  122. var size = s.Length;
  123. if (position > size || position < 0)
  124. {
  125. return "";
  126. }
  127. return s[(int) position].ToString();
  128. }
  129. private static string ValueOf(StringInstance thisObj, object[] arguments)
  130. {
  131. return thisObj.PrimitiveValue;
  132. }
  133. private object ToStringString(object thisObj, object[] arguments)
  134. {
  135. throw new NotImplementedException();
  136. }
  137. }
  138. }