diff options
author | kolczyk@opera.com <kolczyk@opera.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-18 16:46:14 +0000 |
---|---|---|
committer | kolczyk@opera.com <kolczyk@opera.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-18 16:46:14 +0000 |
commit | 744a494d5bdb17533e669bad06ed6ac4195c4e55 (patch) | |
tree | ddad83c59cd27dbaaff5e2ac0e230b747122b615 /gin | |
parent | 7ac4e53266b36040045733a5fde684837b7a4f5a (diff) | |
download | chromium_src-744a494d5bdb17533e669bad06ed6ac4195c4e55.zip chromium_src-744a494d5bdb17533e669bad06ed6ac4195c4e55.tar.gz chromium_src-744a494d5bdb17533e669bad06ed6ac4195c4e55.tar.bz2 |
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
Diffstat (limited to 'gin')
-rw-r--r-- | gin/arguments.cc | 4 | ||||
-rw-r--r-- | gin/arguments.h | 4 | ||||
-rw-r--r-- | gin/wrappable_unittest.cc | 29 |
3 files changed, 35 insertions, 2 deletions
diff --git a/gin/arguments.cc b/gin/arguments.cc index c43ff28..6703fc2 100644 --- a/gin/arguments.cc +++ b/gin/arguments.cc @@ -45,4 +45,8 @@ void Arguments::ThrowTypeError(const std::string& message) const { StringToV8(isolate_, message))); } +bool Arguments::IsConstructCall() const { + return info_->IsConstructCall(); +} + } // namespace gin diff --git a/gin/arguments.h b/gin/arguments.h index 509c22c..96a3701 100644 --- a/gin/arguments.h +++ b/gin/arguments.h @@ -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_; diff --git a/gin/wrappable_unittest.cc b/gin/wrappable_unittest.cc index 9843329..f3de48f 100644 --- a/gin/wrappable_unittest.cc +++ b/gin/wrappable_unittest.cc @@ -97,8 +97,11 @@ class MyCallableObject : public Wrappable<MyCallableObject> { virtual ~MyCallableObject() { } - void Call(int val) { - result_ = val; + void Call(int val, const gin::Arguments& arguments) { + if (arguments.IsConstructCall()) + arguments.ThrowTypeError("Cannot be called as constructor."); + else + result_ = val; } int result_; @@ -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 |