StringConstructor.cs 2.3 KB

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