Ignore:
Timestamp:
Jan 6, 2014, 1:00:03 PM (12 years ago)
Author:
[email protected]
Message:

[JS] Implement Promise.all()
https://bugs.webkit.org/show_bug.cgi?id=126510

Reviewed by Gavin Barraclough.

Source/JavaScriptCore:

Add Promise.all() implementation and factor out performing resolves and rejects
on deferreds to share a bit of code. Also moves the abruptRejection helper to
JSPromiseDeferred so it can be used in JSPromiseFunctions.

  • runtime/CommonIdentifiers.h:
  • runtime/JSPromiseConstructor.cpp:

(JSC::JSPromiseConstructorFuncCast):
(JSC::JSPromiseConstructorFuncResolve):
(JSC::JSPromiseConstructorFuncReject):
(JSC::JSPromiseConstructorFuncAll):

  • runtime/JSPromiseDeferred.cpp:

(JSC::updateDeferredFromPotentialThenable):
(JSC::performDeferredResolve):
(JSC::performDeferredReject):
(JSC::abruptRejection):

  • runtime/JSPromiseDeferred.h:
  • runtime/JSPromiseFunctions.cpp:

(JSC::promiseAllCountdownFunction):
(JSC::createPromiseAllCountdownFunction):

  • runtime/JSPromiseFunctions.h:
  • runtime/JSPromiseReaction.cpp:

(JSC::ExecutePromiseReactionMicrotask::run):

LayoutTests:

Enabled and fix the existing Promise.all() test case.

  • Promise.all() and Promise.all({}) should reject by my reading of the spec.

Also removes the Promise.all() shim used by the crypto tests.

  • crypto/subtle/resources/common.js:
  • js/dom/Promise-static-all-expected.txt:
  • js/dom/Promise-static-all.html:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/runtime/JSPromiseFunctions.cpp

    r161241 r161365  
    3535#include "JSPromiseConstructor.h"
    3636#include "JSPromiseDeferred.h"
     37#include "NumberObject.h"
    3738
    3839namespace JSC {
     
    7071{
    7172    return JSFunction::create(vm, globalObject, 1, ASCIILiteral("IdentityFunction"), identifyFunction);
     73}
     74
     75// Promise.All Countdown Functions
     76
     77static EncodedJSValue JSC_HOST_CALL promiseAllCountdownFunction(ExecState* exec)
     78{
     79    JSValue x = exec->argument(0);
     80    VM& vm = exec->vm();
     81    JSObject* F = exec->callee();
     82
     83    // 1. Let 'index' be the value of F's [[Index]] internal slot.
     84    uint32_t index = F->get(exec, vm.propertyNames->indexPrivateName).asUInt32();
     85
     86    // 2. Let 'values' be the value of F's [[Values]] internal slot..
     87    JSArray* values = jsCast<JSArray*>(F->get(exec, vm.propertyNames->valuesPrivateName));
     88
     89    // 3. Let 'deferred' be the value of F's [[Deferred]] internal slot.
     90    JSPromiseDeferred* deferred = jsCast<JSPromiseDeferred*>(F->get(exec, vm.propertyNames->deferredPrivateName));
     91
     92    // 4. Let 'countdownHolder' be the value of F's [[CountdownHolder]] internal slot.
     93    NumberObject* countdownHolder = jsCast<NumberObject*>(F->get(exec, vm.propertyNames->countdownHolderPrivateName));
     94
     95    // 5. Let 'result' be the result of calling the [[DefineOwnProperty]] internal method
     96    //    of 'values' with arguments 'index' and Property Descriptor { [[Value]]: x,
     97    //    [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true }.
     98    values->putDirectIndex(exec, index, x);
     99
     100    // 6. RejectIfAbrupt(result, deferred).
     101    if (exec->hadException())
     102        abruptRejection(exec, deferred);
     103
     104    // 7. Set countdownHolder.[[Countdown]] to countdownHolder.[[Countdown]] - 1.
     105    uint32_t newCountdownValue = countdownHolder->internalValue().asUInt32() - 1;
     106    countdownHolder->setInternalValue(vm, JSValue(newCountdownValue));
     107
     108    // 8. If countdownHolder.[[Countdown]] is 0,
     109    if (!newCountdownValue) {
     110        // i. Return the result of calling the [[Call]] internal method of deferred.[[Resolve]]
     111        //    with undefined as thisArgument and a List containing 'values' as argumentsList.
     112        performDeferredResolve(exec, deferred, values);
     113    }
     114
     115    // 9. Return.
     116    return JSValue::encode(jsUndefined());
     117}
     118
     119JSFunction* createPromiseAllCountdownFunction(VM& vm, JSGlobalObject* globalObject)
     120{
     121    return JSFunction::create(vm, globalObject, 1, ASCIILiteral("PromiseAllCountdownFunction"), promiseAllCountdownFunction);
    72122}
    73123
Note: See TracChangeset for help on using the changeset viewer.