StringConstructor.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. namespace Jint.Native.String
  11. {
  12. public sealed class StringConstructor : FunctionInstance, IConstructor
  13. {
  14. private static readonly JsString _functionName = new JsString("String");
  15. public StringConstructor(Engine engine)
  16. : base(engine, _functionName, strict: false)
  17. {
  18. }
  19. public static StringConstructor CreateStringConstructor(Engine engine)
  20. {
  21. var obj = new StringConstructor(engine)
  22. {
  23. _prototype = engine.Function.PrototypeObject
  24. };
  25. // The value of the [[Prototype]] internal property of the String constructor is the Function prototype object
  26. obj.PrototypeObject = StringPrototype.CreatePrototypeObject(engine, obj);
  27. obj._length = PropertyDescriptor.AllForbiddenDescriptor.NumberOne;
  28. // The initial value of String.prototype is the String prototype object
  29. obj._prototypeDescriptor = new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden);
  30. return obj;
  31. }
  32. protected override void Initialize()
  33. {
  34. _properties = new StringDictionarySlim<PropertyDescriptor>(3)
  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. }
  41. private static JsValue FromCharCode(JsValue thisObj, JsValue[] arguments)
  42. {
  43. var chars = new char[arguments.Length];
  44. for (var i = 0; i < chars.Length; i++ )
  45. {
  46. chars[i] = (char)TypeConverter.ToUint16(arguments[i]);
  47. }
  48. return JsString.Create(new string(chars));
  49. }
  50. private static JsValue FromCodePoint(JsValue thisObj, JsValue[] arguments)
  51. {
  52. var codeUnits = new List<JsValue>();
  53. string result = "";
  54. for (var i = 0; i < arguments.Length; i++ )
  55. {
  56. var codePoint = TypeConverter.ToNumber(arguments[i]);
  57. if (codePoint < 0
  58. || codePoint > 0x10FFFF
  59. || double.IsInfinity(codePoint)
  60. || double.IsNaN(codePoint)
  61. || TypeConverter.ToInt32(codePoint) != codePoint)
  62. {
  63. return ExceptionHelper.ThrowRangeErrorNoEngine<JsValue>("Invalid code point " + codePoint);
  64. }
  65. var point = (uint) codePoint;
  66. if (point <= 0xFFFF)
  67. {
  68. // BMP code point
  69. codeUnits.Add(JsNumber.Create(point));
  70. }
  71. else
  72. {
  73. // Astral code point; split in surrogate halves
  74. // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
  75. point -= 0x10000;
  76. codeUnits.Add(JsNumber.Create((point >> 10) + 0xD800)); // highSurrogate
  77. codeUnits.Add(JsNumber.Create((point % 0x400) + 0xDC00)); // lowSurrogate
  78. }
  79. if (codeUnits.Count >= 0x3fff)
  80. {
  81. result += FromCharCode(null, codeUnits.ToArray());
  82. codeUnits.Clear();
  83. }
  84. }
  85. return result + FromCharCode(null, codeUnits.ToArray());
  86. }
  87. /// <summary>
  88. /// https://www.ecma-international.org/ecma-262/6.0/#sec-string.raw
  89. /// </summary>
  90. private JsValue Raw(JsValue thisObj, JsValue[] arguments)
  91. {
  92. var cooked = TypeConverter.ToObject(_engine, arguments.At(0));
  93. var raw = TypeConverter.ToObject(_engine, cooked.Get("raw", cooked));
  94. var operations = ArrayOperations.For(raw);
  95. var length = operations.GetLength();
  96. if (length <= 0)
  97. {
  98. return JsString.Empty;
  99. }
  100. using (var result = StringBuilderPool.Rent())
  101. {
  102. for (var i = 0; i < length; i++)
  103. {
  104. if (i > 0)
  105. {
  106. if (i < arguments.Length && !arguments[i].IsUndefined())
  107. {
  108. result.Builder.Append(TypeConverter.ToString(arguments[i]));
  109. }
  110. }
  111. result.Builder.Append(TypeConverter.ToString(operations.Get((ulong) i)));
  112. }
  113. return result.ToString();
  114. }
  115. }
  116. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  117. {
  118. if (arguments.Length == 0)
  119. {
  120. return JsString.Empty;
  121. }
  122. var arg = arguments[0];
  123. var str = arg is JsSymbol s
  124. ? s.ToString()
  125. : TypeConverter.ToString(arg);
  126. return JsString.Create(str);
  127. }
  128. /// <summary>
  129. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.7.2.1
  130. /// </summary>
  131. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  132. {
  133. string value = "";
  134. if (arguments.Length > 0)
  135. {
  136. value = TypeConverter.ToString(arguments[0]);
  137. }
  138. return Construct(value);
  139. }
  140. public StringPrototype PrototypeObject { get; private set; }
  141. public StringInstance Construct(string value)
  142. {
  143. return Construct(JsString.Create(value));
  144. }
  145. public StringInstance Construct(JsString value)
  146. {
  147. var instance = new StringInstance(Engine)
  148. {
  149. _prototype = PrototypeObject,
  150. PrimitiveValue = value,
  151. _length = PropertyDescriptor.AllForbiddenDescriptor.ForNumber(value.Length)
  152. };
  153. return instance;
  154. }
  155. }
  156. }