Chromium Code Reviews| Index: Source/bindings/v8/custom/V8PromiseCustom.cpp |
| diff --git a/Source/bindings/v8/custom/V8PromiseCustom.cpp b/Source/bindings/v8/custom/V8PromiseCustom.cpp |
| index c711a304bd71b27009046de388fea534f202533d..c4d1273322d41fc0952aac649ea92842857e2492 100644 |
| --- a/Source/bindings/v8/custom/V8PromiseCustom.cpp |
| +++ b/Source/bindings/v8/custom/V8PromiseCustom.cpp |
| @@ -51,6 +51,7 @@ namespace WebCore { |
| namespace { |
| +int wrapperCallbackTag = 0; |
| int promiseFulfillCallbackTag = 0; |
| int promiseResolveCallbackTag = 0; |
| int promiseRejectCallbackTag = 0; |
| @@ -58,7 +59,8 @@ int promiseRejectCallbackTag = 0; |
| v8::Local<v8::Function> getFunction(v8::FunctionCallback callback, int* tag, v8::Isolate* isolate) |
| { |
| // tag must be a pointer of one of the above tags. |
| - ASSERT(tag == &promiseFulfillCallbackTag |
| + ASSERT(tag == &wrapperCallbackTag |
| + || tag == &promiseFulfillCallbackTag |
| || tag == &promiseResolveCallbackTag |
| || tag == &promiseRejectCallbackTag); |
| WrapperWorldType worldType = WebCore::worldType(isolate); |
| @@ -119,7 +121,68 @@ v8::Handle<v8::Value> postTask(v8::Handle<v8::Function> callback, v8::Handle<v8: |
| return v8::Undefined(isolate); |
| } |
| -// This function must have the resolver as the first argument when called |
| +// This function must have an environment object as the first argument |
| +// when called. |
| +// See wrapperCallback. |
| +void wrapperCallbackRaw(const v8::FunctionCallbackInfo<v8::Value>& args) |
| +{ |
| + v8::Isolate* isolate = args.GetIsolate(); |
| + v8::Local<v8::Object> environment; |
| + v8::Local<v8::Value> result = v8::Undefined(args.GetIsolate()); |
| + ASSERT(args.Length() > 0); |
| + environment = args[0].As<v8::Object>(); |
| + if (args.Length() > 1) |
| + result = args[1]; |
| + |
| + v8::Local<v8::Object> promise = environment->GetInternalField(V8PromiseCustom::WrapperCallbackEnvironmentPromiseIndex).As<v8::Object>(); |
| + v8::Local<v8::Object> resolver = environment->GetInternalField(V8PromiseCustom::WrapperCallbackEnvironmentPromiseResolverIndex).As<v8::Object>(); |
| + v8::Local<v8::Function> callback = environment->GetInternalField(V8PromiseCustom::WrapperCallbackEnvironmentCallbackIndex).As<v8::Function>(); |
| + |
| + v8::Local<v8::Value> argv[] = { |
| + result, |
| + }; |
| + v8::TryCatch trycatch; |
| + result = V8ScriptRunner::callFunction(callback, getScriptExecutionContext(), promise, WTF_ARRAY_LENGTH(argv), argv); |
| + if (result.IsEmpty()) { |
| + V8PromiseCustom::rejectResolver(resolver, trycatch.Exception(), V8PromiseCustom::Synchronous, isolate); |
| + return; |
| + } |
| + V8PromiseCustom::resolveResolver(resolver, result, V8PromiseCustom::Synchronous, isolate); |
| +} |
| + |
| +v8::Handle<v8::Function> wrapperCallback(v8::Handle<v8::Object> promise, v8::Handle<v8::Object> resolver, v8::Handle<v8::Function> callback, v8::Isolate* isolate) |
| +{ |
| + // FIXME: v8::ObjectTemplate::New should be cached. |
| + v8::Local<v8::ObjectTemplate> objectTemplate = v8::ObjectTemplate::New(); |
| + objectTemplate->SetInternalFieldCount(V8PromiseCustom::WrapperCallbackEnvironmentFieldCount); |
| + v8::Local<v8::Object> environment = objectTemplate->NewInstance(); |
| + environment->SetInternalField(V8PromiseCustom::WrapperCallbackEnvironmentPromiseIndex, promise); |
| + environment->SetInternalField(V8PromiseCustom::WrapperCallbackEnvironmentPromiseResolverIndex, resolver); |
| + environment->SetInternalField(V8PromiseCustom::WrapperCallbackEnvironmentCallbackIndex, callback); |
| + |
| + // We bind |environment| to wrapperCallbackRaw |
| + // |
| + // wrapperCallback(result) will be evaluated as |
| + // wrapperCallbackRaw(environment, result). |
| + |
| + // FIXME: If there is a way to bind an object to a function other than evaluate a JavaScript, it will be preferable. |
| + // We should not depend on the global context that user can change, such as accessing a property, calling a method or so. |
| + v8::Local<v8::String> script = v8::String::New("(function(f, v1) { return function(v2) { return f(v1, v2); }; })"); |
| + v8::Local<v8::Value> value = V8ScriptRunner::compileAndRunInternalScript(script, isolate); |
|
abarth-chromium
2013/07/01 05:49:38
This is kind of ugly....
yhirano
2013/07/01 06:58:54
We have to bind an environment object to the funct
|
| + ASSERT(!value.IsEmpty()); |
| + |
| + v8::Local<v8::Value> argv[] = { |
| + getFunction(wrapperCallbackRaw, &wrapperCallbackTag, isolate), |
| + environment, |
| + }; |
| + v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global(); |
| + |
| + value = V8ScriptRunner::callFunction(value.As<v8::Function>(), getScriptExecutionContext(), global, WTF_ARRAY_LENGTH(argv), argv); |
| + ASSERT(!value.IsEmpty()); |
| + return value.As<v8::Function>(); |
| +} |
| + |
| +// This function must have the resolver as the first argument when called. |
| // See promiseCallback. |
| void promiseFulfillCallback(const v8::FunctionCallbackInfo<v8::Value>& args) |
| { |
| @@ -133,7 +196,7 @@ void promiseFulfillCallback(const v8::FunctionCallbackInfo<v8::Value>& args) |
| V8PromiseCustom::fulfillResolver(resolver, result, V8PromiseCustom::Synchronous, args.GetIsolate()); |
| } |
| -// This function must be bound with the resolver as the first argument. |
| +// This function must have the resolver as the first argument when called. |
| // See promiseCallback. |
| void promiseResolveCallback(const v8::FunctionCallbackInfo<v8::Value>& args) |
| { |
| @@ -147,7 +210,7 @@ void promiseResolveCallback(const v8::FunctionCallbackInfo<v8::Value>& args) |
| V8PromiseCustom::resolveResolver(resolver, result, V8PromiseCustom::Synchronous, args.GetIsolate()); |
| } |
| -// This function must be bound with the resolver as the first argument. |
| +// This function must have the resolver as the first argument when called. |
| // See promiseCallback. |
| void promiseRejectCallback(const v8::FunctionCallbackInfo<v8::Value>& args) |
| { |
| @@ -187,6 +250,7 @@ v8::Local<v8::Function> promiseCallback(v8::Handle<v8::Object> resolver, V8Promi |
| // We should not depend on the global context that user can change, such as accessing a property, calling a method or so. |
| v8::Local<v8::String> script = v8String("(function(f, v1) { return function(v2) { return f(v1, v2); }; })", isolate); |
| v8::Local<v8::Value> value = V8ScriptRunner::compileAndRunInternalScript(script, isolate); |
| + ASSERT(!value.IsEmpty()); |
| v8::Local<v8::Value> argv[] = { |
| callback, |
| @@ -235,6 +299,55 @@ void V8Promise::constructorCustom(const v8::FunctionCallbackInfo<v8::Value>& arg |
| return; |
| } |
| +void V8Promise::thenMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) |
| +{ |
| + v8::Isolate* isolate = args.GetIsolate(); |
| + v8::Local<v8::Function> fulfillWrapper, rejectWrapper; |
| + v8::Local<v8::Object> promise, resolver; |
| + V8PromiseCustom::createPromise(args.Holder(), &promise, &resolver, isolate); |
| + if (args.Length() > 0 && !args[0]->IsUndefined()) { |
| + if (!args[0]->IsFunction()) { |
| + v8SetReturnValue(args, throwTypeError("fulfillCallback must be a function or undefined", isolate)); |
| + return; |
| + } |
| + fulfillWrapper = wrapperCallback(promise, resolver, args[0].As<v8::Function>(), isolate); |
| + } else { |
| + fulfillWrapper = promiseCallback(resolver, V8PromiseCustom::FulfillAlgorithm, isolate); |
| + } |
| + if (args.Length() > 1 && !args[1]->IsUndefined()) { |
| + if (!args[1]->IsFunction()) { |
| + v8SetReturnValue(args, throwTypeError("rejectCallback must be a function or undefined", isolate)); |
| + return; |
| + } |
| + rejectWrapper = wrapperCallback(promise, resolver, args[1].As<v8::Function>(), isolate); |
| + } else { |
| + rejectWrapper = promiseCallback(resolver, V8PromiseCustom::RejectAlgorithm, isolate); |
| + } |
| + V8PromiseCustom::append(args.Holder(), fulfillWrapper, rejectWrapper, isolate); |
| + v8SetReturnValue(args, promise); |
| +} |
| + |
| +void V8Promise::catchMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) |
| +{ |
| + v8::Isolate* isolate = args.GetIsolate(); |
| + v8::Local<v8::Function> fulfillWrapper, rejectWrapper; |
| + v8::Local<v8::Object> promise, resolver; |
| + V8PromiseCustom::createPromise(args.Holder(), &promise, &resolver, isolate); |
| + |
| + if (args.Length() > 0 && !args[0]->IsUndefined()) { |
| + if (!args[0]->IsFunction()) { |
| + v8SetReturnValue(args, throwTypeError("rejectCallback must be a function or undefined", isolate)); |
| + return; |
| + } |
| + rejectWrapper = wrapperCallback(promise, resolver, args[0].As<v8::Function>(), isolate); |
| + } else { |
| + rejectWrapper = promiseCallback(resolver, V8PromiseCustom::RejectAlgorithm, isolate); |
| + } |
| + fulfillWrapper = promiseCallback(resolver, V8PromiseCustom::FulfillAlgorithm, isolate); |
| + V8PromiseCustom::append(args.Holder(), fulfillWrapper, rejectWrapper, isolate); |
| + v8SetReturnValue(args, promise); |
| +} |
| + |
| // |
| // -- V8PromiseCustom -- |
| void V8PromiseCustom::createPromise(v8::Handle<v8::Object> creationContext, v8::Local<v8::Object>* promise, v8::Local<v8::Object>* resolver, v8::Isolate* isolate) |