Commit 744a494d authored by kolczyk@opera.com's avatar kolczyk@opera.com

Added IsConstructorCall to gin::Arguments.

It simply forwards the flag from v8::FunctionCallbackInfo and allows
the function handler that accept gin::Arguments argument to distinguish
between normal function invocation and call as an object construction.

BUG=

Review URL: https://codereview.chromium.org/401823002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@284123 0039d316-1c4b-4281-b951-d872f2087c98
parent 7ac4e532
......@@ -45,4 +45,8 @@ void Arguments::ThrowTypeError(const std::string& message) const {
StringToV8(isolate_, message)));
}
bool Arguments::IsConstructCall() const {
return info_->IsConstructCall();
}
} // namespace gin
......@@ -79,6 +79,10 @@ class GIN_EXPORT Arguments {
v8::Isolate* isolate() const { return isolate_; }
// Allows the function handler to distinguish between normal invocation
// and object construction.
bool IsConstructCall() const;
private:
v8::Isolate* isolate_;
const v8::FunctionCallbackInfo<v8::Value>* info_;
......
......@@ -97,7 +97,10 @@ class MyCallableObject : public Wrappable<MyCallableObject> {
virtual ~MyCallableObject() {
}
void Call(int val) {
void Call(int val, const gin::Arguments& arguments) {
if (arguments.IsConstructCall())
arguments.ThrowTypeError("Cannot be called as constructor.");
else
result_ = val;
}
......@@ -245,4 +248,26 @@ TEST_F(WrappableTest, CallAsFunction) {
EXPECT_EQ(42, object->result());
}
TEST_F(WrappableTest, CallAsConstructor) {
v8::Isolate* isolate = instance_->isolate();
v8::HandleScope handle_scope(isolate);
gin::Handle<MyCallableObject> object(MyCallableObject::Create(isolate));
EXPECT_EQ(0, object->result());
v8::Handle<v8::String> source = StringToV8(isolate,
"(function(obj) {"
"new obj(42);"
"})");
gin::TryCatch try_catch;
v8::Handle<v8::Script> script = v8::Script::Compile(source);
v8::Handle<v8::Value> val = script->Run();
v8::Handle<v8::Function> func;
EXPECT_TRUE(ConvertFromV8(isolate, val, &func));
v8::Handle<v8::Value> argv[] = {
ConvertToV8(isolate, object.get())
};
func->Call(v8::Undefined(isolate), 1, argv);
EXPECT_TRUE(try_catch.HasCaught());
}
} // namespace gin
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