| 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 21 matching lines...) Expand all Loading... |
| 32 #include "bindings/v8/ScriptPromise.h" | 32 #include "bindings/v8/ScriptPromise.h" |
| 33 | 33 |
| 34 #include "bindings/v8/V8Binding.h" | 34 #include "bindings/v8/V8Binding.h" |
| 35 #include "bindings/v8/V8DOMWrapper.h" | 35 #include "bindings/v8/V8DOMWrapper.h" |
| 36 #include "bindings/v8/custom/V8PromiseCustom.h" | 36 #include "bindings/v8/custom/V8PromiseCustom.h" |
| 37 | 37 |
| 38 #include <v8.h> | 38 #include <v8.h> |
| 39 | 39 |
| 40 namespace WebCore { | 40 namespace WebCore { |
| 41 | 41 |
| 42 ScriptPromise::ScriptPromise(v8::Handle<v8::Value> value, v8::Isolate* isolate) |
| 43 { |
| 44 if (value.IsEmpty() || !V8PromiseCustom::isPromise(value, isolate)) { |
| 45 m_promise = ScriptValue(v8::Handle<v8::Value>(), isolate); |
| 46 V8ThrowException::throwTypeError("the given value is not a Promise", iso
late); |
| 47 return; |
| 48 } |
| 49 m_promise = ScriptValue(value, isolate); |
| 50 } |
| 51 |
| 42 ScriptPromise ScriptPromise::then(PassOwnPtr<ScriptFunction> onFulfilled, PassOw
nPtr<ScriptFunction> onRejected) | 52 ScriptPromise ScriptPromise::then(PassOwnPtr<ScriptFunction> onFulfilled, PassOw
nPtr<ScriptFunction> onRejected) |
| 43 { | 53 { |
| 44 if (m_promise.hasNoValue() || !m_promise.isObject()) | 54 if (m_promise.hasNoValue() || !m_promise.isObject()) |
| 45 return ScriptPromise(); | 55 return ScriptPromise(); |
| 46 v8::Handle<v8::Object> promise = m_promise.v8Value().As<v8::Object>(); | 56 v8::Handle<v8::Object> promise = m_promise.v8Value().As<v8::Object>(); |
| 47 return ScriptPromise(V8PromiseCustom::then(promise, adoptByGarbageCollector(
onFulfilled), adoptByGarbageCollector(onRejected), isolate()), isolate()); | 57 return ScriptPromise(V8PromiseCustom::then(promise, adoptByGarbageCollector(
onFulfilled), adoptByGarbageCollector(onRejected), isolate()), isolate()); |
| 48 } | 58 } |
| 49 | 59 |
| 50 ScriptPromise::ScriptPromise(const ScriptValue& value) | 60 ScriptPromise ScriptPromise::createPending() |
| 51 { | 61 { |
| 52 if (value.hasNoValue()) | 62 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 53 return; | 63 ASSERT(isolate->InContext()); |
| 54 v8::Local<v8::Value> v8Value(value.v8Value()); | 64 v8::Handle<v8::Object> promise = V8PromiseCustom::createPromise(v8::Object::
New(isolate), isolate); |
| 55 v8::Isolate* isolate = value.isolate(); | 65 return ScriptPromise(promise, isolate); |
| 56 if (V8PromiseCustom::isPromise(v8Value, isolate)) { | |
| 57 m_promise = value; | |
| 58 return; | |
| 59 } | |
| 60 m_promise = ScriptValue(V8PromiseCustom::toPromise(v8Value, isolate), isolat
e); | |
| 61 } | 66 } |
| 62 | 67 |
| 63 ScriptPromise ScriptPromise::createPending(ExecutionContext* context) | 68 ScriptPromise ScriptPromise::createPending(ExecutionContext* context) |
| 64 { | 69 { |
| 65 ASSERT(context); | 70 ASSERT(context); |
| 66 v8::Isolate* isolate = toIsolate(context); | 71 v8::Isolate* isolate = toIsolate(context); |
| 67 ASSERT(isolate->InContext()); | 72 ASSERT(isolate->InContext()); |
| 68 v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::cu
rrent(isolate)); | 73 v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::cu
rrent(isolate)); |
| 69 v8::Handle<v8::Object> creationContext = v8Context.IsEmpty() ? v8::Object::N
ew(isolate) : v8Context->Global(); | 74 v8::Handle<v8::Object> creationContext = v8Context.IsEmpty() ? v8::Object::N
ew(isolate) : v8Context->Global(); |
| 70 v8::Handle<v8::Object> promise = V8PromiseCustom::createPromise(creationCont
ext, isolate); | 75 v8::Handle<v8::Object> promise = V8PromiseCustom::createPromise(creationCont
ext, isolate); |
| 71 return ScriptPromise(promise, isolate); | 76 return ScriptPromise(promise, isolate); |
| 72 } | 77 } |
| 73 | 78 |
| 74 ScriptPromise ScriptPromise::createPending() | 79 ScriptPromise ScriptPromise::cast(const ScriptValue& value) |
| 75 { | 80 { |
| 76 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 81 if (value.hasNoValue()) |
| 77 ASSERT(isolate->InContext()); | 82 return ScriptPromise(); |
| 78 v8::Handle<v8::Object> promise = V8PromiseCustom::createPromise(v8::Object::
New(isolate), isolate); | 83 v8::Local<v8::Value> v8Value(value.v8Value()); |
| 79 return ScriptPromise(promise, isolate); | 84 v8::Isolate* isolate = value.isolate(); |
| 85 if (V8PromiseCustom::isPromise(v8Value, isolate)) { |
| 86 return ScriptPromise(v8Value, isolate); |
| 87 } |
| 88 return ScriptPromise(V8PromiseCustom::toPromise(v8Value, isolate), isolate); |
| 80 } | 89 } |
| 81 | 90 |
| 82 } // namespace WebCore | 91 } // namespace WebCore |
| OLD | NEW |