StringConstructor.cs 7.9 KB

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