diff --git a/include/v8.h b/include/v8.h index 688f228f50..edba8eaece 100644 --- a/include/v8.h +++ b/include/v8.h @@ -2013,6 +2013,11 @@ class V8_EXPORT Value : public Data { */ bool IsRegExp() const; + /** + * Returns true if this value is an async function. + */ + bool IsAsyncFunction() const; + /** * Returns true if this value is a Generator function. * This is an experimental feature. diff --git a/src/api.cc b/src/api.cc index e4ac2e784e..1d3b06143e 100644 --- a/src/api.cc +++ b/src/api.cc @@ -3384,6 +3384,12 @@ bool Value::IsRegExp() const { return obj->IsJSRegExp(); } +bool Value::IsAsyncFunction() const { + i::Handle obj = Utils::OpenHandle(this); + if (!obj->IsJSFunction()) return false; + i::Handle func = i::Handle::cast(obj); + return func->shared()->is_async(); +} bool Value::IsGeneratorFunction() const { i::Handle obj = Utils::OpenHandle(this); diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc index 8e88e65d51..1575559d38 100644 --- a/test/cctest/test-api.cc +++ b/test/cctest/test-api.cc @@ -1581,6 +1581,26 @@ THREADED_TEST(IsGeneratorFunctionOrObject) { CHECK(!func->IsGeneratorObject()); } +THREADED_TEST(IsAsyncFunction) { + i::FLAG_harmony_async_await = true; + LocalContext env; + v8::Isolate* isolate = env->GetIsolate(); + v8::HandleScope scope(isolate); + + CompileRun("async function foo() {}"); + v8::Local foo = CompileRun("foo"); + + CHECK(foo->IsAsyncFunction()); + CHECK(foo->IsFunction()); + CHECK(!foo->IsGeneratorFunction()); + CHECK(!foo->IsGeneratorObject()); + + CompileRun("function bar() {}"); + v8::Local bar = CompileRun("bar"); + + CHECK(!bar->IsAsyncFunction()); + CHECK(bar->IsFunction()); +} THREADED_TEST(ArgumentsObject) { LocalContext env;