StringConstructor.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.Native.Symbol;
  7. using Jint.Pooling;
  8. using Jint.Runtime;
  9. using Jint.Runtime.Descriptors;
  10. using Jint.Runtime.Interop;
  11. using Jint.Runtime.Interpreter.Expressions;
  12. namespace Jint.Native.String
  13. {
  14. public sealed class StringConstructor : FunctionInstance, IConstructor
  15. {
  16. private static readonly JsString _functionName = new JsString("String");
  17. public StringConstructor(
  18. Engine engine,
  19. Realm realm,
  20. FunctionPrototype functionPrototype,
  21. ObjectPrototype objectPrototype)
  22. : base(engine, realm, _functionName, FunctionThisMode.Global)
  23. {
  24. _prototype = functionPrototype;
  25. PrototypeObject = new StringPrototype(engine, realm, this, objectPrototype);
  26. _length = new PropertyDescriptor(JsNumber.One, PropertyFlag.Configurable);
  27. _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
  28. }
  29. public StringPrototype PrototypeObject { get; }
  30. protected override void Initialize()
  31. {
  32. var properties = new PropertyDictionary(3, checkExistingKeys: false)
  33. {
  34. ["fromCharCode"] = new PropertyDescriptor(new PropertyDescriptor(new ClrFunctionInstance(Engine, "fromCharCode", FromCharCode, 1), PropertyFlag.NonEnumerable)),
  35. ["fromCodePoint"] = new PropertyDescriptor(new PropertyDescriptor(new ClrFunctionInstance(Engine, "fromCodePoint", FromCodePoint, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable)),
  36. ["raw"] = new PropertyDescriptor(new PropertyDescriptor(new ClrFunctionInstance(Engine, "raw", Raw, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable))
  37. };
  38. SetProperties(properties);
  39. }
  40. private static JsValue FromCharCode(JsValue thisObj, JsValue[] arguments)
  41. {
  42. var chars = new char[arguments.Length];
  43. for (var i = 0; i < chars.Length; i++ )
  44. {
  45. chars[i] = (char)TypeConverter.ToUint16(arguments[i]);
  46. }
  47. return JsString.Create(new string(chars));
  48. }
  49. private JsValue FromCodePoint(JsValue thisObj, JsValue[] arguments)
  50. {
  51. var codeUnits = new List<JsValue>();
  52. string result = "";
  53. foreach (var a in arguments)
  54. {
  55. var codePoint = TypeConverter.ToNumber(a);
  56. if (codePoint < 0
  57. || codePoint > 0x10FFFF
  58. || double.IsInfinity(codePoint)
  59. || double.IsNaN(codePoint)
  60. || TypeConverter.ToInt32(codePoint) != codePoint)
  61. {
  62. ExceptionHelper.ThrowRangeError(_realm, "Invalid code point " + codePoint);
  63. }
  64. var point = (uint) codePoint;
  65. if (point <= 0xFFFF)
  66. {
  67. // BMP code point
  68. codeUnits.Add(JsNumber.Create(point));
  69. }
  70. else
  71. {
  72. // Astral code point; split in surrogate halves
  73. // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
  74. point -= 0x10000;
  75. codeUnits.Add(JsNumber.Create((point >> 10) + 0xD800)); // highSurrogate
  76. codeUnits.Add(JsNumber.Create((point % 0x400) + 0xDC00)); // lowSurrogate
  77. }
  78. if (codeUnits.Count >= 0x3fff)
  79. {
  80. result += FromCharCode(null, codeUnits.ToArray());
  81. codeUnits.Clear();
  82. }
  83. }
  84. return result + FromCharCode(null, codeUnits.ToArray());
  85. }
  86. /// <summary>
  87. /// https://www.ecma-international.org/ecma-262/6.0/#sec-string.raw
  88. /// </summary>
  89. private JsValue Raw(JsValue thisObj, JsValue[] arguments)
  90. {
  91. var cooked = TypeConverter.ToObject(_realm, arguments.At(0));
  92. var raw = TypeConverter.ToObject(_realm, cooked.Get(JintTaggedTemplateExpression.PropertyRaw, cooked));
  93. var operations = ArrayOperations.For(raw);
  94. var length = operations.GetLength();
  95. if (length <= 0)
  96. {
  97. return JsString.Empty;
  98. }
  99. using var result = StringBuilderPool.Rent();
  100. for (var i = 0; i < length; i++)
  101. {
  102. if (i > 0)
  103. {
  104. if (i < arguments.Length && !arguments[i].IsUndefined())
  105. {
  106. result.Builder.Append(TypeConverter.ToString(arguments[i]));
  107. }
  108. }
  109. result.Builder.Append(TypeConverter.ToString(operations.Get((ulong) i)));
  110. }
  111. return result.ToString();
  112. }
  113. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  114. {
  115. if (arguments.Length == 0)
  116. {
  117. return JsString.Empty;
  118. }
  119. var arg = arguments[0];
  120. var str = arg is JsSymbol s
  121. ? s.ToString()
  122. : TypeConverter.ToString(arg);
  123. return JsString.Create(str);
  124. }
  125. /// <summary>
  126. /// https://tc39.es/ecma262/#sec-string-constructor-string-value
  127. /// </summary>
  128. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  129. {
  130. JsString s;
  131. if (arguments.Length == 0)
  132. {
  133. s = JsString.Empty;
  134. }
  135. else
  136. {
  137. var value = arguments.At(0);
  138. if (newTarget.IsUndefined() && value.IsSymbol())
  139. {
  140. return StringCreate(JsString.Create(SymbolPrototype.SymbolDescriptiveString((JsSymbol) value)), PrototypeObject);
  141. }
  142. s = TypeConverter.ToJsString(arguments[0]);
  143. }
  144. if (newTarget.IsUndefined())
  145. {
  146. return StringCreate(s, PrototypeObject);
  147. }
  148. return StringCreate(s, GetPrototypeFromConstructor(newTarget, static intrinsics => intrinsics.String.PrototypeObject));
  149. }
  150. public StringInstance Construct(string value)
  151. {
  152. return Construct(JsString.Create(value));
  153. }
  154. public StringInstance Construct(JsString value)
  155. {
  156. return StringCreate(value, PrototypeObject);
  157. }
  158. /// <summary>
  159. /// https://tc39.es/ecma262/#sec-stringcreate
  160. /// </summary>
  161. private StringInstance StringCreate(JsString value, ObjectInstance prototype)
  162. {
  163. var instance = new StringInstance(Engine)
  164. {
  165. _prototype = prototype,
  166. PrimitiveValue = value,
  167. _length = PropertyDescriptor.AllForbiddenDescriptor.ForNumber(value.Length)
  168. };
  169. return instance;
  170. }
  171. }
  172. }