Skip to content

Commit

Permalink
Add range checks to emulate same C# behaviour on IL2CPP
Browse files Browse the repository at this point in the history
  • Loading branch information
yutopp committed Oct 2, 2021
1 parent 03e5b0e commit 3d2f4db
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
20 changes: 20 additions & 0 deletions Packages/net.yutopp.vjson/Runtime/TypeHelper.g.cs
Expand Up @@ -85,6 +85,10 @@ static partial class TypeHelper
private static bool ConvertFromLongToByte(long i, out object o) {
try
{
if ( i < 0 )
{
throw new OverflowException();
}
o = checked((byte)i);
return true;
}
Expand All @@ -111,6 +115,10 @@ static partial class TypeHelper
private static bool ConvertFromLongToChar(long i, out object o) {
try
{
if ( i < 0 )
{
throw new OverflowException();
}
o = checked((char)i);
return true;
}
Expand Down Expand Up @@ -176,6 +184,10 @@ static partial class TypeHelper
private static bool ConvertFromLongToUint(long i, out object o) {
try
{
if ( i < 0 )
{
throw new OverflowException();
}
o = checked((uint)i);
return true;
}
Expand All @@ -189,6 +201,10 @@ static partial class TypeHelper
private static bool ConvertFromLongToUlong(long i, out object o) {
try
{
if ( i < 0 )
{
throw new OverflowException();
}
o = checked((ulong)i);
return true;
}
Expand All @@ -215,6 +231,10 @@ static partial class TypeHelper
private static bool ConvertFromLongToUshort(long i, out object o) {
try
{
if ( i < 0 )
{
throw new OverflowException();
}
o = checked((ushort)i);
return true;
}
Expand Down
55 changes: 39 additions & 16 deletions pre-process/generator.py
Expand Up @@ -2,7 +2,10 @@
import textwrap
from string import Template

def is_convertible(from_kind, to_kind):
def is_convertible(from_kind_tup, to_kind_tup):
(from_kind, _) = from_kind_tup
(to_kind, _) = to_kind_tup

if from_kind == to_kind:
return True

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

return False

def is_signed_to_unsigned(from_kind_tup, to_kind_tup):
(_, from_signed) = from_kind_tup
(_, to_signed) = to_kind_tup

if from_signed == "s" and to_signed == "u":
return True

return False

def mk_indent(num):
return ' ' * (4 * num)

Expand All @@ -35,20 +47,20 @@ def main(args):
"string",
]
kinds = {
"bool": "bool",
"byte": "integer",
"sbyte": "integer",
"char": "integer",
"decimal": "number",
"double": "number",
"float": "number",
"int": "integer",
"uint": "integer",
"long": "integer",
"ulong": "integer",
"short": "integer",
"ushort": "integer",
"string": "string",
"bool": ("bool", "_"),
"byte": ("integer", "u"),
"sbyte": ("integer", "s"),
"char": ("integer", "u"),
"decimal": ("number", "_"),
"double": ("number", "_"),
"float": ("number", "_"),
"int": ("integer", "s"),
"uint": ("integer", "u"),
"long": ("integer", "s"),
"ulong": ("integer", "u"),
"short": ("integer", "s"),
"ushort": ("integer", "u"),
"string": ("string", "_"),
}

from_types = ["bool", "long", "double", "string"]
Expand Down Expand Up @@ -108,10 +120,21 @@ def main(args):
if from_ty == to_ty:
continue

need_signed_check = is_signed_to_unsigned(kinds[from_ty], kinds[to_ty])

output_funcs += textwrap.indent("""
private static bool ConvertFrom{2}To{3}({0} i, out object o) {{
try
{{
{{""".format(from_ty, to_ty, from_ty.capitalize(), to_ty.capitalize()), mk_indent(2))

if need_signed_check:
output_funcs += textwrap.indent("""
if ( i < 0 )
{{
throw new OverflowException();
}}""".format(), mk_indent(2))

output_funcs += textwrap.indent("""
o = checked(({1})i);
return true;
}}
Expand Down

0 comments on commit 3d2f4db

Please sign in to comment.