Avi Drissman | 468e51b6 | 2022-09-13 20:47:01 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "gin/converter.h" |
| 6 | |
| 7 | #include <limits.h> |
avi | 90e658dd | 2015-12-21 07:16:19 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 10 | |
Jan Wilken Dörrie | ad587c3 | 2021-03-11 14:09:27 | [diff] [blame] | 11 | #include <string> |
| 12 | |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
Shelley Vohr | 4c67912 | 2019-09-06 00:41:39 | [diff] [blame] | 14 | #include "base/strings/utf_string_conversions.h" |
Jeremy Apthorp | 789ac3b | 2020-04-01 01:06:45 | [diff] [blame] | 15 | #include "gin/function_template.h" |
Jeremy Roman | eb8a2f17 | 2018-01-29 17:33:40 | [diff] [blame] | 16 | #include "gin/handle.h" |
jochen@chromium.org | f04b0e9 | 2013-11-22 14:20:55 | [diff] [blame] | 17 | #include "gin/public/isolate_holder.h" |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 18 | #include "gin/test/v8_test.h" |
Jeremy Roman | eb8a2f17 | 2018-01-29 17:33:40 | [diff] [blame] | 19 | #include "gin/wrappable.h" |
| 20 | #include "testing/gmock/include/gmock/gmock.h" |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 21 | #include "testing/gtest/include/gtest/gtest.h" |
Dan Elphick | 05acd60 | 2021-08-30 15:22:07 | [diff] [blame] | 22 | #include "v8/include/v8-container.h" |
| 23 | #include "v8/include/v8-forward.h" |
| 24 | #include "v8/include/v8-function.h" |
| 25 | #include "v8/include/v8-isolate.h" |
| 26 | #include "v8/include/v8-primitive.h" |
| 27 | #include "v8/include/v8-template.h" |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 28 | |
ssid | 93b23d9 | 2015-05-07 12:11:07 | [diff] [blame] | 29 | namespace gin { |
| 30 | |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 31 | using v8::Array; |
| 32 | using v8::Boolean; |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 33 | using v8::HandleScope; |
| 34 | using v8::Integer; |
tfarina | 777bfee | 2015-05-04 14:12:29 | [diff] [blame] | 35 | using v8::Local; |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 36 | using v8::Null; |
| 37 | using v8::Number; |
| 38 | using v8::Object; |
| 39 | using v8::String; |
| 40 | using v8::Undefined; |
| 41 | using v8::Value; |
| 42 | |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 43 | typedef V8Test ConverterTest; |
| 44 | |
| 45 | TEST_F(ConverterTest, Bool) { |
jochen@chromium.org | 1b93c23 | 2013-11-19 19:25:12 | [diff] [blame] | 46 | HandleScope handle_scope(instance_->isolate()); |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 47 | |
jochen@chromium.org | 1b93c23 | 2013-11-19 19:25:12 | [diff] [blame] | 48 | EXPECT_TRUE(Converter<bool>::ToV8(instance_->isolate(), true)->StrictEquals( |
jochen@chromium.org | 91cd4fe | 2013-11-28 09:31:58 | [diff] [blame] | 49 | Boolean::New(instance_->isolate(), true))); |
jochen@chromium.org | 1b93c23 | 2013-11-19 19:25:12 | [diff] [blame] | 50 | EXPECT_TRUE(Converter<bool>::ToV8(instance_->isolate(), false)->StrictEquals( |
jochen@chromium.org | 91cd4fe | 2013-11-28 09:31:58 | [diff] [blame] | 51 | Boolean::New(instance_->isolate(), false))); |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 52 | |
| 53 | struct { |
tfarina | 777bfee | 2015-05-04 14:12:29 | [diff] [blame] | 54 | Local<Value> input; |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 55 | bool expected; |
| 56 | } test_data[] = { |
Yang Guo | 26b0f777 | 2018-11-27 08:37:20 | [diff] [blame] | 57 | {Boolean::New(instance_->isolate(), false).As<Value>(), false}, |
| 58 | {Boolean::New(instance_->isolate(), true).As<Value>(), true}, |
| 59 | {Number::New(instance_->isolate(), 0).As<Value>(), false}, |
| 60 | {Number::New(instance_->isolate(), 1).As<Value>(), true}, |
| 61 | {Number::New(instance_->isolate(), -1).As<Value>(), true}, |
| 62 | {Number::New(instance_->isolate(), 0.1).As<Value>(), true}, |
| 63 | {String::NewFromUtf8(instance_->isolate(), "", v8::NewStringType::kNormal) |
| 64 | .ToLocalChecked() |
| 65 | .As<Value>(), |
| 66 | false}, |
| 67 | {String::NewFromUtf8(instance_->isolate(), "foo", |
| 68 | v8::NewStringType::kNormal) |
| 69 | .ToLocalChecked() |
| 70 | .As<Value>(), |
| 71 | true}, |
| 72 | {Object::New(instance_->isolate()).As<Value>(), true}, |
| 73 | {Null(instance_->isolate()).As<Value>(), false}, |
| 74 | {Undefined(instance_->isolate()).As<Value>(), false}, |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 75 | }; |
| 76 | |
Daniel Cheng | ef5f13690 | 2022-02-27 01:05:25 | [diff] [blame] | 77 | for (size_t i = 0; i < std::size(test_data); ++i) { |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 78 | bool result = false; |
aa@chromium.org | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 79 | EXPECT_TRUE(Converter<bool>::FromV8(instance_->isolate(), |
| 80 | test_data[i].input, &result)); |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 81 | EXPECT_EQ(test_data[i].expected, result); |
| 82 | |
| 83 | result = true; |
aa@chromium.org | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 84 | EXPECT_TRUE(Converter<bool>::FromV8(instance_->isolate(), |
| 85 | test_data[i].input, &result)); |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 86 | EXPECT_EQ(test_data[i].expected, result); |
| 87 | } |
| 88 | } |
| 89 | |
Shelley Vohr | 4c67912 | 2019-09-06 00:41:39 | [diff] [blame] | 90 | TEST_F(ConverterTest, String16) { |
| 91 | v8::Isolate* isolate = instance_->isolate(); |
| 92 | |
| 93 | HandleScope handle_scope(isolate); |
| 94 | |
Jan Wilken Dörrie | c92a6d724 | 2021-03-23 17:43:48 | [diff] [blame] | 95 | EXPECT_TRUE(Converter<std::u16string>::ToV8(isolate, u"") |
Shelley Vohr | 4c67912 | 2019-09-06 00:41:39 | [diff] [blame] | 96 | ->StrictEquals(StringToV8(isolate, ""))); |
Jan Wilken Dörrie | c92a6d724 | 2021-03-23 17:43:48 | [diff] [blame] | 97 | EXPECT_TRUE(Converter<std::u16string>::ToV8(isolate, u"hello") |
| 98 | ->StrictEquals(StringToV8(isolate, "hello"))); |
Shelley Vohr | 4c67912 | 2019-09-06 00:41:39 | [diff] [blame] | 99 | |
Jan Wilken Dörrie | 739ccc21 | 2021-03-11 18:13:05 | [diff] [blame] | 100 | std::u16string result; |
Shelley Vohr | 4c67912 | 2019-09-06 00:41:39 | [diff] [blame] | 101 | |
| 102 | ASSERT_FALSE( |
Jan Wilken Dörrie | 739ccc21 | 2021-03-11 18:13:05 | [diff] [blame] | 103 | Converter<std::u16string>::FromV8(isolate, v8::False(isolate), &result)); |
Shelley Vohr | 4c67912 | 2019-09-06 00:41:39 | [diff] [blame] | 104 | ASSERT_FALSE( |
Jan Wilken Dörrie | 739ccc21 | 2021-03-11 18:13:05 | [diff] [blame] | 105 | Converter<std::u16string>::FromV8(isolate, v8::True(isolate), &result)); |
| 106 | ASSERT_TRUE(Converter<std::u16string>::FromV8( |
Shelley Vohr | 4c67912 | 2019-09-06 00:41:39 | [diff] [blame] | 107 | isolate, v8::String::Empty(isolate), &result)); |
Jan Wilken Dörrie | 739ccc21 | 2021-03-11 18:13:05 | [diff] [blame] | 108 | EXPECT_EQ(result, std::u16string()); |
| 109 | ASSERT_TRUE(Converter<std::u16string>::FromV8( |
Shelley Vohr | 4c67912 | 2019-09-06 00:41:39 | [diff] [blame] | 110 | isolate, StringToV8(isolate, "hello"), &result)); |
Jan Wilken Dörrie | c92a6d724 | 2021-03-23 17:43:48 | [diff] [blame] | 111 | EXPECT_EQ(result, u"hello"); |
Shelley Vohr | 4c67912 | 2019-09-06 00:41:39 | [diff] [blame] | 112 | } |
| 113 | |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 114 | TEST_F(ConverterTest, Int32) { |
jochen@chromium.org | 1b93c23 | 2013-11-19 19:25:12 | [diff] [blame] | 115 | HandleScope handle_scope(instance_->isolate()); |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 116 | |
| 117 | int test_data_to[] = {-1, 0, 1}; |
Daniel Cheng | ef5f13690 | 2022-02-27 01:05:25 | [diff] [blame] | 118 | for (size_t i = 0; i < std::size(test_data_to); ++i) { |
jochen@chromium.org | add8fb17 | 2014-01-03 15:13:49 | [diff] [blame] | 119 | EXPECT_TRUE(Converter<int32_t>::ToV8(instance_->isolate(), test_data_to[i]) |
| 120 | ->StrictEquals( |
| 121 | Integer::New(instance_->isolate(), test_data_to[i]))); |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | struct { |
deepak.s | faaa1b6 | 2015-04-30 07:30:48 | [diff] [blame] | 125 | v8::Local<v8::Value> input; |
thestig | 259626c | 2015-11-23 20:18:11 | [diff] [blame] | 126 | bool expect_success; |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 127 | int expected_result; |
| 128 | } test_data_from[] = { |
Yang Guo | 26b0f777 | 2018-11-27 08:37:20 | [diff] [blame] | 129 | {Boolean::New(instance_->isolate(), false).As<Value>(), false, 0}, |
| 130 | {Boolean::New(instance_->isolate(), true).As<Value>(), false, 0}, |
| 131 | {Integer::New(instance_->isolate(), -1).As<Value>(), true, -1}, |
| 132 | {Integer::New(instance_->isolate(), 0).As<Value>(), true, 0}, |
| 133 | {Integer::New(instance_->isolate(), 1).As<Value>(), true, 1}, |
| 134 | {Number::New(instance_->isolate(), -1).As<Value>(), true, -1}, |
| 135 | {Number::New(instance_->isolate(), 1.1).As<Value>(), false, 0}, |
| 136 | {String::NewFromUtf8(instance_->isolate(), "42", |
| 137 | v8::NewStringType::kNormal) |
| 138 | .ToLocalChecked() |
| 139 | .As<Value>(), |
| 140 | false, 0}, |
| 141 | {String::NewFromUtf8(instance_->isolate(), "foo", |
| 142 | v8::NewStringType::kNormal) |
| 143 | .ToLocalChecked() |
| 144 | .As<Value>(), |
| 145 | false, 0}, |
| 146 | {Object::New(instance_->isolate()).As<Value>(), false, 0}, |
| 147 | {Array::New(instance_->isolate()).As<Value>(), false, 0}, |
| 148 | {v8::Null(instance_->isolate()).As<Value>(), false, 0}, |
| 149 | {v8::Undefined(instance_->isolate()).As<Value>(), false, 0}, |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 150 | }; |
| 151 | |
Daniel Cheng | ef5f13690 | 2022-02-27 01:05:25 | [diff] [blame] | 152 | for (size_t i = 0; i < std::size(test_data_from); ++i) { |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 153 | int32_t result = std::numeric_limits<int32_t>::min(); |
aa@chromium.org | 7618ebbb | 2013-11-27 03:38:26 | [diff] [blame] | 154 | bool success = Converter<int32_t>::FromV8(instance_->isolate(), |
| 155 | test_data_from[i].input, &result); |
thestig | 259626c | 2015-11-23 20:18:11 | [diff] [blame] | 156 | EXPECT_EQ(test_data_from[i].expect_success, success) << i; |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 157 | if (success) |
| 158 | EXPECT_EQ(test_data_from[i].expected_result, result) << i; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | TEST_F(ConverterTest, Vector) { |
jochen@chromium.org | 1b93c23 | 2013-11-19 19:25:12 | [diff] [blame] | 163 | HandleScope handle_scope(instance_->isolate()); |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 164 | |
| 165 | std::vector<int> expected; |
| 166 | expected.push_back(-1); |
| 167 | expected.push_back(0); |
| 168 | expected.push_back(1); |
| 169 | |
Jeremy Roman | eb8a2f17 | 2018-01-29 17:33:40 | [diff] [blame] | 170 | auto js_array = |
| 171 | Converter<std::vector<int>>::ToV8(instance_->isolate(), expected) |
| 172 | .As<Array>(); |
| 173 | EXPECT_EQ(3u, js_array->Length()); |
Dan Elphick | d7eeb4a | 2019-02-01 17:55:56 | [diff] [blame] | 174 | v8::Local<v8::Context> context = instance_->isolate()->GetCurrentContext(); |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 175 | for (size_t i = 0; i < expected.size(); ++i) { |
Dan Elphick | d7eeb4a | 2019-02-01 17:55:56 | [diff] [blame] | 176 | EXPECT_TRUE( |
| 177 | Integer::New(instance_->isolate(), expected[i]) |
| 178 | ->StrictEquals( |
| 179 | js_array->Get(context, static_cast<int>(i)).ToLocalChecked())); |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 180 | } |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 181 | } |
| 182 | |
Jeremy Roman | eb8a2f17 | 2018-01-29 17:33:40 | [diff] [blame] | 183 | TEST_F(ConverterTest, VectorOfVectors) { |
| 184 | HandleScope handle_scope(instance_->isolate()); |
| 185 | |
| 186 | std::vector<std::vector<int>> vector_of_vectors = { |
| 187 | {1, 2, 3}, {4, 5, 6}, |
| 188 | }; |
| 189 | |
| 190 | v8::Local<v8::Value> v8_value = |
| 191 | ConvertToV8(instance_->isolate(), vector_of_vectors); |
| 192 | std::vector<std::vector<int>> out_value; |
| 193 | ASSERT_TRUE(ConvertFromV8(instance_->isolate(), v8_value, &out_value)); |
| 194 | EXPECT_THAT(out_value, testing::ContainerEq(vector_of_vectors)); |
| 195 | } |
| 196 | |
| 197 | namespace { |
| 198 | |
| 199 | class MyObject : public Wrappable<MyObject> { |
| 200 | public: |
| 201 | static WrapperInfo kWrapperInfo; |
| 202 | |
| 203 | static gin::Handle<MyObject> Create(v8::Isolate* isolate) { |
| 204 | return CreateHandle(isolate, new MyObject()); |
| 205 | } |
| 206 | }; |
| 207 | |
| 208 | WrapperInfo MyObject::kWrapperInfo = {kEmbedderNativeGin}; |
| 209 | |
| 210 | } // namespace |
| 211 | |
| 212 | TEST_F(ConverterTest, VectorOfWrappables) { |
| 213 | v8::Isolate* isolate = instance_->isolate(); |
| 214 | v8::HandleScope handle_scope(isolate); |
| 215 | |
| 216 | Handle<MyObject> obj = MyObject::Create(isolate); |
| 217 | std::vector<MyObject*> vector = {obj.get()}; |
| 218 | v8::MaybeLocal<v8::Value> maybe = ConvertToV8(isolate, vector); |
| 219 | v8::Local<v8::Value> array; |
| 220 | ASSERT_TRUE(maybe.ToLocal(&array)); |
Jeremy Roman | bc83c77 | 2018-03-16 17:16:55 | [diff] [blame] | 221 | v8::Local<v8::Value> array2; |
| 222 | ASSERT_TRUE(TryConvertToV8(isolate, vector, &array2)); |
Jeremy Roman | eb8a2f17 | 2018-01-29 17:33:40 | [diff] [blame] | 223 | |
| 224 | std::vector<MyObject*> out_value; |
| 225 | ASSERT_TRUE(ConvertFromV8(isolate, array, &out_value)); |
| 226 | EXPECT_THAT(out_value, testing::ContainerEq(vector)); |
Jeremy Roman | bc83c77 | 2018-03-16 17:16:55 | [diff] [blame] | 227 | std::vector<MyObject*> out_value2; |
| 228 | ASSERT_TRUE(ConvertFromV8(isolate, array2, &out_value2)); |
| 229 | EXPECT_THAT(out_value2, testing::ContainerEq(vector)); |
Jeremy Roman | eb8a2f17 | 2018-01-29 17:33:40 | [diff] [blame] | 230 | } |
| 231 | |
Jeremy Apthorp | 789ac3b | 2020-04-01 01:06:45 | [diff] [blame] | 232 | namespace { |
| 233 | |
| 234 | class MoveOnlyObject { |
| 235 | public: |
| 236 | MoveOnlyObject() = default; |
| 237 | MoveOnlyObject(const MoveOnlyObject&) = delete; |
| 238 | MoveOnlyObject& operator=(const MoveOnlyObject&) = delete; |
| 239 | |
| 240 | MoveOnlyObject(MoveOnlyObject&&) noexcept = default; |
| 241 | MoveOnlyObject& operator=(MoveOnlyObject&&) noexcept = default; |
| 242 | }; |
| 243 | |
| 244 | } // namespace |
| 245 | |
| 246 | template <> |
| 247 | struct Converter<MoveOnlyObject> { |
| 248 | static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, MoveOnlyObject in) { |
| 249 | return v8::Undefined(isolate); |
| 250 | } |
| 251 | static bool FromV8(v8::Isolate* isolate, |
| 252 | v8::Local<v8::Value> val, |
| 253 | MoveOnlyObject* out) { |
| 254 | *out = MoveOnlyObject(); |
| 255 | return true; |
| 256 | } |
| 257 | }; |
| 258 | |
| 259 | TEST_F(ConverterTest, MoveOnlyParameters) { |
| 260 | v8::Isolate* isolate = instance_->isolate(); |
| 261 | v8::HandleScope handle_scope(isolate); |
| 262 | |
| 263 | auto receives_move_only_obj = [](MoveOnlyObject obj) {}; |
| 264 | auto func_templ = gin::CreateFunctionTemplate( |
| 265 | isolate, base::BindRepeating(receives_move_only_obj)); |
| 266 | |
| 267 | v8::Local<v8::Context> context = instance_->isolate()->GetCurrentContext(); |
| 268 | auto func = func_templ->GetFunction(context).ToLocalChecked(); |
| 269 | v8::Local<v8::Value> argv[] = {v8::Undefined(isolate)}; |
| 270 | func->Call(context, v8::Undefined(isolate), 1, argv).ToLocalChecked(); |
| 271 | } |
| 272 | |
Joel Klinghed | 99d2a159 | 2023-11-07 19:22:32 | [diff] [blame] | 273 | TEST_F(ConverterTest, VectorOfMoveOnly) { |
| 274 | v8::Isolate* isolate = instance_->isolate(); |
| 275 | v8::HandleScope handle_scope(isolate); |
| 276 | |
| 277 | v8::Local<v8::Value> v8_value = v8::Array::New(instance_->isolate(), 1); |
| 278 | std::vector<MoveOnlyObject> out_value; |
| 279 | ASSERT_TRUE(ConvertFromV8(instance_->isolate(), v8_value, &out_value)); |
| 280 | EXPECT_EQ(out_value.size(), 1u); |
| 281 | } |
| 282 | |
abarth@chromium.org | a22998a | 2013-11-10 05:00:50 | [diff] [blame] | 283 | } // namespace gin |