1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "gin/arguments.h"
7 #include "base/functional/bind.h"
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"
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"
22 using ArgumentsTest = V8Test;
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);
29 v8::Local<v8::Context> creation_context = context_.Get(instance_->isolate());
31 auto check_creation_context = [](v8::Local<v8::Context> expected_context,
32 gin::Arguments* arguments) {
33 EXPECT_EQ(expected_context, arguments->GetHolderCreationContext());
36 // Create an object that will compare GetHolderCreationContext() with
37 // |creation_context|.
38 v8::Local<v8::ObjectTemplate> object_template =
39 ObjectTemplateBuilder(isolate)
41 "checkCreationContext",
42 base::BindRepeating(check_creation_context, creation_context))
45 v8::Local<v8::Object> object =
46 object_template->NewInstance(creation_context).ToLocalChecked();
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))
56 v8::Local<v8::Function> function;
57 ASSERT_TRUE(ConvertFromV8(isolate, script->Run(context).ToLocalChecked(),
59 v8::Local<v8::Value> args[] = {object};
60 function->Call(context, v8::Undefined(isolate), std::size(args), args)
64 // Test calling in the creation context.
65 test_context(creation_context);
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);
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());
81 using V8List = v8::LocalVector<v8::Value>;
85 gin::ConvertToV8(isolate, 1),
86 gin::StringToV8(isolate, "some string"),
87 gin::ConvertToV8(isolate, std::vector<double>({2.0, 3.0})),
91 V8List list2(isolate, {
92 gin::StringToV8(isolate, "some other string"),
93 gin::ConvertToV8(isolate, 42),
97 V8List list3(isolate); // Empty list.
100 auto check_arguments = [](V8List* expected, bool* called,
101 gin::Arguments* arguments) {
103 V8List actual = arguments->GetAll();
104 ASSERT_EQ(expected->size(), actual.size());
105 for (size_t i = 0; i < expected->size(); ++i)
106 EXPECT_EQ(expected->at(i), actual[i]) << i;
109 // Create an object that will compare GetHolderCreationContext() with
110 // |creation_context|.
111 v8::Local<v8::ObjectTemplate> object_template =
112 ObjectTemplateBuilder(isolate)
114 base::BindRepeating(check_arguments, &list1, &called1))
116 base::BindRepeating(check_arguments, &list2, &called2))
118 base::BindRepeating(check_arguments, &list3, &called3))
121 v8::Local<v8::Object> object =
122 object_template->NewInstance(context).ToLocalChecked();
124 auto do_check = [object, context](V8List& args, base::StringPiece key) {
125 v8::Local<v8::Value> val;
127 object->Get(context, gin::StringToSymbol(context->GetIsolate(), key))
129 ASSERT_TRUE(val->IsFunction());
130 val.As<v8::Function>()
131 ->Call(context, object, static_cast<int>(args.size()), args.data())
135 do_check(list1, "check1");
136 EXPECT_TRUE(called1);
137 do_check(list2, "check2");
138 EXPECT_TRUE(called2);
139 do_check(list3, "check3");
140 EXPECT_TRUE(called3);