Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(627)

Side by Side Diff: src/js/promise.js

Issue 2459283004: [promises] Move CreateResolvingFunctions to c++ (Closed)
Patch Set: address comments Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/js/async-await.js ('k') | src/v8.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 (function(global, utils, extrasUtils) { 5 (function(global, utils, extrasUtils) {
6 6
7 "use strict"; 7 "use strict";
8 8
9 %CheckIsBootstrapping(); 9 %CheckIsBootstrapping();
10 10
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 }); 42 });
43 43
44 // ------------------------------------------------------------------- 44 // -------------------------------------------------------------------
45 45
46 // [[PromiseState]] values: 46 // [[PromiseState]] values:
47 // These values should be kept in sync with PromiseStatus in globals.h 47 // These values should be kept in sync with PromiseStatus in globals.h
48 const kPending = 0; 48 const kPending = 0;
49 const kFulfilled = +1; 49 const kFulfilled = +1;
50 const kRejected = +2; 50 const kRejected = +2;
51 51
52 // ES#sec-createresolvingfunctions 52 const kResolveCallback = 0;
53 // CreateResolvingFunctions ( promise ) 53 const kRejectCallback = 1;
54 function CreateResolvingFunctions(promise, debugEvent) {
55 var alreadyResolved = false;
56
57 // ES#sec-promise-resolve-functions
58 // Promise Resolve Functions
59 var resolve = value => {
60 if (alreadyResolved === true) return;
61 alreadyResolved = true;
62 ResolvePromise(promise, value);
63 };
64
65 // ES#sec-promise-reject-functions
66 // Promise Reject Functions
67 var reject = reason => {
68 if (alreadyResolved === true) return;
69 alreadyResolved = true;
70 %PromiseReject(promise, reason, debugEvent);
71 PromiseSet(promise, kRejected, reason);
72 };
73
74 return {
75 __proto__: null,
76 resolve: resolve,
77 reject: reject
78 };
79 }
80
81 54
82 // ES#sec-promise-executor 55 // ES#sec-promise-executor
83 // Promise ( executor ) 56 // Promise ( executor )
84 var GlobalPromise = function Promise(executor) { 57 var GlobalPromise = function Promise(executor) {
85 if (executor === promiseRawSymbol) { 58 if (executor === promiseRawSymbol) {
86 return %_NewObject(GlobalPromise, new.target); 59 return %_NewObject(GlobalPromise, new.target);
87 } 60 }
88 if (IS_UNDEFINED(new.target)) throw %make_type_error(kNotAPromise, this); 61 if (IS_UNDEFINED(new.target)) throw %make_type_error(kNotAPromise, this);
89 if (!IS_CALLABLE(executor)) { 62 if (!IS_CALLABLE(executor)) {
90 throw %make_type_error(kResolverNotAFunction, executor); 63 throw %make_type_error(kResolverNotAFunction, executor);
91 } 64 }
92 65
93 var promise = PromiseInit(%_NewObject(GlobalPromise, new.target)); 66 var promise = PromiseInit(%_NewObject(GlobalPromise, new.target));
94 // Calling the reject function would be a new exception, so debugEvent = true 67 // Calling the reject function would be a new exception, so debugEvent = true
95 var callbacks = CreateResolvingFunctions(promise, true); 68 // TODO(gsathya): Remove container for callbacks when this is moved
69 // to CPP/TF.
70 var callbacks = %create_resolving_functions(promise, true);
96 var debug_is_active = DEBUG_IS_ACTIVE; 71 var debug_is_active = DEBUG_IS_ACTIVE;
97 try { 72 try {
98 if (debug_is_active) %DebugPushPromise(promise); 73 if (debug_is_active) %DebugPushPromise(promise);
99 executor(callbacks.resolve, callbacks.reject); 74 executor(callbacks[kResolveCallback], callbacks[kRejectCallback]);
100 } %catch (e) { // Natives syntax to mark this catch block. 75 } %catch (e) { // Natives syntax to mark this catch block.
101 %_Call(callbacks.reject, UNDEFINED, e); 76 %_Call(callbacks[kRejectCallback], UNDEFINED, e);
102 } finally { 77 } finally {
103 if (debug_is_active) %DebugPopPromise(); 78 if (debug_is_active) %DebugPopPromise();
104 } 79 }
105 80
106 return promise; 81 return promise;
107 } 82 }
108 83
109 // Core functionality. 84 // Core functionality.
110 85
111 function PromiseSet(promise, status, value) { 86 function PromiseSet(promise, status, value) {
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 } 259 }
285 // Don't cause a debug event as this case is forwarding a rejection 260 // Don't cause a debug event as this case is forwarding a rejection
286 %PromiseReject(promise, thenableValue, false); 261 %PromiseReject(promise, thenableValue, false);
287 PromiseSet(promise, kRejected, thenableValue); 262 PromiseSet(promise, kRejected, thenableValue);
288 SET_PRIVATE(resolution, promiseHasHandlerSymbol, true); 263 SET_PRIVATE(resolution, promiseHasHandlerSymbol, true);
289 return; 264 return;
290 } 265 }
291 } 266 }
292 267
293 if (IS_CALLABLE(then)) { 268 if (IS_CALLABLE(then)) {
294 var callbacks = CreateResolvingFunctions(promise, false); 269 // TODO(gsathya): Remove container for callbacks when this is
270 // moved to CPP/TF.
271 var callbacks = %create_resolving_functions(promise, false);
295 if (DEBUG_IS_ACTIVE && IsPromise(resolution)) { 272 if (DEBUG_IS_ACTIVE && IsPromise(resolution)) {
296 // Mark the dependency of the new promise on the resolution 273 // Mark the dependency of the new promise on the resolution
297 SET_PRIVATE(resolution, promiseHandledBySymbol, promise); 274 SET_PRIVATE(resolution, promiseHandledBySymbol, promise);
298 } 275 }
299 %EnqueuePromiseResolveThenableJob( 276 %EnqueuePromiseResolveThenableJob(
300 resolution, then, callbacks.resolve, callbacks.reject); 277 resolution, then, callbacks[kResolveCallback],
278 callbacks[kRejectCallback]);
301 return; 279 return;
302 } 280 }
303 } 281 }
304 %PromiseFulfill(promise, kFulfilled, resolution, 282 %PromiseFulfill(promise, kFulfilled, resolution,
305 promiseFulfillReactionsSymbol); 283 promiseFulfillReactionsSymbol);
306 PromiseSet(promise, kFulfilled, resolution); 284 PromiseSet(promise, kFulfilled, resolution);
307 } 285 }
308 286
309 // Only used by async-await.js 287 // Only used by async-await.js
310 function RejectPromise(promise, reason) { 288 function RejectPromise(promise, reason, debugEvent) {
311 %PromiseReject(promise, reason, false); 289 %PromiseReject(promise, reason, debugEvent);
312 PromiseSet(promise, kRejected, reason); 290 PromiseSet(promise, kRejected, reason);
313 } 291 }
314 292
315 // Export to bindings 293 // Export to bindings
316 function DoRejectPromise(promise, reason) { 294 function DoRejectPromise(promise, reason) {
317 %PromiseReject(promise, reason, true); 295 %PromiseReject(promise, reason, true);
318 PromiseSet(promise, kRejected, reason); 296 PromiseSet(promise, kRejected, reason);
319 } 297 }
320 298
321 // ES#sec-newpromisecapability 299 // ES#sec-newpromisecapability
322 // NewPromiseCapability ( C ) 300 // NewPromiseCapability ( C )
323 function NewPromiseCapability(C, debugEvent) { 301 function NewPromiseCapability(C, debugEvent) {
324 if (C === GlobalPromise) { 302 if (C === GlobalPromise) {
325 // Optimized case, avoid extra closure. 303 // Optimized case, avoid extra closure.
326 var promise = PromiseCreate(); 304 var promise = PromiseCreate();
327 var callbacks = CreateResolvingFunctions(promise, debugEvent); 305 // TODO(gsathya): Remove container for callbacks when this is
306 // moved to CPP/TF.
307 var callbacks = %create_resolving_functions(promise, debugEvent);
328 return { 308 return {
329 promise: promise, 309 promise: promise,
330 resolve: callbacks.resolve, 310 resolve: callbacks[kResolveCallback],
331 reject: callbacks.reject 311 reject: callbacks[kRejectCallback]
332 }; 312 };
333 } 313 }
334 314
335 var result = {promise: UNDEFINED, resolve: UNDEFINED, reject: UNDEFINED }; 315 var result = {promise: UNDEFINED, resolve: UNDEFINED, reject: UNDEFINED };
336 result.promise = new C((resolve, reject) => { 316 result.promise = new C((resolve, reject) => {
337 if (!IS_UNDEFINED(result.resolve) || !IS_UNDEFINED(result.reject)) 317 if (!IS_UNDEFINED(result.resolve) || !IS_UNDEFINED(result.reject))
338 throw %make_type_error(kPromiseExecutorAlreadyInvoked); 318 throw %make_type_error(kPromiseExecutorAlreadyInvoked);
339 result.resolve = resolve; 319 result.resolve = resolve;
340 result.reject = reject; 320 result.reject = reject;
341 }); 321 });
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
636 utils.InstallFunctions(GlobalPromise.prototype, DONT_ENUM, [ 616 utils.InstallFunctions(GlobalPromise.prototype, DONT_ENUM, [
637 "then", PromiseThen, 617 "then", PromiseThen,
638 "catch", PromiseCatch 618 "catch", PromiseCatch
639 ]); 619 ]);
640 620
641 %InstallToContext([ 621 %InstallToContext([
642 "promise_catch", PromiseCatch, 622 "promise_catch", PromiseCatch,
643 "promise_create", PromiseCreate, 623 "promise_create", PromiseCreate,
644 "promise_has_user_defined_reject_handler", PromiseHasUserDefinedRejectHandler, 624 "promise_has_user_defined_reject_handler", PromiseHasUserDefinedRejectHandler,
645 "promise_reject", DoRejectPromise, 625 "promise_reject", DoRejectPromise,
626 // TODO(gsathya): Remove this once we update the promise builtin.
627 "promise_internal_reject", RejectPromise,
646 "promise_resolve", ResolvePromise, 628 "promise_resolve", ResolvePromise,
647 "promise_then", PromiseThen, 629 "promise_then", PromiseThen,
648 "promise_handle", PromiseHandle, 630 "promise_handle", PromiseHandle,
649 "promise_debug_get_info", PromiseDebugGetInfo 631 "promise_debug_get_info", PromiseDebugGetInfo
650 ]); 632 ]);
651 633
652 // This allows extras to create promises quickly without building extra 634 // This allows extras to create promises quickly without building extra
653 // resolve/reject closures, and allows them to later resolve and reject any 635 // resolve/reject closures, and allows them to later resolve and reject any
654 // promise without having to hold on to those closures forever. 636 // promise without having to hold on to those closures forever.
655 utils.InstallFunctions(extrasUtils, 0, [ 637 utils.InstallFunctions(extrasUtils, 0, [
656 "createPromise", PromiseCreate, 638 "createPromise", PromiseCreate,
657 "resolvePromise", ResolvePromise, 639 "resolvePromise", ResolvePromise,
658 "rejectPromise", DoRejectPromise 640 "rejectPromise", DoRejectPromise
659 ]); 641 ]);
660 642
661 utils.Export(function(to) { 643 utils.Export(function(to) {
662 to.IsPromise = IsPromise; 644 to.IsPromise = IsPromise;
663 to.PromiseCreate = PromiseCreate; 645 to.PromiseCreate = PromiseCreate;
664 to.PromiseThen = PromiseThen; 646 to.PromiseThen = PromiseThen;
665 647
666 to.GlobalPromise = GlobalPromise; 648 to.GlobalPromise = GlobalPromise;
667 to.NewPromiseCapability = NewPromiseCapability; 649 to.NewPromiseCapability = NewPromiseCapability;
668 to.PerformPromiseThen = PerformPromiseThen; 650 to.PerformPromiseThen = PerformPromiseThen;
669 to.ResolvePromise = ResolvePromise; 651 to.ResolvePromise = ResolvePromise;
670 to.RejectPromise = RejectPromise; 652 to.RejectPromise = RejectPromise;
671 }); 653 });
672 654
673 }) 655 })
OLDNEW
« no previous file with comments | « src/js/async-await.js ('k') | src/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698