| 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 24 matching lines...) Expand all Loading... |
| 35 #include "bindings/v8/ScriptState.h" | 35 #include "bindings/v8/ScriptState.h" |
| 36 #include "bindings/v8/ScriptValue.h" | 36 #include "bindings/v8/ScriptValue.h" |
| 37 #include "bindings/v8/V8Binding.h" | 37 #include "bindings/v8/V8Binding.h" |
| 38 #include "bindings/v8/V8DOMWrapper.h" | 38 #include "bindings/v8/V8DOMWrapper.h" |
| 39 #include "bindings/v8/custom/V8PromiseCustom.h" | 39 #include "bindings/v8/custom/V8PromiseCustom.h" |
| 40 | 40 |
| 41 #include <v8.h> | 41 #include <v8.h> |
| 42 | 42 |
| 43 namespace WebCore { | 43 namespace WebCore { |
| 44 | 44 |
| 45 ScriptPromiseResolver::ScriptPromiseResolver(v8::Handle<v8::Object> creationCont
ext, v8::Isolate* isolate) | 45 ScriptPromiseResolver::ScriptPromiseResolver(ScriptPromise promise, v8::Isolate*
isolate) |
| 46 : m_isolate(isolate) | 46 : m_isolate(isolate) |
| 47 , m_promiseForExposeDetached(false) | 47 , m_promise(promise) |
| 48 , m_promiseForResolveDetached(false) | |
| 49 { | 48 { |
| 50 ASSERT(RuntimeEnabledFeatures::promiseEnabled()); | 49 ASSERT(RuntimeEnabledFeatures::promiseEnabled()); |
| 51 v8::Local<v8::Object> promise = V8PromiseCustom::createPromise(creationConte
xt, isolate); | |
| 52 m_promise = ScriptPromise(promise, isolate); | |
| 53 } | 50 } |
| 54 | 51 |
| 55 ScriptPromiseResolver::~ScriptPromiseResolver() | 52 ScriptPromiseResolver::~ScriptPromiseResolver() |
| 56 { | 53 { |
| 57 // We don't call "detach" here because it requires a caller | 54 // We don't call "reject" here because it requires a caller |
| 58 // to be in a v8 context. | 55 // to be in a v8 context. |
| 59 | 56 |
| 60 detachPromiseForExpose(); | 57 m_promise.clear(); |
| 61 detachPromiseForResolve(); | |
| 62 } | 58 } |
| 63 | 59 |
| 64 void ScriptPromiseResolver::detachPromise() | 60 PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create(ScriptPromise pr
omise, ExecutionContext* context) |
| 65 { | |
| 66 detachPromiseForExpose(); | |
| 67 } | |
| 68 | |
| 69 void ScriptPromiseResolver::detachPromiseForExpose() | |
| 70 { | |
| 71 m_promiseForExposeDetached = true; | |
| 72 if (m_promiseForResolveDetached) | |
| 73 m_promise.clear(); | |
| 74 } | |
| 75 | |
| 76 void ScriptPromiseResolver::detachPromiseForResolve() | |
| 77 { | |
| 78 m_promiseForResolveDetached = true; | |
| 79 if (m_promiseForExposeDetached) | |
| 80 m_promise.clear(); | |
| 81 } | |
| 82 | |
| 83 PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create(ExecutionContext
* context) | |
| 84 { | 61 { |
| 85 ASSERT(v8::Context::InContext()); | 62 ASSERT(v8::Context::InContext()); |
| 86 ASSERT(context); | 63 ASSERT(context); |
| 87 return adoptRef(new ScriptPromiseResolver(toV8Context(context, DOMWrapperWor
ld::current())->Global(), toIsolate(context))); | 64 return adoptRef(new ScriptPromiseResolver(promise, toIsolate(context))); |
| 88 } | 65 } |
| 89 | 66 |
| 90 PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create() | 67 PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create(ScriptPromise pr
omise) |
| 91 { | 68 { |
| 92 ASSERT(v8::Context::InContext()); | 69 ASSERT(v8::Context::InContext()); |
| 93 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 70 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 94 return adoptRef(new ScriptPromiseResolver(v8::Object::New(), isolate)); | 71 return adoptRef(new ScriptPromiseResolver(promise, isolate)); |
| 95 } | 72 } |
| 96 | 73 |
| 97 bool ScriptPromiseResolver::isPending() const | 74 bool ScriptPromiseResolver::isPending() const |
| 98 { | 75 { |
| 99 ASSERT(v8::Context::InContext()); | 76 ASSERT(v8::Context::InContext()); |
| 100 if (m_promiseForResolveDetached) | 77 if (m_promise.hasNoValue()) |
| 101 return false; | 78 return false; |
| 102 ASSERT(!m_promise.hasNoValue()); | |
| 103 v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>(); | 79 v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>(); |
| 104 v8::Local<v8::Object> internal = V8PromiseCustom::getInternal(promise); | 80 v8::Local<v8::Object> internal = V8PromiseCustom::getInternal(promise); |
| 105 V8PromiseCustom::PromiseState state = V8PromiseCustom::getState(internal); | 81 V8PromiseCustom::PromiseState state = V8PromiseCustom::getState(internal); |
| 106 return state == V8PromiseCustom::Pending; | 82 return state == V8PromiseCustom::Pending; |
| 107 } | 83 } |
| 108 | 84 |
| 109 void ScriptPromiseResolver::detach() | |
| 110 { | |
| 111 ASSERT(v8::Context::InContext()); | |
| 112 detachPromiseForExpose(); | |
| 113 reject(v8::Undefined(m_isolate)); | |
| 114 detachPromiseForResolve(); | |
| 115 } | |
| 116 | |
| 117 void ScriptPromiseResolver::fulfill(v8::Handle<v8::Value> value) | |
| 118 { | |
| 119 resolve(value); | |
| 120 } | |
| 121 | |
| 122 void ScriptPromiseResolver::resolve(v8::Handle<v8::Value> value) | 85 void ScriptPromiseResolver::resolve(v8::Handle<v8::Value> value) |
| 123 { | 86 { |
| 124 ASSERT(v8::Context::InContext()); | 87 ASSERT(v8::Context::InContext()); |
| 125 if (!isPending()) | 88 if (!isPending()) |
| 126 return; | 89 return; |
| 127 V8PromiseCustom::resolve(m_promise.v8Value().As<v8::Object>(), value, m_isol
ate); | 90 V8PromiseCustom::resolve(m_promise.v8Value().As<v8::Object>(), value, m_isol
ate); |
| 128 detachPromiseForResolve(); | 91 m_promise.clear(); |
| 129 } | 92 } |
| 130 | 93 |
| 131 void ScriptPromiseResolver::reject(v8::Handle<v8::Value> value) | 94 void ScriptPromiseResolver::reject(v8::Handle<v8::Value> value) |
| 132 { | 95 { |
| 133 ASSERT(v8::Context::InContext()); | 96 ASSERT(v8::Context::InContext()); |
| 134 if (!isPending()) | 97 if (!isPending()) |
| 135 return; | 98 return; |
| 136 V8PromiseCustom::reject(m_promise.v8Value().As<v8::Object>(), value, m_isola
te); | 99 V8PromiseCustom::reject(m_promise.v8Value().As<v8::Object>(), value, m_isola
te); |
| 137 detachPromiseForResolve(); | 100 m_promise.clear(); |
| 138 } | |
| 139 | |
| 140 void ScriptPromiseResolver::fulfill(ScriptValue value) | |
| 141 { | |
| 142 resolve(value); | |
| 143 } | 101 } |
| 144 | 102 |
| 145 void ScriptPromiseResolver::resolve(ScriptValue value) | 103 void ScriptPromiseResolver::resolve(ScriptValue value) |
| 146 { | 104 { |
| 147 ASSERT(v8::Context::InContext()); | 105 ASSERT(v8::Context::InContext()); |
| 148 resolve(value.v8Value()); | 106 resolve(value.v8Value()); |
| 149 } | 107 } |
| 150 | 108 |
| 151 void ScriptPromiseResolver::reject(ScriptValue value) | 109 void ScriptPromiseResolver::reject(ScriptValue value) |
| 152 { | 110 { |
| 153 ASSERT(v8::Context::InContext()); | 111 ASSERT(v8::Context::InContext()); |
| 154 reject(value.v8Value()); | 112 reject(value.v8Value()); |
| 155 } | 113 } |
| 156 | 114 |
| 157 } // namespace WebCore | 115 } // namespace WebCore |
| OLD | NEW |