StringConstructor.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System.Collections.Generic;
  2. using System.Text;
  3. using Jint.Native.Array;
  4. using Jint.Native.Function;
  5. using Jint.Native.Object;
  6. using Jint.Pooling;
  7. using Jint.Runtime;
  8. using Jint.Runtime.Descriptors;
  9. using Jint.Runtime.Interop;
  10. namespace Jint.Native.String
  11. {
  12. public sealed class StringConstructor : FunctionInstance, IConstructor
  13. {
  14. public StringConstructor(Engine engine)
  15. : base(engine, "String", null, null, false)
  16. {
  17. }
  18. public static StringConstructor CreateStringConstructor(Engine engine)
  19. {
  20. var obj = new StringConstructor(engine);
  21. obj.Extensible = true;
  22. // The value of the [[Prototype]] internal property of the String constructor is the Function prototype object
  23. obj.Prototype = engine.Function.PrototypeObject;
  24. obj.PrototypeObject = StringPrototype.CreatePrototypeObject(engine, obj);
  25. obj.SetOwnProperty("length", new PropertyDescriptor(1, PropertyFlag.AllForbidden));
  26. // The initial value of String.prototype is the String prototype object
  27. obj.SetOwnProperty("prototype", new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden));
  28. return obj;
  29. }
  30. public void Configure()
  31. {
  32. SetOwnProperty("fromCharCode", new PropertyDescriptor(new ClrFunctionInstance(Engine, "fromCharCode", FromCharCode, 1), PropertyFlag.NonEnumerable));
  33. SetOwnProperty("fromCodePoint", new PropertyDescriptor(new ClrFunctionInstance(Engine, "fromCodePoint", FromCodePoint, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable));
  34. SetOwnProperty("raw", new PropertyDescriptor(new ClrFunctionInstance(Engine, "raw", Raw, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable));
  35. }
  36. private static JsValue FromCharCode(JsValue thisObj, JsValue[] arguments)
  37. {
  38. var chars = new char[arguments.Length];
  39. for (var i = 0; i < chars.Length; i++ )
  40. {
  41. chars[i] = (char)TypeConverter.ToUint16(arguments[i]);
  42. }
  43. return JsString.Create(new string(chars));
  44. }
  45. private static JsValue FromCodePoint(JsValue thisObj, JsValue[] arguments)
  46. {
  47. var codeUnits = new List<JsValue>();
  48. string result = "";
  49. for (var i = 0; i < arguments.Length; i++ )
  50. {
  51. var codePoint = TypeConverter.ToNumber(arguments[i]);
  52. if (codePoint < 0
  53. || codePoint > 0x10FFFF
  54. || double.IsInfinity(codePoint)
  55. || double.IsNaN(codePoint)
  56. || TypeConverter.ToInt32(codePoint) != codePoint)
  57. {
  58. return ExceptionHelper.ThrowRangeErrorNoEngine<JsValue>("Invalid code point " + codePoint);
  59. }
  60. var point = (uint) codePoint;
  61. if (point <= 0xFFFF)
  62. {
  63. // BMP code point
  64. codeUnits.Add(JsNumber.Create(point));
  65. }
  66. else
  67. {
  68. // Astral code point; split in surrogate halves
  69. // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
  70. point -= 0x10000;
  71. codeUnits.Add(JsNumber.Create((point >> 10) + 0xD800)); // highSurrogate
  72. codeUnits.Add(JsNumber.Create((point % 0x400) + 0xDC00)); // lowSurrogate
  73. }
  74. if (codeUnits.Count >= 0x3fff)
  75. {
  76. result += FromCharCode(null, codeUnits.ToArray());
  77. codeUnits.Clear();
  78. }
  79. }
  80. return result + FromCharCode(null, codeUnits.ToArray());
  81. }
  82. /// <summary>
  83. /// https://www.ecma-international.org/ecma-262/6.0/#sec-string.raw
  84. /// </summary>
  85. private JsValue Raw(JsValue thisObj, JsValue[] arguments)
  86. {
  87. var cooked = TypeConverter.ToObject(_engine, arguments.At(0));
  88. var raw = TypeConverter.ToObject(_engine, cooked.Get("raw"));
  89. var operations = ArrayPrototype.ArrayOperations.For(raw);
  90. var length = operations.GetLength();
  91. if (length <= 0)
  92. {
  93. return JsString.Empty;
  94. }
  95. using (var result = StringBuilderPool.GetInstance())
  96. {
  97. for (var i = 0; i < length; i++)
  98. {
  99. if (i > 0)
  100. {
  101. if (i < arguments.Length && !arguments[i].IsUndefined())
  102. {
  103. result.Builder.Append(TypeConverter.ToString(arguments[i]));
  104. }
  105. }
  106. result.Builder.Append(TypeConverter.ToString(operations.Get((ulong) i)));
  107. }
  108. return result.ToString();
  109. }
  110. }
  111. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  112. {
  113. if (arguments.Length == 0)
  114. {
  115. return JsString.Empty;
  116. }
  117. var arg = arguments[0];
  118. var str = arg is JsSymbol s
  119. ? s.ToString()
  120. : TypeConverter.ToString(arg);
  121. return JsString.Create(str);
  122. }
  123. /// <summary>
  124. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.7.2.1
  125. /// </summary>
  126. /// <param name="arguments"></param>
  127. /// <returns></returns>
  128. public ObjectInstance Construct(JsValue[] arguments)
  129. {
  130. string value = "";
  131. if (arguments.Length > 0)
  132. {
  133. value = TypeConverter.ToString(arguments[0]);
  134. }
  135. return Construct(value);
  136. }
  137. public StringPrototype PrototypeObject { get; private set; }
  138. public StringInstance Construct(string value)
  139. {
  140. return Construct(JsString.Create(value));
  141. }
  142. public StringInstance Construct(JsString value)
  143. {
  144. var instance = new StringInstance(Engine)
  145. {
  146. Prototype = PrototypeObject,
  147. PrimitiveValue = value,
  148. Extensible = true,
  149. _length = new PropertyDescriptor(value.Length, PropertyFlag.AllForbidden)
  150. };
  151. return instance;
  152. }
  153. }
  154. }