OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 13 matching lines...) Expand all Loading... |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "bindings/v8/ScriptPromise.h" | 32 #include "bindings/v8/ScriptPromise.h" |
33 | 33 |
| 34 #include "RuntimeEnabledFeatures.h" |
34 #include "bindings/v8/V8Binding.h" | 35 #include "bindings/v8/V8Binding.h" |
35 #include "bindings/v8/V8DOMWrapper.h" | 36 #include "bindings/v8/V8DOMWrapper.h" |
36 #include "bindings/v8/custom/V8PromiseCustom.h" | 37 #include "bindings/v8/custom/V8PromiseCustom.h" |
37 | 38 |
38 #include <v8.h> | 39 #include <v8.h> |
39 | 40 |
40 namespace WebCore { | 41 namespace WebCore { |
41 | 42 |
42 ScriptPromise::ScriptPromise(v8::Handle<v8::Value> value, v8::Isolate* isolate) | 43 ScriptPromise::ScriptPromise(v8::Handle<v8::Value> value, v8::Isolate* isolate) |
43 { | 44 { |
44 if (value.IsEmpty() || !V8PromiseCustom::isPromise(value, isolate)) { | 45 if (value.IsEmpty()) |
| 46 return; |
| 47 |
| 48 if (!V8PromiseCustom::isPromise(value, isolate) && !value->IsPromise()) { |
45 m_promise = ScriptValue(v8::Handle<v8::Value>(), isolate); | 49 m_promise = ScriptValue(v8::Handle<v8::Value>(), isolate); |
46 V8ThrowException::throwTypeError("the given value is not a Promise", iso
late); | 50 V8ThrowException::throwTypeError("the given value is not a Promise", iso
late); |
47 return; | 51 return; |
48 } | 52 } |
49 m_promise = ScriptValue(value, isolate); | 53 m_promise = ScriptValue(value, isolate); |
50 } | 54 } |
51 | 55 |
52 ScriptPromise ScriptPromise::then(PassOwnPtr<ScriptFunction> onFulfilled, PassOw
nPtr<ScriptFunction> onRejected) | 56 ScriptPromise ScriptPromise::then(PassOwnPtr<ScriptFunction> onFulfilled, PassOw
nPtr<ScriptFunction> onRejected) |
53 { | 57 { |
54 if (m_promise.hasNoValue() || !m_promise.isObject()) | 58 if (m_promise.hasNoValue()) |
55 return ScriptPromise(); | 59 return ScriptPromise(); |
56 v8::Handle<v8::Object> promise = m_promise.v8Value().As<v8::Object>(); | |
57 return ScriptPromise(V8PromiseCustom::then(promise, adoptByGarbageCollector(
onFulfilled), adoptByGarbageCollector(onRejected), isolate()), isolate()); | |
58 } | |
59 | 60 |
60 ScriptPromise ScriptPromise::createPending() | 61 v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>(); |
61 { | 62 v8::Local<v8::Function> v8OnFulfilled = adoptByGarbageCollector(onFulfilled)
; |
62 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 63 v8::Local<v8::Function> v8OnRejected = adoptByGarbageCollector(onRejected); |
63 ASSERT(isolate->InContext()); | |
64 v8::Handle<v8::Object> promise = V8PromiseCustom::createPromise(v8::Object::
New(isolate), isolate); | |
65 return ScriptPromise(promise, isolate); | |
66 } | |
67 | 64 |
68 ScriptPromise ScriptPromise::createPending(ExecutionContext* context) | 65 if (V8PromiseCustom::isPromise(promise, isolate())) |
69 { | 66 return ScriptPromise(V8PromiseCustom::then(promise, v8OnFulfilled, v8OnR
ejected, isolate()), isolate()); |
70 ASSERT(context); | 67 |
71 v8::Isolate* isolate = toIsolate(context); | 68 ASSERT(promise->IsPromise()); |
72 ASSERT(isolate->InContext()); | 69 // Return this Promise if no handlers are given. |
73 v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::cu
rrent(isolate)); | 70 // In fact it is not the exact bahavior of Promise.prototype.then |
74 v8::Handle<v8::Object> creationContext = v8Context.IsEmpty() ? v8::Object::N
ew(isolate) : v8Context->Global(); | 71 // but that is not a problem in this case. |
75 v8::Handle<v8::Object> promise = V8PromiseCustom::createPromise(creationCont
ext, isolate); | 72 v8::Local<v8::Promise> resultPromise = promise.As<v8::Promise>(); |
76 return ScriptPromise(promise, isolate); | 73 // FIXME: Use Then once it is introduced. |
| 74 if (!v8OnFulfilled.IsEmpty()) { |
| 75 resultPromise = resultPromise->Chain(v8OnFulfilled); |
| 76 if (resultPromise.IsEmpty()) { |
| 77 // v8::Promise::Chain may return an empty value, for example when |
| 78 // the stack is exhausted. |
| 79 return ScriptPromise(); |
| 80 } |
| 81 } |
| 82 if (!v8OnRejected.IsEmpty()) |
| 83 resultPromise = resultPromise->Catch(v8OnRejected); |
| 84 |
| 85 return ScriptPromise(resultPromise, isolate()); |
77 } | 86 } |
78 | 87 |
79 ScriptPromise ScriptPromise::cast(const ScriptValue& value) | 88 ScriptPromise ScriptPromise::cast(const ScriptValue& value) |
80 { | 89 { |
81 if (value.hasNoValue()) | 90 if (value.hasNoValue()) |
82 return ScriptPromise(); | 91 return ScriptPromise(); |
83 v8::Local<v8::Value> v8Value(value.v8Value()); | 92 v8::Local<v8::Value> v8Value(value.v8Value()); |
84 v8::Isolate* isolate = value.isolate(); | 93 v8::Isolate* isolate = value.isolate(); |
85 if (V8PromiseCustom::isPromise(v8Value, isolate)) { | 94 if (V8PromiseCustom::isPromise(v8Value, isolate) || v8Value->IsPromise()) { |
86 return ScriptPromise(v8Value, isolate); | 95 return ScriptPromise(v8Value, isolate); |
87 } | 96 } |
| 97 if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled()) { |
| 98 v8::Local<v8::Promise::Resolver> resolver = v8::Promise::Resolver::New(i
solate); |
| 99 if (resolver.IsEmpty()) { |
| 100 // The Promise constructor may return an empty value, for example |
| 101 // when the stack is exhausted. |
| 102 return ScriptPromise(); |
| 103 } |
| 104 resolver->Resolve(v8Value); |
| 105 return ScriptPromise(resolver->GetPromise(), isolate); |
| 106 } |
88 return ScriptPromise(V8PromiseCustom::toPromise(v8Value, isolate), isolate); | 107 return ScriptPromise(V8PromiseCustom::toPromise(v8Value, isolate), isolate); |
89 } | 108 } |
90 | 109 |
91 } // namespace WebCore | 110 } // namespace WebCore |
OLD | NEW |