StringConstructor.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #pragma warning disable CA1859 // Use concrete types when possible for improved performance -- most of prototype methods return JsValue
  2. using Jint.Collections;
  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. using Jint.Runtime.Interpreter.Expressions;
  11. namespace Jint.Native.String
  12. {
  13. /// <summary>
  14. /// https://tc39.es/ecma262/#sec-string-constructor
  15. /// </summary>
  16. internal sealed class StringConstructor : Constructor
  17. {
  18. private static readonly JsString _functionName = new JsString("String");
  19. public StringConstructor(
  20. Engine engine,
  21. Realm realm,
  22. FunctionPrototype functionPrototype,
  23. ObjectPrototype objectPrototype)
  24. : base(engine, realm, _functionName)
  25. {
  26. _prototype = functionPrototype;
  27. PrototypeObject = new StringPrototype(engine, realm, this, objectPrototype);
  28. _length = new PropertyDescriptor(JsNumber.PositiveOne, PropertyFlag.Configurable);
  29. _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
  30. }
  31. public StringPrototype PrototypeObject { get; }
  32. protected override void Initialize()
  33. {
  34. var properties = new PropertyDictionary(3, checkExistingKeys: false)
  35. {
  36. ["fromCharCode"] = new PropertyDescriptor(new PropertyDescriptor(new ClrFunctionInstance(Engine, "fromCharCode", FromCharCode, 1), PropertyFlag.NonEnumerable)),
  37. ["fromCodePoint"] = new PropertyDescriptor(new PropertyDescriptor(new ClrFunctionInstance(Engine, "fromCodePoint", FromCodePoint, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable)),
  38. ["raw"] = new PropertyDescriptor(new PropertyDescriptor(new ClrFunctionInstance(Engine, "raw", Raw, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable))
  39. };
  40. SetProperties(properties);
  41. }
  42. /// <summary>
  43. /// https://tc39.es/ecma262/#sec-string.fromcharcode
  44. /// </summary>
  45. private static JsValue FromCharCode(JsValue? thisObj, JsValue[] arguments)
  46. {
  47. var length = arguments.Length;
  48. if (length == 0)
  49. {
  50. return JsString.Empty;
  51. }
  52. if (arguments.Length == 1)
  53. {
  54. return JsString.Create((char) TypeConverter.ToUint16(arguments[0]));
  55. }
  56. #if SUPPORTS_SPAN_PARSE
  57. var elements = length < 512 ? stackalloc char[length] : new char[length];
  58. #else
  59. var elements = new char[length];
  60. #endif
  61. for (var i = 0; i < elements.Length; i++ )
  62. {
  63. var nextCu = TypeConverter.ToUint16(arguments[i]);
  64. elements[i] = (char) nextCu;
  65. }
  66. return JsString.Create(new string(elements));
  67. }
  68. private JsValue FromCodePoint(JsValue thisObject, JsValue[] arguments)
  69. {
  70. var codeUnits = new List<JsValue>();
  71. string result = "";
  72. foreach (var a in arguments)
  73. {
  74. var codePoint = TypeConverter.ToNumber(a);
  75. if (codePoint < 0
  76. || codePoint > 0x10FFFF
  77. || double.IsInfinity(codePoint)
  78. || double.IsNaN(codePoint)
  79. || TypeConverter.ToInt32(codePoint) != codePoint)
  80. {
  81. ExceptionHelper.ThrowRangeError(_realm, "Invalid code point " + codePoint);
  82. }
  83. var point = (uint) codePoint;
  84. if (point <= 0xFFFF)
  85. {
  86. // BMP code point
  87. codeUnits.Add(JsNumber.Create(point));
  88. }
  89. else
  90. {
  91. // Astral code point; split in surrogate halves
  92. // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
  93. point -= 0x10000;
  94. codeUnits.Add(JsNumber.Create((point >> 10) + 0xD800)); // highSurrogate
  95. codeUnits.Add(JsNumber.Create((point % 0x400) + 0xDC00)); // lowSurrogate
  96. }
  97. if (codeUnits.Count >= 0x3fff)
  98. {
  99. result += FromCharCode(null, codeUnits.ToArray());
  100. codeUnits.Clear();
  101. }
  102. }
  103. return result + FromCharCode(null, codeUnits.ToArray());
  104. }
  105. /// <summary>
  106. /// https://www.ecma-international.org/ecma-262/6.0/#sec-string.raw
  107. /// </summary>
  108. private JsValue Raw(JsValue thisObject, JsValue[] arguments)
  109. {
  110. var cooked = TypeConverter.ToObject(_realm, arguments.At(0));
  111. var raw = TypeConverter.ToObject(_realm, cooked.Get(JintTaggedTemplateExpression.PropertyRaw));
  112. var operations = ArrayOperations.For(raw);
  113. var length = operations.GetLength();
  114. if (length <= 0)
  115. {
  116. return JsString.Empty;
  117. }
  118. using var result = StringBuilderPool.Rent();
  119. for (var i = 0; i < length; i++)
  120. {
  121. if (i > 0)
  122. {
  123. if (i < arguments.Length && !arguments[i].IsUndefined())
  124. {
  125. result.Builder.Append(TypeConverter.ToString(arguments[i]));
  126. }
  127. }
  128. result.Builder.Append(TypeConverter.ToString(operations.Get((ulong) i)));
  129. }
  130. return result.ToString();
  131. }
  132. protected internal override JsValue Call(JsValue thisObject, JsValue[] arguments)
  133. {
  134. if (arguments.Length == 0)
  135. {
  136. return JsString.Empty;
  137. }
  138. var arg = arguments[0];
  139. var str = arg is JsSymbol s
  140. ? s.ToString()
  141. : TypeConverter.ToString(arg);
  142. return JsString.Create(str);
  143. }
  144. /// <summary>
  145. /// https://tc39.es/ecma262/#sec-string-constructor-string-value
  146. /// </summary>
  147. public override ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  148. {
  149. JsString s;
  150. if (arguments.Length == 0)
  151. {
  152. s = JsString.Empty;
  153. }
  154. else
  155. {
  156. var value = arguments.At(0);
  157. if (newTarget.IsUndefined() && value.IsSymbol())
  158. {
  159. return StringCreate(JsString.Create(((JsSymbol) value).ToString()), PrototypeObject);
  160. }
  161. s = TypeConverter.ToJsString(arguments[0]);
  162. }
  163. if (newTarget.IsUndefined())
  164. {
  165. return StringCreate(s, PrototypeObject);
  166. }
  167. return StringCreate(s, GetPrototypeFromConstructor(newTarget, static intrinsics => intrinsics.String.PrototypeObject));
  168. }
  169. public StringInstance Construct(string value)
  170. {
  171. return Construct(JsString.Create(value));
  172. }
  173. public StringInstance Construct(JsString value)
  174. {
  175. return StringCreate(value, PrototypeObject);
  176. }
  177. /// <summary>
  178. /// https://tc39.es/ecma262/#sec-stringcreate
  179. /// </summary>
  180. private StringInstance StringCreate(JsString value, ObjectInstance prototype)
  181. {
  182. var instance = new StringInstance(Engine, value)
  183. {
  184. _prototype = prototype
  185. };
  186. return instance;
  187. }
  188. }
  189. }