Skip to content

Commit 3d2f4db

Browse files
committed
Add range checks to emulate same C# behaviour on IL2CPP
1 parent 03e5b0e commit 3d2f4db

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

Packages/net.yutopp.vjson/Runtime/TypeHelper.g.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ private static bool ConvertFromDoubleToFloat(double i, out object o) {
8585
private static bool ConvertFromLongToByte(long i, out object o) {
8686
try
8787
{
88+
if ( i < 0 )
89+
{
90+
throw new OverflowException();
91+
}
8892
o = checked((byte)i);
8993
return true;
9094
}
@@ -111,6 +115,10 @@ private static bool ConvertFromLongToSbyte(long i, out object o) {
111115
private static bool ConvertFromLongToChar(long i, out object o) {
112116
try
113117
{
118+
if ( i < 0 )
119+
{
120+
throw new OverflowException();
121+
}
114122
o = checked((char)i);
115123
return true;
116124
}
@@ -176,6 +184,10 @@ private static bool ConvertFromLongToInt(long i, out object o) {
176184
private static bool ConvertFromLongToUint(long i, out object o) {
177185
try
178186
{
187+
if ( i < 0 )
188+
{
189+
throw new OverflowException();
190+
}
179191
o = checked((uint)i);
180192
return true;
181193
}
@@ -189,6 +201,10 @@ private static bool ConvertFromLongToUint(long i, out object o) {
189201
private static bool ConvertFromLongToUlong(long i, out object o) {
190202
try
191203
{
204+
if ( i < 0 )
205+
{
206+
throw new OverflowException();
207+
}
192208
o = checked((ulong)i);
193209
return true;
194210
}
@@ -215,6 +231,10 @@ private static bool ConvertFromLongToShort(long i, out object o) {
215231
private static bool ConvertFromLongToUshort(long i, out object o) {
216232
try
217233
{
234+
if ( i < 0 )
235+
{
236+
throw new OverflowException();
237+
}
218238
o = checked((ushort)i);
219239
return true;
220240
}

pre-process/generator.py

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
import textwrap
33
from string import Template
44

5-
def is_convertible(from_kind, to_kind):
5+
def is_convertible(from_kind_tup, to_kind_tup):
6+
(from_kind, _) = from_kind_tup
7+
(to_kind, _) = to_kind_tup
8+
69
if from_kind == to_kind:
710
return True
811

@@ -11,6 +14,15 @@ def is_convertible(from_kind, to_kind):
1114

1215
return False
1316

17+
def is_signed_to_unsigned(from_kind_tup, to_kind_tup):
18+
(_, from_signed) = from_kind_tup
19+
(_, to_signed) = to_kind_tup
20+
21+
if from_signed == "s" and to_signed == "u":
22+
return True
23+
24+
return False
25+
1426
def mk_indent(num):
1527
return ' ' * (4 * num)
1628

@@ -35,20 +47,20 @@ def main(args):
3547
"string",
3648
]
3749
kinds = {
38-
"bool": "bool",
39-
"byte": "integer",
40-
"sbyte": "integer",
41-
"char": "integer",
42-
"decimal": "number",
43-
"double": "number",
44-
"float": "number",
45-
"int": "integer",
46-
"uint": "integer",
47-
"long": "integer",
48-
"ulong": "integer",
49-
"short": "integer",
50-
"ushort": "integer",
51-
"string": "string",
50+
"bool": ("bool", "_"),
51+
"byte": ("integer", "u"),
52+
"sbyte": ("integer", "s"),
53+
"char": ("integer", "u"),
54+
"decimal": ("number", "_"),
55+
"double": ("number", "_"),
56+
"float": ("number", "_"),
57+
"int": ("integer", "s"),
58+
"uint": ("integer", "u"),
59+
"long": ("integer", "s"),
60+
"ulong": ("integer", "u"),
61+
"short": ("integer", "s"),
62+
"ushort": ("integer", "u"),
63+
"string": ("string", "_"),
5264
}
5365

5466
from_types = ["bool", "long", "double", "string"]
@@ -108,10 +120,21 @@ def main(args):
108120
if from_ty == to_ty:
109121
continue
110122

123+
need_signed_check = is_signed_to_unsigned(kinds[from_ty], kinds[to_ty])
124+
111125
output_funcs += textwrap.indent("""
112126
private static bool ConvertFrom{2}To{3}({0} i, out object o) {{
113127
try
114-
{{
128+
{{""".format(from_ty, to_ty, from_ty.capitalize(), to_ty.capitalize()), mk_indent(2))
129+
130+
if need_signed_check:
131+
output_funcs += textwrap.indent("""
132+
if ( i < 0 )
133+
{{
134+
throw new OverflowException();
135+
}}""".format(), mk_indent(2))
136+
137+
output_funcs += textwrap.indent("""
115138
o = checked(({1})i);
116139
return true;
117140
}}

0 commit comments

Comments
 (0)