ProxyInstance.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. using Jint.Native.Function;
  2. using Jint.Native.Object;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Descriptors;
  5. namespace Jint.Native.Proxy
  6. {
  7. public sealed 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. IsCallable = target.IsCallable;
  35. }
  36. /// <summary>
  37. /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-call-thisargument-argumentslist
  38. /// </summary>
  39. JsValue ICallable.Call(JsValue thisObject, JsValue[] arguments)
  40. {
  41. if (_target is not ICallable)
  42. {
  43. ExceptionHelper.ThrowTypeError(_engine.Realm, "(intermediate value) is not a function");
  44. }
  45. var jsValues = new[] { _target, thisObject, _engine.Realm.Intrinsics.Array.ConstructFast(arguments) };
  46. if (TryCallHandler(TrapApply, jsValues, out var result))
  47. {
  48. return result;
  49. }
  50. var callable = _target as ICallable;
  51. if (callable is null)
  52. {
  53. ExceptionHelper.ThrowTypeError(_engine.Realm, _target + " is not a function");
  54. }
  55. return callable.Call(thisObject, arguments);
  56. }
  57. /// <summary>
  58. /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-construct-argumentslist-newtarget
  59. /// </summary>
  60. ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget)
  61. {
  62. if (_target is not ICallable)
  63. {
  64. ExceptionHelper.ThrowTypeError(_engine.Realm, "(intermediate value) is not a constructor");
  65. }
  66. var argArray = _engine.Realm.Intrinsics.Array.Construct(arguments, _engine.Realm.Intrinsics.Array);
  67. if (!TryCallHandler(TrapConstruct, new[] { _target, argArray, newTarget }, out var result))
  68. {
  69. var constructor = _target as IConstructor;
  70. if (constructor is null)
  71. {
  72. ExceptionHelper.ThrowTypeError(_engine.Realm);
  73. }
  74. return constructor.Construct(arguments, newTarget);
  75. }
  76. var oi = result as ObjectInstance;
  77. if (oi is null)
  78. {
  79. ExceptionHelper.ThrowTypeError(_engine.Realm);
  80. }
  81. return oi;
  82. }
  83. public override bool IsArray()
  84. {
  85. AssertNotRevoked(KeyIsArray);
  86. return _target.IsArray();
  87. }
  88. internal override bool IsConstructor
  89. {
  90. get
  91. {
  92. if (_target is not null && _target.IsConstructor)
  93. {
  94. return true;
  95. }
  96. if (_handler is not null && _handler.TryGetValue(TrapConstruct, out var handlerFunction) && handlerFunction is IConstructor)
  97. {
  98. return true;
  99. }
  100. return false;
  101. }
  102. }
  103. /// <summary>
  104. /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-get-p-receiver
  105. /// </summary>
  106. public override JsValue Get(JsValue property, JsValue receiver)
  107. {
  108. AssertTargetNotRevoked(property);
  109. var target = _target;
  110. if (property == KeyFunctionRevoke || !TryCallHandler(TrapGet, new[] { target, TypeConverter.ToPropertyKey(property), receiver }, out var result))
  111. {
  112. return target.Get(property, receiver);
  113. }
  114. var targetDesc = target.GetOwnProperty(property);
  115. if (targetDesc != PropertyDescriptor.Undefined)
  116. {
  117. if (targetDesc.IsDataDescriptor() && !targetDesc.Configurable && !targetDesc.Writable && !ReferenceEquals(result, targetDesc._value))
  118. {
  119. ExceptionHelper.ThrowTypeError(_engine.Realm);
  120. }
  121. if (targetDesc.IsAccessorDescriptor() && !targetDesc.Configurable && (targetDesc.Get ?? Undefined).IsUndefined() && !result.IsUndefined())
  122. {
  123. ExceptionHelper.ThrowTypeError(_engine.Realm, $"'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}')");
  124. }
  125. }
  126. return result;
  127. }
  128. /// <summary>
  129. /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-ownpropertykeys
  130. /// </summary>
  131. public override List<JsValue> GetOwnPropertyKeys(Types types = Types.None | Types.String | Types.Symbol)
  132. {
  133. if (!TryCallHandler(TrapOwnKeys, new JsValue[] { _target }, out var result))
  134. {
  135. return _target.GetOwnPropertyKeys(types);
  136. }
  137. var trapResult = new List<JsValue>(FunctionPrototype.CreateListFromArrayLike(_engine.Realm, result, Types.String | Types.Symbol));
  138. if (trapResult.Count != new HashSet<JsValue>(trapResult).Count)
  139. {
  140. ExceptionHelper.ThrowTypeError(_engine.Realm);
  141. }
  142. var extensibleTarget = _target.Extensible;
  143. var targetKeys = _target.GetOwnPropertyKeys();
  144. var targetConfigurableKeys = new List<JsValue>();
  145. var targetNonconfigurableKeys = new List<JsValue>();
  146. foreach (var property in targetKeys)
  147. {
  148. var desc = _target.GetOwnProperty(property);
  149. if (desc != PropertyDescriptor.Undefined && !desc.Configurable)
  150. {
  151. targetNonconfigurableKeys.Add(property);
  152. }
  153. else
  154. {
  155. targetConfigurableKeys.Add(property);
  156. }
  157. }
  158. var uncheckedResultKeys = new HashSet<JsValue>(trapResult);
  159. for (var i = 0; i < targetNonconfigurableKeys.Count; i++)
  160. {
  161. var key = targetNonconfigurableKeys[i];
  162. if (!uncheckedResultKeys.Remove(key))
  163. {
  164. ExceptionHelper.ThrowTypeError(_engine.Realm);
  165. }
  166. }
  167. if (extensibleTarget)
  168. {
  169. return trapResult;
  170. }
  171. for (var i = 0; i < targetConfigurableKeys.Count; i++)
  172. {
  173. var key = targetConfigurableKeys[i];
  174. if (!uncheckedResultKeys.Remove(key))
  175. {
  176. ExceptionHelper.ThrowTypeError(_engine.Realm);
  177. }
  178. }
  179. if (uncheckedResultKeys.Count > 0)
  180. {
  181. ExceptionHelper.ThrowTypeError(_engine.Realm);
  182. }
  183. return trapResult;
  184. }
  185. /// <summary>
  186. /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getownproperty-p
  187. /// </summary>
  188. public override PropertyDescriptor GetOwnProperty(JsValue property)
  189. {
  190. if (!TryCallHandler(TrapGetOwnPropertyDescriptor, new[] { _target, TypeConverter.ToPropertyKey(property) }, out var trapResultObj))
  191. {
  192. return _target.GetOwnProperty(property);
  193. }
  194. if (!trapResultObj.IsObject() && !trapResultObj.IsUndefined())
  195. {
  196. ExceptionHelper.ThrowTypeError(_engine.Realm);
  197. }
  198. var targetDesc = _target.GetOwnProperty(property);
  199. if (trapResultObj.IsUndefined())
  200. {
  201. if (targetDesc == PropertyDescriptor.Undefined)
  202. {
  203. return targetDesc;
  204. }
  205. if (!targetDesc.Configurable || !_target.Extensible)
  206. {
  207. ExceptionHelper.ThrowTypeError(_engine.Realm);
  208. }
  209. return PropertyDescriptor.Undefined;
  210. }
  211. var extensibleTarget = _target.Extensible;
  212. var resultDesc = PropertyDescriptor.ToPropertyDescriptor(_engine.Realm, trapResultObj);
  213. CompletePropertyDescriptor(resultDesc);
  214. var valid = IsCompatiblePropertyDescriptor(extensibleTarget, resultDesc, targetDesc);
  215. if (!valid)
  216. {
  217. ExceptionHelper.ThrowTypeError(_engine.Realm);
  218. }
  219. if (!resultDesc.Configurable)
  220. {
  221. if (targetDesc == PropertyDescriptor.Undefined || targetDesc.Configurable)
  222. {
  223. ExceptionHelper.ThrowTypeError(_engine.Realm);
  224. }
  225. if (resultDesc.WritableSet && !resultDesc.Writable)
  226. {
  227. if (targetDesc.Writable)
  228. {
  229. ExceptionHelper.ThrowTypeError(_engine.Realm);
  230. }
  231. }
  232. }
  233. return resultDesc;
  234. }
  235. /// <summary>
  236. /// https://tc39.es/ecma262/#sec-completepropertydescriptor
  237. /// </summary>
  238. private void CompletePropertyDescriptor(PropertyDescriptor desc)
  239. {
  240. if (desc.IsGenericDescriptor() || desc.IsDataDescriptor())
  241. {
  242. desc.Value ??= Undefined;
  243. if (!desc.WritableSet)
  244. {
  245. desc.Writable = false;
  246. }
  247. }
  248. else
  249. {
  250. var getSet = (GetSetPropertyDescriptor) desc;
  251. getSet.SetGet(getSet.Get ?? Undefined);
  252. getSet.SetSet(getSet.Set ?? Undefined);
  253. }
  254. if (!desc.EnumerableSet)
  255. {
  256. desc.Enumerable = false;
  257. }
  258. if (!desc.ConfigurableSet)
  259. {
  260. desc.Configurable = false;
  261. }
  262. }
  263. /// <summary>
  264. /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-set-p-v-receiver
  265. /// </summary>
  266. public override bool Set(JsValue property, JsValue value, JsValue receiver)
  267. {
  268. if (!TryCallHandler(TrapSet, new[] { _target, TypeConverter.ToPropertyKey(property), value, receiver }, out var trapResult))
  269. {
  270. return _target.Set(property, value, receiver);
  271. }
  272. var result = TypeConverter.ToBoolean(trapResult);
  273. if (!result)
  274. {
  275. return false;
  276. }
  277. var targetDesc = _target.GetOwnProperty(property);
  278. if (targetDesc != PropertyDescriptor.Undefined)
  279. {
  280. if (targetDesc.IsDataDescriptor() && !targetDesc.Configurable && !targetDesc.Writable)
  281. {
  282. if (targetDesc.Value != value)
  283. {
  284. ExceptionHelper.ThrowTypeError(_engine.Realm);
  285. }
  286. }
  287. if (targetDesc.IsAccessorDescriptor() && !targetDesc.Configurable)
  288. {
  289. if ((targetDesc.Set ?? Undefined).IsUndefined())
  290. {
  291. ExceptionHelper.ThrowTypeError(_engine.Realm);
  292. }
  293. }
  294. }
  295. return true;
  296. }
  297. /// <summary>
  298. /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-defineownproperty-p-desc
  299. /// </summary>
  300. public override bool DefineOwnProperty(JsValue property, PropertyDescriptor desc)
  301. {
  302. var arguments = new[] { _target, TypeConverter.ToPropertyKey(property), PropertyDescriptor.FromPropertyDescriptor(_engine, desc, strictUndefined: true) };
  303. if (!TryCallHandler(TrapDefineProperty, arguments, out var result))
  304. {
  305. return _target.DefineOwnProperty(property, desc);
  306. }
  307. var success = TypeConverter.ToBoolean(result);
  308. if (!success)
  309. {
  310. return false;
  311. }
  312. var targetDesc = _target.GetOwnProperty(property);
  313. var extensibleTarget = _target.Extensible;
  314. var settingConfigFalse = desc.ConfigurableSet && !desc.Configurable;
  315. if (targetDesc == PropertyDescriptor.Undefined)
  316. {
  317. if (!extensibleTarget || settingConfigFalse)
  318. {
  319. ExceptionHelper.ThrowTypeError(_engine.Realm);
  320. }
  321. }
  322. else
  323. {
  324. if (!IsCompatiblePropertyDescriptor(extensibleTarget, desc, targetDesc))
  325. {
  326. ExceptionHelper.ThrowTypeError(_engine.Realm);
  327. }
  328. if (targetDesc.Configurable && settingConfigFalse)
  329. {
  330. ExceptionHelper.ThrowTypeError(_engine.Realm);
  331. }
  332. if (targetDesc.IsDataDescriptor() && !targetDesc.Configurable && targetDesc.Writable)
  333. {
  334. if (desc.WritableSet && !desc.Writable)
  335. {
  336. ExceptionHelper.ThrowTypeError(_engine.Realm);
  337. }
  338. }
  339. }
  340. return true;
  341. }
  342. private static bool IsCompatiblePropertyDescriptor(bool extensible, PropertyDescriptor desc, PropertyDescriptor current)
  343. {
  344. return ValidateAndApplyPropertyDescriptor(null, JsString.Empty, extensible, desc, current);
  345. }
  346. /// <summary>
  347. /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-hasproperty-p
  348. /// </summary>
  349. public override bool HasProperty(JsValue property)
  350. {
  351. if (!TryCallHandler(TrapHas, new [] { _target, TypeConverter.ToPropertyKey(property) }, out var jsValue))
  352. {
  353. return _target.HasProperty(property);
  354. }
  355. var trapResult = TypeConverter.ToBoolean(jsValue);
  356. if (!trapResult)
  357. {
  358. var targetDesc = _target.GetOwnProperty(property);
  359. if (targetDesc != PropertyDescriptor.Undefined)
  360. {
  361. if (!targetDesc.Configurable)
  362. {
  363. ExceptionHelper.ThrowTypeError(_engine.Realm);
  364. }
  365. if (!_target.Extensible)
  366. {
  367. ExceptionHelper.ThrowTypeError(_engine.Realm);
  368. }
  369. }
  370. }
  371. return trapResult;
  372. }
  373. /// <summary>
  374. /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-delete-p
  375. /// </summary>
  376. public override bool Delete(JsValue property)
  377. {
  378. if (!TryCallHandler(TrapDeleteProperty, new[] { _target, TypeConverter.ToPropertyKey(property) }, out var result))
  379. {
  380. return _target.Delete(property);
  381. }
  382. var booleanTrapResult = TypeConverter.ToBoolean(result);
  383. if (!booleanTrapResult)
  384. {
  385. return false;
  386. }
  387. var targetDesc = _target.GetOwnProperty(property);
  388. if (targetDesc == PropertyDescriptor.Undefined)
  389. {
  390. return true;
  391. }
  392. if (!targetDesc.Configurable)
  393. {
  394. ExceptionHelper.ThrowTypeError(_engine.Realm, $"'deleteProperty' on proxy: trap returned truish for property '{property}' which is non-configurable in the proxy target");
  395. }
  396. if (!_target.Extensible)
  397. {
  398. ExceptionHelper.ThrowTypeError(_engine.Realm);
  399. }
  400. return true;
  401. }
  402. /// <summary>
  403. /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-preventextensions
  404. /// </summary>
  405. public override bool PreventExtensions()
  406. {
  407. if (!TryCallHandler(TrapPreventExtensions, new JsValue[] { _target }, out var result))
  408. {
  409. return _target.PreventExtensions();
  410. }
  411. var success = TypeConverter.ToBoolean(result);
  412. if (success && _target.Extensible)
  413. {
  414. ExceptionHelper.ThrowTypeError(_engine.Realm);
  415. }
  416. return success;
  417. }
  418. /// <summary>
  419. /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-isextensible
  420. /// </summary>
  421. public override bool Extensible
  422. {
  423. get
  424. {
  425. if (!TryCallHandler(TrapIsExtensible, new JsValue[] { _target }, out var result))
  426. {
  427. return _target.Extensible;
  428. }
  429. var booleanTrapResult = TypeConverter.ToBoolean(result);
  430. var targetResult = _target.Extensible;
  431. if (booleanTrapResult != targetResult)
  432. {
  433. ExceptionHelper.ThrowTypeError(_engine.Realm);
  434. }
  435. return booleanTrapResult;
  436. }
  437. }
  438. /// <summary>
  439. /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getprototypeof
  440. /// </summary>
  441. protected internal override ObjectInstance? GetPrototypeOf()
  442. {
  443. if (!TryCallHandler(TrapGetProtoTypeOf, new JsValue[] { _target }, out var handlerProto ))
  444. {
  445. return _target.Prototype;
  446. }
  447. if (!handlerProto.IsObject() && !handlerProto.IsNull())
  448. {
  449. ExceptionHelper.ThrowTypeError(_engine.Realm, "'getPrototypeOf' on proxy: trap returned neither object nor null");
  450. }
  451. if (_target.Extensible)
  452. {
  453. return (ObjectInstance) handlerProto;
  454. }
  455. if (!ReferenceEquals(handlerProto, _target.Prototype))
  456. {
  457. ExceptionHelper.ThrowTypeError(_engine.Realm);
  458. }
  459. return (ObjectInstance) handlerProto;
  460. }
  461. /// <summary>
  462. /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-setprototypeof-v
  463. /// </summary>
  464. public override bool SetPrototypeOf(JsValue value)
  465. {
  466. if (!TryCallHandler(TrapSetProtoTypeOf, new[] { _target, value }, out var result))
  467. {
  468. return _target.SetPrototypeOf(value);
  469. }
  470. var success = TypeConverter.ToBoolean(result);
  471. if (!success)
  472. {
  473. return false;
  474. }
  475. if (_target.Extensible)
  476. {
  477. return true;
  478. }
  479. if (!ReferenceEquals(value, _target.Prototype))
  480. {
  481. ExceptionHelper.ThrowTypeError(_engine.Realm);
  482. }
  483. return true;
  484. }
  485. internal override bool IsCallable { get; }
  486. private bool TryCallHandler(JsValue propertyName, JsValue[] arguments, out JsValue result)
  487. {
  488. AssertNotRevoked(propertyName);
  489. result = Undefined;
  490. var handlerFunction = _handler!.Get(propertyName);
  491. if (!handlerFunction.IsNullOrUndefined())
  492. {
  493. var callable = handlerFunction as ICallable;
  494. if (callable is null)
  495. {
  496. ExceptionHelper.ThrowTypeError(_engine.Realm, $"{_handler} returned for property '{propertyName}' of object '{_target}' is not a function");
  497. }
  498. result = callable.Call(_handler, arguments);
  499. return true;
  500. }
  501. return false;
  502. }
  503. private void AssertNotRevoked(JsValue key)
  504. {
  505. if (_handler is null)
  506. {
  507. ExceptionHelper.ThrowTypeError(_engine.Realm, $"Cannot perform '{key}' on a proxy that has been revoked");
  508. }
  509. }
  510. private void AssertTargetNotRevoked(JsValue key)
  511. {
  512. if (_target is null)
  513. {
  514. ExceptionHelper.ThrowTypeError(_engine.Realm, $"Cannot perform '{key}' on a proxy that has been revoked");
  515. }
  516. }
  517. public override string ToString() => IsCallable ? "function () { [native code] }" : base.ToString();
  518. }
  519. }