[vm] Remove irregular type arguments parameter from factories

In the VM, factory constructors always had an extra "type arguments"
parameter, even if class is not generic. Factory constructor bodies
were using class type parameters instead of function type parameters.

This results in extra code when calling non-generic factories
which is slightly inefficient in terms of code size and performance.
Also, it creates an additional complexity throughout the system as
factories should be special cased in many places.

This change removes artificial "type arguments" parameter, treating
factory constructors basically as static methods. This matches
kernel AST representation.

TEST=ci

Change-Id: I957583cb2ce9a3c408699880a04036e06b01dd31
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/501762
Reviewed-by: Slava Egorov <vegorov@google.com>
Commit-Queue: Alexander Markov <alexmarkov@google.com>
Reviewed-by: Ryan Macnak <rmacnak@google.com>
This commit is contained in:
Alexander Markov
2026-05-12 06:11:15 -07:00
committed by dart-scoped@luci-project-accounts.iam.gserviceaccount.com
parent 66b54232ee
commit 6195ea86bc
39 changed files with 310 additions and 539 deletions
+36 -23
View File
@@ -3205,11 +3205,14 @@ DART_EXPORT Dart_Handle Dart_NewMap(Dart_Handle keys_type,
Function& factory_method = Function::ZoneHandle(Z);
factory_method = map_class.LookupFactoryAllowPrivate(
Library::PrivateCoreLibName(Symbols::MapKeyValuesFactory()));
const Array& arguments_descriptor =
Array::Handle(Z, ArgumentsDescriptor::NewBoxed(2, 2));
const Array& args = Array::Handle(Z, Array::New(3));
args.SetAt(0, type_arguments);
args.SetAt(1, keys_obj);
args.SetAt(2, values_obj);
return Api::NewHandle(T, DartEntry::InvokeFunction(factory_method, args));
return Api::NewHandle(
T, DartEntry::InvokeFunction(factory_method, args, arguments_descriptor));
}
DART_EXPORT Dart_Handle Dart_NewListOfTypeFilled(Dart_Handle element_type,
@@ -3427,7 +3430,7 @@ DART_EXPORT Dart_Handle Dart_ListSetAt(Dart_Handle list,
static ObjectPtr ResolveConstructor(const char* current_func,
const Class& cls,
const String& class_name,
const String& dotted_name,
const String& constr_name,
int num_args);
static ObjectPtr ThrowArgumentError(const char* exception_message) {
@@ -4079,13 +4082,12 @@ DART_EXPORT Dart_Handle Dart_NewByteBuffer(Dart_Handle typed_data) {
ASSERT(result.IsFunction());
const Function& factory = Function::Cast(result);
ASSERT(!factory.IsGenerativeConstructor());
ASSERT(factory.NumParameters() == 1);
// Create the argument list.
const Array& args = Array::Handle(Z, Array::New(2));
// Factories get type arguments.
args.SetAt(0, Object::null_type_arguments());
const Array& args = Array::Handle(Z, Array::New(1));
const Object& obj = Object::Handle(Z, Api::UnwrapHandle(typed_data));
args.SetAt(1, obj);
args.SetAt(0, obj);
// Invoke the factory constructor and return the new object.
result = DartEntry::InvokeFunction(factory, args);
@@ -4297,10 +4299,11 @@ static ObjectPtr ResolveConstructor(const char* current_func,
return ApiError::New(message);
}
}
const int kTypeArgsLen = 0;
const int extra_args = 1;
const int type_args_len =
constructor.IsGenerativeConstructor() ? 0 : cls.NumTypeParameters();
const int extra_args = constructor.IsGenerativeConstructor() ? 1 : 0;
String& error_message = String::Handle();
if (!constructor.AreValidArgumentCounts(kTypeArgsLen, num_args + extra_args,
if (!constructor.AreValidArgumentCounts(type_args_len, num_args + extra_args,
0, &error_message)) {
const String& message = String::Handle(String::NewFormatted(
"%s: wrong argument count for "
@@ -4344,9 +4347,6 @@ DART_EXPORT Dart_Handle Dart_New(Dart_Handle type,
Class& cls = Class::Handle(Z, type_obj.type_class());
CHECK_ERROR_HANDLE(cls.EnsureIsAllocateFinalized(T));
TypeArguments& type_arguments =
TypeArguments::Handle(Z, type_obj.GetInstanceTypeArguments(T));
const String& base_constructor_name = String::Handle(Z, cls.Name());
// And get the name of the constructor to invoke.
@@ -4388,22 +4388,33 @@ DART_EXPORT Dart_Handle Dart_New(Dart_Handle type,
}
// Create the argument list.
const intptr_t type_args_len =
constructor.IsGenerativeConstructor() ? 0 : cls.NumTypeParameters();
const intptr_t num_implicit_positional_args =
constructor.IsGenerativeConstructor() ? 1 : 0;
intptr_t arg_index = 0;
int extra_args = 1;
const Array& args =
Array::Handle(Z, Array::New(number_of_arguments + extra_args));
Array& args = Array::Handle(Z);
TypeArguments& instantiator_type_arguments = TypeArguments::Handle(Z);
TypeArguments& function_type_arguments = TypeArguments::Handle(Z);
if (constructor.IsGenerativeConstructor()) {
// Constructors get the uninitialized object.
if (!type_arguments.IsNull()) {
args = Array::New(number_of_arguments + num_implicit_positional_args);
instantiator_type_arguments = type_obj.GetInstanceTypeArguments(T);
if (!instantiator_type_arguments.IsNull()) {
// The type arguments will be null if the class has no type parameters, in
// which case the following call would fail because there is no slot
// reserved in the object for the type vector.
new_object.SetTypeArguments(type_arguments);
new_object.SetTypeArguments(instantiator_type_arguments);
}
args.SetAt(arg_index++, new_object);
} else {
// Factories get type arguments.
args.SetAt(arg_index++, type_arguments);
args = Array::New(number_of_arguments + ((type_args_len > 0) ? 1 : 0));
if (type_args_len > 0) {
function_type_arguments = type_obj.arguments();
ASSERT(function_type_arguments.IsNull() ||
function_type_arguments.Length() == type_args_len);
args.SetAt(arg_index++, function_type_arguments);
}
}
Object& argument = Object::Handle(Z);
for (int i = 0; i < number_of_arguments; i++) {
@@ -4420,19 +4431,21 @@ DART_EXPORT Dart_Handle Dart_New(Dart_Handle type,
args.SetAt(arg_index++, argument);
}
const int kTypeArgsLen = 0;
Array& args_descriptor_array = Array::Handle(
Z, ArgumentsDescriptor::NewBoxed(kTypeArgsLen, args.Length()));
Z,
ArgumentsDescriptor::NewBoxed(
type_args_len, number_of_arguments + num_implicit_positional_args));
ArgumentsDescriptor args_descriptor(args_descriptor_array);
ObjectPtr type_error = constructor.DoArgumentTypesMatch(
args, args_descriptor, type_arguments, Object::empty_type_arguments());
args, args_descriptor, instantiator_type_arguments,
function_type_arguments);
if (type_error != Error::null()) {
return Api::NewHandle(T, type_error);
}
// Invoke the constructor and return the new object.
result = DartEntry::InvokeFunction(constructor, args);
result = DartEntry::InvokeFunction(constructor, args, args_descriptor_array);
if (result.IsError()) {
return Api::NewHandle(T, result.ptr());
}