blob: 0ff6d010160a510e54802c1c5d9872bc2000c132 [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
Md Hasibul Hasanfcbd62e32024-04-10 10:38:397#include <string_view>
8
Avi Drissman93a273dd2023-01-11 00:38:279#include "base/functional/bind.h"
rdevlin.cronind982fdf2017-03-23 22:17:4310#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 Elphick05acd602021-08-30 15:22:0714#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.cronind982fdf2017-03-23 22:17:4321
22namespace gin {
23
24using ArgumentsTest = V8Test;
25
26// Test that Arguments::GetHolderCreationContext returns the proper context.
27TEST_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)
tzik6934a312018-03-08 01:03:1642 .SetMethod(
43 "checkCreationContext",
44 base::BindRepeating(check_creation_context, creation_context))
rdevlin.cronind982fdf2017-03-23 22:17:4345 .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 Klein8a35b5b2018-01-17 00:51:0059 ASSERT_TRUE(ConvertFromV8(isolate, script->Run(context).ToLocalChecked(),
60 &function));
rdevlin.cronind982fdf2017-03-23 22:17:4361 v8::Local<v8::Value> args[] = {object};
Daniel Chengef5f136902022-02-27 01:05:2562 function->Call(context, v8::Undefined(isolate), std::size(args), args)
Ross McIlroyd298cced2018-11-23 16:14:1363 .ToLocalChecked();
rdevlin.cronind982fdf2017-03-23 22:17:4364 };
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.cronincd6754502017-04-19 16:14:1478TEST_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 Papaspyrou5ce2a2f2023-10-19 09:09:0083 using V8List = v8::LocalVector<v8::Value>;
rdevlin.cronincd6754502017-04-19 16:14:1484
Nikolaos Papaspyrou5ce2a2f2023-10-19 09:09:0085 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.cronincd6754502017-04-19 16:14:1491 bool called1 = false;
92
Nikolaos Papaspyrou5ce2a2f2023-10-19 09:09:0093 V8List list2(isolate, {
94 gin::StringToV8(isolate, "some other string"),
95 gin::ConvertToV8(isolate, 42),
96 });
rdevlin.cronincd6754502017-04-19 16:14:1497 bool called2 = false;
98
Nikolaos Papaspyrou5ce2a2f2023-10-19 09:09:0099 V8List list3(isolate); // Empty list.
rdevlin.cronincd6754502017-04-19 16:14:14100 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)
tzik6934a312018-03-08 01:03:16115 .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.cronincd6754502017-04-19 16:14:14121 .Build();
122
123 v8::Local<v8::Object> object =
124 object_template->NewInstance(context).ToLocalChecked();
125
Md Hasibul Hasanfcbd62e32024-04-10 10:38:39126 auto do_check = [object, context](V8List& args, std::string_view key) {
rdevlin.cronincd6754502017-04-19 16:14:14127 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.cronind982fdf2017-03-23 22:17:43145} // namespace gin