kswoll / WootzJs

C# to Javascript Compiler implemented via Roslyn
MIT License
110 stars 21 forks source link

Support for clipped Integers in JsPrimitiveExpression #7

Closed Danielku15 closed 10 years ago

Danielku15 commented 10 years ago

Currently JsPrimitiveExpression and the Js.Literal() do not support all C# primitives types, the clipped integers are missing. Even if JavaScript might not support them and some special TypedArray down casting might be needed in the future, currently it completely crashes the compiler with "Unexpected primitive type".

Primitive Type State
string :white_check_mark: Supported
byte :x: Unsupported
sbyte :x: Unsupported
short :x: Unsupported
ushort :x: Unsupported
int :white_check_mark: Supported
uint :white_check_mark: Supported
long :white_check_mark: Supported
ulong :white_check_mark: Supported
float :white_check_mark: Supported
double :white_check_mark: Supported
char :white_check_mark: Supported
kswoll commented 10 years ago

Can you elaborate on what you were doing to produce the error? I tried writing a few unit tests with byte and I couldn't repro the error.

Danielku15 commented 10 years ago

I just debugged into the compiler when compiling my source and it seems to happen if you declare your enum as one of those clipped integer types. Here's a unit test for EnumTest.cs:

        [Test]
        public void ValueByte()
        {
            AssertEquals(((byte)ByteEnum.One), 0);
            AssertEquals(((byte)ByteEnum.Two), 1);
            AssertEquals(((byte)ByteEnum.Three), 2);
        }

        public enum ByteEnum : byte
        {
            One = 0,
            Two = 1,
            Three = 2
        }

The 0,1,2 literals of the enum value initializers are interfered as byte literal which will result in this problem.

Therefore maybe the title of this issue should be: "Support for changing underlying types of enums" http://msdn.microsoft.com/en-us/library/sbbt4032.aspx

kswoll commented 10 years ago

Thanks! I've fixed the issues around enums for byte/sbyte/short/ushort. Let me know if there are other scenarios where you can reproduce similar issues with respect to these primitive types.

Danielku15 commented 10 years ago

The problem also occurs for byte parameters with default values:

        public static byte Clipped(byte test = 255) // fails
        {
            return test;
        }

But your last commit also fixed this cases. Maybe you want to add some unit tests for this case.

With this fix I am finally able to compile alphaTab. Now I'll start testing the generated JavaScript.

kswoll commented 10 years ago

Thanks, I've added some unit tests in NumberTests accordingly