StringConstructor.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #pragma warning disable CA1859 // Use concrete types when possible for improved performance -- most of prototype methods return JsValue
  2. using System.Text;
  3. using Jint.Collections;
  4. using Jint.Native.Array;
  5. using Jint.Native.Function;
  6. using Jint.Native.Object;
  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 ClrFunction(Engine, "fromCharCode", FromCharCode, 1), PropertyFlag.NonEnumerable)),
  37. ["fromCodePoint"] = new PropertyDescriptor(new PropertyDescriptor(new ClrFunction(Engine, "fromCodePoint", FromCodePoint, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable)),
  38. ["raw"] = new PropertyDescriptor(new PropertyDescriptor(new ClrFunction(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. /// <summary>
  69. /// https://tc39.es/ecma262/#sec-string.fromcodepoint
  70. /// </summary>
  71. private JsValue FromCodePoint(JsValue thisObject, JsValue[] arguments)
  72. {
  73. JsNumber codePoint;
  74. using var result = new ValueStringBuilder(stackalloc char[128]);
  75. foreach (var a in arguments)
  76. {
  77. int point;
  78. codePoint = TypeConverter.ToJsNumber(a);
  79. if (codePoint.IsInteger())
  80. {
  81. point = (int) codePoint._value;
  82. if (point is < 0 or > 0x10FFFF)
  83. {
  84. goto rangeError;
  85. }
  86. }
  87. else
  88. {
  89. var pointTemp = codePoint._value;
  90. if (pointTemp < 0 || pointTemp > 0x10FFFF || double.IsInfinity(pointTemp) || double.IsNaN(pointTemp) || TypeConverter.ToInt32(pointTemp) != pointTemp)
  91. {
  92. goto rangeError;
  93. }
  94. point = (int) pointTemp;
  95. }
  96. if (point <= 0xFFFF)
  97. {
  98. // BMP code point
  99. result.Append((char) point);
  100. }
  101. else
  102. {
  103. // Astral code point; split in surrogate halves
  104. // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
  105. point -= 0x10000;
  106. result.Append((char) ((point >> 10) + 0xD800)); // highSurrogate
  107. result.Append((char) (point % 0x400 + 0xDC00)); // lowSurrogate
  108. }
  109. }
  110. return JsString.Create(result.ToString());
  111. rangeError:
  112. _engine.SignalError(ExceptionHelper.CreateRangeError(_realm, "Invalid code point " + codePoint));
  113. return JsEmpty.Instance;
  114. }
  115. /// <summary>
  116. /// https://tc39.es/ecma262/#sec-string.raw
  117. /// </summary>
  118. private JsValue Raw(JsValue thisObject, JsValue[] arguments)
  119. {
  120. var cooked = TypeConverter.ToObject(_realm, arguments.At(0));
  121. var raw = cooked.Get(JintTaggedTemplateExpression.PropertyRaw);
  122. var operations = ArrayOperations.For(_realm, raw, forWrite: false);
  123. var length = operations.GetLength();
  124. if (length <= 0)
  125. {
  126. return JsString.Empty;
  127. }
  128. using var result = new ValueStringBuilder();
  129. for (var i = 0; i < length; i++)
  130. {
  131. if (i > 0)
  132. {
  133. if (i < arguments.Length && !arguments[i].IsUndefined())
  134. {
  135. result.Append(TypeConverter.ToString(arguments[i]));
  136. }
  137. }
  138. result.Append(TypeConverter.ToString(operations.Get((ulong) i)));
  139. }
  140. return result.ToString();
  141. }
  142. protected internal override JsValue Call(JsValue thisObject, JsValue[] arguments)
  143. {
  144. if (arguments.Length == 0)
  145. {
  146. return JsString.Empty;
  147. }
  148. var arg = arguments[0];
  149. var str = arg is JsSymbol s
  150. ? s.ToString()
  151. : TypeConverter.ToString(arg);
  152. return JsString.Create(str);
  153. }
  154. /// <summary>
  155. /// https://tc39.es/ecma262/#sec-string-constructor-string-value
  156. /// </summary>
  157. public override ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  158. {
  159. JsString s;
  160. if (arguments.Length == 0)
  161. {
  162. s = JsString.Empty;
  163. }
  164. else
  165. {
  166. var value = arguments.At(0);
  167. if (newTarget.IsUndefined() && value.IsSymbol())
  168. {
  169. return StringCreate(JsString.Create(((JsSymbol) value).ToString()), PrototypeObject);
  170. }
  171. s = TypeConverter.ToJsString(arguments[0]);
  172. }
  173. if (newTarget.IsUndefined())
  174. {
  175. return StringCreate(s, PrototypeObject);
  176. }
  177. return StringCreate(s, GetPrototypeFromConstructor(newTarget, static intrinsics => intrinsics.String.PrototypeObject));
  178. }
  179. public StringInstance Construct(JsString value)
  180. {
  181. return StringCreate(value, PrototypeObject);
  182. }
  183. /// <summary>
  184. /// https://tc39.es/ecma262/#sec-stringcreate
  185. /// </summary>
  186. private StringInstance StringCreate(JsString value, ObjectInstance prototype)
  187. {
  188. var instance = new StringInstance(Engine, value)
  189. {
  190. _prototype = prototype
  191. };
  192. return instance;
  193. }
  194. }
  195. }