ReflectInstance.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #pragma warning disable CA1859 // Use concrete types when possible for improved performance -- most of prototype methods return JsValue
  2. using Jint.Collections;
  3. using Jint.Native.Function;
  4. using Jint.Native.Object;
  5. using Jint.Native.Symbol;
  6. using Jint.Runtime;
  7. using Jint.Runtime.Descriptors;
  8. using Jint.Runtime.Interop;
  9. namespace Jint.Native.Reflect;
  10. /// <summary>
  11. /// https://www.ecma-international.org/ecma-262/6.0/index.html#sec-reflect-object
  12. /// </summary>
  13. internal sealed class ReflectInstance : ObjectInstance
  14. {
  15. private readonly Realm _realm;
  16. internal ReflectInstance(
  17. Engine engine,
  18. Realm realm,
  19. ObjectPrototype objectPrototype) : base(engine)
  20. {
  21. _realm = realm;
  22. _prototype = objectPrototype;
  23. }
  24. protected override void Initialize()
  25. {
  26. var properties = new PropertyDictionary(14, checkExistingKeys: false)
  27. {
  28. ["apply"] = new PropertyDescriptor(new ClrFunction(Engine, "apply", Apply, 3, PropertyFlag.Configurable), true, false, true),
  29. ["construct"] = new PropertyDescriptor(new ClrFunction(Engine, "construct", Construct, 2, PropertyFlag.Configurable), true, false, true),
  30. ["defineProperty"] = new PropertyDescriptor(new ClrFunction(Engine, "defineProperty", DefineProperty, 3, PropertyFlag.Configurable), true, false, true),
  31. ["deleteProperty"] = new PropertyDescriptor(new ClrFunction(Engine, "deleteProperty", DeleteProperty, 2, PropertyFlag.Configurable), true, false, true),
  32. ["get"] = new PropertyDescriptor(new ClrFunction(Engine, "get", Get, 2, PropertyFlag.Configurable), true, false, true),
  33. ["getOwnPropertyDescriptor"] = new PropertyDescriptor(new ClrFunction(Engine, "getOwnPropertyDescriptor", GetOwnPropertyDescriptor, 2, PropertyFlag.Configurable), true, false, true),
  34. ["getPrototypeOf"] = new PropertyDescriptor(new ClrFunction(Engine, "getPrototypeOf", GetPrototypeOf, 1, PropertyFlag.Configurable), true, false, true),
  35. ["has"] = new PropertyDescriptor(new ClrFunction(Engine, "has", Has, 2, PropertyFlag.Configurable), true, false, true),
  36. ["isExtensible"] = new PropertyDescriptor(new ClrFunction(Engine, "isExtensible", IsExtensible, 1, PropertyFlag.Configurable), true, false, true),
  37. ["ownKeys"] = new PropertyDescriptor(new ClrFunction(Engine, "ownKeys", OwnKeys, 1, PropertyFlag.Configurable), true, false, true),
  38. ["preventExtensions"] = new PropertyDescriptor(new ClrFunction(Engine, "preventExtensions", PreventExtensions, 1, PropertyFlag.Configurable), true, false, true),
  39. ["set"] = new PropertyDescriptor(new ClrFunction(Engine, "set", Set, 3, PropertyFlag.Configurable), true, false, true),
  40. ["setPrototypeOf"] = new PropertyDescriptor(new ClrFunction(Engine, "setPrototypeOf", SetPrototypeOf, 2, PropertyFlag.Configurable), true, false, true),
  41. };
  42. SetProperties(properties);
  43. var symbols = new SymbolDictionary(1)
  44. {
  45. [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor("Reflect", false, false, true)
  46. };
  47. SetSymbols(symbols);
  48. }
  49. private JsValue Apply(JsValue thisObject, JsValue[] arguments)
  50. {
  51. var target = arguments.At(0);
  52. var thisArgument = arguments.At(1);
  53. var argumentsList = arguments.At(2);
  54. if (!target.IsCallable)
  55. {
  56. ExceptionHelper.ThrowTypeError(_realm);
  57. }
  58. var args = FunctionPrototype.CreateListFromArrayLike(_realm, argumentsList);
  59. // 3. Perform PrepareForTailCall().
  60. return ((ICallable) target).Call(thisArgument, args);
  61. }
  62. /// <summary>
  63. /// https://tc39.es/ecma262/#sec-reflect.construct
  64. /// </summary>
  65. private JsValue Construct(JsValue thisObject, JsValue[] arguments)
  66. {
  67. var target = AssertConstructor(_engine, arguments.At(0));
  68. var newTargetArgument = arguments.At(2, arguments[0]);
  69. AssertConstructor(_engine, newTargetArgument);
  70. var args = FunctionPrototype.CreateListFromArrayLike(_realm, arguments.At(1));
  71. return target.Construct(args, newTargetArgument);
  72. }
  73. /// <summary>
  74. /// https://tc39.es/ecma262/#sec-reflect.defineproperty
  75. /// </summary>
  76. private JsValue DefineProperty(JsValue thisObject, JsValue[] arguments)
  77. {
  78. var target = arguments.At(0) as ObjectInstance;
  79. if (target is null)
  80. {
  81. ExceptionHelper.ThrowTypeError(_realm, "Reflect.defineProperty called on non-object");
  82. }
  83. var propertyKey = arguments.At(1);
  84. var attributes = arguments.At(2);
  85. var key = TypeConverter.ToPropertyKey(propertyKey);
  86. var desc = PropertyDescriptor.ToPropertyDescriptor(_realm, attributes);
  87. return target.DefineOwnProperty(key, desc);
  88. }
  89. private JsValue DeleteProperty(JsValue thisObject, JsValue[] arguments)
  90. {
  91. var o = arguments.At(0) as ObjectInstance;
  92. if (o is null)
  93. {
  94. ExceptionHelper.ThrowTypeError(_realm, "Reflect.deleteProperty called on non-object");
  95. }
  96. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  97. return o.Delete(property) ? JsBoolean.True : JsBoolean.False;
  98. }
  99. private JsValue Has(JsValue thisObject, JsValue[] arguments)
  100. {
  101. var o = arguments.At(0) as ObjectInstance;
  102. if (o is null)
  103. {
  104. ExceptionHelper.ThrowTypeError(_realm, "Reflect.has called on non-object");
  105. }
  106. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  107. return o.HasProperty(property) ? JsBoolean.True : JsBoolean.False;
  108. }
  109. private JsValue Set(JsValue thisObject, JsValue[] arguments)
  110. {
  111. var target = arguments.At(0);
  112. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  113. var value = arguments.At(2);
  114. var receiver = arguments.At(3, target);
  115. var o = target as ObjectInstance;
  116. if (o is null)
  117. {
  118. ExceptionHelper.ThrowTypeError(_realm, "Reflect.set called on non-object");
  119. }
  120. return o.Set(property, value, receiver);
  121. }
  122. private JsValue Get(JsValue thisObject, JsValue[] arguments)
  123. {
  124. var target = arguments.At(0);
  125. var o = target as ObjectInstance;
  126. if (o is null)
  127. {
  128. ExceptionHelper.ThrowTypeError(_realm, "Reflect.get called on non-object");
  129. }
  130. var receiver = arguments.At(2, target);
  131. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  132. return o.Get(property, receiver);
  133. }
  134. private JsValue GetOwnPropertyDescriptor(JsValue thisObject, JsValue[] arguments)
  135. {
  136. if (!arguments.At(0).IsObject())
  137. {
  138. ExceptionHelper.ThrowTypeError(_realm, "Reflect.getOwnPropertyDescriptor called on non-object");
  139. }
  140. return _realm.Intrinsics.Object.GetOwnPropertyDescriptor(Undefined, arguments);
  141. }
  142. private JsValue OwnKeys(JsValue thisObject, JsValue[] arguments)
  143. {
  144. var o = arguments.At(0) as ObjectInstance;
  145. if (o is null)
  146. {
  147. ExceptionHelper.ThrowTypeError(_realm, "Reflect.get called on non-object");
  148. }
  149. var keys = o.GetOwnPropertyKeys();
  150. return _realm.Intrinsics.Array.CreateArrayFromList(keys);
  151. }
  152. private JsValue IsExtensible(JsValue thisObject, JsValue[] arguments)
  153. {
  154. var o = arguments.At(0) as ObjectInstance;
  155. if (o is null)
  156. {
  157. ExceptionHelper.ThrowTypeError(_realm, "Reflect.isExtensible called on non-object");
  158. }
  159. return o.Extensible;
  160. }
  161. private JsValue PreventExtensions(JsValue thisObject, JsValue[] arguments)
  162. {
  163. var o = arguments.At(0) as ObjectInstance;
  164. if (o is null)
  165. {
  166. ExceptionHelper.ThrowTypeError(_realm, "Reflect.preventExtensions called on non-object");
  167. }
  168. return o.PreventExtensions();
  169. }
  170. private JsValue GetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  171. {
  172. var target = arguments.At(0);
  173. if (!target.IsObject())
  174. {
  175. ExceptionHelper.ThrowTypeError(_realm, "Reflect.getPrototypeOf called on non-object");
  176. }
  177. return _realm.Intrinsics.Object.GetPrototypeOf(Undefined, arguments);
  178. }
  179. private JsValue SetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  180. {
  181. var target = arguments.At(0);
  182. var o = target as ObjectInstance;
  183. if (o is null)
  184. {
  185. ExceptionHelper.ThrowTypeError(_realm, "Reflect.setPrototypeOf called on non-object");
  186. }
  187. var prototype = arguments.At(1);
  188. if (!prototype.IsObject() && !prototype.IsNull())
  189. {
  190. ExceptionHelper.ThrowTypeError(_realm, $"Object prototype may only be an Object or null: {prototype}");
  191. }
  192. return o.SetPrototypeOf(prototype);
  193. }
  194. }