diff --git a/deps/chakrashim/chakrashim.gyp b/deps/chakrashim/chakrashim.gyp index 0349e5167ec..6274fc0d91b 100644 --- a/deps/chakrashim/chakrashim.gyp +++ b/deps/chakrashim/chakrashim.gyp @@ -145,7 +145,6 @@ 'src/v8persistent.cc', 'src/v8private.cc', 'src/v8promise.cc', - 'src/v8resolver.cc', 'src/v8propertydescriptor.cc', 'src/v8proxy.cc', 'src/v8regexp.cc', diff --git a/deps/chakrashim/src/v8promise.cc b/deps/chakrashim/src/v8promise.cc index 84650954dc3..f0390be613c 100644 --- a/deps/chakrashim/src/v8promise.cc +++ b/deps/chakrashim/src/v8promise.cc @@ -21,38 +21,100 @@ #include "v8chakra.h" #include "jsrtutils.h" +#include + namespace v8 { +class PromiseResolverData : public ExternalData { + public: + static const ExternalDataTypes ExternalDataType = + ExternalDataTypes::PromiseResolverData; + + private: + Persistent resolve; + Persistent reject; + Persistent result; + Promise::PromiseState state; + + public: + PromiseResolverData(Local resolve, + Local reject) + : ExternalData(ExternalDataType), + resolve(nullptr, resolve), + reject(nullptr, reject), + state(Promise::PromiseState::kPending) {} + + ~PromiseResolverData() { + this->resolve.Reset(); + this->reject.Reset(); + this->result.Reset(); + } + + static void CHAKRA_CALLBACK FinalizeCallback(void *data) { + if (data != nullptr) { + PromiseResolverData* promiseResolverData = + reinterpret_cast(data); + delete promiseResolverData; + } + } + + Promise::PromiseState State() { + return this->state; + } + + Local Result() { + return Local::New(nullptr, this->result); + } + + JsErrorCode Resolve(Local value) { + JsValueRef result = nullptr; + std::array args = { jsrt::GetUndefined(), *value }; + JsErrorCode err = JsCallFunction(*this->resolve, args.data(), args.size(), + &result); + + if (err == JsNoError) { + this->state = Promise::PromiseState::kFulfilled; + this->result = value; + } + + return err; + } + + JsErrorCode Reject(Local value) { + JsValueRef result = nullptr; + std::array args = { jsrt::GetUndefined(), *value }; + JsErrorCode err = JsCallFunction(*this->reject, args.data(), args.size(), + &result); + + if (err == JsNoError) { + this->state = Promise::PromiseState::kRejected; + this->result = value; + } + + return err; + } +}; + Promise::Promise() { } Local Promise::Result() { - JsValueRef value; - if (jsrt::GetProperty( - this, - jsrt::CachedPropertyIdRef::value, - &value) != JsNoError) { + PromiseResolverData* data = nullptr; + if (!ExternalData::TryGetFromProperty(this, jsrt::GetExternalPropertyId(), + &data)) { return Local(); } - return Local::New(value); + + return data->Result(); } Promise::PromiseState Promise::State() { - JsValueRef state; - if (jsrt::GetProperty( - this, - jsrt::CachedPropertyIdRef::state, - &state) != JsNoError) { - return PromiseState::kPending; - } - int stateNumber; - if (JsNumberToInt(state, &stateNumber) != JsNoError) { + PromiseResolverData* data = nullptr; + if (!ExternalData::TryGetFromProperty(this, jsrt::GetExternalPropertyId(), + &data)) { return PromiseState::kPending; } - switch (stateNumber) { - case 1: return PromiseState::kFulfilled; - case 2: return PromiseState::kRejected; - default: return PromiseState::kPending; - } + + return data->State(); } MaybeLocal Promise::Then(Local context, @@ -72,4 +134,68 @@ Promise* Promise::Cast(Value* obj) { return static_cast(obj); } +MaybeLocal Promise::Resolver::New(Local context) { + JsValueRef promise, resolve, reject; + if (JsCreatePromise(&promise, &resolve, &reject) != JsNoError) { + return Local(); + } + + PromiseResolverData* data = new PromiseResolverData(resolve, reject); + if (jsrt::AddExternalData( + promise, data, PromiseResolverData::FinalizeCallback) != JsNoError) { + delete data; + return Local(); + } + + return Local::New(promise); +} + +Local Promise::Resolver::New(Isolate* isolate) { + return New(isolate->GetCurrentContext()).ToLocalChecked(); +} + +Local Promise::Resolver::GetPromise() { + return Local::New(static_cast(this)); +} + +Promise::Resolver* Promise::Resolver::Cast(Value* obj) { + CHAKRA_ASSERT(obj->IsPromise()); + return static_cast(obj); +} + +Maybe Promise::Resolver::Resolve(Local context, + Local value) { + PromiseResolverData* data = nullptr; + if (!ExternalData::TryGetFromProperty(this, jsrt::GetExternalPropertyId(), + &data)) { + return Nothing(); + } + + if (data->Resolve(value) != JsNoError) { + return Nothing(); + } + + return Just(true); +} + +void Promise::Resolver::Resolve(Local value) { + Local context; + Resolve(context, value); +} + +Maybe Promise::Resolver::Reject(Local context, + Local value) { + PromiseResolverData* data = nullptr; + if (!ExternalData::TryGetFromProperty(this, jsrt::GetExternalPropertyId(), + &data)) { + return Nothing(); + } + + if (data->Reject(value) != JsNoError) { + return Nothing(); + } + + return Just(true); +} + } // namespace v8 diff --git a/deps/chakrashim/src/v8resolver.cc b/deps/chakrashim/src/v8resolver.cc deleted file mode 100644 index 25bb34d9a73..00000000000 --- a/deps/chakrashim/src/v8resolver.cc +++ /dev/null @@ -1,174 +0,0 @@ -// Copyright Microsoft. All rights reserved. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files(the "Software"), to -// deal in the Software without restriction, including without limitation the -// rights to use, copy, modify, merge, publish, distribute, sublicense, and / or -// sell copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions : -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS -// IN THE SOFTWARE. - -#include "v8chakra.h" -#include "jsrtutils.h" - -#include - -namespace v8 { - -class PromiseResolverData : public ExternalData { - public: - static const ExternalDataTypes ExternalDataType = - ExternalDataTypes::PromiseResolverData; - - private: - Persistent resolve; - Persistent reject; - - public: - PromiseResolverData(Local resolve, - Local reject) - : ExternalData(ExternalDataType), - resolve(nullptr, resolve), - reject(nullptr, reject) {} - - ~PromiseResolverData() { - resolve.Reset(); - reject.Reset(); - } - - static void CHAKRA_CALLBACK FinalizeCallback(void *data) { - if (data != nullptr) { - PromiseResolverData* promiseResolverData = - reinterpret_cast(data); - delete promiseResolverData; - } - } - - JsErrorCode Resolve(Local value) { - JsValueRef result = nullptr; - std::array args = { jsrt::GetUndefined(), *value }; - return JsCallFunction(*resolve, args.data(), args.size(), &result); - } - - JsErrorCode Reject(Local value) { - JsValueRef result = nullptr; - std::array args = { jsrt::GetUndefined(), *value }; - return JsCallFunction(*reject, args.data(), args.size(), &result); - } -}; - -MaybeLocal Promise::Resolver::New(Local context) { - JsValueRef promise, resolve, reject; - if (JsCreatePromise(&promise, &resolve, &reject) != JsNoError) { - return Local(); - } - - PromiseResolverData* data = new PromiseResolverData(resolve, reject); - if (jsrt::AddExternalData( - promise, data, PromiseResolverData::FinalizeCallback) != JsNoError) { - delete data; - return Local(); - } - - JsValueRef state; - jsrt::UintToValue(0, &state); - if (jsrt::SetProperty( - promise, - jsrt::CachedPropertyIdRef::state, - state) != JsNoError) { - return Local(); - } - - return Local::New(promise); -} - -Local Promise::Resolver::New(Isolate* isolate) { - return New(isolate->GetCurrentContext()).ToLocalChecked(); -} - -Local Promise::Resolver::GetPromise() { - return Local::New(static_cast(this)); -} - -Promise::Resolver* Promise::Resolver::Cast(Value* obj) { - return static_cast(obj); -} - -Maybe Promise::Resolver::Resolve(Local context, - Local value) { - PromiseResolverData* data = nullptr; - if (!ExternalData::TryGetFromProperty(this, jsrt::GetExternalPropertyId(), - &data)) { - return Nothing(); - } - - if (jsrt::SetProperty( - this, - jsrt::CachedPropertyIdRef::value, - *value) != JsNoError) { - return Nothing(); - } - - JsValueRef state; - jsrt::UintToValue(1, &state); - if (jsrt::SetProperty( - this, - jsrt::CachedPropertyIdRef::state, - state) != JsNoError) { - return Nothing(); - } - - if (data->Resolve(value) != JsNoError) { - return Nothing(); - } - - return Just(true); -} - -void Promise::Resolver::Resolve(Local value) { - Local context; - Resolve(context, value); -} - -Maybe Promise::Resolver::Reject(Local context, - Local value) { - PromiseResolverData* data = nullptr; - if (!ExternalData::TryGetFromProperty(this, jsrt::GetExternalPropertyId(), - &data)) { - return Nothing(); - } - - if (jsrt::SetProperty( - this, - jsrt::CachedPropertyIdRef::value, - *value) != JsNoError) { - return Nothing(); - } - - JsValueRef state; - jsrt::UintToValue(2, &state); - if (jsrt::SetProperty( - this, - jsrt::CachedPropertyIdRef::state, - state) != JsNoError) { - return Nothing(); - } - - if (data->Reject(value) != JsNoError) { - return Nothing(); - } - - return Just(true); -} - -} // namespace v8 diff --git a/test/parallel/parallel.status b/test/parallel/parallel.status index 6a5651a8d35..b6d569f6c8d 100644 --- a/test/parallel/parallel.status +++ b/test/parallel/parallel.status @@ -34,7 +34,6 @@ test-async-hooks-promise-triggerid : SKIP test-async-wrap-promise-after-enabled : SKIP test-buffer-bindingobj-no-zerofill : SKIP test-buffer-sharedarraybuffer : SKIP -test-child-process-promisified : SKIP test-crypto-dh : SKIP test-crypto-dh-leak : SKIP test-domain-abort-on-uncaught : SKIP @@ -49,11 +48,9 @@ test-domain-no-error-handler-abort-on-uncaught-7 : SKIP test-domain-no-error-handler-abort-on-uncaught-8 : SKIP test-domain-no-error-handler-abort-on-uncaught-9 : SKIP test-domain-promise : SKIP -test-fs-promisified : SKIP test-fs-stat : SKIP test-http-pipeline-flood : SKIP test-http-same-map : SKIP -test-http2-client-promisify-connect : SKIP test-http2-server-rst-stream : SKIP test-inspect-async-hook-setup-at-inspect : SKIP test-intl : SKIP @@ -62,7 +59,6 @@ test-module-main-extension-lookup : SKIP test-performance-function : SKIP test-performance-gc : SKIP test-process-env-symbols : SKIP -test-promise-internal-creation : SKIP test-promises-unhandled-symbol-rejections : SKIP test-regress-GH-12371 : SKIP test-repl : SKIP @@ -73,7 +69,6 @@ test-repl-pretty-stack : SKIP test-repl-sigint : SKIP test-repl-tab-complete : SKIP test-string-decoder : SKIP -test-timers-promisified : SKIP # ChakraCore does not support the chrome tracing framework test-trace-events-all : PASS,SKIP @@ -89,7 +84,6 @@ test-util : SKIP test-util-format-shared-arraybuffer : SKIP test-util-inspect-proxy : SKIP test-util-inspect : SKIP -test-util-promisify : SKIP test-v8-serdes : SKIP test-v8-serdes-sharedarraybuffer : SKIP test-vm-cached-data : SKIP