polytronicgr / sharpkit

Automatically exported from code.google.com/p/sharpkit
0 stars 0 forks source link

Multi-dimensional arrays are not supported well #340

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

Compile the following code:

    [JsType(JsMode.Clr, Filename = "res/Default.js")]
    public class TestClasses
    {
        public TestClasses()
        {
            HtmlContext.console.log(new int[5, 4]);
        }
    }

What is the expected output? What do you see instead?

The compiler crashes with the following stack trace:

1>...\TestClasses.cs(13,37): error SK0000: Sequence contains more than one 
element 1>  ICSharpCode.NRefactory.CSharp.CompilerException: Sequence contains 
more than one element ---> System.InvalidOperationException: Sequence contains 
more than one element
1>     at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
1>     at 
SharpKit.Compiler.JsCodeImporter.VisitArrayCreateResolveResult(ArrayCreateResolv
eResult res)
1>     at 
ICSharpCode.NRefactory.Semantics.ArrayCreateResolveResult.AcceptVisitor[R](IReso
lveResultVisitor`1 visitor)
1>     at SharpKit.Compiler.JsCodeImporter.Visit(ResolveResult res)
1>     --- End of inner exception stack trace ---
1>     at SharpKit.Compiler.JsTypeImporter.Visit(IEntity node)
1>     at SharpKit.Compiler.JsModelImporter.ExportType(ITypeDefinition ce)
1>     at SharpKit.Compiler.JsModelImporter.ExportType(ITypeDefinition ce, 
JsFile jsFile)
1>     at System.Collections.Generic.List`1.ForEach(Action`1 action)
1>     at System.Linq.Extensions.ForEach[T](IEnumerable`1 items, Action`1 
action) in c:\Dev\corex\src\corex\Extensions\System.Linq.cs:line 85
1>     at SharpKit.Compiler.JsModelImporter.Process()
1>     at SharpKit.Compiler.CompilerTool.ConvertCsToJs()
1>     at SharpKit.Compiler.CompilerTool.Time(Action action)
1>     at SharpKit.Compiler.CompilerTool.InternalRun()

The code in the sharpkit compiler is something like:

        public JsNode VisitArrayCreateResolveResult(ArrayCreateResolveResult res)
        {
            string jsArrayType = SkJs.GetJsArrayType((ArrayType)res.Type);
            JsExpression[] items = null;
            JsExpression size = null;
            if (res.InitializerElements.IsNotNullOrEmpty())
                items = VisitExpressions(res.InitializerElements).ToArray();
            else if (res.SizeArguments.IsNotNullOrEmpty())
                size = VisitExpression(res.SizeArguments.Single());
            return Js.NewArray(jsArrayType, size, items);
        }

As you can see, it calls `.Single()` on SizeArguments, which is not always only 
a single element.

Original issue reported on code.google.com by kirk.w...@sqor.com on 15 Dec 2013 at 12:16

GoogleCodeExporter commented 8 years ago
That's correct, multidimensional arrays aren't supported in js. It's possible 
to emulate them using jagged arrays, and fill them up, although this has some 
overhead.
We can inject something like this:
var arr = $CreateArray([5,4])

Then implement it using a for loop, the resulting array will have to use this 
syntax:
arr[2][3]
Because there are no multidimensional arrays in JavaScript..

Original comment by DanelK...@gmail.com on 16 Dec 2013 at 4:37