StringConstructor.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Jint.Native.Function;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. namespace Jint.Native.String
  5. {
  6. public sealed class StringConstructor : FunctionInstance, IConstructor
  7. {
  8. public StringConstructor(Engine engine)
  9. : base(engine, null, null, false)
  10. {
  11. }
  12. public static StringConstructor CreateStringConstructor(Engine engine)
  13. {
  14. var obj = new StringConstructor(engine);
  15. obj.Extensible = true;
  16. // The value of the [[Prototype]] internal property of the String constructor is the Function prototype object
  17. obj.Prototype = engine.Function.PrototypeObject;
  18. obj.PrototypeObject = StringPrototype.CreatePrototypeObject(engine, obj);
  19. obj.FastAddProperty("length", 1, false, false, false);
  20. // The initial value of String.prototype is the String prototype object
  21. obj.FastAddProperty("prototype", obj.PrototypeObject, false, false, false);
  22. return obj;
  23. }
  24. public override object Call(object thisObject, object[] arguments)
  25. {
  26. if (arguments.Length == 0)
  27. {
  28. return "";
  29. }
  30. return TypeConverter.ToString(arguments[0]);
  31. }
  32. /// <summary>
  33. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.7.2.1
  34. /// </summary>
  35. /// <param name="arguments"></param>
  36. /// <returns></returns>
  37. public ObjectInstance Construct(object[] arguments)
  38. {
  39. return Construct(arguments.Length > 0 ? TypeConverter.ToString(arguments[0]) : "");
  40. }
  41. public ObjectInstance PrototypeObject { get; private set; }
  42. public StringInstance Construct(string value)
  43. {
  44. var instance = new StringInstance(Engine);
  45. instance.Prototype = PrototypeObject;
  46. instance.PrimitiveValue = value;
  47. instance.Extensible = true;
  48. return instance;
  49. }
  50. }
  51. }