ReflectInstance.cs 8.6 KB

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