PromiseConstructor.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. using Jint.Collections;
  2. using Jint.Native.Function;
  3. using Jint.Native.Iterator;
  4. using Jint.Native.Object;
  5. using Jint.Native.Symbol;
  6. using Jint.Runtime;
  7. using Jint.Runtime.Descriptors;
  8. using Jint.Runtime.Interop;
  9. namespace Jint.Native.Promise
  10. {
  11. internal sealed record PromiseCapability(
  12. JsValue PromiseInstance,
  13. ICallable Resolve,
  14. ICallable Reject,
  15. JsValue RejectObj,
  16. JsValue ResolveObj
  17. );
  18. public sealed class PromiseConstructor : FunctionInstance, IConstructor
  19. {
  20. private static readonly JsString _functionName = new JsString("Promise");
  21. internal PromisePrototype PrototypeObject { get; private set; }
  22. internal PromiseConstructor(
  23. Engine engine,
  24. Realm realm,
  25. FunctionPrototype functionPrototype,
  26. ObjectPrototype objectPrototype)
  27. : base(engine, realm, _functionName)
  28. {
  29. _prototype = functionPrototype;
  30. PrototypeObject = new PromisePrototype(engine, realm, this, objectPrototype);
  31. _length = new PropertyDescriptor(1, PropertyFlag.Configurable);
  32. _prototypeDescriptor = new PropertyDescriptor(PrototypeObject, PropertyFlag.AllForbidden);
  33. }
  34. protected override void Initialize()
  35. {
  36. const PropertyFlag propertyFlags = PropertyFlag.Configurable | PropertyFlag.Writable;
  37. const PropertyFlag lengthFlags = PropertyFlag.Configurable;
  38. var properties = new PropertyDictionary(6, checkExistingKeys: false)
  39. {
  40. ["resolve"] = new(new PropertyDescriptor(new ClrFunctionInstance(Engine, "resolve", Resolve, 1, lengthFlags), propertyFlags)),
  41. ["reject"] = new(new PropertyDescriptor(new ClrFunctionInstance(Engine, "reject", Reject, 1, lengthFlags), propertyFlags)),
  42. ["all"] = new(new PropertyDescriptor(new ClrFunctionInstance(Engine, "all", All, 1, lengthFlags), propertyFlags)),
  43. ["allSettled"] = new(new PropertyDescriptor(new ClrFunctionInstance(Engine, "allSettled", AllSettled, 1, lengthFlags), propertyFlags)),
  44. ["any"] = new(new PropertyDescriptor(new ClrFunctionInstance(Engine, "any", Any, 1, lengthFlags), propertyFlags)),
  45. ["race"] = new(new PropertyDescriptor(new ClrFunctionInstance(Engine, "race", Race, 1, lengthFlags), propertyFlags)),
  46. };
  47. SetProperties(properties);
  48. var symbols = new SymbolDictionary(1)
  49. {
  50. [GlobalSymbolRegistry.Species] = new GetSetPropertyDescriptor(
  51. get: new ClrFunctionInstance(_engine, "get [Symbol.species]", (thisObj, _) => thisObj, 0, PropertyFlag.Configurable),
  52. set: Undefined, PropertyFlag.Configurable)
  53. };
  54. SetSymbols(symbols);
  55. }
  56. protected internal override JsValue Call(JsValue thisObject, JsValue[] arguments)
  57. {
  58. ExceptionHelper.ThrowTypeError(_realm, "Constructor Promise requires 'new'");
  59. return null;
  60. }
  61. ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget)
  62. {
  63. if (newTarget.IsUndefined())
  64. {
  65. ExceptionHelper.ThrowTypeError(_realm, "Constructor Promise requires 'new'");
  66. }
  67. var promiseExecutor = arguments.At(0) as ICallable;
  68. if (promiseExecutor is null)
  69. {
  70. ExceptionHelper.ThrowTypeError(_realm, $"Promise executor {(arguments.At(0))} is not a function");
  71. }
  72. var instance = OrdinaryCreateFromConstructor(
  73. newTarget,
  74. static intrinsics => intrinsics.Promise.PrototypeObject,
  75. static (Engine engine, Realm _, object? _) => new PromiseInstance(engine));
  76. var (resolve, reject) = instance.CreateResolvingFunctions();
  77. promiseExecutor.Call(Undefined, new JsValue[] { resolve, reject });
  78. return instance;
  79. }
  80. // The abstract operation PromiseResolve takes arguments C (a constructor) and x (an ECMAScript language value).
  81. // It returns a new promise resolved with x. It performs the following steps when called:
  82. //
  83. // 1. Assert: Type(C) is Object.
  84. // 2. If IsPromise(x) is true, then
  85. // a. Let xConstructor be ? Get(x, "constructor").
  86. // b. If SameValue(xConstructor, C) is true, return x.
  87. // 3. Let promiseCapability be ? NewPromiseCapability(C).
  88. // 4. Perform ? Call(promiseCapability.[[Resolve]], undefined, « x »).
  89. // 5. Return promiseCapability.[[Promise]].
  90. internal JsValue Resolve(JsValue thisObj, JsValue[] arguments)
  91. {
  92. if (!thisObj.IsObject())
  93. {
  94. ExceptionHelper.ThrowTypeError(_realm, "PromiseResolve called on non-object");
  95. }
  96. if (thisObj is not IConstructor)
  97. {
  98. ExceptionHelper.ThrowTypeError(_realm, "Promise.resolve invoked on a non-constructor value");
  99. }
  100. JsValue x = arguments.At(0);
  101. if (x.IsPromise())
  102. {
  103. var xConstructor = x.Get(CommonProperties.Constructor);
  104. if (SameValue(xConstructor, thisObj))
  105. {
  106. return x;
  107. }
  108. }
  109. var (instance, resolve, _, _, _) = NewPromiseCapability(_engine, thisObj);
  110. resolve.Call(Undefined, new[] { x });
  111. return instance;
  112. }
  113. private JsValue Reject(JsValue thisObj, JsValue[] arguments)
  114. {
  115. if (!thisObj.IsObject())
  116. {
  117. ExceptionHelper.ThrowTypeError(_realm, "Promise.reject called on non-object");
  118. }
  119. if (thisObj is not IConstructor)
  120. {
  121. ExceptionHelper.ThrowTypeError(_realm, "Promise.reject invoked on a non-constructor value");
  122. }
  123. var r = arguments.At(0);
  124. var (instance, _, reject, _, _) = NewPromiseCapability(_engine, thisObj);
  125. reject.Call(Undefined, new[] { r });
  126. return instance;
  127. }
  128. // This helper methods executes the first 6 steps in the specs belonging to static Promise methods like all, any etc.
  129. // If it returns false, that means it has an error and it is already rejected
  130. // If it returns true, the logic specific to the calling function should continue executing
  131. private bool TryGetPromiseCapabilityAndIterator(JsValue thisObj, JsValue[] arguments, string callerName, out PromiseCapability capability, out ICallable promiseResolve, out IteratorInstance iterator)
  132. {
  133. if (!thisObj.IsObject())
  134. {
  135. ExceptionHelper.ThrowTypeError(_realm, $"{callerName} called on non-object");
  136. }
  137. //2. Let promiseCapability be ? NewPromiseCapability(C).
  138. capability = NewPromiseCapability(_engine, thisObj);
  139. var reject = capability.Reject;
  140. //3. Let promiseResolve be GetPromiseResolve(C).
  141. // 4. IfAbruptRejectPromise(promiseResolve, promiseCapability).
  142. try
  143. {
  144. promiseResolve = GetPromiseResolve(thisObj);
  145. }
  146. catch (JavaScriptException e)
  147. {
  148. reject.Call(Undefined, new[] { e.Error });
  149. promiseResolve = null!;
  150. iterator = null!;
  151. return false;
  152. }
  153. // 5. Let iteratorRecord be GetIterator(iterable).
  154. // 6. IfAbruptRejectPromise(iteratorRecord, promiseCapability).
  155. try
  156. {
  157. if (arguments.Length == 0)
  158. {
  159. ExceptionHelper.ThrowTypeError(_realm, $"no arguments were passed to {callerName}");
  160. }
  161. var iterable = arguments.At(0);
  162. iterator = iterable.GetIterator(_realm);
  163. }
  164. catch (JavaScriptException e)
  165. {
  166. reject.Call(Undefined, new[] { e.Error });
  167. iterator = null!;
  168. return false;
  169. }
  170. return true;
  171. }
  172. // https://tc39.es/ecma262/#sec-promise.all
  173. private JsValue All(JsValue thisObj, JsValue[] arguments)
  174. {
  175. if (!TryGetPromiseCapabilityAndIterator(thisObj, arguments, "Promise.all", out var capability, out var promiseResolve, out var iterator))
  176. return capability.PromiseInstance;
  177. var (resultingPromise, resolve, reject, _, rejectObj) = capability;
  178. var results = new List<JsValue>();
  179. bool doneIterating = false;
  180. void ResolveIfFinished()
  181. {
  182. // that means all of them were resolved
  183. // Note that "Undefined" is not null, thus the logic is sound, even though awkward
  184. // also note that it is important to check if we are done iterating.
  185. // if "then" method is sync then it will be resolved BEFORE the next iteration cycle
  186. if (results.TrueForAll(static x => x != null) && doneIterating)
  187. {
  188. var array = _realm.Intrinsics.Array.ConstructFast(results);
  189. resolve.Call(Undefined, new JsValue[] { array });
  190. }
  191. }
  192. // 27.2.4.1.2 PerformPromiseAll ( iteratorRecord, constructor, resultCapability, promiseResolve )
  193. // https://tc39.es/ecma262/#sec-performpromiseall
  194. try
  195. {
  196. int index = 0;
  197. do
  198. {
  199. JsValue value;
  200. try
  201. {
  202. if (!iterator.TryIteratorStep(out var nextItem))
  203. {
  204. doneIterating = true;
  205. ResolveIfFinished();
  206. break;
  207. }
  208. value = nextItem.Get(CommonProperties.Value);
  209. }
  210. catch (JavaScriptException e)
  211. {
  212. reject.Call(Undefined, new[] { e.Error });
  213. return resultingPromise;
  214. }
  215. // note that null here is important
  216. // it will help to detect if all inner promises were resolved
  217. // In F# it would be Option<JsValue>
  218. results.Add(null!);
  219. var item = promiseResolve.Call(thisObj, new JsValue[] { value });
  220. var thenProps = item.Get("then");
  221. if (thenProps is ICallable thenFunc)
  222. {
  223. var capturedIndex = index;
  224. var alreadyCalled = false;
  225. var onSuccess =
  226. new ClrFunctionInstance(_engine, "", (_, args) =>
  227. {
  228. if (!alreadyCalled)
  229. {
  230. alreadyCalled = true;
  231. results[capturedIndex] = args.At(0);
  232. ResolveIfFinished();
  233. }
  234. return Undefined;
  235. }, 1, PropertyFlag.Configurable);
  236. thenFunc.Call(item, new JsValue[] { onSuccess, rejectObj });
  237. }
  238. else
  239. {
  240. ExceptionHelper.ThrowTypeError(_realm, "Passed non Promise-like value");
  241. }
  242. index += 1;
  243. } while (true);
  244. }
  245. catch (JavaScriptException e)
  246. {
  247. iterator.Close(CompletionType.Throw);
  248. reject.Call(Undefined, new[] { e.Error });
  249. return resultingPromise;
  250. }
  251. return resultingPromise;
  252. }
  253. // https://tc39.es/ecma262/#sec-promise.allsettled
  254. private JsValue AllSettled(JsValue thisObj, JsValue[] arguments)
  255. {
  256. if (!TryGetPromiseCapabilityAndIterator(thisObj, arguments, "Promise.allSettled", out var capability, out var promiseResolve, out var iterator))
  257. return capability.PromiseInstance;
  258. var (resultingPromise, resolve, reject, _, rejectObj) = capability;
  259. var results = new List<JsValue>();
  260. bool doneIterating = false;
  261. void ResolveIfFinished()
  262. {
  263. // that means all of them were resolved
  264. // Note that "Undefined" is not null, thus the logic is sound, even though awkward
  265. // also note that it is important to check if we are done iterating.
  266. // if "then" method is sync then it will be resolved BEFORE the next iteration cycle
  267. if (results.TrueForAll(static x => x != null) && doneIterating)
  268. {
  269. var array = _realm.Intrinsics.Array.ConstructFast(results);
  270. resolve.Call(Undefined, new JsValue[] { array });
  271. }
  272. }
  273. // 27.2.4.1.2 PerformPromiseAll ( iteratorRecord, constructor, resultCapability, promiseResolve )
  274. // https://tc39.es/ecma262/#sec-performpromiseall
  275. try
  276. {
  277. int index = 0;
  278. do
  279. {
  280. JsValue value;
  281. try
  282. {
  283. if (!iterator.TryIteratorStep(out var nextItem))
  284. {
  285. doneIterating = true;
  286. ResolveIfFinished();
  287. break;
  288. }
  289. value = nextItem.Get(CommonProperties.Value);
  290. }
  291. catch (JavaScriptException e)
  292. {
  293. reject.Call(Undefined, new[] { e.Error });
  294. return resultingPromise;
  295. }
  296. // note that null here is important
  297. // it will help to detect if all inner promises were resolved
  298. // In F# it would be Option<JsValue>
  299. results.Add(null!);
  300. var item = promiseResolve.Call(thisObj, new JsValue[] { value });
  301. var thenProps = item.Get("then");
  302. if (thenProps is ICallable thenFunc)
  303. {
  304. var capturedIndex = index;
  305. var alreadyCalled = false;
  306. var onSuccess =
  307. new ClrFunctionInstance(_engine, "", (_, args) =>
  308. {
  309. if (!alreadyCalled)
  310. {
  311. alreadyCalled = true;
  312. var res = Engine.Realm.Intrinsics.Object.Construct(2);
  313. res.FastAddProperty("status", "fulfilled", true, true, true);
  314. res.FastAddProperty("value", args.At(0), true, true, true);
  315. results[capturedIndex] = res;
  316. ResolveIfFinished();
  317. }
  318. return Undefined;
  319. }, 1, PropertyFlag.Configurable);
  320. var onFailure =
  321. new ClrFunctionInstance(_engine, "", (_, args) =>
  322. {
  323. if (!alreadyCalled)
  324. {
  325. alreadyCalled = true;
  326. var res = Engine.Realm.Intrinsics.Object.Construct(2);
  327. res.FastAddProperty("status", "rejected", true, true, true);
  328. res.FastAddProperty("reason", args.At(0), true, true, true);
  329. results[capturedIndex] = res;
  330. ResolveIfFinished();
  331. }
  332. return Undefined;
  333. }, 1, PropertyFlag.Configurable);
  334. thenFunc.Call(item, new JsValue[] { onSuccess, onFailure });
  335. }
  336. else
  337. {
  338. ExceptionHelper.ThrowTypeError(_realm, "Passed non Promise-like value");
  339. }
  340. index += 1;
  341. } while (true);
  342. }
  343. catch (JavaScriptException e)
  344. {
  345. iterator.Close(CompletionType.Throw);
  346. reject.Call(Undefined, new[] { e.Error });
  347. return resultingPromise;
  348. }
  349. return resultingPromise;
  350. }
  351. // https://tc39.es/ecma262/#sec-promise.any
  352. private JsValue Any(JsValue thisObj, JsValue[] arguments)
  353. {
  354. if (!TryGetPromiseCapabilityAndIterator(thisObj, arguments, "Promise.any", out var capability, out var promiseResolve, out var iterator))
  355. return capability.PromiseInstance;
  356. var (resultingPromise, resolve, reject, resolveObj, _) = capability;
  357. var errors = new List<JsValue>();
  358. bool doneIterating = false;
  359. void RejectIfAllRejected()
  360. {
  361. // that means all of them were rejected
  362. // Note that "Undefined" is not null, thus the logic is sound, even though awkward
  363. // also note that it is important to check if we are done iterating.
  364. // if "then" method is sync then it will be resolved BEFORE the next iteration cycle
  365. if (errors.TrueForAll(static x => x != null) && doneIterating)
  366. {
  367. var array = _realm.Intrinsics.Array.ConstructFast(errors);
  368. reject.Call(Undefined, new JsValue[] { Construct(_realm.Intrinsics.AggregateError, new JsValue[] { array }) });
  369. }
  370. }
  371. // https://tc39.es/ecma262/#sec-performpromiseany
  372. try
  373. {
  374. int index = 0;
  375. do
  376. {
  377. JsValue value;
  378. try
  379. {
  380. if (!iterator.TryIteratorStep(out var nextItem))
  381. {
  382. doneIterating = true;
  383. RejectIfAllRejected();
  384. break;
  385. }
  386. value = nextItem.Get(CommonProperties.Value);
  387. }
  388. catch (JavaScriptException e)
  389. {
  390. errors.Add(e.Error);
  391. continue;
  392. }
  393. // note that null here is important
  394. // it will help to detect if all inner promises were rejected
  395. // In F# it would be Option<JsValue>
  396. errors.Add(null!);
  397. var item = promiseResolve.Call(thisObj, new JsValue[] { value });
  398. var thenProps = item.Get("then");
  399. if (thenProps is ICallable thenFunc)
  400. {
  401. var capturedIndex = index;
  402. var alreadyCalled = false;
  403. var onError =
  404. new ClrFunctionInstance(_engine, "", (_, args) =>
  405. {
  406. if (!alreadyCalled)
  407. {
  408. alreadyCalled = true;
  409. errors[capturedIndex] = args.At(0);
  410. RejectIfAllRejected();
  411. }
  412. return Undefined;
  413. }, 1, PropertyFlag.Configurable);
  414. thenFunc.Call(item, new JsValue[] { resolveObj, onError });
  415. }
  416. else
  417. {
  418. ExceptionHelper.ThrowTypeError(_realm, "Passed non Promise-like value");
  419. }
  420. index += 1;
  421. } while (true);
  422. }
  423. catch (JavaScriptException e)
  424. {
  425. iterator.Close(CompletionType.Throw);
  426. reject.Call(Undefined, new[] { e.Error });
  427. return resultingPromise;
  428. }
  429. return resultingPromise;
  430. }
  431. // https://tc39.es/ecma262/#sec-promise.race
  432. private JsValue Race(JsValue thisObj, JsValue[] arguments)
  433. {
  434. if (!TryGetPromiseCapabilityAndIterator(thisObj, arguments, "Promise.race", out var capability, out var promiseResolve, out var iterator))
  435. return capability.PromiseInstance;
  436. var (resultingPromise, resolve, reject, _, rejectObj) = capability;
  437. // 7. Let result be PerformPromiseRace(iteratorRecord, C, promiseCapability, promiseResolve).
  438. // https://tc39.es/ecma262/#sec-performpromiserace
  439. try
  440. {
  441. do
  442. {
  443. JsValue nextValue;
  444. try
  445. {
  446. if (!iterator.TryIteratorStep(out var nextItem))
  447. {
  448. break;
  449. }
  450. nextValue = nextItem.Get(CommonProperties.Value);
  451. }
  452. catch (JavaScriptException e)
  453. {
  454. reject.Call(Undefined, new[] { e.Error });
  455. return resultingPromise;
  456. }
  457. // h. Let nextPromise be ? Call(promiseResolve, constructor, « nextValue »).
  458. var nextPromise = promiseResolve.Call(thisObj, new JsValue[] { nextValue });
  459. // i. Perform ? Invoke(nextPromise, "then", « resultCapability.[[Resolve]], resultCapability.[[Reject]] »).
  460. _engine.Invoke(nextPromise, "then", new[] { (JsValue) resolve, rejectObj });
  461. } while (true);
  462. }
  463. catch (JavaScriptException e)
  464. {
  465. // 8. If result is an abrupt completion, then
  466. // a. If iteratorRecord.[[Done]] is false, set result to IteratorClose(iteratorRecord, result).
  467. // b. IfAbruptRejectPromise(result, promiseCapability).
  468. iterator.Close(CompletionType.Throw);
  469. reject.Call(Undefined, new[] { e.Error });
  470. return resultingPromise;
  471. }
  472. // 9. Return Completion(result).
  473. // Note that PerformPromiseRace returns a Promise instance in success case
  474. return resultingPromise;
  475. }
  476. // https://tc39.es/ecma262/#sec-getpromiseresolve
  477. // 27.2.4.1.1 GetPromiseResolve ( promiseConstructor )
  478. // The abstract operation GetPromiseResolve takes argument promiseConstructor. It performs the following steps when called:
  479. //
  480. // 1. Assert: IsConstructor(promiseConstructor) is true.
  481. // 2. Let promiseResolve be ? Get(promiseConstructor, "resolve").
  482. // 3. If IsCallable(promiseResolve) is false, throw a TypeError exception.
  483. // 4. Return promiseResolve.
  484. private ICallable GetPromiseResolve(JsValue promiseConstructor)
  485. {
  486. AssertConstructor(_engine, promiseConstructor);
  487. var resolveProp = promiseConstructor.Get("resolve");
  488. if (resolveProp is ICallable resolve)
  489. {
  490. return resolve;
  491. }
  492. ExceptionHelper.ThrowTypeError(_realm, "resolve is not a function");
  493. // Note: throws right before return
  494. return null;
  495. }
  496. // https://tc39.es/ecma262/#sec-newpromisecapability
  497. // The abstract operation NewPromiseCapability takes argument C.
  498. // It attempts to use C as a constructor in the fashion of the built-in Promise constructor to create a Promise
  499. // object and extract its resolve and reject functions.
  500. // The Promise object plus the resolve and reject functions are used to initialize a new PromiseCapability Record.
  501. // It performs the following steps when called:
  502. //
  503. // 1. If IsConstructor(C) is false, throw a TypeError exception.
  504. // 2. NOTE: C is assumed to be a constructor function that supports the parameter conventions of the Promise constructor (see 27.2.3.1).
  505. // 3. Let promiseCapability be the PromiseCapability Record { [[Promise]]: undefined, [[Resolve]]: undefined, [[Reject]]: undefined }.
  506. // 4. Let steps be the algorithm steps defined in GetCapabilitiesExecutor Functions.
  507. // 5. Let length be the number of non-optional parameters of the function definition in GetCapabilitiesExecutor Functions.
  508. // 6. Let executor be ! CreateBuiltinFunction(steps, length, "", « [[Capability]] »).
  509. // 7. Set executor.[[Capability]] to promiseCapability.
  510. // 8. Let promise be ? Construct(C, « executor »).
  511. // 9. If IsCallable(promiseCapability.[[Resolve]]) is false, throw a TypeError exception.
  512. // 10. If IsCallable(promiseCapability.[[Reject]]) is false, throw a TypeError exception.
  513. // 11. Set promiseCapability.[[Promise]] to promise.
  514. // 12. Return promiseCapability.
  515. internal static PromiseCapability NewPromiseCapability(Engine engine, JsValue c)
  516. {
  517. var ctor = AssertConstructor(engine, c);
  518. JsValue? resolveArg = null;
  519. JsValue? rejectArg = null;
  520. JsValue Executor(JsValue thisObj, JsValue[] arguments)
  521. {
  522. // 25.4.1.5.1 GetCapabilitiesExecutor Functions
  523. // 3. If promiseCapability.[[Resolve]] is not undefined, throw a TypeError exception.
  524. // 4. If promiseCapability.[[Reject]] is not undefined, throw a TypeError exception.
  525. // 5. Set promiseCapability.[[Resolve]] to resolve.
  526. // 6. Set promiseCapability.[[Reject]] to reject.
  527. if (resolveArg != null && resolveArg != Undefined ||
  528. rejectArg != null && rejectArg != Undefined)
  529. {
  530. ExceptionHelper.ThrowTypeError(engine.Realm, "executor was already called with not undefined args");
  531. }
  532. resolveArg = arguments.At(0);
  533. rejectArg = arguments.At(1);
  534. return Undefined;
  535. }
  536. var executor = new ClrFunctionInstance(engine, "", Executor, 2, PropertyFlag.Configurable);
  537. var instance = ctor.Construct(new JsValue[] { executor }, c);
  538. ICallable? resolve = null;
  539. ICallable? reject = null;
  540. if (resolveArg is ICallable resFunc)
  541. {
  542. resolve = resFunc;
  543. }
  544. else
  545. {
  546. ExceptionHelper.ThrowTypeError(engine.Realm, "resolve is not a function");
  547. }
  548. if (rejectArg is ICallable rejFunc)
  549. {
  550. reject = rejFunc;
  551. }
  552. else
  553. {
  554. ExceptionHelper.ThrowTypeError(engine.Realm, "reject is not a function");
  555. }
  556. return new PromiseCapability(instance, resolve, reject, resolveArg, rejectArg);
  557. }
  558. }
  559. }