Commit 8033be88 authored by ager@chromium.org's avatar ager@chromium.org

Add IsCallable method for Object in the API

Patch by Peter Varga.

BUG=none
TEST=cctest/test-api/CallableObject

Review URL: http://codereview.chromium.org/6964005

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@7828 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 45d07bcd
......@@ -1606,6 +1606,13 @@ class Object : public Value {
V8EXPORT ExternalArrayType GetIndexedPropertiesExternalArrayDataType();
V8EXPORT int GetIndexedPropertiesExternalArrayDataLength();
/**
* Checks whether a callback is set by the
* ObjectTemplate::SetCallAsFunctionHandler method.
* When an Object is callable this method returns true.
*/
V8EXPORT bool IsCallable();
/**
* Call an Object as a function if a callback is set by the
* ObjectTemplate::SetCallAsFunctionHandler method.
......
......@@ -3255,6 +3255,17 @@ int v8::Object::GetIndexedPropertiesExternalArrayDataLength() {
}
bool v8::Object::IsCallable() {
i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
ON_BAILOUT(isolate, "v8::Object::IsCallable()", return false);
ENTER_V8(isolate);
i::HandleScope scope(isolate);
i::Handle<i::JSObject> obj = Utils::OpenHandle(this);
if (obj->IsJSFunction()) return true;
return i::Execution::GetFunctionDelegate(obj)->IsJSFunction();
}
Local<v8::Value> Object::CallAsFunction(v8::Handle<v8::Object> recv, int argc,
v8::Handle<v8::Value> argv[]) {
i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
......
......@@ -7264,6 +7264,49 @@ THREADED_TEST(CallAsFunction) {
}
// Check whether a non-function object is callable.
THREADED_TEST(CallableObject) {
v8::HandleScope scope;
LocalContext context;
{ Local<ObjectTemplate> instance_template = ObjectTemplate::New();
instance_template->SetCallAsFunctionHandler(call_as_function);
Local<Object> instance = instance_template->NewInstance();
v8::TryCatch try_catch;
CHECK(instance->IsCallable());
CHECK(!try_catch.HasCaught());
}
{ Local<ObjectTemplate> instance_template = ObjectTemplate::New();
Local<Object> instance = instance_template->NewInstance();
v8::TryCatch try_catch;
CHECK(!instance->IsCallable());
CHECK(!try_catch.HasCaught());
}
{ Local<FunctionTemplate> function_template =
FunctionTemplate::New(call_as_function);
Local<Function> function = function_template->GetFunction();
Local<Object> instance = function;
v8::TryCatch try_catch;
CHECK(instance->IsCallable());
CHECK(!try_catch.HasCaught());
}
{ Local<FunctionTemplate> function_template = FunctionTemplate::New();
Local<Function> function = function_template->GetFunction();
Local<Object> instance = function;
v8::TryCatch try_catch;
CHECK(instance->IsCallable());
CHECK(!try_catch.HasCaught());
}
}
static int CountHandles() {
return v8::HandleScope::NumberOfHandles();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment