Avi Drissman | 468e51b6 | 2022-09-13 20:47:01 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
rdevlin.cronin | d982fdf | 2017-03-23 22:17:43 | [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/arguments.h" |
| 6 | |
Md Hasibul Hasan | fcbd62e3 | 2024-04-10 10:38:39 | [diff] [blame] | 7 | #include <string_view> |
| 8 | |
Avi Drissman | 93a273dd | 2023-01-11 00:38:27 | [diff] [blame] | 9 | #include "base/functional/bind.h" |
rdevlin.cronin | d982fdf | 2017-03-23 22:17:43 | [diff] [blame] | 10 | #include "gin/converter.h" |
| 11 | #include "gin/object_template_builder.h" |
| 12 | #include "gin/public/isolate_holder.h" |
| 13 | #include "gin/test/v8_test.h" |
Dan Elphick | 05acd60 | 2021-08-30 15:22:07 | [diff] [blame] | 14 | #include "v8/include/v8-context.h" |
| 15 | #include "v8/include/v8-forward.h" |
| 16 | #include "v8/include/v8-function.h" |
| 17 | #include "v8/include/v8-object.h" |
| 18 | #include "v8/include/v8-primitive.h" |
| 19 | #include "v8/include/v8-script.h" |
| 20 | #include "v8/include/v8-template.h" |
rdevlin.cronin | d982fdf | 2017-03-23 22:17:43 | [diff] [blame] | 21 | |
| 22 | namespace gin { |
| 23 | |
| 24 | using ArgumentsTest = V8Test; |
| 25 | |
| 26 | // Test that Arguments::GetHolderCreationContext returns the proper context. |
| 27 | TEST_F(ArgumentsTest, TestArgumentsHolderCreationContext) { |
| 28 | v8::Isolate* isolate = instance_->isolate(); |
| 29 | v8::HandleScope handle_scope(isolate); |
| 30 | |
| 31 | v8::Local<v8::Context> creation_context = context_.Get(instance_->isolate()); |
| 32 | |
| 33 | auto check_creation_context = [](v8::Local<v8::Context> expected_context, |
| 34 | gin::Arguments* arguments) { |
| 35 | EXPECT_EQ(expected_context, arguments->GetHolderCreationContext()); |
| 36 | }; |
| 37 | |
| 38 | // Create an object that will compare GetHolderCreationContext() with |
| 39 | // |creation_context|. |
| 40 | v8::Local<v8::ObjectTemplate> object_template = |
| 41 | ObjectTemplateBuilder(isolate) |
tzik | 6934a31 | 2018-03-08 01:03:16 | [diff] [blame] | 42 | .SetMethod( |
| 43 | "checkCreationContext", |
| 44 | base::BindRepeating(check_creation_context, creation_context)) |
rdevlin.cronin | d982fdf | 2017-03-23 22:17:43 | [diff] [blame] | 45 | .Build(); |
| 46 | |
| 47 | v8::Local<v8::Object> object = |
| 48 | object_template->NewInstance(creation_context).ToLocalChecked(); |
| 49 | |
| 50 | // Call checkCreationContext() on the generated object using the passed-in |
| 51 | // context as the current context. |
| 52 | auto test_context = [object, isolate](v8::Local<v8::Context> context) { |
| 53 | v8::Context::Scope context_scope(context); |
| 54 | const char kCallFunction[] = "(function(o) { o.checkCreationContext(); })"; |
| 55 | v8::Local<v8::Script> script = |
| 56 | v8::Script::Compile(context, StringToV8(isolate, kCallFunction)) |
| 57 | .ToLocalChecked(); |
| 58 | v8::Local<v8::Function> function; |
Adam Klein | 8a35b5b | 2018-01-17 00:51:00 | [diff] [blame] | 59 | ASSERT_TRUE(ConvertFromV8(isolate, script->Run(context).ToLocalChecked(), |
| 60 | &function)); |
rdevlin.cronin | d982fdf | 2017-03-23 22:17:43 | [diff] [blame] | 61 | v8::Local<v8::Value> args[] = {object}; |
Daniel Cheng | ef5f13690 | 2022-02-27 01:05:25 | [diff] [blame] | 62 | function->Call(context, v8::Undefined(isolate), std::size(args), args) |
Ross McIlroy | d298cced | 2018-11-23 16:14:13 | [diff] [blame] | 63 | .ToLocalChecked(); |
rdevlin.cronin | d982fdf | 2017-03-23 22:17:43 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | // Test calling in the creation context. |
| 67 | test_context(creation_context); |
| 68 | |
| 69 | { |
| 70 | // Create a second context, and test calling in that. The creation context |
| 71 | // should be the same (even though the current context has changed). |
| 72 | v8::Local<v8::Context> second_context = |
| 73 | v8::Context::New(isolate, nullptr, v8::Local<v8::ObjectTemplate>()); |
| 74 | test_context(second_context); |
| 75 | } |
| 76 | } |
| 77 | |
rdevlin.cronin | cd675450 | 2017-04-19 16:14:14 | [diff] [blame] | 78 | TEST_F(ArgumentsTest, TestGetAll) { |
| 79 | v8::Isolate* isolate = instance_->isolate(); |
| 80 | v8::HandleScope handle_scope(isolate); |
| 81 | v8::Local<v8::Context> context = context_.Get(instance_->isolate()); |
| 82 | |
Nikolaos Papaspyrou | 5ce2a2f | 2023-10-19 09:09:00 | [diff] [blame] | 83 | using V8List = v8::LocalVector<v8::Value>; |
rdevlin.cronin | cd675450 | 2017-04-19 16:14:14 | [diff] [blame] | 84 | |
Nikolaos Papaspyrou | 5ce2a2f | 2023-10-19 09:09:00 | [diff] [blame] | 85 | V8List list1(isolate, |
| 86 | { |
| 87 | gin::ConvertToV8(isolate, 1), |
| 88 | gin::StringToV8(isolate, "some string"), |
| 89 | gin::ConvertToV8(isolate, std::vector<double>({2.0, 3.0})), |
| 90 | }); |
rdevlin.cronin | cd675450 | 2017-04-19 16:14:14 | [diff] [blame] | 91 | bool called1 = false; |
| 92 | |
Nikolaos Papaspyrou | 5ce2a2f | 2023-10-19 09:09:00 | [diff] [blame] | 93 | V8List list2(isolate, { |
| 94 | gin::StringToV8(isolate, "some other string"), |
| 95 | gin::ConvertToV8(isolate, 42), |
| 96 | }); |
rdevlin.cronin | cd675450 | 2017-04-19 16:14:14 | [diff] [blame] | 97 | bool called2 = false; |
| 98 | |
Nikolaos Papaspyrou | 5ce2a2f | 2023-10-19 09:09:00 | [diff] [blame] | 99 | V8List list3(isolate); // Empty list. |
rdevlin.cronin | cd675450 | 2017-04-19 16:14:14 | [diff] [blame] | 100 | bool called3 = false; |
| 101 | |
| 102 | auto check_arguments = [](V8List* expected, bool* called, |
| 103 | gin::Arguments* arguments) { |
| 104 | *called = true; |
| 105 | V8List actual = arguments->GetAll(); |
| 106 | ASSERT_EQ(expected->size(), actual.size()); |
| 107 | for (size_t i = 0; i < expected->size(); ++i) |
| 108 | EXPECT_EQ(expected->at(i), actual[i]) << i; |
| 109 | }; |
| 110 | |
| 111 | // Create an object that will compare GetHolderCreationContext() with |
| 112 | // |creation_context|. |
| 113 | v8::Local<v8::ObjectTemplate> object_template = |
| 114 | ObjectTemplateBuilder(isolate) |
tzik | 6934a31 | 2018-03-08 01:03:16 | [diff] [blame] | 115 | .SetMethod("check1", |
| 116 | base::BindRepeating(check_arguments, &list1, &called1)) |
| 117 | .SetMethod("check2", |
| 118 | base::BindRepeating(check_arguments, &list2, &called2)) |
| 119 | .SetMethod("check3", |
| 120 | base::BindRepeating(check_arguments, &list3, &called3)) |
rdevlin.cronin | cd675450 | 2017-04-19 16:14:14 | [diff] [blame] | 121 | .Build(); |
| 122 | |
| 123 | v8::Local<v8::Object> object = |
| 124 | object_template->NewInstance(context).ToLocalChecked(); |
| 125 | |
Md Hasibul Hasan | fcbd62e3 | 2024-04-10 10:38:39 | [diff] [blame] | 126 | auto do_check = [object, context](V8List& args, std::string_view key) { |
rdevlin.cronin | cd675450 | 2017-04-19 16:14:14 | [diff] [blame] | 127 | v8::Local<v8::Value> val; |
| 128 | ASSERT_TRUE( |
| 129 | object->Get(context, gin::StringToSymbol(context->GetIsolate(), key)) |
| 130 | .ToLocal(&val)); |
| 131 | ASSERT_TRUE(val->IsFunction()); |
| 132 | val.As<v8::Function>() |
| 133 | ->Call(context, object, static_cast<int>(args.size()), args.data()) |
| 134 | .ToLocalChecked(); |
| 135 | }; |
| 136 | |
| 137 | do_check(list1, "check1"); |
| 138 | EXPECT_TRUE(called1); |
| 139 | do_check(list2, "check2"); |
| 140 | EXPECT_TRUE(called2); |
| 141 | do_check(list3, "check3"); |
| 142 | EXPECT_TRUE(called3); |
| 143 | } |
| 144 | |
rdevlin.cronin | d982fdf | 2017-03-23 22:17:43 | [diff] [blame] | 145 | } // namespace gin |