ReflectInstance.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using Jint.Collections;
  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. {
  10. /// <summary>
  11. /// https://www.ecma-international.org/ecma-262/6.0/index.html#sec-reflect-object
  12. /// </summary>
  13. public sealed class ReflectInstance : ObjectInstance
  14. {
  15. private readonly Realm _realm;
  16. internal ReflectInstance(
  17. Engine engine,
  18. Realm realm,
  19. ObjectPrototype objectPrototype) : base(engine, ObjectClass.Reflect)
  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 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. 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. private JsValue Construct(JsValue thisObject, JsValue[] arguments)
  63. {
  64. var targetArgument = arguments.At(0);
  65. var target = AssertConstructor(_engine, targetArgument);
  66. var newTargetArgument = arguments.At(2, arguments[0]);
  67. AssertConstructor(_engine, newTargetArgument);
  68. var args = FunctionPrototype.CreateListFromArrayLike(_realm, arguments.At(1));
  69. return target.Construct(args, newTargetArgument);
  70. }
  71. /// <summary>
  72. /// https://tc39.es/ecma262/#sec-reflect.defineproperty
  73. /// </summary>
  74. private JsValue DefineProperty(JsValue thisObject, JsValue[] arguments)
  75. {
  76. var target = arguments.At(0) as ObjectInstance;
  77. if (target is null)
  78. {
  79. ExceptionHelper.ThrowTypeError(_realm, "Reflect.defineProperty called on non-object");
  80. }
  81. var propertyKey = arguments.At(1);
  82. var attributes = arguments.At(2);
  83. var key = TypeConverter.ToPropertyKey(propertyKey);
  84. var desc = PropertyDescriptor.ToPropertyDescriptor(_realm, attributes);
  85. return target.DefineOwnProperty(key, desc);
  86. }
  87. private JsValue DeleteProperty(JsValue thisObject, JsValue[] arguments)
  88. {
  89. var o = arguments.At(0) as ObjectInstance;
  90. if (o is null)
  91. {
  92. ExceptionHelper.ThrowTypeError(_realm, "Reflect.deleteProperty called on non-object");
  93. }
  94. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  95. return o.Delete(property) ? JsBoolean.True : JsBoolean.False;
  96. }
  97. private JsValue Has(JsValue thisObject, JsValue[] arguments)
  98. {
  99. var o = arguments.At(0) as ObjectInstance;
  100. if (o is null)
  101. {
  102. ExceptionHelper.ThrowTypeError(_realm, "Reflect.has called on non-object");
  103. }
  104. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  105. return o.HasProperty(property) ? JsBoolean.True : JsBoolean.False;
  106. }
  107. private JsValue Set(JsValue thisObject, JsValue[] arguments)
  108. {
  109. var target = arguments.At(0);
  110. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  111. var value = arguments.At(2);
  112. var receiver = arguments.At(3, target);
  113. var o = target as ObjectInstance;
  114. if (o is null)
  115. {
  116. ExceptionHelper.ThrowTypeError(_realm, "Reflect.set called on non-object");
  117. }
  118. return o.Set(property, value, receiver);
  119. }
  120. private JsValue Get(JsValue thisObject, JsValue[] arguments)
  121. {
  122. var target = arguments.At(0);
  123. var o = target as ObjectInstance;
  124. if (o is null)
  125. {
  126. ExceptionHelper.ThrowTypeError(_realm, "Reflect.get called on non-object");
  127. }
  128. var receiver = arguments.At(2, target);
  129. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  130. return o.Get(property, receiver);
  131. }
  132. private JsValue GetOwnPropertyDescriptor(JsValue thisObject, JsValue[] arguments)
  133. {
  134. if (!arguments.At(0).IsObject())
  135. {
  136. ExceptionHelper.ThrowTypeError(_realm, "Reflect.getOwnPropertyDescriptor called on non-object");
  137. }
  138. return _realm.Intrinsics.Object.GetOwnPropertyDescriptor(Undefined, arguments);
  139. }
  140. private JsValue OwnKeys(JsValue thisObject, JsValue[] arguments)
  141. {
  142. var o = arguments.At(0) as ObjectInstance;
  143. if (o is null)
  144. {
  145. ExceptionHelper.ThrowTypeError(_realm, "Reflect.get called on non-object");
  146. }
  147. var keys = o.GetOwnPropertyKeys();
  148. return _realm.Intrinsics.Array.CreateArrayFromList(keys);
  149. }
  150. private JsValue IsExtensible(JsValue thisObject, JsValue[] arguments)
  151. {
  152. var o = arguments.At(0) as ObjectInstance;
  153. if (o is null)
  154. {
  155. ExceptionHelper.ThrowTypeError(_realm, "Reflect.isExtensible called on non-object");
  156. }
  157. return o.Extensible;
  158. }
  159. private JsValue PreventExtensions(JsValue thisObject, JsValue[] arguments)
  160. {
  161. var o = arguments.At(0) as ObjectInstance;
  162. if (o is null)
  163. {
  164. ExceptionHelper.ThrowTypeError(_realm, "Reflect.preventExtensions called on non-object");
  165. }
  166. return o.PreventExtensions();
  167. }
  168. private JsValue GetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  169. {
  170. var target = arguments.At(0);
  171. if (!target.IsObject())
  172. {
  173. ExceptionHelper.ThrowTypeError(_realm, "Reflect.getPrototypeOf called on non-object");
  174. }
  175. return _realm.Intrinsics.Object.GetPrototypeOf(Undefined, arguments);
  176. }
  177. private JsValue SetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  178. {
  179. var target = arguments.At(0);
  180. var o = target as ObjectInstance;
  181. if (o is null)
  182. {
  183. ExceptionHelper.ThrowTypeError(_realm, "Reflect.setPrototypeOf called on non-object");
  184. }
  185. var prototype = arguments.At(1);
  186. if (!prototype.IsObject() && !prototype.IsNull())
  187. {
  188. ExceptionHelper.ThrowTypeError(_realm, $"Object prototype may only be an Object or null: {prototype}");
  189. }
  190. return o.SetPrototypeOf(prototype);
  191. }
  192. }
  193. }