2
0

ReflectInstance.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 readonly Realm _realm;
  14. internal ReflectInstance(
  15. Engine engine,
  16. Realm realm,
  17. ObjectPrototype objectPrototype) : base(engine, ObjectClass.Reflect)
  18. {
  19. _realm = realm;
  20. _prototype = objectPrototype;
  21. }
  22. protected override void Initialize()
  23. {
  24. var properties = new PropertyDictionary(14, checkExistingKeys: false)
  25. {
  26. ["apply"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "apply", Apply, 3, PropertyFlag.Configurable), true, false, true),
  27. ["construct"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "construct", Construct, 2, PropertyFlag.Configurable), true, false, true),
  28. ["defineProperty"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "defineProperty", DefineProperty, 3, PropertyFlag.Configurable), true, false, true),
  29. ["deleteProperty"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "deleteProperty", DeleteProperty, 2, PropertyFlag.Configurable), true, false, true),
  30. ["get"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "get", Get, 2, PropertyFlag.Configurable), true, false, true),
  31. ["getOwnPropertyDescriptor"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getOwnPropertyDescriptor", GetOwnPropertyDescriptor, 2, PropertyFlag.Configurable), true, false, true),
  32. ["getPrototypeOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "getPrototypeOf", GetPrototypeOf, 1, PropertyFlag.Configurable), true, false, true),
  33. ["has"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "has", Has, 2, PropertyFlag.Configurable), true, false, true),
  34. ["isExtensible"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "isExtensible", IsExtensible, 1, PropertyFlag.Configurable), true, false, true),
  35. ["ownKeys"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "ownKeys", OwnKeys, 1, PropertyFlag.Configurable), true, false, true),
  36. ["preventExtensions"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "preventExtensions", PreventExtensions, 1, PropertyFlag.Configurable), true, false, true),
  37. ["set"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "set", Set, 3, PropertyFlag.Configurable), true, false, true),
  38. ["setPrototypeOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "setPrototypeOf", SetPrototypeOf, 2, PropertyFlag.Configurable), true, false, true),
  39. };
  40. SetProperties(properties);
  41. }
  42. private JsValue Apply(JsValue thisObject, JsValue[] arguments)
  43. {
  44. return _realm.Intrinsics.Function.PrototypeObject.Apply(arguments.At(0), new[]
  45. {
  46. arguments.At(1),
  47. arguments.At(2)
  48. });
  49. }
  50. private JsValue Construct(JsValue thisObject, JsValue[] arguments)
  51. {
  52. var targetArgument = arguments.At(0);
  53. var target = AssertConstructor(_engine, targetArgument);
  54. var newTargetArgument = arguments.At(2, arguments[0]);
  55. AssertConstructor(_engine, newTargetArgument);
  56. var args = _realm.Intrinsics.Function.PrototypeObject.CreateListFromArrayLike(arguments.At(1));
  57. return target.Construct(args, newTargetArgument);
  58. }
  59. private JsValue DefineProperty(JsValue thisObject, JsValue[] arguments)
  60. {
  61. var o = arguments.At(0) as ObjectInstance;
  62. if (o is null)
  63. {
  64. ExceptionHelper.ThrowTypeError(_realm, "Reflect.defineProperty called on non-object");
  65. }
  66. var p = arguments.At(1);
  67. var name = TypeConverter.ToPropertyKey(p);
  68. var attributes = arguments.At(2);
  69. var desc = PropertyDescriptor.ToPropertyDescriptor(_realm, attributes);
  70. return o.DefineOwnProperty(name, desc);
  71. }
  72. private JsValue DeleteProperty(JsValue thisObject, JsValue[] arguments)
  73. {
  74. var o = arguments.At(0) as ObjectInstance;
  75. if (o is null)
  76. {
  77. ExceptionHelper.ThrowTypeError(_realm, "Reflect.deleteProperty called on non-object");
  78. }
  79. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  80. return o.Delete(property) ? JsBoolean.True : JsBoolean.False;
  81. }
  82. private JsValue Has(JsValue thisObject, JsValue[] arguments)
  83. {
  84. var o = arguments.At(0) as ObjectInstance;
  85. if (o is null)
  86. {
  87. ExceptionHelper.ThrowTypeError(_realm, "Reflect.has called on non-object");
  88. }
  89. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  90. return o.HasProperty(property) ? JsBoolean.True : JsBoolean.False;
  91. }
  92. private JsValue Set(JsValue thisObject, JsValue[] arguments)
  93. {
  94. var target = arguments.At(0);
  95. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  96. var value = arguments.At(2);
  97. var receiver = arguments.At(3, target);
  98. var o = target as ObjectInstance;
  99. if (o is null)
  100. {
  101. ExceptionHelper.ThrowTypeError(_realm, "Reflect.set called on non-object");
  102. }
  103. return o.Set(property, value, receiver);
  104. }
  105. private JsValue Get(JsValue thisObject, JsValue[] arguments)
  106. {
  107. var target = arguments.At(0);
  108. var o = target as ObjectInstance;
  109. if (o is null)
  110. {
  111. ExceptionHelper.ThrowTypeError(_realm, "Reflect.get called on non-object");
  112. }
  113. var receiver = arguments.At(2, target);
  114. var property = TypeConverter.ToPropertyKey(arguments.At(1));
  115. return o.Get(property, receiver);
  116. }
  117. private JsValue GetOwnPropertyDescriptor(JsValue thisObject, JsValue[] arguments)
  118. {
  119. if (!arguments.At(0).IsObject())
  120. {
  121. ExceptionHelper.ThrowTypeError(_realm, "Reflect.getOwnPropertyDescriptor called on non-object");
  122. }
  123. return _realm.Intrinsics.Object.GetOwnPropertyDescriptor(Undefined, arguments);
  124. }
  125. private JsValue OwnKeys(JsValue thisObject, JsValue[] arguments)
  126. {
  127. var o = arguments.At(0) as ObjectInstance;
  128. if (o is null)
  129. {
  130. ExceptionHelper.ThrowTypeError(_realm, "Reflect.get called on non-object");
  131. }
  132. var keys = o.GetOwnPropertyKeys();
  133. return _realm.Intrinsics.Array.CreateArrayFromList(keys);
  134. }
  135. private JsValue IsExtensible(JsValue thisObject, JsValue[] arguments)
  136. {
  137. var o = arguments.At(0) as ObjectInstance;
  138. if (o is null)
  139. {
  140. ExceptionHelper.ThrowTypeError(_realm, "Reflect.isExtensible called on non-object");
  141. }
  142. return o.Extensible;
  143. }
  144. private JsValue PreventExtensions(JsValue thisObject, JsValue[] arguments)
  145. {
  146. var o = arguments.At(0) as ObjectInstance;
  147. if (o is null)
  148. {
  149. ExceptionHelper.ThrowTypeError(_realm, "Reflect.preventExtensions called on non-object");
  150. }
  151. return o.PreventExtensions();
  152. }
  153. private JsValue GetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  154. {
  155. var target = arguments.At(0);
  156. if (!target.IsObject())
  157. {
  158. ExceptionHelper.ThrowTypeError(_realm, "Reflect.getPrototypeOf called on non-object");
  159. }
  160. return _realm.Intrinsics.Object.GetPrototypeOf(Undefined, arguments);
  161. }
  162. private JsValue SetPrototypeOf(JsValue thisObject, JsValue[] arguments)
  163. {
  164. var target = arguments.At(0);
  165. var o = target as ObjectInstance;
  166. if (o is null)
  167. {
  168. ExceptionHelper.ThrowTypeError(_realm, "Reflect.setPrototypeOf called on non-object");
  169. }
  170. var prototype = arguments.At(1);
  171. if (!prototype.IsObject() && !prototype.IsNull())
  172. {
  173. ExceptionHelper.ThrowTypeError(_realm, $"Object prototype may only be an Object or null: {prototype}");
  174. }
  175. return o.SetPrototypeOf(prototype);
  176. }
  177. }
  178. }