ReflectInstance.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. private JsValue DefineProperty(JsValue thisObject, JsValue[] arguments)
  72. {
  73. var o = arguments.At(0) as ObjectInstance;
  74. if (o is null)
  75. {
  76. ExceptionHelper.ThrowTypeError(_realm, "Reflect.defineProperty called on non-object");
  77. }
  78. var p = arguments.At(1);
  79. var name = TypeConverter.ToPropertyKey(p);
  80. var attributes = arguments.At(2);
  81. var desc = PropertyDescriptor.ToPropertyDescriptor(_realm, attributes);
  82. return o.DefineOwnProperty(name, desc);
  83. }
  84. private JsValue DeleteProperty(JsValue thisObject, JsValue[] arguments)
  85. {
  86. var o = arguments.At(0) as ObjectInstance;
  87. if (o is null)
  88. {
  89. ExceptionHelper.ThrowTypeError(_realm, "Reflect.deleteProperty called on non-object");
  90. }
  91. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  92. return o.Delete(property) ? JsBoolean.True : JsBoolean.False;
  93. }
  94. private JsValue Has(JsValue thisObject, JsValue[] arguments)
  95. {
  96. var o = arguments.At(0) as ObjectInstance;
  97. if (o is null)
  98. {
  99. ExceptionHelper.ThrowTypeError(_realm, "Reflect.has called on non-object");
  100. }
  101. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  102. return o.HasProperty(property) ? JsBoolean.True : JsBoolean.False;
  103. }
  104. private JsValue Set(JsValue thisObject, JsValue[] arguments)
  105. {
  106. var target = arguments.At(0);
  107. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  108. var value = arguments.At(2);
  109. var receiver = arguments.At(3, target);
  110. var o = target as ObjectInstance;
  111. if (o is null)
  112. {
  113. ExceptionHelper.ThrowTypeError(_realm, "Reflect.set called on non-object");
  114. }
  115. return o.Set(property, value, receiver);
  116. }
  117. private JsValue Get(JsValue thisObject, JsValue[] arguments)
  118. {
  119. var target = arguments.At(0);
  120. var o = target as ObjectInstance;
  121. if (o is null)
  122. {
  123. ExceptionHelper.ThrowTypeError(_realm, "Reflect.get called on non-object");
  124. }
  125. var receiver = arguments.At(2, target);
  126. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  127. return o.Get(property, receiver);
  128. }
  129. private JsValue GetOwnPropertyDescriptor(JsValue thisObject, JsValue[] arguments)
  130. {
  131. if (!arguments.At(0).IsObject())
  132. {
  133. ExceptionHelper.ThrowTypeError(_realm, "Reflect.getOwnPropertyDescriptor called on non-object");
  134. }
  135. return _realm.Intrinsics.Object.GetOwnPropertyDescriptor(Undefined, arguments);
  136. }
  137. private JsValue OwnKeys(JsValue thisObject, JsValue[] arguments)
  138. {
  139. var o = arguments.At(0) as ObjectInstance;
  140. if (o is null)
  141. {
  142. ExceptionHelper.ThrowTypeError(_realm, "Reflect.get called on non-object");
  143. }
  144. var keys = o.GetOwnPropertyKeys();
  145. return _realm.Intrinsics.Array.CreateArrayFromList(keys);
  146. }
  147. private JsValue IsExtensible(JsValue thisObject, JsValue[] arguments)
  148. {
  149. var o = arguments.At(0) as ObjectInstance;
  150. if (o is null)
  151. {
  152. ExceptionHelper.ThrowTypeError(_realm, "Reflect.isExtensible called on non-object");
  153. }
  154. return o.Extensible;
  155. }
  156. private JsValue PreventExtensions(JsValue thisObject, JsValue[] arguments)
  157. {
  158. var o = arguments.At(0) as ObjectInstance;
  159. if (o is null)
  160. {
  161. ExceptionHelper.ThrowTypeError(_realm, "Reflect.preventExtensions called on non-object");
  162. }
  163. return o.PreventExtensions();
  164. }
  165. private JsValue GetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  166. {
  167. var target = arguments.At(0);
  168. if (!target.IsObject())
  169. {
  170. ExceptionHelper.ThrowTypeError(_realm, "Reflect.getPrototypeOf called on non-object");
  171. }
  172. return _realm.Intrinsics.Object.GetPrototypeOf(Undefined, arguments);
  173. }
  174. private JsValue SetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  175. {
  176. var target = arguments.At(0);
  177. var o = target as ObjectInstance;
  178. if (o is null)
  179. {
  180. ExceptionHelper.ThrowTypeError(_realm, "Reflect.setPrototypeOf called on non-object");
  181. }
  182. var prototype = arguments.At(1);
  183. if (!prototype.IsObject() && !prototype.IsNull())
  184. {
  185. ExceptionHelper.ThrowTypeError(_realm, $"Object prototype may only be an Object or null: {prototype}");
  186. }
  187. return o.SetPrototypeOf(prototype);
  188. }
  189. }
  190. }