ProxyInstance.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. using System.Collections.Generic;
  2. using Jint.Native.Function;
  3. using Jint.Native.Object;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Descriptors;
  6. namespace Jint.Native.Proxy
  7. {
  8. public class ProxyInstance : FunctionInstance, IConstructor
  9. {
  10. internal ObjectInstance _target;
  11. internal ObjectInstance _handler;
  12. private static readonly Key TrapApply = "apply";
  13. private static readonly Key TrapGet = "get";
  14. private static readonly Key TrapSet = "set";
  15. private static readonly Key TrapPreventExtensions = "preventExtensions";
  16. private static readonly Key TrapIsExtensible = "isExtensible";
  17. private static readonly Key TrapDefineProperty = "defineProperty";
  18. private static readonly Key TrapDeleteProperty = "deleteProperty";
  19. private static readonly Key TrapGetOwnPropertyDescriptor = "getOwnPropertyDescriptor";
  20. private static readonly Key TrapHas = "has";
  21. private static readonly Key TrapGetProtoTypeOf = "getPrototypeOf";
  22. private static readonly Key TrapSetProtoTypeOf = "setPrototypeOf";
  23. private static readonly Key TrapOwnKeys = "ownKeys";
  24. private static readonly Key TrapConstruct = "construct";
  25. private static readonly Key KeyFunctionRevoke = "revoke";
  26. public ProxyInstance(
  27. Engine engine,
  28. ObjectInstance target,
  29. ObjectInstance handler)
  30. : base(engine, JsString.Empty, false, "Proxy")
  31. {
  32. _target = target;
  33. _handler = handler;
  34. }
  35. public override JsValue Call(JsValue thisObject, JsValue[] arguments)
  36. {
  37. var jsValues = new[] { _target, thisObject, _engine.Array.Construct(arguments) };
  38. if (TryCallHandler(TrapApply, jsValues, out var result))
  39. {
  40. return result;
  41. }
  42. return ((ICallable) _target).Call(thisObject, arguments);
  43. }
  44. public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
  45. {
  46. var argArray = _engine.Array.Construct(arguments, _engine.Array);
  47. if (!TryCallHandler(TrapConstruct, new[] { _target, argArray, newTarget }, out var result))
  48. {
  49. if (!(_target is IConstructor constructor))
  50. {
  51. return ExceptionHelper.ThrowTypeError<ObjectInstance>(_engine);
  52. }
  53. return constructor.Construct(arguments, newTarget);
  54. }
  55. if (!(result is ObjectInstance oi))
  56. {
  57. return ExceptionHelper.ThrowTypeError<ObjectInstance>(_engine);
  58. }
  59. return oi;
  60. }
  61. public override bool IsArray()
  62. {
  63. if (_handler is null)
  64. {
  65. return ExceptionHelper.ThrowTypeError<bool>(_engine);
  66. }
  67. return _target.IsArray();
  68. }
  69. internal override bool IsConstructor =>
  70. _handler.TryGetValue(TrapConstruct, out var handlerFunction) && handlerFunction is IConstructor;
  71. public override JsValue Get(in Key propertyName, JsValue receiver)
  72. {
  73. if (propertyName == KeyFunctionRevoke || !TryCallHandler(TrapGet, new JsValue[] {_target, propertyName, this}, out var result))
  74. {
  75. AssertTargetNotRevoked(propertyName);
  76. return _target.Get(in propertyName, receiver);
  77. }
  78. AssertTargetNotRevoked(propertyName);
  79. var targetDesc = _target.GetOwnProperty(propertyName);
  80. if (targetDesc != PropertyDescriptor.Undefined)
  81. {
  82. if (targetDesc.IsDataDescriptor() && !targetDesc.Configurable && !targetDesc.Writable && !ReferenceEquals(result, targetDesc._value))
  83. {
  84. ExceptionHelper.ThrowTypeError(_engine);
  85. }
  86. if (targetDesc.IsAccessorDescriptor() && !targetDesc.Configurable && targetDesc.Get.IsUndefined() && !result.IsUndefined())
  87. {
  88. ExceptionHelper.ThrowTypeError(_engine, $"'get' on proxy: property '{propertyName}' is a non-configurable accessor property on the proxy target and does not have a getter function, but the trap did not return 'undefined' (got '{result}')");
  89. }
  90. }
  91. return result;
  92. }
  93. internal override List<JsValue> GetOwnPropertyKeys(Types types)
  94. {
  95. if (!TryCallHandler(TrapOwnKeys, new JsValue[] {_target }, out var result))
  96. {
  97. return _target.GetOwnPropertyKeys(types);
  98. }
  99. var trapResult = new List<JsValue>(_engine.Function.PrototypeObject.CreateListFromArrayLike(result, Types.String | Types.Symbol));
  100. if (trapResult.Count != new HashSet<JsValue>(trapResult).Count)
  101. {
  102. ExceptionHelper.ThrowTypeError(_engine);
  103. }
  104. var extensibleTarget = _target.Extensible;
  105. var targetKeys = _target.GetOwnPropertyKeys(types);
  106. var targetConfigurableKeys = new List<Key>();
  107. var targetNonconfigurableKeys = new List<Key>();
  108. foreach (var jsValue in targetKeys)
  109. {
  110. var key = jsValue.ToPropertyKey();
  111. var desc = _target.GetOwnProperty(key);
  112. if (desc != PropertyDescriptor.Undefined && !desc.Configurable)
  113. {
  114. targetNonconfigurableKeys.Add(key);
  115. }
  116. else
  117. {
  118. targetConfigurableKeys.Add(key);
  119. }
  120. }
  121. if (extensibleTarget && targetNonconfigurableKeys.Count == 0)
  122. {
  123. return trapResult;
  124. }
  125. var uncheckedResultKeys = new HashSet<Key>();
  126. foreach (var jsValue in trapResult)
  127. {
  128. uncheckedResultKeys.Add(jsValue.ToPropertyKey());
  129. }
  130. for (var i = 0; i < targetNonconfigurableKeys.Count; i++)
  131. {
  132. var key = targetNonconfigurableKeys[i];
  133. if (!uncheckedResultKeys.Remove(key))
  134. {
  135. ExceptionHelper.ThrowTypeError(_engine);
  136. }
  137. }
  138. if (extensibleTarget)
  139. {
  140. return trapResult;
  141. }
  142. for (var i = 0; i < targetConfigurableKeys.Count; i++)
  143. {
  144. var key = targetConfigurableKeys[i];
  145. if (!uncheckedResultKeys.Remove(key))
  146. {
  147. ExceptionHelper.ThrowTypeError(_engine);
  148. }
  149. }
  150. if (uncheckedResultKeys.Count > 0)
  151. {
  152. ExceptionHelper.ThrowTypeError(_engine);
  153. }
  154. return trapResult;
  155. }
  156. public override PropertyDescriptor GetOwnProperty(in Key propertyName)
  157. {
  158. if (!TryCallHandler(TrapGetOwnPropertyDescriptor, new JsValue[] {_target, propertyName, this}, out var result))
  159. {
  160. return _target.GetOwnProperty(propertyName);
  161. }
  162. if (!result.IsObject() && !result.IsUndefined())
  163. {
  164. ExceptionHelper.ThrowTypeError(_engine);
  165. }
  166. var targetDesc = _target.GetOwnProperty(propertyName);
  167. if (result.IsUndefined())
  168. {
  169. if (targetDesc == PropertyDescriptor.Undefined)
  170. {
  171. return targetDesc;
  172. }
  173. if (!targetDesc.Configurable || !_target.Extensible)
  174. {
  175. ExceptionHelper.ThrowTypeError(_engine);
  176. }
  177. return PropertyDescriptor.Undefined;
  178. }
  179. var extensibleTarget = _target.Extensible;
  180. var resultDesc = PropertyDescriptor.ToPropertyDescriptor(_engine, result);
  181. var valid = IsCompatiblePropertyDescriptor(extensibleTarget, resultDesc, targetDesc);
  182. if (!valid)
  183. {
  184. ExceptionHelper.ThrowTypeError(_engine);
  185. }
  186. if (!resultDesc.Configurable && (targetDesc == PropertyDescriptor.Undefined || targetDesc.Configurable))
  187. {
  188. ExceptionHelper.ThrowTypeError(_engine);
  189. }
  190. return resultDesc;
  191. }
  192. public override bool Set(in Key propertyName, JsValue value, JsValue receiver)
  193. {
  194. if (!TryCallHandler(TrapSet, new[] { _target, propertyName, value, this }, out var trapResult))
  195. {
  196. return _target.Set(propertyName, value, receiver);
  197. }
  198. var result = TypeConverter.ToBoolean(trapResult);
  199. if (!result)
  200. {
  201. return false;
  202. }
  203. var targetDesc = _target.GetOwnProperty(propertyName);
  204. if (targetDesc != PropertyDescriptor.Undefined)
  205. {
  206. if (targetDesc.IsDataDescriptor() && !targetDesc.Configurable && !targetDesc.Writable)
  207. {
  208. if (targetDesc.Value != value)
  209. {
  210. ExceptionHelper.ThrowTypeError(_engine);
  211. }
  212. }
  213. if (targetDesc.IsAccessorDescriptor() && !targetDesc.Configurable)
  214. {
  215. if (targetDesc.Set.IsUndefined())
  216. {
  217. ExceptionHelper.ThrowTypeError(_engine);
  218. }
  219. }
  220. }
  221. return true;
  222. }
  223. public override bool DefineOwnProperty(in Key propertyName, PropertyDescriptor desc)
  224. {
  225. var arguments = new[] { _target, propertyName, PropertyDescriptor.FromPropertyDescriptor(_engine, desc) };
  226. if (!TryCallHandler(TrapDefineProperty, arguments, out var result))
  227. {
  228. return _target.DefineOwnProperty(propertyName, desc);
  229. }
  230. var success = TypeConverter.ToBoolean(result);
  231. if (!success)
  232. {
  233. return false;
  234. }
  235. var targetDesc = _target.GetOwnProperty(propertyName);
  236. var extensibleTarget = _target.Extensible;
  237. var settingConfigFalse = !desc.Configurable;
  238. if (targetDesc == PropertyDescriptor.Undefined)
  239. {
  240. if (!extensibleTarget || settingConfigFalse)
  241. {
  242. ExceptionHelper.ThrowTypeError(_engine);
  243. }
  244. }
  245. else
  246. {
  247. if (!IsCompatiblePropertyDescriptor(extensibleTarget, desc, targetDesc))
  248. {
  249. ExceptionHelper.ThrowTypeError(_engine);
  250. }
  251. if (targetDesc.Configurable && settingConfigFalse)
  252. {
  253. ExceptionHelper.ThrowTypeError(_engine);
  254. }
  255. }
  256. return true;
  257. }
  258. private static bool IsCompatiblePropertyDescriptor(bool extensible, PropertyDescriptor desc, PropertyDescriptor current)
  259. {
  260. return ValidateAndApplyPropertyDescriptor(null, "", extensible, desc, current);
  261. }
  262. public override bool HasProperty(in Key propertyName)
  263. {
  264. if (!TryCallHandler(TrapHas, new JsValue[] { _target, propertyName }, out var jsValue))
  265. {
  266. return _target.HasProperty(propertyName);
  267. }
  268. var trapResult = TypeConverter.ToBoolean(jsValue);
  269. if (!trapResult)
  270. {
  271. var targetDesc = _target.GetOwnProperty(propertyName);
  272. if (targetDesc != PropertyDescriptor.Undefined)
  273. {
  274. if (!targetDesc.Configurable)
  275. {
  276. ExceptionHelper.ThrowTypeError(_engine);
  277. }
  278. if (!_target.Extensible)
  279. {
  280. ExceptionHelper.ThrowTypeError(_engine);
  281. }
  282. }
  283. }
  284. return trapResult;
  285. }
  286. public override bool Delete(in Key propertyName)
  287. {
  288. if (!TryCallHandler(TrapDeleteProperty, new JsValue[] { _target, propertyName }, out var result))
  289. {
  290. return _target.Delete(propertyName);
  291. }
  292. var success = TypeConverter.ToBoolean(result);
  293. if (success)
  294. {
  295. var targetDesc = _target.GetOwnProperty(propertyName);
  296. if (targetDesc != PropertyDescriptor.Undefined && !targetDesc.Configurable)
  297. {
  298. ExceptionHelper.ThrowTypeError(_engine, $"'deleteProperty' on proxy: trap returned truish for property '{propertyName}' which is non-configurable in the proxy target");
  299. }
  300. }
  301. return success;
  302. }
  303. public override JsValue PreventExtensions()
  304. {
  305. if (!TryCallHandler(TrapPreventExtensions, new[] { _target }, out var result))
  306. {
  307. return _target.PreventExtensions();
  308. }
  309. var success = TypeConverter.ToBoolean(result);
  310. if (success && _target.Extensible)
  311. {
  312. ExceptionHelper.ThrowTypeError(_engine);
  313. }
  314. return success ? JsBoolean.True : JsBoolean.False;
  315. }
  316. public override bool Extensible
  317. {
  318. get
  319. {
  320. if (!TryCallHandler(TrapIsExtensible, new[] { _target }, out var result))
  321. {
  322. return _target.Extensible;
  323. }
  324. var booleanTrapResult = TypeConverter.ToBoolean(result);
  325. var targetResult = _target.Extensible;
  326. if (booleanTrapResult != targetResult)
  327. {
  328. ExceptionHelper.ThrowTypeError(_engine);
  329. }
  330. return booleanTrapResult;
  331. }
  332. }
  333. protected override ObjectInstance GetPrototypeOf()
  334. {
  335. if (!TryCallHandler(TrapGetProtoTypeOf, new [] { _target }, out var handlerProto ))
  336. {
  337. return _target.Prototype;
  338. }
  339. if (!handlerProto.IsObject() && !handlerProto.IsNull())
  340. {
  341. ExceptionHelper.ThrowTypeError(_engine, "'getPrototypeOf' on proxy: trap returned neither object nor null");
  342. }
  343. if (_target.Extensible)
  344. {
  345. return (ObjectInstance) handlerProto;
  346. }
  347. if (!ReferenceEquals(handlerProto, _target.Prototype))
  348. {
  349. ExceptionHelper.ThrowTypeError(_engine);
  350. }
  351. return (ObjectInstance) handlerProto;
  352. }
  353. public override bool SetPrototypeOf(JsValue value)
  354. {
  355. if (!TryCallHandler(TrapSetProtoTypeOf, new[] { _target, value }, out var result))
  356. {
  357. return _target.SetPrototypeOf(value);
  358. }
  359. var success = TypeConverter.ToBoolean(result);
  360. if (!success)
  361. {
  362. return false;
  363. }
  364. if (_target.Extensible)
  365. {
  366. return true;
  367. }
  368. if (!ReferenceEquals(value, _target.Prototype))
  369. {
  370. ExceptionHelper.ThrowTypeError(_engine);
  371. }
  372. return true;
  373. }
  374. private bool TryCallHandler(in Key propertyName, JsValue[] arguments, out JsValue result)
  375. {
  376. AssertNotRevoked(propertyName);
  377. result = Undefined;
  378. if (_handler.TryGetValue(propertyName, out var handlerFunction)
  379. && !handlerFunction.IsNullOrUndefined())
  380. {
  381. if (!(handlerFunction is ICallable callable))
  382. {
  383. return ExceptionHelper.ThrowTypeError<bool>(_engine, $"{_handler} returned for property '{propertyName}' of object '{_target}' is not a function");
  384. }
  385. result = callable.Call(_handler, arguments);
  386. return true;
  387. }
  388. return false;
  389. }
  390. internal void AssertNotRevoked(in Key key)
  391. {
  392. if (_handler is null)
  393. {
  394. ExceptionHelper.ThrowTypeError(_engine, $"Cannot perform '{key}' on a proxy that has been revoked");
  395. }
  396. }
  397. internal void AssertTargetNotRevoked(in Key key)
  398. {
  399. if (_target is null)
  400. {
  401. ExceptionHelper.ThrowTypeError(_engine, $"Cannot perform '{key}' on a proxy that has been revoked");
  402. }
  403. }
  404. }
  405. }