[Java] Fix regression wrapping enum values

Fix regression wrapping enum values which don't fit in a Java signed
int.

Fixes #3070
This commit is contained in:
Olly Betts 2024-11-21 16:39:18 +13:00
parent d9edb22d1c
commit dfe9d8ea2f
3 changed files with 38 additions and 2 deletions

View File

@ -7,6 +7,10 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.4.0 (in progress)
===========================
2024-11-21: olly
[Java] #3070 Fix regression wrapping enum values which don't fit
in a Java signed int.
2024-11-09: wsfulton
#3064 Perform repeated typedef lookups instead of a single typedef
lookup on the type being applied in %apply when looking for a family

View File

@ -578,6 +578,14 @@ enumWithMacro enumWithMacroTest(enumWithMacro e) { return e; }
}
%}
#ifdef SWIGCSHARP
// It seems these never worked for SWIG/C#.
%ignore global_typeunsigned1;
%ignore global_typeunsigned2;
%ignore global_typeunsigned3;
%ignore global_typeunsigned4;
#endif
%inline %{
namespace DifferentSpace {
enum DifferentTypes {
@ -600,7 +608,13 @@ enum {
global_typechar = 'C',
global_typedefaultint,
global_typecharcompound='A'+1,
global_typecharcompound2='B' << 2
global_typecharcompound2='B' << 2,
// Regression tests for #3070 affecting SWIG/Java, introduced in 4.3.0:
global_typeunsigned1 = 0x80000000,
global_typeunsigned2 = 0xffffffff,
// These should also work in > 4.3.0.
global_typeunsigned3 = 2147483648,
global_typeunsigned4 = 4294967295
};
int globalDifferentTypesTest(int n) { return n; }
}

View File

@ -1424,7 +1424,25 @@ public:
}
} else {
String *numval = Getattr(n, "enumnumval");
if (numval) Setattr(n, "enumvalue", numval);
if (numval) {
const char *p = Char(numval);
if (isdigit(p[0])) {
char *e;
unsigned long long value = strtoull(p, &e, 0);
if (errno != ERANGE && *e == '\0' && value >= 0x80000000) {
// Use hex for larger unsigned integer constants in Java code since
// Java allows implicit conversion to a signed integer value.
String *hexval = NewStringf("0x%llx", value);
Setattr(n, "enumvalue", hexval);
Delete(hexval);
} else {
Setattr(n, "enumvalue", numval);
}
} else {
// Emit negative values as-is.
Setattr(n, "enumvalue", numval);
}
}
}
{