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 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 ASSERT(context->isWorkerGlobalScope()); | 129 ASSERT(context->isWorkerGlobalScope()); |
130 state = scriptStateFromWorkerGlobalScope(toWorkerGlobalScope(context)); | 130 state = scriptStateFromWorkerGlobalScope(toWorkerGlobalScope(context)); |
131 } | 131 } |
132 ASSERT(state); | 132 ASSERT(state); |
133 | 133 |
134 v8::Isolate* isolate = state->isolate(); | 134 v8::Isolate* isolate = state->isolate(); |
135 v8::HandleScope handleScope(isolate); | 135 v8::HandleScope handleScope(isolate); |
136 v8::Handle<v8::Context> v8Context = state->context(); | 136 v8::Handle<v8::Context> v8Context = state->context(); |
137 v8::Context::Scope scope(v8Context); | 137 v8::Context::Scope scope(v8Context); |
138 v8::Handle<v8::Value> args[] = { m_result.newLocal(isolate) }; | 138 v8::Handle<v8::Value> args[] = { m_result.newLocal(isolate) }; |
139 V8ScriptRunner::callFunction(m_callback.newLocal(isolate), context, m_receiv
er.newLocal(isolate), WTF_ARRAY_LENGTH(args), args); | 139 V8ScriptRunner::callFunction(m_callback.newLocal(isolate), context, m_receiv
er.newLocal(isolate), WTF_ARRAY_LENGTH(args), args, isolate); |
140 }; | 140 }; |
141 | 141 |
142 v8::Handle<v8::Value> postTask(v8::Handle<v8::Function> callback, v8::Handle<v8:
:Object> receiver, v8::Handle<v8::Value> value, v8::Isolate* isolate) | 142 v8::Handle<v8::Value> postTask(v8::Handle<v8::Function> callback, v8::Handle<v8:
:Object> receiver, v8::Handle<v8::Value> value, v8::Isolate* isolate) |
143 { | 143 { |
144 ScriptExecutionContext* scriptExecutionContext = getScriptExecutionContext()
; | 144 ScriptExecutionContext* scriptExecutionContext = getScriptExecutionContext()
; |
145 ASSERT(scriptExecutionContext && scriptExecutionContext->isContextThread()); | 145 ASSERT(scriptExecutionContext && scriptExecutionContext->isContextThread()); |
146 scriptExecutionContext->postTask(adoptPtr(new PromiseTask(callback, receiver
, value))); | 146 scriptExecutionContext->postTask(adoptPtr(new PromiseTask(callback, receiver
, value))); |
147 return v8::Undefined(isolate); | 147 return v8::Undefined(isolate); |
148 } | 148 } |
149 | 149 |
150 void wrapperCallback(const v8::FunctionCallbackInfo<v8::Value>& args) | 150 void wrapperCallback(const v8::FunctionCallbackInfo<v8::Value>& args) |
151 { | 151 { |
152 v8::Isolate* isolate = args.GetIsolate(); | 152 v8::Isolate* isolate = args.GetIsolate(); |
153 ASSERT(!args.Data().IsEmpty()); | 153 ASSERT(!args.Data().IsEmpty()); |
154 v8::Local<v8::Object> environment = args.Data().As<v8::Object>(); | 154 v8::Local<v8::Object> environment = args.Data().As<v8::Object>(); |
155 v8::Local<v8::Value> result = v8::Undefined(isolate); | 155 v8::Local<v8::Value> result = v8::Undefined(isolate); |
156 if (args.Length() > 0) | 156 if (args.Length() > 0) |
157 result = args[0]; | 157 result = args[0]; |
158 | 158 |
159 v8::Local<v8::Object> promise = environment->GetInternalField(V8PromiseCusto
m::WrapperCallbackEnvironmentPromiseIndex).As<v8::Object>(); | 159 v8::Local<v8::Object> promise = environment->GetInternalField(V8PromiseCusto
m::WrapperCallbackEnvironmentPromiseIndex).As<v8::Object>(); |
160 v8::Local<v8::Object> resolver = environment->GetInternalField(V8PromiseCust
om::WrapperCallbackEnvironmentPromiseResolverIndex).As<v8::Object>(); | 160 v8::Local<v8::Object> resolver = environment->GetInternalField(V8PromiseCust
om::WrapperCallbackEnvironmentPromiseResolverIndex).As<v8::Object>(); |
161 v8::Local<v8::Function> callback = environment->GetInternalField(V8PromiseCu
stom::WrapperCallbackEnvironmentCallbackIndex).As<v8::Function>(); | 161 v8::Local<v8::Function> callback = environment->GetInternalField(V8PromiseCu
stom::WrapperCallbackEnvironmentCallbackIndex).As<v8::Function>(); |
162 | 162 |
163 v8::Local<v8::Value> argv[] = { | 163 v8::Local<v8::Value> argv[] = { |
164 result, | 164 result, |
165 }; | 165 }; |
166 v8::TryCatch trycatch; | 166 v8::TryCatch trycatch; |
167 result = V8ScriptRunner::callFunction(callback, getScriptExecutionContext(),
promise, WTF_ARRAY_LENGTH(argv), argv); | 167 result = V8ScriptRunner::callFunction(callback, getScriptExecutionContext(),
promise, WTF_ARRAY_LENGTH(argv), argv, isolate); |
168 if (result.IsEmpty()) { | 168 if (result.IsEmpty()) { |
169 V8PromiseCustom::rejectResolver(resolver, trycatch.Exception(), V8Promis
eCustom::Synchronous, isolate); | 169 V8PromiseCustom::rejectResolver(resolver, trycatch.Exception(), V8Promis
eCustom::Synchronous, isolate); |
170 return; | 170 return; |
171 } | 171 } |
172 V8PromiseCustom::resolveResolver(resolver, result, V8PromiseCustom::Synchron
ous, isolate); | 172 V8PromiseCustom::resolveResolver(resolver, result, V8PromiseCustom::Synchron
ous, isolate); |
173 } | 173 } |
174 | 174 |
175 v8::Local<v8::Object> wrapperCallbackEnvironment(v8::Handle<v8::Object> promise,
v8::Handle<v8::Object> resolver, v8::Handle<v8::Function> callback, v8::Isolate
* isolate) | 175 v8::Local<v8::Object> wrapperCallbackEnvironment(v8::Handle<v8::Object> promise,
v8::Handle<v8::Object> resolver, v8::Handle<v8::Function> callback, v8::Isolate
* isolate) |
176 { | 176 { |
177 v8::Local<v8::ObjectTemplate> objectTemplate = wrapperCallbackEnvironmentObj
ectTemplate(isolate); | 177 v8::Local<v8::ObjectTemplate> objectTemplate = wrapperCallbackEnvironmentObj
ectTemplate(isolate); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 throwTypeError("Promise constructor takes a function argument", isolate)
; | 297 throwTypeError("Promise constructor takes a function argument", isolate)
; |
298 return; | 298 return; |
299 } | 299 } |
300 v8::Local<v8::Function> init = args[0].As<v8::Function>(); | 300 v8::Local<v8::Function> init = args[0].As<v8::Function>(); |
301 v8::Local<v8::Object> promise, resolver; | 301 v8::Local<v8::Object> promise, resolver; |
302 V8PromiseCustom::createPromise(args.Holder(), &promise, &resolver, isolate); | 302 V8PromiseCustom::createPromise(args.Holder(), &promise, &resolver, isolate); |
303 v8::Handle<v8::Value> argv[] = { | 303 v8::Handle<v8::Value> argv[] = { |
304 resolver, | 304 resolver, |
305 }; | 305 }; |
306 v8::TryCatch trycatch; | 306 v8::TryCatch trycatch; |
307 if (V8ScriptRunner::callFunction(init, getScriptExecutionContext(), promise,
WTF_ARRAY_LENGTH(argv), argv).IsEmpty()) { | 307 if (V8ScriptRunner::callFunction(init, getScriptExecutionContext(), promise,
WTF_ARRAY_LENGTH(argv), argv, isolate).IsEmpty()) { |
308 // An exception is thrown. Reject the promise if its resolved flag is un
set. | 308 // An exception is thrown. Reject the promise if its resolved flag is un
set. |
309 if (!V8PromiseCustom::isInternalDetached(resolver) && V8PromiseCustom::g
etState(V8PromiseCustom::getInternal(resolver)) == V8PromiseCustom::Pending) | 309 if (!V8PromiseCustom::isInternalDetached(resolver) && V8PromiseCustom::g
etState(V8PromiseCustom::getInternal(resolver)) == V8PromiseCustom::Pending) |
310 V8PromiseCustom::rejectResolver(resolver, trycatch.Exception(), V8Pr
omiseCustom::Asynchronous, isolate); | 310 V8PromiseCustom::rejectResolver(resolver, trycatch.Exception(), V8Pr
omiseCustom::Asynchronous, isolate); |
311 } | 311 } |
312 v8SetReturnValue(args, promise); | 312 v8SetReturnValue(args, promise); |
313 return; | 313 return; |
314 } | 314 } |
315 | 315 |
316 void V8Promise::thenMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args
) | 316 void V8Promise::thenMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args
) |
317 { | 317 { |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 } | 526 } |
527 } | 527 } |
528 | 528 |
529 if (!then.IsEmpty() && then->IsFunction()) { | 529 if (!then.IsEmpty() && then->IsFunction()) { |
530 ASSERT(result->IsObject()); | 530 ASSERT(result->IsObject()); |
531 v8::TryCatch trycatch; | 531 v8::TryCatch trycatch; |
532 v8::Handle<v8::Value> argv[] = { | 532 v8::Handle<v8::Value> argv[] = { |
533 createClosure(promiseResolveCallback, resolver), | 533 createClosure(promiseResolveCallback, resolver), |
534 createClosure(promiseRejectCallback, resolver), | 534 createClosure(promiseRejectCallback, resolver), |
535 }; | 535 }; |
536 if (V8ScriptRunner::callFunction(then.As<v8::Function>(), getScriptExecu
tionContext(), result.As<v8::Object>(), WTF_ARRAY_LENGTH(argv), argv).IsEmpty()) | 536 if (V8ScriptRunner::callFunction(then.As<v8::Function>(), getScriptExecu
tionContext(), result.As<v8::Object>(), WTF_ARRAY_LENGTH(argv), argv, isolate).I
sEmpty()) |
537 rejectResolver(resolver, trycatch.Exception(), mode, isolate); | 537 rejectResolver(resolver, trycatch.Exception(), mode, isolate); |
538 return; | 538 return; |
539 } | 539 } |
540 | 540 |
541 fulfillResolver(resolver, result, mode, isolate); | 541 fulfillResolver(resolver, result, mode, isolate); |
542 } | 542 } |
543 | 543 |
544 void V8PromiseCustom::rejectResolver(v8::Handle<v8::Object> resolver, v8::Handle
<v8::Value> result, SynchronousMode mode, v8::Isolate* isolate) | 544 void V8PromiseCustom::rejectResolver(v8::Handle<v8::Object> resolver, v8::Handle
<v8::Value> result, SynchronousMode mode, v8::Isolate* isolate) |
545 { | 545 { |
546 if (isInternalDetached(resolver)) | 546 if (isInternalDetached(resolver)) |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
630 internal->SetInternalField(V8PromiseCustom::InternalStateIndex, v8::Integer:
:New(state)); | 630 internal->SetInternalField(V8PromiseCustom::InternalStateIndex, v8::Integer:
:New(state)); |
631 } | 631 } |
632 | 632 |
633 void V8PromiseCustom::call(v8::Handle<v8::Function> function, v8::Handle<v8::Obj
ect> receiver, v8::Handle<v8::Value> result, SynchronousMode mode, v8::Isolate*
isolate) | 633 void V8PromiseCustom::call(v8::Handle<v8::Function> function, v8::Handle<v8::Obj
ect> receiver, v8::Handle<v8::Value> result, SynchronousMode mode, v8::Isolate*
isolate) |
634 { | 634 { |
635 if (mode == Synchronous) { | 635 if (mode == Synchronous) { |
636 v8::Context::Scope scope(isolate->GetCurrentContext()); | 636 v8::Context::Scope scope(isolate->GetCurrentContext()); |
637 // If an exception is thrown, catch it and do nothing. | 637 // If an exception is thrown, catch it and do nothing. |
638 v8::TryCatch trycatch; | 638 v8::TryCatch trycatch; |
639 v8::Handle<v8::Value> args[] = { result }; | 639 v8::Handle<v8::Value> args[] = { result }; |
640 V8ScriptRunner::callFunction(function, getScriptExecutionContext(), rece
iver, WTF_ARRAY_LENGTH(args), args); | 640 V8ScriptRunner::callFunction(function, getScriptExecutionContext(), rece
iver, WTF_ARRAY_LENGTH(args), args, isolate); |
641 } else { | 641 } else { |
642 ASSERT(mode == Asynchronous); | 642 ASSERT(mode == Asynchronous); |
643 postTask(function, receiver, result, isolate); | 643 postTask(function, receiver, result, isolate); |
644 } | 644 } |
645 } | 645 } |
646 | 646 |
647 } // namespace WebCore | 647 } // namespace WebCore |
OLD | NEW |