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