ObjectPrototype.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using Jint.Runtime;
  3. using Jint.Runtime.Descriptors;
  4. using Jint.Runtime.Interop;
  5. namespace Jint.Native.Object
  6. {
  7. public sealed class ObjectPrototype : ObjectInstance
  8. {
  9. private ObjectPrototype(Engine engine) : base(engine)
  10. {
  11. }
  12. public static ObjectPrototype CreatePrototypeObject(Engine engine, ObjectConstructor objectConstructor)
  13. {
  14. var obj = new ObjectPrototype(engine) { Extensible = true };
  15. obj.FastAddProperty("constructor", objectConstructor, false, false, false);
  16. return obj;
  17. }
  18. public void Configure()
  19. {
  20. FastAddProperty("toString", new ClrFunctionInstance<object, string>(Engine, ToString), false, false, false);
  21. FastAddProperty("toLocaleString", new ClrFunctionInstance<object, string>(Engine, ToLocaleString), false, false, false);
  22. FastAddProperty("valueOf", new ClrFunctionInstance<object, object>(Engine, ValueOf), false, false, false);
  23. FastAddProperty("hasOwnProperty", new ClrFunctionInstance<object, bool>(Engine, HasOwnProperty), false, false, false);
  24. FastAddProperty("isPrototypeOf", new ClrFunctionInstance<object, bool>(Engine, IsPrototypeOf), false, false, false);
  25. FastAddProperty("propertyIsEnumerable", new ClrFunctionInstance<object, bool>(Engine, PropertyIsEnumerable), false, false, false);
  26. }
  27. private bool PropertyIsEnumerable(object thisObject, object[] arguments)
  28. {
  29. throw new NotImplementedException();
  30. }
  31. private object ValueOf(object thisObject, object[] arguments)
  32. {
  33. throw new NotImplementedException();
  34. }
  35. private bool IsPrototypeOf(object thisObject, object[] arguments)
  36. {
  37. throw new NotImplementedException();
  38. }
  39. private string ToLocaleString(object thisObject, object[] arguments)
  40. {
  41. throw new NotImplementedException();
  42. }
  43. /// <summary>
  44. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.4.2
  45. /// </summary>
  46. /// <param name="thisObject"></param>
  47. /// <param name="arguments"></param>
  48. /// <returns></returns>
  49. public string ToString(object thisObject, object[] arguments)
  50. {
  51. if (thisObject == Undefined.Instance)
  52. {
  53. return "[object Undefined]";
  54. }
  55. if (thisObject == Null.Instance)
  56. {
  57. return "[object Null]";
  58. }
  59. var o = TypeConverter.ToObject(Engine, thisObject);
  60. return "[object " + o.Class + "]";
  61. }
  62. /// <summary>
  63. /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.4.5
  64. /// </summary>
  65. /// <param name="thisObject"></param>
  66. /// <param name="arguments"></param>
  67. /// <returns></returns>
  68. public bool HasOwnProperty(object thisObject, object[] arguments)
  69. {
  70. var p = TypeConverter.ToString(arguments[0]);
  71. var o = TypeConverter.ToObject(Engine, thisObject);
  72. var desc = o.GetOwnProperty(p);
  73. return desc != PropertyDescriptor.Undefined;
  74. }
  75. }
  76. }