Index: src/api.cc |
diff --git a/src/api.cc b/src/api.cc |
index 0dd4ee576a4c94b74fd30e9f8be8d0b22aab04eb..f4500e9a021611d4855158525658c44ceb3d341a 100644 |
--- a/src/api.cc |
+++ b/src/api.cc |
@@ -2651,6 +2651,13 @@ void v8::Array::CheckCast(Value* that) { |
} |
+void v8::Promise::CheckCast(Value* that) { |
+ Utils::ApiCheck(that->IsPromise(), |
+ "v8::Promise::Cast()", |
+ "Could not convert to promise"); |
+} |
+ |
+ |
void v8::ArrayBuffer::CheckCast(Value* that) { |
i::Handle<i::Object> obj = Utils::OpenHandle(that); |
Utils::ApiCheck(obj->IsJSArrayBuffer(), |
@@ -5731,6 +5738,124 @@ Local<Object> Array::CloneElementAt(uint32_t index) { |
} |
+bool Value::IsPromise() const { |
+ i::Handle<i::Object> val = Utils::OpenHandle(this); |
+ if (!i::FLAG_harmony_promises || !val->IsJSObject()) return false; |
+ i::Handle<i::JSObject> obj = i::Handle<i::JSObject>::cast(val); |
+ i::Isolate* isolate = obj->GetIsolate(); |
+ LOG_API(isolate, "IsPromise"); |
+ ENTER_V8(isolate); |
+ EXCEPTION_PREAMBLE(isolate); |
+ i::Handle<i::Object> argv[] = { obj }; |
+ i::Handle<i::Object> b = i::Execution::Call( |
+ isolate, |
+ handle( |
+ isolate->context()->global_object()->native_context()->is_promise()), |
+ isolate->factory()->undefined_value(), |
+ ARRAY_SIZE(argv), argv, |
+ &has_pending_exception, |
+ false); |
+ EXCEPTION_BAILOUT_CHECK(isolate, false); |
+ return b->BooleanValue(); |
+} |
+ |
+ |
+Local<Promise> Promise::New(Isolate* v8_isolate) { |
+ i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate); |
+ LOG_API(isolate, "Promise::New"); |
+ ENTER_V8(isolate); |
+ EXCEPTION_PREAMBLE(isolate); |
+ i::Handle<i::Object> result = i::Execution::Call( |
+ isolate, |
+ handle(isolate->context()->global_object()->native_context()-> |
+ promise_create()), |
+ isolate->factory()->undefined_value(), |
+ 0, NULL, |
+ &has_pending_exception, |
+ false); |
+ EXCEPTION_BAILOUT_CHECK(isolate, Local<Promise>()); |
+ return Local<Promise>::Cast(Utils::ToLocal(result)); |
+} |
+ |
+ |
+void Promise::Resolve(Handle<Value> value) { |
+ i::Handle<i::JSObject> promise = Utils::OpenHandle(this); |
+ i::Isolate* isolate = promise->GetIsolate(); |
+ LOG_API(isolate, "Promise::Resolve"); |
+ ENTER_V8(isolate); |
+ EXCEPTION_PREAMBLE(isolate); |
+ i::Handle<i::Object> argv[] = { promise, Utils::OpenHandle(*value) }; |
+ i::Execution::Call( |
+ isolate, |
+ handle(isolate->context()->global_object()->native_context()-> |
+ promise_resolve()), |
+ isolate->factory()->undefined_value(), |
+ ARRAY_SIZE(argv), argv, |
+ &has_pending_exception, |
+ false); |
+ EXCEPTION_BAILOUT_CHECK(isolate, /* void */ ;); |
+} |
+ |
+ |
+void Promise::Reject(Handle<Value> value) { |
+ i::Handle<i::JSObject> promise = Utils::OpenHandle(this); |
+ i::Isolate* isolate = promise->GetIsolate(); |
+ LOG_API(isolate, "Promise::Reject"); |
+ ENTER_V8(isolate); |
+ EXCEPTION_PREAMBLE(isolate); |
+ i::Handle<i::Object> argv[] = { promise, Utils::OpenHandle(*value) }; |
+ i::Execution::Call( |
+ isolate, |
+ handle(isolate->context()->global_object()->native_context()-> |
+ promise_reject()), |
+ isolate->factory()->undefined_value(), |
+ ARRAY_SIZE(argv), argv, |
+ &has_pending_exception, |
+ false); |
+ EXCEPTION_BAILOUT_CHECK(isolate, /* void */ ;); |
+} |
+ |
+ |
+Local<Promise> Promise::Chain(Handle<Function> handler) { |
+ i::Handle<i::JSObject> promise = Utils::OpenHandle(this); |
+ i::Isolate* isolate = promise->GetIsolate(); |
+ LOG_API(isolate, "Promise::Chain"); |
+ ENTER_V8(isolate); |
+ EXCEPTION_PREAMBLE(isolate); |
+ i::Handle<i::Object> argv[] = { Utils::OpenHandle(*handler) }; |
+ i::Handle<i::Object> result = i::Execution::Call( |
+ isolate, |
+ handle(isolate->context()->global_object()->native_context()-> |
+ promise_chain()), |
+ promise, |
+ ARRAY_SIZE(argv), argv, |
+ &has_pending_exception, |
+ false); |
+ EXCEPTION_BAILOUT_CHECK(isolate, Local<Promise>()); |
+ return Local<Promise>::Cast(Utils::ToLocal(result)); |
+} |
+ |
+ |
+Local<Promise> Promise::Catch(Handle<Function> handler) { |
+ i::Handle<i::JSObject> promise = Utils::OpenHandle(this); |
+ i::Isolate* isolate = promise->GetIsolate(); |
+ LOG_API(isolate, "Promise::Catch"); |
+ ENTER_V8(isolate); |
+ EXCEPTION_PREAMBLE(isolate); |
+ i::Handle<i::Object> argv[] = { Utils::OpenHandle(*handler) }; |
+ i::Handle<i::Object> result = i::Execution::Call( |
+ isolate, |
+ handle(isolate->context()->global_object()->native_context()-> |
+ promise_catch()), |
+ promise, |
+ ARRAY_SIZE(argv), argv, |
+ &has_pending_exception, |
+ false); |
+ EXCEPTION_BAILOUT_CHECK(isolate, Local<Promise>()); |
+ return Local<Promise>::Cast(Utils::ToLocal(result)); |
+} |
+ |
+ |
bool v8::ArrayBuffer::IsExternal() const { |
return Utils::OpenHandle(this)->is_external(); |
} |