StringConstructor.cs 7.0 KB

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