2
0

ReflectInstance.cs 9.0 KB

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