| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/logging.h" | 5 #include "base/logging.h" |
| 6 #include "gin/arguments.h" | 6 #include "gin/arguments.h" |
| 7 #include "gin/handle.h" | 7 #include "gin/handle.h" |
| 8 #include "gin/object_template_builder.h" | 8 #include "gin/object_template_builder.h" |
| 9 #include "gin/per_isolate_data.h" | 9 #include "gin/per_isolate_data.h" |
| 10 #include "gin/public/isolate_holder.h" | 10 #include "gin/public/isolate_holder.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 | 33 |
| 34 static gin::Handle<MyObject> Create(v8::Isolate* isolate) { | 34 static gin::Handle<MyObject> Create(v8::Isolate* isolate) { |
| 35 return CreateHandle(isolate, new MyObject()); | 35 return CreateHandle(isolate, new MyObject()); |
| 36 } | 36 } |
| 37 | 37 |
| 38 int value() const { return value_; } | 38 int value() const { return value_; } |
| 39 void set_value(int value) { value_ = value; } | 39 void set_value(int value) { value_ = value; } |
| 40 | 40 |
| 41 protected: | 41 protected: |
| 42 MyObject() : value_(0) {} | 42 MyObject() : value_(0) {} |
| 43 virtual ObjectTemplateBuilder GetObjectTemplateBuilder( | 43 ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) override; |
| 44 v8::Isolate* isolate) override; | 44 ~MyObject() override {} |
| 45 virtual ~MyObject() {} | |
| 46 | 45 |
| 47 private: | 46 private: |
| 48 int value_; | 47 int value_; |
| 49 }; | 48 }; |
| 50 | 49 |
| 51 class MyObjectSubclass : public MyObject { | 50 class MyObjectSubclass : public MyObject { |
| 52 public: | 51 public: |
| 53 static gin::Handle<MyObjectSubclass> Create(v8::Isolate* isolate) { | 52 static gin::Handle<MyObjectSubclass> Create(v8::Isolate* isolate) { |
| 54 return CreateHandle(isolate, new MyObjectSubclass()); | 53 return CreateHandle(isolate, new MyObjectSubclass()); |
| 55 } | 54 } |
| 56 | 55 |
| 57 void SayHello(const std::string& name) { | 56 void SayHello(const std::string& name) { |
| 58 result = std::string("Hello, ") + name; | 57 result = std::string("Hello, ") + name; |
| 59 } | 58 } |
| 60 | 59 |
| 61 std::string result; | 60 std::string result; |
| 62 | 61 |
| 63 private: | 62 private: |
| 64 virtual ObjectTemplateBuilder GetObjectTemplateBuilder( | 63 ObjectTemplateBuilder GetObjectTemplateBuilder( |
| 65 v8::Isolate* isolate) override { | 64 v8::Isolate* isolate) override { |
| 66 return MyObject::GetObjectTemplateBuilder(isolate) | 65 return MyObject::GetObjectTemplateBuilder(isolate) |
| 67 .SetMethod("sayHello", &MyObjectSubclass::SayHello); | 66 .SetMethod("sayHello", &MyObjectSubclass::SayHello); |
| 68 } | 67 } |
| 69 | 68 |
| 70 MyObjectSubclass() { | 69 MyObjectSubclass() { |
| 71 } | 70 } |
| 72 | 71 |
| 73 virtual ~MyObjectSubclass() { | 72 ~MyObjectSubclass() override {} |
| 74 } | |
| 75 }; | 73 }; |
| 76 | 74 |
| 77 class MyCallableObject : public Wrappable<MyCallableObject> { | 75 class MyCallableObject : public Wrappable<MyCallableObject> { |
| 78 public: | 76 public: |
| 79 static WrapperInfo kWrapperInfo; | 77 static WrapperInfo kWrapperInfo; |
| 80 | 78 |
| 81 static gin::Handle<MyCallableObject> Create(v8::Isolate* isolate) { | 79 static gin::Handle<MyCallableObject> Create(v8::Isolate* isolate) { |
| 82 return CreateHandle(isolate, new MyCallableObject()); | 80 return CreateHandle(isolate, new MyCallableObject()); |
| 83 } | 81 } |
| 84 | 82 |
| 85 int result() { return result_; } | 83 int result() { return result_; } |
| 86 | 84 |
| 87 private: | 85 private: |
| 88 virtual ObjectTemplateBuilder GetObjectTemplateBuilder( | 86 ObjectTemplateBuilder GetObjectTemplateBuilder( |
| 89 v8::Isolate* isolate) override { | 87 v8::Isolate* isolate) override { |
| 90 return Wrappable<MyCallableObject>::GetObjectTemplateBuilder(isolate) | 88 return Wrappable<MyCallableObject>::GetObjectTemplateBuilder(isolate) |
| 91 .SetCallAsFunctionHandler(&MyCallableObject::Call); | 89 .SetCallAsFunctionHandler(&MyCallableObject::Call); |
| 92 } | 90 } |
| 93 | 91 |
| 94 MyCallableObject() : result_(0) { | 92 MyCallableObject() : result_(0) { |
| 95 } | 93 } |
| 96 | 94 |
| 97 virtual ~MyCallableObject() { | 95 ~MyCallableObject() override {} |
| 98 } | |
| 99 | 96 |
| 100 void Call(int val, const gin::Arguments& arguments) { | 97 void Call(int val, const gin::Arguments& arguments) { |
| 101 if (arguments.IsConstructCall()) | 98 if (arguments.IsConstructCall()) |
| 102 arguments.ThrowTypeError("Cannot be called as constructor."); | 99 arguments.ThrowTypeError("Cannot be called as constructor."); |
| 103 else | 100 else |
| 104 result_ = val; | 101 result_ = val; |
| 105 } | 102 } |
| 106 | 103 |
| 107 int result_; | 104 int result_; |
| 108 }; | 105 }; |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 v8::Handle<v8::Function> func; | 261 v8::Handle<v8::Function> func; |
| 265 EXPECT_TRUE(ConvertFromV8(isolate, val, &func)); | 262 EXPECT_TRUE(ConvertFromV8(isolate, val, &func)); |
| 266 v8::Handle<v8::Value> argv[] = { | 263 v8::Handle<v8::Value> argv[] = { |
| 267 ConvertToV8(isolate, object.get()) | 264 ConvertToV8(isolate, object.get()) |
| 268 }; | 265 }; |
| 269 func->Call(v8::Undefined(isolate), 1, argv); | 266 func->Call(v8::Undefined(isolate), 1, argv); |
| 270 EXPECT_TRUE(try_catch.HasCaught()); | 267 EXPECT_TRUE(try_catch.HasCaught()); |
| 271 } | 268 } |
| 272 | 269 |
| 273 } // namespace gin | 270 } // namespace gin |
| OLD | NEW |