| 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() |
| 46 : m_isolate(isolate) | |
| 47 { | 46 { |
| 48 ASSERT(RuntimeEnabledFeatures::promiseEnabled()); | 47 ASSERT(RuntimeEnabledFeatures::promiseEnabled()); |
| 49 v8::Local<v8::Object> promise, resolver; | 48 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 50 V8PromiseCustom::createPromise(creationContext, &promise, &resolver, isolate
); | 49 m_isolate = isolate; |
| 51 m_promise.set(isolate, promise); | 50 } |
| 52 m_resolver.set(isolate, resolver); | 51 |
| 52 ScriptPromiseResolver::ScriptPromiseResolver(ScriptValue resolver) |
| 53 : m_resolver(resolver) |
| 54 { |
| 55 ASSERT(RuntimeEnabledFeatures::promiseEnabled()); |
| 56 ASSERT(!m_resolver.hasNoValue()); |
| 57 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 58 m_isolate = isolate; |
| 59 } |
| 60 |
| 61 ScriptPromiseResolver::ScriptPromiseResolver(v8::Handle<v8::Value> resolver, v8:
:Isolate* isolate) |
| 62 : m_isolate(isolate) |
| 63 , m_resolver(resolver) |
| 64 { |
| 65 ASSERT(RuntimeEnabledFeatures::promiseEnabled()); |
| 66 ASSERT(!m_resolver.hasNoValue()); |
| 67 } |
| 68 |
| 69 ScriptPromiseResolver::ScriptPromiseResolver(v8::Handle<v8::Value> resolver, v8:
:Handle<v8::Value> promise, v8::Isolate* isolate) |
| 70 : m_isolate(isolate) |
| 71 , m_resolver(resolver) |
| 72 , m_promise(promise) |
| 73 { |
| 74 ASSERT(RuntimeEnabledFeatures::promiseEnabled()); |
| 75 ASSERT(!m_resolver.hasNoValue()); |
| 53 } | 76 } |
| 54 | 77 |
| 55 ScriptPromiseResolver::~ScriptPromiseResolver() | 78 ScriptPromiseResolver::~ScriptPromiseResolver() |
| 56 { | 79 { |
| 57 // We don't call "detach" here because it requires a caller | 80 // We don't call "detach" here because it requires a caller |
| 58 // to be in a v8 context. | 81 // to be in a v8 context. |
| 59 | 82 |
| 60 detachPromise(); | 83 detachPromise(); |
| 61 m_resolver.clear(); | 84 m_resolver.clear(); |
| 62 } | 85 } |
| 63 | 86 |
| 64 PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create(ScriptExecutionC
ontext* context) | 87 ScriptPromiseResolver ScriptPromiseResolver::create(ScriptExecutionContext* cont
ext) |
| 65 { | 88 { |
| 66 ASSERT(v8::Context::InContext()); | 89 ASSERT(v8::Context::InContext()); |
| 67 ASSERT(context); | 90 ASSERT(context); |
| 68 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 91 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 69 return adoptRef(new ScriptPromiseResolver(toV8Context(context, DOMWrapperWor
ld::current())->Global(), isolate)); | 92 v8::Local<v8::Object> promise, resolver; |
| 93 V8PromiseCustom::createPromise(v8::Handle<v8::Object>(), &promise, &resolver
, isolate); |
| 94 return ScriptPromiseResolver(resolver, promise, isolate); |
| 70 } | 95 } |
| 71 | 96 |
| 72 PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create() | 97 ScriptPromiseResolver ScriptPromiseResolver::create() |
| 73 { | 98 { |
| 74 ASSERT(v8::Context::InContext()); | 99 ASSERT(v8::Context::InContext()); |
| 75 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 100 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 76 return adoptRef(new ScriptPromiseResolver(v8::Object::New(), isolate)); | 101 v8::Local<v8::Object> promise, resolver; |
| 102 V8PromiseCustom::createPromise(v8::Handle<v8::Object>(), &promise, &resolver
, isolate); |
| 103 return ScriptPromiseResolver(resolver, promise, isolate); |
| 77 } | 104 } |
| 78 | 105 |
| 79 bool ScriptPromiseResolver::isPending() const | 106 bool ScriptPromiseResolver::isPending() const |
| 80 { | 107 { |
| 81 ASSERT(v8::Context::InContext()); | 108 ASSERT(v8::Context::InContext()); |
| 82 return isPendingInternal(); | 109 return isPendingInternal(); |
| 83 } | 110 } |
| 84 | 111 |
| 85 void ScriptPromiseResolver::detach() | 112 void ScriptPromiseResolver::detach() |
| 86 { | 113 { |
| 87 ASSERT(v8::Context::InContext()); | 114 ASSERT(v8::Context::InContext()); |
| 88 detachPromise(); | 115 detachPromise(); |
| 89 reject(v8::Undefined(m_isolate)); | 116 reject(v8::Undefined(m_isolate)); |
| 90 m_resolver.clear(); | 117 m_resolver.clear(); |
| 91 } | 118 } |
| 92 | 119 |
| 93 void ScriptPromiseResolver::fulfill(v8::Handle<v8::Value> value) | 120 void ScriptPromiseResolver::fulfill(v8::Handle<v8::Value> value) |
| 94 { | 121 { |
| 95 ASSERT(v8::Context::InContext()); | 122 ASSERT(v8::Context::InContext()); |
| 96 if (!isPendingInternal()) | 123 if (!isPendingInternal()) |
| 97 return; | 124 return; |
| 98 V8PromiseCustom::fulfillResolver(m_resolver.newLocal(m_isolate), value, V8Pr
omiseCustom::Asynchronous, m_isolate); | 125 V8PromiseCustom::fulfillResolver(m_resolver.v8Value().As<v8::Object>(), valu
e, V8PromiseCustom::Asynchronous, m_isolate); |
| 99 m_resolver.clear(); | 126 m_resolver.clear(); |
| 100 } | 127 } |
| 101 | 128 |
| 102 void ScriptPromiseResolver::resolve(v8::Handle<v8::Value> value) | 129 void ScriptPromiseResolver::resolve(v8::Handle<v8::Value> value) |
| 103 { | 130 { |
| 104 ASSERT(v8::Context::InContext()); | 131 ASSERT(v8::Context::InContext()); |
| 105 if (!isPendingInternal()) | 132 if (!isPendingInternal()) |
| 106 return; | 133 return; |
| 107 V8PromiseCustom::resolveResolver(m_resolver.newLocal(m_isolate), value, V8Pr
omiseCustom::Asynchronous, m_isolate); | 134 V8PromiseCustom::resolveResolver(m_resolver.v8Value().As<v8::Object>(), valu
e, V8PromiseCustom::Asynchronous, m_isolate); |
| 108 m_resolver.clear(); | 135 m_resolver.clear(); |
| 109 } | 136 } |
| 110 | 137 |
| 111 void ScriptPromiseResolver::reject(v8::Handle<v8::Value> value) | 138 void ScriptPromiseResolver::reject(v8::Handle<v8::Value> value) |
| 112 { | 139 { |
| 113 ASSERT(v8::Context::InContext()); | 140 ASSERT(v8::Context::InContext()); |
| 114 if (!isPendingInternal()) | 141 if (!isPendingInternal()) |
| 115 return; | 142 return; |
| 116 V8PromiseCustom::rejectResolver(m_resolver.newLocal(m_isolate), value, V8Pro
miseCustom::Asynchronous, m_isolate); | 143 V8PromiseCustom::rejectResolver(m_resolver.v8Value().As<v8::Object>(), value
, V8PromiseCustom::Asynchronous, m_isolate); |
| 117 m_resolver.clear(); | 144 m_resolver.clear(); |
| 118 } | 145 } |
| 119 | 146 |
| 120 void ScriptPromiseResolver::fulfill(ScriptValue value) | 147 void ScriptPromiseResolver::fulfill(ScriptValue value) |
| 121 { | 148 { |
| 122 ASSERT(v8::Context::InContext()); | 149 ASSERT(v8::Context::InContext()); |
| 123 fulfill(value.v8Value()); | 150 fulfill(value.v8Value()); |
| 124 } | 151 } |
| 125 | 152 |
| 126 void ScriptPromiseResolver::resolve(ScriptValue value) | 153 void ScriptPromiseResolver::resolve(ScriptValue value) |
| 127 { | 154 { |
| 128 ASSERT(v8::Context::InContext()); | 155 ASSERT(v8::Context::InContext()); |
| 129 resolve(value.v8Value()); | 156 resolve(value.v8Value()); |
| 130 } | 157 } |
| 131 | 158 |
| 132 void ScriptPromiseResolver::reject(ScriptValue value) | 159 void ScriptPromiseResolver::reject(ScriptValue value) |
| 133 { | 160 { |
| 134 ASSERT(v8::Context::InContext()); | 161 ASSERT(v8::Context::InContext()); |
| 135 reject(value.v8Value()); | 162 reject(value.v8Value()); |
| 136 } | 163 } |
| 137 | 164 |
| 138 bool ScriptPromiseResolver::isPendingInternal() const | 165 bool ScriptPromiseResolver::isPendingInternal() const |
| 139 { | 166 { |
| 140 ASSERT(v8::Context::InContext()); | 167 ASSERT(v8::Context::InContext()); |
| 141 if (m_resolver.isEmpty()) | 168 if (m_resolver.hasNoValue()) |
| 142 return false; | 169 return false; |
| 143 v8::Local<v8::Object> resolver = m_resolver.newLocal(m_isolate); | 170 if (!m_resolver.isObject()) |
| 171 return false; |
| 172 v8::Local<v8::Object> resolver = m_resolver.v8Value().As<v8::Object>(); |
| 144 if (V8PromiseCustom::isInternalDetached(resolver)) | 173 if (V8PromiseCustom::isInternalDetached(resolver)) |
| 145 return false; | 174 return false; |
| 146 v8::Local<v8::Object> internal = V8PromiseCustom::getInternal(resolver); | 175 v8::Local<v8::Object> internal = V8PromiseCustom::getInternal(resolver); |
| 147 V8PromiseCustom::PromiseState state = V8PromiseCustom::getState(internal); | 176 V8PromiseCustom::PromiseState state = V8PromiseCustom::getState(internal); |
| 148 return state == V8PromiseCustom::Pending; | 177 return state == V8PromiseCustom::Pending; |
| 149 } | 178 } |
| 150 | 179 |
| 151 } // namespace WebCore | 180 } // namespace WebCore |
| OLD | NEW |