ReflectInstance.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using Jint.Collections;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. using Jint.Runtime.Interop;
  6. namespace Jint.Native.Reflect
  7. {
  8. /// <summary>
  9. /// https://www.ecma-international.org/ecma-262/6.0/index.html#sec-reflect-object
  10. /// </summary>
  11. public sealed class ReflectInstance : ObjectInstance
  12. {
  13. private ReflectInstance(Engine engine) : base(engine, "Reflect")
  14. {
  15. }
  16. public static ReflectInstance CreateReflectObject(Engine engine)
  17. {
  18. var math = new ReflectInstance(engine)
  19. {
  20. _prototype = engine.Object.PrototypeObject
  21. };
  22. return math;
  23. }
  24. protected override void Initialize()
  25. {
  26. _properties = new StringDictionarySlim<PropertyDescriptor>(14)
  27. {
  28. ["apply"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "apply", Apply, 3, PropertyFlag.Configurable), true, false, true),
  29. ["construct"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "construct", Construct, 2, PropertyFlag.Configurable), true, false, true),
  30. ["defineProperty"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "defineProperty", DefineProperty, 3, PropertyFlag.Configurable), true, false, true),
  31. ["deleteProperty"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "deleteProperty", DeleteProperty, 2, PropertyFlag.Configurable), true, false, true),
  32. ["get"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "get", Get, 2, PropertyFlag.Configurable), true, false, true),
  33. ["getOwnPropertyDescriptor"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertyDescriptor", GetOwnPropertyDescriptor, 2, PropertyFlag.Configurable), true, false, true),
  34. ["getPrototypeOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getPrototypeOf", GetPrototypeOf, 1, PropertyFlag.Configurable), true, false, true),
  35. ["has"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "has", Has, 2, PropertyFlag.Configurable), true, false, true),
  36. ["isExtensible"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isExtensible", IsExtensible, 1, PropertyFlag.Configurable), true, false, true),
  37. ["ownKeys"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "ownKeys", OwnKeys, 1, PropertyFlag.Configurable), true, false, true),
  38. ["preventExtensions"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "preventExtensions", PreventExtensions, 1, PropertyFlag.Configurable), true, false, true),
  39. ["set"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "set", Set, 3, PropertyFlag.Configurable), true, false, true),
  40. ["setPrototypeOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setPrototypeOf", SetPrototypeOf, 2, PropertyFlag.Configurable), true, false, true),
  41. };
  42. }
  43. private JsValue Apply(JsValue thisObject, JsValue[] arguments)
  44. {
  45. return _engine.Function.PrototypeObject.Apply(arguments.At(0), new[]
  46. {
  47. arguments.At(1),
  48. arguments.At(2)
  49. });
  50. }
  51. private JsValue Construct(JsValue thisObject, JsValue[] arguments)
  52. {
  53. var targetArgument = arguments.At(0);
  54. if (!(targetArgument is IConstructor target))
  55. {
  56. return ExceptionHelper.ThrowTypeError<JsValue>(_engine, targetArgument + " is not a constructor");
  57. }
  58. var newTargetArgument = arguments.At(2, arguments[0]);
  59. if (!(newTargetArgument is IConstructor newTarget))
  60. {
  61. return ExceptionHelper.ThrowTypeError<JsValue>(_engine, newTargetArgument + " is not a constructor");
  62. }
  63. var args = _engine.Function.PrototypeObject.CreateListFromArrayLike(arguments.At(1));
  64. return target.Construct(args, newTargetArgument);
  65. }
  66. private JsValue DefineProperty(JsValue thisObject, JsValue[] arguments)
  67. {
  68. var o = arguments.As<ObjectInstance>(0, _engine);
  69. var p = arguments.At(1);
  70. var name = TypeConverter.ToPropertyKey(p);
  71. var attributes = arguments.At(2);
  72. var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, attributes);
  73. return o.DefineOwnProperty(name, desc);
  74. }
  75. private JsValue DeleteProperty(JsValue thisObject, JsValue[] arguments)
  76. {
  77. var target = arguments.At(0);
  78. if (!(target is ObjectInstance o))
  79. {
  80. return ExceptionHelper.ThrowTypeError<JsValue>(_engine, "Reflect.deleteProperty called on non-object");
  81. }
  82. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  83. return o.Delete(property) ? JsBoolean.True : JsBoolean.False;
  84. }
  85. private JsValue Has(JsValue thisObject, JsValue[] arguments)
  86. {
  87. var target = arguments.At(0);
  88. if (!(target is ObjectInstance o))
  89. {
  90. return ExceptionHelper.ThrowTypeError<JsValue>(_engine, "Reflect.has called on non-object");
  91. }
  92. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  93. return o.HasProperty(property) ? JsBoolean.True : JsBoolean.False;
  94. }
  95. private JsValue Set(JsValue thisObject, JsValue[] arguments)
  96. {
  97. var target = arguments.At(0);
  98. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  99. var value = arguments.At(2);
  100. var receiver = arguments.At(3, target);
  101. if (!(target is ObjectInstance o))
  102. {
  103. return ExceptionHelper.ThrowTypeError<JsValue>(_engine, "Reflect.set called on non-object");
  104. }
  105. return o.Set(property, value, receiver);
  106. }
  107. private JsValue Get(JsValue thisObject, JsValue[] arguments)
  108. {
  109. var target = arguments.At(0);
  110. if (!(target is ObjectInstance o))
  111. {
  112. return ExceptionHelper.ThrowTypeError<JsValue>(_engine, "Reflect.get called on non-object");
  113. }
  114. var receiver = arguments.At(2, target);
  115. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  116. return o.Get(property, receiver);
  117. }
  118. private JsValue GetOwnPropertyDescriptor(JsValue thisObject, JsValue[] arguments)
  119. {
  120. return _engine.Object.GetOwnPropertyDescriptor(Undefined, arguments);
  121. }
  122. private JsValue OwnKeys(JsValue thisObject, JsValue[] arguments)
  123. {
  124. var target = arguments.At(0);
  125. if (!(target is ObjectInstance o))
  126. {
  127. return ExceptionHelper.ThrowTypeError<JsValue>(_engine, "Reflect.get called on non-object");
  128. }
  129. var keys = o.GetOwnPropertyKeys(Types.String | Types.Symbol);
  130. return _engine.Array.CreateArrayFromList(keys);
  131. }
  132. private JsValue IsExtensible(JsValue thisObject, JsValue[] arguments)
  133. {
  134. return _engine.Object.IsExtensible(Undefined, arguments);
  135. }
  136. private JsValue PreventExtensions(JsValue thisObject, JsValue[] arguments)
  137. {
  138. var target = arguments.At(0);
  139. if (!(target is ObjectInstance o))
  140. {
  141. return ExceptionHelper.ThrowTypeError<JsValue>(_engine, "Reflect.preventExtensions called on non-object");
  142. }
  143. return o.PreventExtensions();
  144. }
  145. private JsValue GetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  146. {
  147. var target = arguments.At(0);
  148. if (!target.IsObject())
  149. {
  150. return ExceptionHelper.ThrowTypeError<JsValue>(_engine, "Reflect.getPrototypeOf called on non-object");
  151. }
  152. return _engine.Object.GetPrototypeOf(Undefined, arguments);
  153. }
  154. private JsValue SetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  155. {
  156. var target = arguments.At(0);
  157. if (!(target is ObjectInstance o))
  158. {
  159. return ExceptionHelper.ThrowTypeError<JsValue>(_engine, "Reflect.setPrototypeOf called on non-object");
  160. }
  161. var prototype = arguments.At(1);
  162. if (!prototype.IsObject() && !prototype.IsNull())
  163. {
  164. ExceptionHelper.ThrowTypeError(_engine, $"Object prototype may only be an Object or null: {prototype}");
  165. }
  166. return o.SetPrototypeOf(prototype);
  167. }
  168. }
  169. }