2
0

StringConstructor.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System.Collections.Generic;
  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. public sealed class StringConstructor : FunctionInstance, IConstructor
  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, FunctionThisMode.Global)
  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. var elements = new char[length];
  53. for (var i = 0; i < elements.Length; i++ )
  54. {
  55. var nextCu = TypeConverter.ToUint16(arguments[i]);
  56. elements[i] = (char) nextCu;
  57. }
  58. return JsString.Create(new string(elements));
  59. }
  60. private JsValue FromCodePoint(JsValue thisObj, JsValue[] arguments)
  61. {
  62. var codeUnits = new List<JsValue>();
  63. string result = "";
  64. foreach (var a in arguments)
  65. {
  66. var codePoint = TypeConverter.ToNumber(a);
  67. if (codePoint < 0
  68. || codePoint > 0x10FFFF
  69. || double.IsInfinity(codePoint)
  70. || double.IsNaN(codePoint)
  71. || TypeConverter.ToInt32(codePoint) != codePoint)
  72. {
  73. ExceptionHelper.ThrowRangeError(_realm, "Invalid code point " + codePoint);
  74. }
  75. var point = (uint) codePoint;
  76. if (point <= 0xFFFF)
  77. {
  78. // BMP code point
  79. codeUnits.Add(JsNumber.Create(point));
  80. }
  81. else
  82. {
  83. // Astral code point; split in surrogate halves
  84. // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
  85. point -= 0x10000;
  86. codeUnits.Add(JsNumber.Create((point >> 10) + 0xD800)); // highSurrogate
  87. codeUnits.Add(JsNumber.Create((point % 0x400) + 0xDC00)); // lowSurrogate
  88. }
  89. if (codeUnits.Count >= 0x3fff)
  90. {
  91. result += FromCharCode(null, codeUnits.ToArray());
  92. codeUnits.Clear();
  93. }
  94. }
  95. return result + FromCharCode(null, codeUnits.ToArray());
  96. }
  97. /// <summary>
  98. /// https://www.ecma-international.org/ecma-262/6.0/#sec-string.raw
  99. /// </summary>
  100. private JsValue Raw(JsValue thisObj, JsValue[] arguments)
  101. {
  102. var cooked = TypeConverter.ToObject(_realm, arguments.At(0));
  103. var raw = TypeConverter.ToObject(_realm, cooked.Get(JintTaggedTemplateExpression.PropertyRaw, cooked));
  104. var operations = ArrayOperations.For(raw);
  105. var length = operations.GetLength();
  106. if (length <= 0)
  107. {
  108. return JsString.Empty;
  109. }
  110. using var result = StringBuilderPool.Rent();
  111. for (var i = 0; i < length; i++)
  112. {
  113. if (i > 0)
  114. {
  115. if (i < arguments.Length && !arguments[i].IsUndefined())
  116. {
  117. result.Builder.Append(TypeConverter.ToString(arguments[i]));
  118. }
  119. }
  120. result.Builder.Append(TypeConverter.ToString(operations.Get((ulong) i)));
  121. }
  122. return result.ToString();
  123. }
  124. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  125. {
  126. if (arguments.Length == 0)
  127. {
  128. return JsString.Empty;
  129. }
  130. var arg = arguments[0];
  131. var str = arg is JsSymbol s
  132. ? s.ToString()
  133. : TypeConverter.ToString(arg);
  134. return JsString.Create(str);
  135. }
  136. /// <summary>
  137. /// https://tc39.es/ecma262/#sec-string-constructor-string-value
  138. /// </summary>
  139. ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget)
  140. {
  141. JsString s;
  142. if (arguments.Length == 0)
  143. {
  144. s = JsString.Empty;
  145. }
  146. else
  147. {
  148. var value = arguments.At(0);
  149. if (newTarget.IsUndefined() && value.IsSymbol())
  150. {
  151. return StringCreate(JsString.Create(((JsSymbol) value).ToString()), PrototypeObject);
  152. }
  153. s = TypeConverter.ToJsString(arguments[0]);
  154. }
  155. if (newTarget.IsUndefined())
  156. {
  157. return StringCreate(s, PrototypeObject);
  158. }
  159. return StringCreate(s, GetPrototypeFromConstructor(newTarget, static intrinsics => intrinsics.String.PrototypeObject));
  160. }
  161. public StringInstance Construct(string value)
  162. {
  163. return Construct(JsString.Create(value));
  164. }
  165. public StringInstance Construct(JsString value)
  166. {
  167. return StringCreate(value, PrototypeObject);
  168. }
  169. /// <summary>
  170. /// https://tc39.es/ecma262/#sec-stringcreate
  171. /// </summary>
  172. private StringInstance StringCreate(JsString value, ObjectInstance prototype)
  173. {
  174. var instance = new StringInstance(Engine)
  175. {
  176. _prototype = prototype,
  177. StringData = value,
  178. _length = PropertyDescriptor.AllForbiddenDescriptor.ForNumber(value.Length)
  179. };
  180. return instance;
  181. }
  182. }
  183. }