polytronicgr / sharpkit

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

Named constuctor parameters cause #325

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
Create an object using:
var m = new MyObject(){Property1=true, Propery2="hello"};

What is the expected output? What do you see instead?
Worked up to including 5.2.0

in 5.2.2/3/4 the following error is shown

MyCode.cs(138,36): error SK0000: Cannot cast from source type to destination 
type. [MyCode.cs]
    ICSharpCode.NRefactory.CSharp.CompilerException: Cannot cast from source type to destination type. ---> System.InvalidCastException: Cannot cast from source type to destination type.
      at (wrapper castclass) object:__castclass_with_cache (object,intptr,intptr)
      at System.Linq.Enumerable+<CreateCastIterator>c__Iterator15`1[ICSharpCode.NRefactory.CSharp.Resolver.CSharpInvocationResolveResult].MoveNext () [0x00000] in <filename unknown>:0 
      at System.Linq.Enumerable+<CreateSelectIterator>c__Iterator25`2[ICSharpCode.NRefactory.CSharp.Resolver.CSharpInvocationResolveResult,ICSharpCode.NRefactory.Semantics.ResolveResult].MoveNext () [0x00000] in <filename unknown>:0 
      at System.Collections.Generic.List`1[ICSharpCode.NRefactory.Semantics.ResolveResult].AddEnumerable (IEnumerable`1 enumerable) [0x00000] in <filename unknown>:0 
      at System.Collections.Generic.List`1[ICSharpCode.NRefactory.Semantics.ResolveResult]..ctor (IEnumerable`1 collection) [0x00000] in <filename unknown>:0 
      at System.Linq.Enumerable.ToList[ResolveResult] (IEnumerable`1 source) [0x00000] in <filename unknown>:0 
      at SharpKit.Compiler.JsCodeImporter.InitializersToJson (IList`1 initializerStatements, IType type) [0x00000] in <filename unknown>:0 
      at SharpKit.Compiler.JsCodeImporter.VisitInvocationResolveResultAsCtor (ICSharpCode.NRefactory.CSharp.Resolver.CSharpInvocationResolveResult res) [0x00000] in <filename unknown>:0 
      at SharpKit.Compiler.JsCodeImporter.VisitCSharpInvocationResolveResult (ICSharpCode.NRefactory.CSharp.Resolver.CSharpInvocationResolveResult res) [0x00000] in <filename unknown>:0 
      at ICSharpCode.NRefactory.CSharp.Resolver.CSharpInvocationResolveResult.AcceptVisitor[JsNode] (IResolveResultVisitor`1 visitor) [0x00000] in <filename unknown>:0 
      at SharpKit.Compiler.JsCodeImporter.Visit (ICSharpCode.NRefactory.Semantics.ResolveResult res) [0x00000] in <filename unknown>:0 
      --- End of inner exception stack trace ---
      at SharpKit.Compiler.JsCodeImporter.Visit (ICSharpCode.NRefactory.Semantics.ResolveResult res) [0x00000] in <filename unknown>:0 
      at SharpKit.Compiler.JsCodeImporter.VisitConversion (ICSharpCode.NRefactory.Semantics.ResolveResult input, ICSharpCode.NRefactory.Semantics.Conversion conversion, IType conversionType) [0x00000] in <filename unknown>:0 
      at SharpKit.Compiler.JsCodeImporter.VisitConversionResolveResult (ICSharpCode.NRefactory.Semantics.ConversionResolveResult res) [0x00000] in <filename unknown>:0 
      at ICSharpCode.NRefactory.Semantics.ConversionResolveResult.AcceptVisitor[JsNode] (IResolveResultVisitor`1 visitor) [0x00000] in <filename unknown>:0 
      at SharpKit.Compiler.JsCodeImporter.Visit (ICSharpCode.NRefactory.Semantics.ResolveResult res) [0x00000] in <filename unknown>:0 
    Total: 2534ms

Please provide any additional information below.

5.2.5 and 5.2.6 is not tested because of separate install bug.

Running on Linux/Ubuntu 13.10 using mono 3.2.1(.net 4.5)

Original issue reported on code.google.com by Hultqvis...@gmail.com on 10 Nov 2013 at 6:06

GoogleCodeExporter commented 8 years ago
Are you still not able to install later versions of SharpKit?

Original comment by DanelK...@gmail.com on 21 Nov 2013 at 3:27

GoogleCodeExporter commented 8 years ago
#324 was fixed with 5.2.7

I tried this issue in 5.2.7 and it is still there.

Original comment by Hultqvis...@gmail.com on 21 Nov 2013 at 3:51

GoogleCodeExporter commented 8 years ago
Cool, thanks for pointing it out.
Sebastian do you mind having a look?

Original comment by DanelK...@gmail.com on 21 Nov 2013 at 4:39

GoogleCodeExporter commented 8 years ago
I can't reproduce this issue. Please submit an minimalistic sample project.

Code used:
using SharpKit.JavaScript;
using SharpKit.Html;
using SharpKit.jQuery;

[assembly: JsType(NativeOperatorOverloads = true, TargetType = typeof(int))]

[assembly: JsExport(FilenameFormat = "res/{0}", CodeInjectionFilename = 
"res/delegates.js", UseStrict = true, UseExactEquals = true, 
DefaultFilenameAsCsFilename = true)]
[assembly: JsType(Mode = JsMode.Clr, NativeParams = false, NativeJsons = true)]

[assembly: JsMethod(TargetType = typeof(Nullable<>), TargetMethod = 
"get_HasValue", Name = "!=null", OmitDotOperator = true, OmitParanthesis = 
true)]

namespace SharpKitWebApp3
{

    public class test
    {
        public void func()
        {
            var m = new MyObject() { Property1 = true, Propery2 = "hello" };
        }
    }

    public class MyObject
    {
        public bool Property1 { get; set; }
        public string Propery2 { get; set; }
    }

}

Original comment by sebastia...@gmail.com on 22 Nov 2013 at 6:26

GoogleCodeExporter commented 8 years ago
This example works in 5.2.0 but triggers the bug in later versions.

using SharpKit.JavaScript;

//Trigger part 1/2
[assembly: JsType(NativeArrayEnumerator = true)]

namespace Issue325
{
    [JsType(JsMode.Prototype)]
    public class Test
    {
        public void func ()
        {
            //Trigger part 2/2: parameters
            var t = new MyObject () { 
                PropertyA = 123,
            };
        }

    }

    [JsType (JsMode.Json)]
    public class MyObject
    {
        public int PropertyA { get; set; }
    }

}

Original comment by Hultqvis...@gmail.com on 22 Nov 2013 at 8:37

GoogleCodeExporter commented 8 years ago
The problem is:
[assembly: JsType(NativeArrayEnumerator = true)]
marks ALL classes classes with that (by default).

The logic with initializers is:
When a type have NativeArrayEnumerator = true, an array constructor will be 
generated.

I'm not sure, but i think, this is by design. But when this should be fixed, 
than this could be the solution: Check, if the inizializer statements are a 
single expression per item, or a nassignment per item.

Dan-el, what do you think about this issue?

Original comment by sebastia...@gmail.com on 23 Nov 2013 at 2:19

GoogleCodeExporter commented 8 years ago
I think that it's very dangerous to mark JsNativeArrayEnumerator globally. It 
should only be used on classes that need to behave like js arrays. Still, the 
compiler shouldn't crash. 
I suppose that if:
JsNativeArrayEnumerator=true and JsNativeEnumerator=false,
and
trying to initialize an object using initializer, we should initialize it as if 
it's a standard class:
(function(){var $x = new MyObject(); x.Propery1="Hello"; return x;})();

What do you guys think? Is this your expected behavior?

Original comment by DanelK...@gmail.com on 23 Nov 2013 at 8:44

GoogleCodeExporter commented 8 years ago
I have reviewed my own code and removed the problematic use of the attribute. 
It appears as if all is working fine.

From what I read in Sebastians comment the behaviour is a reasonable one.
An array constructor with parameters does not make sense - thus the original 
crash.

The issue might then be limited to the naming or documentation of the 
attribute. As it is written now it appears to only affect foreach statements, 
whereas what it actually does is treating the c# object as an jsArray from 
constructor to foreach loops.
Renaming the Attribute would be a breaking change but amending the summary is 
something I could do.

Original comment by Hultqvis...@gmail.com on 23 Nov 2013 at 9:54

GoogleCodeExporter commented 8 years ago
"As it is written now it appears to only affect foreach statements"

That why i think, the compiler should not use this attribute on inizializers. 
the compielr should check, if its the "initializerStatements" are a collection 
of expressions (=array items), or assignment (=property inizializer).

Original comment by sebastia...@gmail.com on 23 Nov 2013 at 11:51

GoogleCodeExporter commented 8 years ago
The error should not occur anymore in the next version.

Original comment by sebastia...@gmail.com on 25 Nov 2013 at 10:30