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 (start == double.NegativeInfinity)
  275. {
  276. start = 0;
  277. }
  278. if (start == double.PositiveInfinity)
  279. {
  280. return string.Empty;
  281. }
  282. var end = TypeConverter.ToNumber(arguments.At(1));
  283. if (end == double.PositiveInfinity)
  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. //if (match.Groups.Count == 0) args.Add(match.Value);
  413. for (var k = 0; k < match.Groups.Count; k++)
  414. {
  415. var group = match.Groups[k];
  416. if (group.Success)
  417. args.Add(group.Value);
  418. }
  419. args.Add(match.Index);
  420. args.Add(thisString);
  421. var v = TypeConverter.ToString(replaceFunction.Call(Undefined.Instance, args.ToArray()));
  422. return v;
  423. }, rx.Global == true ? -1 : 1);
  424. // Set the deprecated RegExp properties if at least one match was found.
  425. //if (lastMatch != null)
  426. // this.Engine.RegExp.SetDeprecatedProperties(input, lastMatch);
  427. return result;
  428. }
  429. // searchValue is a string
  430. else
  431. {
  432. var substr = TypeConverter.ToString(searchValue);
  433. // Find the first occurrance of substr.
  434. int start = thisString.IndexOf(substr, StringComparison.Ordinal);
  435. if (start == -1)
  436. return thisString;
  437. int end = start + substr.Length;
  438. var args = new List<JsValue>();
  439. args.Add(substr);
  440. args.Add(start);
  441. args.Add(thisString);
  442. var replaceString = TypeConverter.ToString(replaceFunction.Call(Undefined.Instance, args.ToArray()));
  443. // Replace only the first match.
  444. var result = new StringBuilder(thisString.Length + (substr.Length - substr.Length));
  445. result.Append(thisString, 0, start);
  446. result.Append(replaceString);
  447. result.Append(thisString, end, thisString.Length - end);
  448. return result.ToString();
  449. }
  450. }
  451. private JsValue Match(JsValue thisObj, JsValue[] arguments)
  452. {
  453. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  454. var s = TypeConverter.ToString(thisObj);
  455. var regex = arguments.At(0);
  456. var rx = regex.TryCast<RegExpInstance>();
  457. rx = rx ?? (RegExpInstance) Engine.RegExp.Construct(new[] {regex});
  458. var global = rx.Get("global").AsBoolean();
  459. if (!global)
  460. {
  461. return Engine.RegExp.PrototypeObject.Exec(rx, Arguments.From(s));
  462. }
  463. else
  464. {
  465. rx.Put("lastIndex", 0, false);
  466. var a = Engine.Array.Construct(Arguments.Empty);
  467. double previousLastIndex = 0;
  468. var n = 0;
  469. var lastMatch = true;
  470. while (lastMatch)
  471. {
  472. var result = Engine.RegExp.PrototypeObject.Exec(rx, Arguments.From(s)).TryCast<ObjectInstance>();
  473. if (result == null)
  474. {
  475. lastMatch = false;
  476. }
  477. else
  478. {
  479. var thisIndex = rx.Get("lastIndex").AsNumber();
  480. if (thisIndex == previousLastIndex)
  481. {
  482. rx.Put("lastIndex", thisIndex + 1, false);
  483. previousLastIndex = thisIndex;
  484. }
  485. var matchStr = result.Get("0");
  486. a.DefineOwnProperty(TypeConverter.ToString(n), new PropertyDescriptor(matchStr, true, true, true), false);
  487. n++;
  488. }
  489. }
  490. if (n == 0)
  491. {
  492. return Null.Instance;
  493. }
  494. return a;
  495. }
  496. }
  497. private JsValue LocaleCompare(JsValue thisObj, JsValue[] arguments)
  498. {
  499. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  500. var s = TypeConverter.ToString(thisObj);
  501. var that = TypeConverter.ToString(arguments.At(0));
  502. return string.CompareOrdinal(s, that);
  503. }
  504. private JsValue LastIndexOf(JsValue thisObj, JsValue[] arguments)
  505. {
  506. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  507. var s = TypeConverter.ToString(thisObj);
  508. var searchStr = TypeConverter.ToString(arguments.At(0));
  509. double numPos = arguments.At(0) == Undefined.Instance ? double.NaN : TypeConverter.ToNumber(arguments.At(0));
  510. double pos = double.IsNaN(numPos) ? double.PositiveInfinity : TypeConverter.ToInteger(numPos);
  511. var len = s.Length;
  512. var start = System.Math.Min(len, System.Math.Max(pos, 0));
  513. var searchLen = searchStr.Length;
  514. return s.LastIndexOf(searchStr, len - (int) start, StringComparison.Ordinal);
  515. }
  516. private JsValue IndexOf(JsValue thisObj, JsValue[] arguments)
  517. {
  518. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  519. var s = TypeConverter.ToString(thisObj);
  520. var searchStr = TypeConverter.ToString(arguments.At(0));
  521. double pos = 0;
  522. if (arguments.Length > 1 && arguments[1] != Undefined.Instance)
  523. {
  524. pos = TypeConverter.ToInteger(arguments[1]);
  525. }
  526. if (pos >= s.Length)
  527. {
  528. return -1;
  529. }
  530. if (pos < 0)
  531. {
  532. pos = 0;
  533. }
  534. return s.IndexOf(searchStr, (int) pos, StringComparison.Ordinal);
  535. }
  536. private JsValue Concat(JsValue thisObj, JsValue[] arguments)
  537. {
  538. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  539. var s = TypeConverter.ToString(thisObj);
  540. var sb = new StringBuilder(s);
  541. for (int i = 0; i < arguments.Length; i++)
  542. {
  543. sb.Append(TypeConverter.ToString(arguments[i]));
  544. }
  545. return sb.ToString();
  546. }
  547. private JsValue CharCodeAt(JsValue thisObj, JsValue[] arguments)
  548. {
  549. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  550. JsValue pos = arguments.Length > 0 ? arguments[0] : 0;
  551. var s = TypeConverter.ToString(thisObj);
  552. var position = (int)TypeConverter.ToInteger(pos);
  553. if (position < 0 || position >= s.Length)
  554. {
  555. return double.NaN;
  556. }
  557. return s[position];
  558. }
  559. private JsValue CharAt(JsValue thisObj, JsValue[] arguments)
  560. {
  561. TypeConverter.CheckObjectCoercible(Engine, thisObj);
  562. var s = TypeConverter.ToString(thisObj);
  563. var position = TypeConverter.ToInteger(arguments.At(0));
  564. var size = s.Length;
  565. if (position >= size || position < 0)
  566. {
  567. return "";
  568. }
  569. return s[(int) position].ToString();
  570. }
  571. private JsValue ValueOf(JsValue thisObj, JsValue[] arguments)
  572. {
  573. var s = thisObj.TryCast<StringInstance>();
  574. if (s == null)
  575. {
  576. throw new JavaScriptException(Engine.TypeError);
  577. }
  578. return s.PrimitiveValue;
  579. }
  580. }
  581. }