Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slice2java: fix constructor parameter count check #1540

Merged
merged 7 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions cpp/src/slice2java/Gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,28 @@ string ofFactory(const TypePtr& type)
return "java.util.Optional.ofNullable";
}

// Returns the "length" of the a constructor parameter list from the given data member list.
externl marked this conversation as resolved.
Show resolved Hide resolved
// All types are counted as 1, except for long and double which are counted as 2.
externl marked this conversation as resolved.
Show resolved Hide resolved
// See https://docs.oracle.com/javase/specs/jvms/se20/html/jvms-4.html#jvms-4.3.3
int
constructorParameterLength(const DataMemberList& members)
{
int length = 0;
externl marked this conversation as resolved.
Show resolved Hide resolved
for(DataMemberList::const_iterator i = members.begin(); i != members.end(); ++i)
externl marked this conversation as resolved.
Show resolved Hide resolved
{
BuiltinPtr builtin = BuiltinPtr::dynamicCast((*i)->type());
if(builtin && (builtin->kind() == Builtin::KindLong || builtin->kind() == Builtin::KindDouble))
{
length += 2;
}
else
{
length++;
}
}
return length;
}

}

Slice::JavaVisitor::JavaVisitor(const string& dir) :
Expand Down Expand Up @@ -2554,7 +2576,7 @@ Slice::Gen::TypesVisitor::visitClassDefStart(const ClassDefPtr& p)
//
// A method cannot have more than 255 parameters (including the implicit "this" argument).
//
if(allDataMembers.size() < 255)
if(constructorParameterLength(allDataMembers) < 255)
externl marked this conversation as resolved.
Show resolved Hide resolved
{
DataMemberList baseDataMembers;
if(baseClass)
Expand Down Expand Up @@ -2947,7 +2969,7 @@ Slice::Gen::TypesVisitor::visitExceptionStart(const ExceptionPtr& p)
//
// A method cannot have more than 255 parameters (including the implicit "this" argument).
//
if(allDataMembers.size() < 255)
if(constructorParameterLength(allDataMembers) < 255)
externl marked this conversation as resolved.
Show resolved Hide resolved
{
if(hasRequiredMembers && hasOptionalMembers)
{
Expand Down Expand Up @@ -3384,7 +3406,7 @@ Slice::Gen::TypesVisitor::visitStructEnd(const StructPtr& p)
//
// A method cannot have more than 255 parameters (including the implicit "this" argument).
//
if(members.size() < 255)
if(constructorParameterLength(members) < 255)
{
vector<string> paramDecl;
vector<string> paramNames;
Expand Down
28 changes: 25 additions & 3 deletions cpp/src/slice2java/GenCompat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,28 @@ writeParamList(Output& out, vector<string> params, bool end = true, bool newLine
}
}

// Returns the "length" of the a constructor parameter list from the given data member list.
externl marked this conversation as resolved.
Show resolved Hide resolved
// All types are counted as 1, except for long and double which are counted as 2.
externl marked this conversation as resolved.
Show resolved Hide resolved
// See https://docs.oracle.com/javase/specs/jvms/se20/html/jvms-4.html#jvms-4.3.3
int
constructorParameterLength(const DataMemberList& members)
externl marked this conversation as resolved.
Show resolved Hide resolved
{
int length = 0;
for(DataMemberList::const_iterator i = members.begin(); i != members.end(); ++i)
{
BuiltinPtr builtin = BuiltinPtr::dynamicCast((*i)->type());
if(builtin && (builtin->kind() == Builtin::KindLong || builtin->kind() == Builtin::KindDouble))
{
length += 2;
}
else
{
length++;
}
}
return length;
}

}

Slice::JavaCompatVisitor::JavaCompatVisitor(const string& dir) :
Expand Down Expand Up @@ -2947,7 +2969,7 @@ Slice::GenCompat::TypesVisitor::visitClassDefStart(const ClassDefPtr& p)
//
// A method cannot have more than 255 parameters (including the implicit "this" argument).
//
if(allDataMembers.size() < 255)
if(constructorParameterLength(allDataMembers) < 255)
{
DataMemberList baseDataMembers;
if(baseClass)
Expand Down Expand Up @@ -3257,7 +3279,7 @@ Slice::GenCompat::TypesVisitor::visitExceptionStart(const ExceptionPtr& p)
//
// A method cannot have more than 255 parameters (including the implicit "this" argument).
//
if(allDataMembers.size() < 255)
if(constructorParameterLength(allDataMembers) < 255)
{
if(hasRequiredMembers && hasOptionalMembers)
{
Expand Down Expand Up @@ -3672,7 +3694,7 @@ Slice::GenCompat::TypesVisitor::visitStructEnd(const StructPtr& p)
//
// A method cannot have more than 255 parameters (including the implicit "this" argument).
//
if(members.size() < 255)
if(constructorParameterLength(members) < 255)
{
vector<string> paramDecl;
vector<string> paramNames;
Expand Down