StringConstructor.cs 7.2 KB

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