StringPrototype.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.Extensible = true;
  21. obj.FastAddProperty("constructor", stringConstructor, false, false, false);
  22. return obj;
  23. }
  24. public void Configure()
  25. {
  26. FastAddProperty("toString", new ClrFunctionInstance<object, object>(Engine, ToStringString), false, false, false);
  27. FastAddProperty("valueOf", new ClrFunctionInstance<StringInstance, string>(Engine, ValueOf), false, false, false);
  28. FastAddProperty("charAt", new ClrFunctionInstance<object, object>(Engine, CharAt), false, false, false);
  29. FastAddProperty("charCodeAt", new ClrFunctionInstance<object, object>(Engine, CharCodeAt), false, false, false);
  30. FastAddProperty("concat", new ClrFunctionInstance<object, object>(Engine, Concat), false, false, false);
  31. FastAddProperty("indexOf", new ClrFunctionInstance<object, object>(Engine, IndexOf), false, false, false);
  32. FastAddProperty("lastIndexOf", new ClrFunctionInstance<object, object>(Engine, LastIndexOf), false, false, false);
  33. FastAddProperty("localeCompare", new ClrFunctionInstance<object, object>(Engine, LocaleCompare), false, false, false);
  34. FastAddProperty("match", new ClrFunctionInstance<object, object>(Engine, Match), false, false, false);
  35. FastAddProperty("replace", new ClrFunctionInstance<object, object>(Engine, Replace), false, false, false);
  36. FastAddProperty("search", new ClrFunctionInstance<object, object>(Engine, Search), false, false, false);
  37. FastAddProperty("slice", new ClrFunctionInstance<object, object>(Engine, Slice), false, false, false);
  38. FastAddProperty("split", new ClrFunctionInstance<object, object>(Engine, Split), false, false, false);
  39. FastAddProperty("substring", new ClrFunctionInstance<object, object>(Engine, Substring), false, false, false);
  40. FastAddProperty("toLowerCase", new ClrFunctionInstance<object, object>(Engine, ToLowerCase), false, false, false);
  41. FastAddProperty("toLocaleLowerCase", new ClrFunctionInstance<object, object>(Engine, ToLocaleLowerCase), false, false, false);
  42. FastAddProperty("toUpperCase", new ClrFunctionInstance<object, object>(Engine, ToUpperCase), false, false, false);
  43. FastAddProperty("toLocaleUpperCase", new ClrFunctionInstance<object, object>(Engine, ToLocaleUpperCase), false, false, false);
  44. FastAddProperty("trim", new ClrFunctionInstance<object, object>(Engine, Trim), false, false, false);
  45. }
  46. private object Trim(object thisObj, object[] arguments)
  47. {
  48. throw new NotImplementedException();
  49. }
  50. private object ToLocaleUpperCase(object thisObj, object[] arguments)
  51. {
  52. throw new NotImplementedException();
  53. }
  54. private object ToUpperCase(object thisObj, object[] arguments)
  55. {
  56. throw new NotImplementedException();
  57. }
  58. private object ToLocaleLowerCase(object thisObj, object[] arguments)
  59. {
  60. throw new NotImplementedException();
  61. }
  62. private object ToLowerCase(object thisObj, object[] arguments)
  63. {
  64. throw new NotImplementedException();
  65. }
  66. private object Substring(object thisObj, object[] arguments)
  67. {
  68. throw new NotImplementedException();
  69. }
  70. private object Split(object thisObj, object[] arguments)
  71. {
  72. throw new NotImplementedException();
  73. }
  74. private object Slice(object thisObj, object[] arguments)
  75. {
  76. throw new NotImplementedException();
  77. }
  78. private object Search(object thisObj, object[] arguments)
  79. {
  80. throw new NotImplementedException();
  81. }
  82. private object Replace(object thisObj, object[] arguments)
  83. {
  84. throw new NotImplementedException();
  85. }
  86. private object Match(object thisObj, object[] arguments)
  87. {
  88. throw new NotImplementedException();
  89. }
  90. private object LocaleCompare(object thisObj, object[] arguments)
  91. {
  92. throw new NotImplementedException();
  93. }
  94. private object LastIndexOf(object thisObj, object[] arguments)
  95. {
  96. throw new NotImplementedException();
  97. }
  98. private object IndexOf(object thisObj, object[] arguments)
  99. {
  100. throw new NotImplementedException();
  101. }
  102. private object Concat(object thisObj, object[] arguments)
  103. {
  104. throw new NotImplementedException();
  105. }
  106. private object CharCodeAt(object thisObj, object[] arguments)
  107. {
  108. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  109. object pos = arguments.Length > 0 ? arguments[0] : 0;
  110. var s = TypeConverter.ToString(thisObj);
  111. var position = (int)TypeConverter.ToInteger(pos);
  112. if (position < 0 || position >= s.Length)
  113. {
  114. return double.NaN;
  115. }
  116. return (uint)s[position];
  117. }
  118. private object CharAt(object thisObj, object[] arguments)
  119. {
  120. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  121. var s = TypeConverter.ToString(thisObj);
  122. var position = TypeConverter.ToInteger(arguments.Length > 0 ? arguments[0] : Undefined.Instance);
  123. var size = s.Length;
  124. if (position > size || position < 0)
  125. {
  126. return "";
  127. }
  128. return s[(int) position].ToString();
  129. }
  130. private static string ValueOf(StringInstance thisObj, object[] arguments)
  131. {
  132. return thisObj.PrimitiveValue;
  133. }
  134. private object ToStringString(object thisObj, object[] arguments)
  135. {
  136. throw new NotImplementedException();
  137. }
  138. }
  139. }