StringPrototype.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Jint.Native.Array;
  6. using Jint.Native.Function;
  7. using Jint.Native.Object;
  8. using Jint.Native.RegExp;
  9. using Jint.Runtime;
  10. using Jint.Runtime.Descriptors;
  11. using Jint.Runtime.Interop;
  12. namespace Jint.Native.String
  13. {
  14. /// <summary>
  15. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.5.4
  16. /// </summary>
  17. public sealed class StringPrototype : StringInstance
  18. {
  19. private StringPrototype(Engine engine)
  20. : base(engine)
  21. {
  22. }
  23. public static StringPrototype CreatePrototypeObject(Engine engine, StringConstructor stringConstructor)
  24. {
  25. var obj = new StringPrototype(engine);
  26. obj.Prototype = engine.Object.PrototypeObject;
  27. obj.PrimitiveValue = "";
  28. obj.Extensible = true;
  29. obj.FastAddProperty("length", 0, false, false, false);
  30. obj.FastAddProperty("constructor", stringConstructor, true, false, true);
  31. return obj;
  32. }
  33. public void Configure()
  34. {
  35. FastAddProperty("toString", new ClrFunctionInstance(Engine, ToStringString), true, false, true);
  36. FastAddProperty("valueOf", new ClrFunctionInstance(Engine, ValueOf), true, false, true);
  37. FastAddProperty("charAt", new ClrFunctionInstance(Engine, CharAt, 1), true, false, true);
  38. FastAddProperty("charCodeAt", new ClrFunctionInstance(Engine, CharCodeAt, 1), true, false, true);
  39. FastAddProperty("concat", new ClrFunctionInstance(Engine, Concat, 1), true, false, true);
  40. FastAddProperty("indexOf", new ClrFunctionInstance(Engine, IndexOf, 1), true, false, true);
  41. FastAddProperty("lastIndexOf", new ClrFunctionInstance(Engine, LastIndexOf, 1), true, false, true);
  42. FastAddProperty("localeCompare", new ClrFunctionInstance(Engine, LocaleCompare), true, false, true);
  43. FastAddProperty("match", new ClrFunctionInstance(Engine, Match, 1), true, false, true);
  44. FastAddProperty("replace", new ClrFunctionInstance(Engine, Replace, 2), true, false, true);
  45. FastAddProperty("search", new ClrFunctionInstance(Engine, Search, 1), true, false, true);
  46. FastAddProperty("slice", new ClrFunctionInstance(Engine, Slice, 2), true, false, true);
  47. FastAddProperty("split", new ClrFunctionInstance(Engine, Split, 2), true, false, true);
  48. FastAddProperty("substring", new ClrFunctionInstance(Engine, Substring, 2), true, false, true);
  49. FastAddProperty("toLowerCase", new ClrFunctionInstance(Engine, ToLowerCase), true, false, true);
  50. FastAddProperty("toLocaleLowerCase", new ClrFunctionInstance(Engine, ToLocaleLowerCase), true, false, true);
  51. FastAddProperty("toUpperCase", new ClrFunctionInstance(Engine, ToUpperCase), true, false, true);
  52. FastAddProperty("toLocaleUpperCase", new ClrFunctionInstance(Engine, ToLocaleUpperCase), true, false, true);
  53. FastAddProperty("trim", new ClrFunctionInstance(Engine, Trim), true, false, true);
  54. }
  55. private JsValue ToStringString(JsValue thisObj, JsValue[] arguments)
  56. {
  57. var s = TypeConverter.ToObject(Engine, thisObj) as StringInstance;
  58. if (s == null)
  59. {
  60. throw new JavaScriptException(Engine.TypeError);
  61. }
  62. return s.PrimitiveValue;
  63. }
  64. // http://msdn.microsoft.com/en-us/library/system.char.iswhitespace(v=vs.110).aspx
  65. // http://en.wikipedia.org/wiki/Byte_order_mark
  66. const char BOM_CHAR = '\uFEFF';
  67. private static bool IsWhiteSpaceEx(char c)
  68. {
  69. return char.IsWhiteSpace(c) || c == BOM_CHAR;
  70. }
  71. private static string TrimEndEx(string s)
  72. {
  73. if (s.Length == 0)
  74. return string.Empty;
  75. var i = s.Length - 1;
  76. while (i >= 0)
  77. {
  78. if (IsWhiteSpaceEx(s[i]))
  79. i--;
  80. else
  81. break;
  82. }
  83. if (i >= 0)
  84. return s.Substring(0, i + 1);
  85. else
  86. return string.Empty;
  87. }
  88. private static string TrimStartEx(string s)
  89. {
  90. if (s.Length == 0)
  91. return string.Empty;
  92. var i = 0;
  93. while (i < s.Length)
  94. {
  95. if (IsWhiteSpaceEx(s[i]))
  96. i++;
  97. else
  98. break;
  99. }
  100. if (i >= s.Length)
  101. return string.Empty;
  102. else
  103. return s.Substring(i);
  104. }
  105. private static string TrimEx(string s)
  106. {
  107. return TrimEndEx(TrimStartEx(s));
  108. }
  109. private JsValue Trim(JsValue thisObj, JsValue[] arguments)
  110. {
  111. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  112. var s = TypeConverter.ToString(thisObj);
  113. return TrimEx(s);
  114. }
  115. private static JsValue ToLocaleUpperCase(JsValue thisObj, JsValue[] arguments)
  116. {
  117. var s = TypeConverter.ToString(thisObj);
  118. return s.ToUpper();
  119. }
  120. private static JsValue ToUpperCase(JsValue thisObj, JsValue[] arguments)
  121. {
  122. var s = TypeConverter.ToString(thisObj);
  123. return s.ToUpperInvariant();
  124. }
  125. private static JsValue ToLocaleLowerCase(JsValue thisObj, JsValue[] arguments)
  126. {
  127. var s = TypeConverter.ToString(thisObj);
  128. return s.ToLower();
  129. }
  130. private static JsValue ToLowerCase(JsValue thisObj, JsValue[] arguments)
  131. {
  132. var s = TypeConverter.ToString(thisObj);
  133. return s.ToLowerInvariant();
  134. }
  135. private static int ToIntegerSupportInfinity(JsValue numberVal)
  136. {
  137. var doubleVal = TypeConverter.ToInteger(numberVal);
  138. var intVal = (int) doubleVal;
  139. if (double.IsPositiveInfinity(doubleVal))
  140. intVal = int.MaxValue;
  141. else if (double.IsNegativeInfinity(doubleVal))
  142. intVal = int.MinValue;
  143. else
  144. intVal = (int) doubleVal;
  145. return intVal;
  146. }
  147. private JsValue Substring(JsValue thisObj, JsValue[] arguments)
  148. {
  149. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  150. var s = TypeConverter.ToString(thisObj);
  151. var start = TypeConverter.ToNumber(arguments.At(0));
  152. var end = TypeConverter.ToNumber(arguments.At(1));
  153. if (double.IsNaN(start) || start < 0)
  154. {
  155. start = 0;
  156. }
  157. if (double.IsNaN(end) || end < 0)
  158. {
  159. end = 0;
  160. }
  161. var len = s.Length;
  162. var intStart = ToIntegerSupportInfinity(start);
  163. var intEnd = arguments.At(1) == Undefined.Instance ? len : (int)ToIntegerSupportInfinity(end);
  164. var finalStart = System.Math.Min(len, System.Math.Max(intStart, 0));
  165. var finalEnd = System.Math.Min(len, System.Math.Max(intEnd, 0));
  166. // Swap value if finalStart < finalEnd
  167. var from = System.Math.Min(finalStart, finalEnd);
  168. var to = System.Math.Max(finalStart, finalEnd);
  169. return s.Substring(from, to - from);
  170. }
  171. private JsValue Split(JsValue thisObj, JsValue[] arguments)
  172. {
  173. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  174. var s = TypeConverter.ToString(thisObj);
  175. var separator = arguments.At(0);
  176. // Coerce into a number, true will become 1
  177. var l = arguments.At(1);
  178. var a = (ArrayInstance) Engine.Array.Construct(Arguments.Empty);
  179. var limit = l == Undefined.Instance ? UInt32.MaxValue : TypeConverter.ToUint32(l);
  180. var len = s.Length;
  181. if (limit == 0)
  182. {
  183. return a;
  184. }
  185. if (separator == Null.Instance)
  186. {
  187. separator = Null.Text;
  188. }
  189. else if (separator == Undefined.Instance)
  190. {
  191. return (ArrayInstance)Engine.Array.Construct(Arguments.From(s));
  192. }
  193. else
  194. {
  195. if (!separator.IsRegExp())
  196. {
  197. separator = TypeConverter.ToString(separator); // Coerce into a string, for an object call toString()
  198. }
  199. }
  200. var rx = TypeConverter.ToObject(Engine, separator) as RegExpInstance;
  201. const string regExpForMatchingAllCharactere = "(?:)";
  202. if (rx != null &&
  203. rx.Source != regExpForMatchingAllCharactere // We need pattern to be defined -> for s.split(new RegExp)
  204. )
  205. {
  206. var match = rx.Value.Match(s, 0);
  207. if (!match.Success) // No match at all return the string in an array
  208. {
  209. a.DefineOwnProperty("0", new PropertyDescriptor(s, true, true, true), false);
  210. return a;
  211. }
  212. int lastIndex = 0;
  213. int index = 0;
  214. while (match.Success && index < limit)
  215. {
  216. if (match.Length == 0 && (match.Index == 0 || match.Index == len || match.Index == lastIndex))
  217. {
  218. match = match.NextMatch();
  219. continue;
  220. }
  221. // Add the match results to the array.
  222. a.DefineOwnProperty(index++.ToString(), new PropertyDescriptor(s.Substring(lastIndex, match.Index - lastIndex), true, true, true), false);
  223. if (index >= limit)
  224. {
  225. return a;
  226. }
  227. lastIndex = match.Index + match.Length;
  228. for (int i = 1; i < match.Groups.Count; i++)
  229. {
  230. var group = match.Groups[i];
  231. var item = Undefined.Instance;
  232. if (group.Captures.Count > 0)
  233. {
  234. item = match.Groups[i].Value;
  235. }
  236. a.DefineOwnProperty(index++.ToString(), new PropertyDescriptor(item, true, true, true ), false);
  237. if (index >= limit)
  238. {
  239. return a;
  240. }
  241. }
  242. match = match.NextMatch();
  243. if (!match.Success) // Add the last part of the split
  244. {
  245. a.DefineOwnProperty(index++.ToString(), new PropertyDescriptor(s.Substring(lastIndex), true, true, true), false);
  246. }
  247. }
  248. return a;
  249. }
  250. else
  251. {
  252. var segments = new List<string>();
  253. var sep = TypeConverter.ToString(separator);
  254. if (sep == string.Empty || (rx != null && rx.Source == regExpForMatchingAllCharactere)) // for s.split(new RegExp)
  255. {
  256. segments.AddRange(from object c in s select c.ToString());
  257. }
  258. else
  259. {
  260. segments = s.Split(new[] {sep}, StringSplitOptions.None).ToList();
  261. }
  262. for (int i = 0; i < segments.Count && i < limit; i++)
  263. {
  264. a.DefineOwnProperty(i.ToString(), new PropertyDescriptor(segments[i], true, true, true), false);
  265. }
  266. return a;
  267. }
  268. }
  269. private JsValue Slice(JsValue thisObj, JsValue[] arguments)
  270. {
  271. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  272. var s = TypeConverter.ToString(thisObj);
  273. var start = TypeConverter.ToNumber(arguments.At(0));
  274. if (double.NegativeInfinity.Equals(start))
  275. {
  276. start = 0;
  277. }
  278. if (double.PositiveInfinity.Equals(start))
  279. {
  280. return string.Empty;
  281. }
  282. var end = TypeConverter.ToNumber(arguments.At(1));
  283. if (double.PositiveInfinity.Equals(end))
  284. {
  285. end = s.Length;
  286. }
  287. var len = s.Length;
  288. var intStart = (int)TypeConverter.ToInteger(start);
  289. var intEnd = arguments.At(1) == Undefined.Instance ? len : (int)TypeConverter.ToInteger(end);
  290. var from = intStart < 0 ? System.Math.Max(len + intStart, 0) : System.Math.Min(intStart, len);
  291. var to = intEnd < 0 ? System.Math.Max(len + intEnd, 0) : System.Math.Min(intEnd, len);
  292. var span = System.Math.Max(to - from, 0);
  293. return s.Substring(from, span);
  294. }
  295. private JsValue Search(JsValue thisObj, JsValue[] arguments)
  296. {
  297. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  298. var s = TypeConverter.ToString(thisObj);
  299. var regex = arguments.At(0);
  300. if (regex.IsUndefined())
  301. {
  302. regex = string.Empty;
  303. }
  304. else if (regex.IsNull())
  305. {
  306. regex = Null.Text;
  307. }
  308. var rx = TypeConverter.ToObject(Engine, regex) as RegExpInstance ?? (RegExpInstance)Engine.RegExp.Construct(new[] { regex });
  309. var match = rx.Value.Match(s);
  310. if (!match.Success)
  311. {
  312. return -1;
  313. }
  314. return match.Index;
  315. }
  316. private JsValue Replace(JsValue thisObj, JsValue[] arguments)
  317. {
  318. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  319. var thisString = TypeConverter.ToString(thisObj);
  320. var searchValue = arguments.At(0);
  321. var replaceValue = arguments.At(1);
  322. // If the second parameter is not a function we create one
  323. var replaceFunction = replaceValue.TryCast<FunctionInstance>();
  324. if (replaceFunction == null)
  325. {
  326. replaceFunction = new ClrFunctionInstance(Engine, (self, args) =>
  327. {
  328. var replaceString = TypeConverter.ToString(replaceValue);
  329. var matchValue = TypeConverter.ToString(args.At(0));
  330. var matchIndex = (int)TypeConverter.ToInteger(args.At(args.Length - 2));
  331. // Check if the replacement string contains any patterns.
  332. bool replaceTextContainsPattern = replaceString.IndexOf('$') >= 0;
  333. // If there is no pattern, replace the pattern as is.
  334. if (replaceTextContainsPattern == false)
  335. return replaceString;
  336. // Patterns
  337. // $$ Inserts a "$".
  338. // $& Inserts the matched substring.
  339. // $` Inserts the portion of the string that precedes the matched substring.
  340. // $' Inserts the portion of the string that follows the matched substring.
  341. // $n or $nn Where n or nn are decimal digits, inserts the nth parenthesized submatch string, provided the first argument was a RegExp object.
  342. var replacementBuilder = new StringBuilder();
  343. for (int i = 0; i < replaceString.Length; i++)
  344. {
  345. char c = replaceString[i];
  346. if (c == '$' && i < replaceString.Length - 1)
  347. {
  348. c = replaceString[++i];
  349. if (c == '$')
  350. replacementBuilder.Append('$');
  351. else if (c == '&')
  352. replacementBuilder.Append(matchValue);
  353. else if (c == '`')
  354. replacementBuilder.Append(thisString.Substring(0, matchIndex));
  355. else if (c == '\'')
  356. replacementBuilder.Append(thisString.Substring(matchIndex + matchValue.Length));
  357. else if (c >= '0' && c <= '9')
  358. {
  359. int matchNumber1 = c - '0';
  360. // The match number can be one or two digits long.
  361. int matchNumber2 = 0;
  362. if (i < replaceString.Length - 1 && replaceString[i + 1] >= '0' && replaceString[i + 1] <= '9')
  363. matchNumber2 = matchNumber1 * 10 + (replaceString[i + 1] - '0');
  364. // Try the two digit capture first.
  365. if (matchNumber2 > 0 && matchNumber2 < args.Length - 2)
  366. {
  367. // Two digit capture replacement.
  368. replacementBuilder.Append(TypeConverter.ToString(args[matchNumber2]));
  369. i++;
  370. }
  371. else if (matchNumber1 > 0 && matchNumber1 < args.Length - 2)
  372. {
  373. // Single digit capture replacement.
  374. replacementBuilder.Append(TypeConverter.ToString(args[matchNumber1]));
  375. }
  376. else
  377. {
  378. // Capture does not exist.
  379. replacementBuilder.Append('$');
  380. i--;
  381. }
  382. }
  383. else
  384. {
  385. // Unknown replacement pattern.
  386. replacementBuilder.Append('$');
  387. replacementBuilder.Append(c);
  388. }
  389. }
  390. else
  391. replacementBuilder.Append(c);
  392. }
  393. return replacementBuilder.ToString();
  394. });
  395. }
  396. // searchValue is a regular expression
  397. if (searchValue.IsNull())
  398. {
  399. searchValue = new JsValue(Null.Text);
  400. }
  401. if (searchValue.IsUndefined())
  402. {
  403. searchValue = new JsValue(Undefined.Text);
  404. }
  405. var rx = TypeConverter.ToObject(Engine, searchValue) as RegExpInstance;
  406. if (rx != null)
  407. {
  408. // Replace the input string with replaceText, recording the last match found.
  409. string result = rx.Value.Replace(thisString, match =>
  410. {
  411. var args = new List<JsValue>();
  412. for (var k = 0; k < match.Groups.Count; k++)
  413. {
  414. var group = match.Groups[k];
  415. if (group.Success)
  416. args.Add(group.Value);
  417. }
  418. args.Add(match.Index);
  419. args.Add(thisString);
  420. var v = TypeConverter.ToString(replaceFunction.Call(Undefined.Instance, args.ToArray()));
  421. return v;
  422. }, rx.Global == true ? -1 : 1);
  423. // Set the deprecated RegExp properties if at least one match was found.
  424. //if (lastMatch != null)
  425. // this.Engine.RegExp.SetDeprecatedProperties(input, lastMatch);
  426. return result;
  427. }
  428. // searchValue is a string
  429. else
  430. {
  431. var substr = TypeConverter.ToString(searchValue);
  432. // Find the first occurrance of substr.
  433. int start = thisString.IndexOf(substr, StringComparison.Ordinal);
  434. if (start == -1)
  435. return thisString;
  436. int end = start + substr.Length;
  437. var args = new List<JsValue>();
  438. args.Add(substr);
  439. args.Add(start);
  440. args.Add(thisString);
  441. var replaceString = TypeConverter.ToString(replaceFunction.Call(Undefined.Instance, args.ToArray()));
  442. // Replace only the first match.
  443. var result = new StringBuilder(thisString.Length + (substr.Length - substr.Length));
  444. result.Append(thisString, 0, start);
  445. result.Append(replaceString);
  446. result.Append(thisString, end, thisString.Length - end);
  447. return result.ToString();
  448. }
  449. }
  450. private JsValue Match(JsValue thisObj, JsValue[] arguments)
  451. {
  452. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  453. var s = TypeConverter.ToString(thisObj);
  454. var regex = arguments.At(0);
  455. var rx = regex.TryCast<RegExpInstance>();
  456. rx = rx ?? (RegExpInstance) Engine.RegExp.Construct(new[] {regex});
  457. var global = rx.Get("global").AsBoolean();
  458. if (!global)
  459. {
  460. return Engine.RegExp.PrototypeObject.Exec(rx, Arguments.From(s));
  461. }
  462. else
  463. {
  464. rx.Put("lastIndex", 0, false);
  465. var a = Engine.Array.Construct(Arguments.Empty);
  466. double previousLastIndex = 0;
  467. var n = 0;
  468. var lastMatch = true;
  469. while (lastMatch)
  470. {
  471. var result = Engine.RegExp.PrototypeObject.Exec(rx, Arguments.From(s)).TryCast<ObjectInstance>();
  472. if (result == null)
  473. {
  474. lastMatch = false;
  475. }
  476. else
  477. {
  478. var thisIndex = rx.Get("lastIndex").AsNumber();
  479. if (thisIndex == previousLastIndex)
  480. {
  481. rx.Put("lastIndex", thisIndex + 1, false);
  482. previousLastIndex = thisIndex;
  483. }
  484. var matchStr = result.Get("0");
  485. a.DefineOwnProperty(TypeConverter.ToString(n), new PropertyDescriptor(matchStr, true, true, true), false);
  486. n++;
  487. }
  488. }
  489. if (n == 0)
  490. {
  491. return Null.Instance;
  492. }
  493. return a;
  494. }
  495. }
  496. private JsValue LocaleCompare(JsValue thisObj, JsValue[] arguments)
  497. {
  498. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  499. var s = TypeConverter.ToString(thisObj);
  500. var that = TypeConverter.ToString(arguments.At(0));
  501. return string.CompareOrdinal(s, that);
  502. }
  503. private JsValue LastIndexOf(JsValue thisObj, JsValue[] arguments)
  504. {
  505. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  506. var s = TypeConverter.ToString(thisObj);
  507. var searchStr = TypeConverter.ToString(arguments.At(0));
  508. double numPos = arguments.At(0) == Undefined.Instance ? double.NaN : TypeConverter.ToNumber(arguments.At(0));
  509. double pos = double.IsNaN(numPos) ? double.PositiveInfinity : TypeConverter.ToInteger(numPos);
  510. var len = s.Length;
  511. var start = System.Math.Min(len, System.Math.Max(pos, 0));
  512. var searchLen = searchStr.Length;
  513. return s.LastIndexOf(searchStr, len - (int) start, StringComparison.Ordinal);
  514. }
  515. private JsValue IndexOf(JsValue thisObj, JsValue[] arguments)
  516. {
  517. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  518. var s = TypeConverter.ToString(thisObj);
  519. var searchStr = TypeConverter.ToString(arguments.At(0));
  520. double pos = 0;
  521. if (arguments.Length > 1 && arguments[1] != Undefined.Instance)
  522. {
  523. pos = TypeConverter.ToInteger(arguments[1]);
  524. }
  525. if (pos >= s.Length)
  526. {
  527. return -1;
  528. }
  529. if (pos < 0)
  530. {
  531. pos = 0;
  532. }
  533. return s.IndexOf(searchStr, (int) pos, StringComparison.Ordinal);
  534. }
  535. private JsValue Concat(JsValue thisObj, JsValue[] arguments)
  536. {
  537. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  538. var s = TypeConverter.ToString(thisObj);
  539. var sb = new StringBuilder(s);
  540. for (int i = 0; i < arguments.Length; i++)
  541. {
  542. sb.Append(TypeConverter.ToString(arguments[i]));
  543. }
  544. return sb.ToString();
  545. }
  546. private JsValue CharCodeAt(JsValue thisObj, JsValue[] arguments)
  547. {
  548. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  549. JsValue pos = arguments.Length > 0 ? arguments[0] : 0;
  550. var s = TypeConverter.ToString(thisObj);
  551. var position = (int)TypeConverter.ToInteger(pos);
  552. if (position < 0 || position >= s.Length)
  553. {
  554. return double.NaN;
  555. }
  556. return s[position];
  557. }
  558. private JsValue CharAt(JsValue thisObj, JsValue[] arguments)
  559. {
  560. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  561. var s = TypeConverter.ToString(thisObj);
  562. var position = TypeConverter.ToInteger(arguments.At(0));
  563. var size = s.Length;
  564. if (position >= size || position < 0)
  565. {
  566. return "";
  567. }
  568. return s[(int) position].ToString();
  569. }
  570. private JsValue ValueOf(JsValue thisObj, JsValue[] arguments)
  571. {
  572. var s = thisObj.TryCast<StringInstance>();
  573. if (s == null)
  574. {
  575. throw new JavaScriptException(Engine.TypeError);
  576. }
  577. return s.PrimitiveValue;
  578. }
  579. }
  580. }