ProxyInstance.cs 17 KB

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