StringConstructor.cs 6.3 KB

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