NumberPrototype.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using Jint.Runtime;
  2. using Jint.Runtime.Interop;
  3. namespace Jint.Native.Number
  4. {
  5. /// <summary>
  6. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.7.4
  7. /// </summary>
  8. public sealed class NumberPrototype : NumberInstance
  9. {
  10. private NumberPrototype(Engine engine)
  11. : base(engine)
  12. {
  13. }
  14. public static NumberPrototype CreatePrototypeObject(Engine engine, NumberConstructor numberConstructor)
  15. {
  16. var obj = new NumberPrototype(engine);
  17. obj.Prototype = engine.Object.PrototypeObject;
  18. obj.PrimitiveValue = 0;
  19. obj.Extensible = true;
  20. obj.FastAddProperty("constructor", numberConstructor, false, false, false);
  21. return obj;
  22. }
  23. public void Configure()
  24. {
  25. FastAddProperty("toString", new ClrFunctionInstance<object, object>(Engine, ToNumberString), true, false, true);
  26. FastAddProperty("toLocaleString", new ClrFunctionInstance<object, object>(Engine, ToLocaleString), true, false, true);
  27. FastAddProperty("valueOf", new ClrFunctionInstance<object, object>(Engine, ValueOf), true, false, true);
  28. FastAddProperty("toFixed", new ClrFunctionInstance<object, object>(Engine, ToFixed), true, false, true);
  29. FastAddProperty("toExponential", new ClrFunctionInstance<object, object>(Engine, ToExponential), true, false, true);
  30. FastAddProperty("toPrecision", new ClrFunctionInstance<object, object>(Engine, ToPrecision), true, false, true);
  31. }
  32. private object ToLocaleString(object thisObj, object[] arguments)
  33. {
  34. throw new System.NotImplementedException();
  35. }
  36. private object ValueOf(object thisObj, object[] arguments)
  37. {
  38. var number = thisObj as NumberInstance;
  39. if (number == null)
  40. {
  41. throw new JavaScriptException(Engine.TypeError);
  42. }
  43. return number.PrimitiveValue;
  44. }
  45. private object ToFixed(object thisObj, object[] arguments)
  46. {
  47. throw new System.NotImplementedException();
  48. }
  49. private object ToExponential(object thisObj, object[] arguments)
  50. {
  51. throw new System.NotImplementedException();
  52. }
  53. private object ToPrecision(object thisObj, object[] arguments)
  54. {
  55. throw new System.NotImplementedException();
  56. }
  57. private object ToNumberString(object thisObject, object[] arguments)
  58. {
  59. if (TypeConverter.GetType(thisObject) != Types.Number)
  60. {
  61. throw new JavaScriptException(Engine.TypeError);
  62. }
  63. return TypeConverter.ToString(thisObject);
  64. }
  65. }
  66. }