| 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/ExceptionMessages.h" |
| 35 #include "bindings/v8/ExceptionState.h" |
| 36 #include "bindings/v8/ScriptFunction.h" |
| 37 #include "bindings/v8/ScriptPromiseResolver.h" |
| 35 #include "bindings/v8/V8Binding.h" | 38 #include "bindings/v8/V8Binding.h" |
| 36 #include "bindings/v8/V8DOMWrapper.h" | 39 #include "bindings/v8/V8DOMWrapper.h" |
| 40 #include "bindings/v8/V8ThrowException.h" |
| 37 #include "bindings/v8/custom/V8PromiseCustom.h" | 41 #include "bindings/v8/custom/V8PromiseCustom.h" |
| 38 | 42 |
| 39 #include <v8.h> | 43 #include <v8.h> |
| 40 | 44 |
| 41 namespace WebCore { | 45 namespace WebCore { |
| 42 | 46 |
| 43 ScriptPromise::ScriptPromise(v8::Handle<v8::Value> value, v8::Isolate* isolate) | 47 ScriptPromise::ScriptPromise(v8::Handle<v8::Value> value, v8::Isolate* isolate) |
| 44 { | 48 { |
| 45 if (value.IsEmpty()) | 49 if (value.IsEmpty()) |
| 46 return; | 50 return; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 if (!v8OnRejected.IsEmpty()) | 86 if (!v8OnRejected.IsEmpty()) |
| 83 resultPromise = resultPromise->Catch(v8OnRejected); | 87 resultPromise = resultPromise->Catch(v8OnRejected); |
| 84 | 88 |
| 85 return ScriptPromise(resultPromise, isolate()); | 89 return ScriptPromise(resultPromise, isolate()); |
| 86 } | 90 } |
| 87 | 91 |
| 88 ScriptPromise ScriptPromise::cast(const ScriptValue& value) | 92 ScriptPromise ScriptPromise::cast(const ScriptValue& value) |
| 89 { | 93 { |
| 90 if (value.hasNoValue()) | 94 if (value.hasNoValue()) |
| 91 return ScriptPromise(); | 95 return ScriptPromise(); |
| 92 v8::Local<v8::Value> v8Value(value.v8Value()); | 96 return cast(value.v8Value(), value.isolate()); |
| 93 v8::Isolate* isolate = value.isolate(); | 97 } |
| 94 if (V8PromiseCustom::isPromise(v8Value, isolate) || v8Value->IsPromise()) { | 98 |
| 95 return ScriptPromise(v8Value, isolate); | 99 ScriptPromise ScriptPromise::cast(v8::Handle<v8::Value> value, v8::Isolate* isol
ate) |
| 96 } | 100 { |
| 97 if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled()) { | 101 if (value.IsEmpty()) |
| 98 v8::Local<v8::Promise::Resolver> resolver = v8::Promise::Resolver::New(i
solate); | 102 return ScriptPromise(); |
| 99 if (resolver.IsEmpty()) { | 103 if (V8PromiseCustom::isPromise(value, isolate) || value->IsPromise()) |
| 100 // The Promise constructor may return an empty value, for example | 104 return ScriptPromise(value, isolate); |
| 101 // when the stack is exhausted. | 105 |
| 102 return ScriptPromise(); | 106 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(isola
te); |
| 103 } | 107 ScriptPromise promise = resolver->promise(); |
| 104 resolver->Resolve(v8Value); | 108 resolver->resolve(ScriptValue(value, isolate)); |
| 105 return ScriptPromise(resolver->GetPromise(), isolate); | 109 return promise; |
| 106 } | 110 } |
| 107 return ScriptPromise(V8PromiseCustom::toPromise(v8Value, isolate), isolate); | 111 |
| 112 ScriptPromise ScriptPromise::reject(const ScriptValue& value) |
| 113 { |
| 114 if (value.hasNoValue()) |
| 115 return ScriptPromise(); |
| 116 return reject(value.v8Value(), value.isolate()); |
| 117 } |
| 118 |
| 119 ScriptPromise ScriptPromise::reject(v8::Handle<v8::Value> value, v8::Isolate* is
olate) |
| 120 { |
| 121 if (value.IsEmpty()) |
| 122 return ScriptPromise(); |
| 123 |
| 124 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(isola
te); |
| 125 ScriptPromise promise = resolver->promise(); |
| 126 resolver->reject(ScriptValue(value, isolate)); |
| 127 return promise; |
| 128 } |
| 129 |
| 130 ScriptPromise ScriptPromise::rejectWithError(V8ErrorType type, const String& mes
sage, v8::Isolate* isolate) |
| 131 { |
| 132 return reject(V8ThrowException::createError(type, message, isolate), isolate
); |
| 133 } |
| 134 |
| 135 ScriptPromise ScriptPromise::rejectWithError(v8::Handle<v8::Value> value, v8::Is
olate* isolate) |
| 136 { |
| 137 return reject(value, isolate); |
| 138 } |
| 139 |
| 140 ScriptPromise ScriptPromise::rejectWithTypeError(const String& message, v8::Isol
ate* isolate) |
| 141 { |
| 142 return reject(V8ThrowException::createTypeError(message, isolate), isolate); |
| 143 } |
| 144 |
| 145 ScriptPromise ScriptPromise::rejectWithArityTypeErrorForMethod( |
| 146 const char* method, const char* type, unsigned expected, unsigned providedLe
astNumMandatoryParams, v8::Isolate* isolate) |
| 147 { |
| 148 String message = ExceptionMessages::failedToExecute(method, type, ExceptionM
essages::notEnoughArguments(expected, providedLeastNumMandatoryParams)); |
| 149 return rejectWithTypeError(message, isolate); |
| 150 } |
| 151 |
| 152 ScriptPromise ScriptPromise::rejectWithArityTypeErrorForConstructor( |
| 153 const char* type, unsigned expected, unsigned providedLeastNumMandatoryParam
s, v8::Isolate* isolate) |
| 154 { |
| 155 String message = ExceptionMessages::failedToConstruct(type, ExceptionMessage
s::notEnoughArguments(expected, providedLeastNumMandatoryParams)); |
| 156 return rejectWithTypeError(message, isolate); |
| 157 } |
| 158 |
| 159 ScriptPromise ScriptPromise::rejectWithArityTypeError(ExceptionState& exceptionS
tate, unsigned expected, unsigned providedLeastNumMandatoryParams) |
| 160 { |
| 161 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(expected
, providedLeastNumMandatoryParams)); |
| 162 return exceptionState.rejectedPromise(); |
| 108 } | 163 } |
| 109 | 164 |
| 110 } // namespace WebCore | 165 } // namespace WebCore |
| OLD | NEW |