blob: 69bd940c7650b4ca1977bc1e8d1ddab239229047 [file] [log] [blame]
Avi Drissman468e51b62022-09-13 20:47:011// Copyright 2017 The Chromium Authors
rdevlin.cronind982fdf2017-03-23 22:17:432// 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 Drissman93a273dd2023-01-11 00:38:277#include "base/functional/bind.h"
rdevlin.cronind982fdf2017-03-23 22:17:438#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 Elphick05acd602021-08-30 15:22:0712#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.cronind982fdf2017-03-23 22:17:4319
20namespace gin {
21
22using ArgumentsTest = V8Test;
23
24// Test that Arguments::GetHolderCreationContext returns the proper context.
25TEST_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)
tzik6934a312018-03-08 01:03:1640 .SetMethod(
41 "checkCreationContext",
42 base::BindRepeating(check_creation_context, creation_context))
rdevlin.cronind982fdf2017-03-23 22:17:4343 .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 Klein8a35b5b2018-01-17 00:51:0057 ASSERT_TRUE(ConvertFromV8(isolate, script->Run(context).ToLocalChecked(),
58 &function));
rdevlin.cronind982fdf2017-03-23 22:17:4359 v8::Local<v8::Value> args[] = {object};
Daniel Chengef5f136902022-02-27 01:05:2560 function->Call(context, v8::Undefined(isolate), std::size(args), args)
Ross McIlroyd298cced2018-11-23 16:14:1361 .ToLocalChecked();
rdevlin.cronind982fdf2017-03-23 22:17:4362 };
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.cronincd6754502017-04-19 16:14:1476TEST_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 Romaneb8a2f172018-01-29 17:33:4085 gin::ConvertToV8(isolate, std::vector<double>({2.0, 3.0})),
rdevlin.cronincd6754502017-04-19 16:14:1486 };
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)
tzik6934a312018-03-08 01:03:16111 .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.cronincd6754502017-04-19 16:14:14117 .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.cronind982fdf2017-03-23 22:17:43141} // namespace gin