fjgandrade / sharpkit

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

Anonymous types are broken in CLR mode because no getters are generated #337

Closed GoogleCodeExporter closed 9 years ago

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

1. Build the following class:

    [JsType(JsMode.Clr, Filename = "res/Default.js")]
    public class TestClasses
    {
        public TestClasses()
        {
            var result = new[] { "Hello" }
                .Select(x => new { Greeting = x })
                .Select(x => x.Greeting);
        }
    }

2. Notice the compiled result:

            var result = System.Linq.Enumerable.Select$2$$IEnumerable$1$$Func$2(null , System.String.ctor, System.Linq.Enumerable.Select$2$$IEnumerable$1$$Func$2(System.String.ctor, null , ["Hello"], $CreateAnonymousDelegate(this, function (x)
            {
                return {Greeting: x};
            })), $CreateAnonymousDelegate(this, function (x)
            {
                return x.get_Greeting();
            }));

You'll notice that the first anonymous function has:

return {Greeting: x};

The second one consumes the first one, but uses a (nonexistent) getter:

return x.get_Greeting();

I believe the correct fix is to generate getters for anonymous types created in 
CLS classes.

Original issue reported on code.google.com by kirk.w...@gmail.com on 9 Dec 2013 at 10:11

GoogleCodeExporter commented 9 years ago
This was disabled by default by popular demand. You can re-enable this feature 
if you apply this in your class where you use anonymous objects:
http://sharpkit.net/help/SharpKit.JavaScript/SharpKit.JavaScript/JsTypeAttribute
/NativeJsons.htm

Original comment by DanelK...@gmail.com on 12 Dec 2013 at 12:42

GoogleCodeExporter commented 9 years ago
What's the downside to this feature?

An alternative and perhaps nicer fix is to do the x.Greeting without the 
indirection.

Original comment by co...@gravill.com on 12 Dec 2013 at 1:46

GoogleCodeExporter commented 9 years ago
no specific downside, people just wanted native jsons by default, the getters 
and setters didn't really help anyone...

Original comment by DanelK...@gmail.com on 12 Dec 2013 at 1:52

GoogleCodeExporter commented 9 years ago
I don't see a need for the getters and setters either. It would be nice to 
support LINQ statements though, by generating calls to the fields rather than 
to non-existent getters.

Original comment by co...@gravill.com on 12 Dec 2013 at 1:56

GoogleCodeExporter commented 9 years ago
Well by default, it should work like that. Currently it generates get_X() ?

D.

Original comment by DanelK...@gmail.com on 12 Dec 2013 at 1:58

GoogleCodeExporter commented 9 years ago
Verified bug, temporary workaround, set:
NativeJsons = true

Original comment by DanelK...@gmail.com on 14 Dec 2013 at 11:21

GoogleCodeExporter commented 9 years ago
Fixed, real clr style anonymous objects will be created in clr mode, you can 
turn it off by setting NativeJsons=true.
Added injection function:
$CreateAnonymousObject(json)

Original comment by DanelK...@gmail.com on 14 Dec 2013 at 12:40

GoogleCodeExporter commented 9 years ago

Original comment by DanelK...@gmail.com on 11 Jan 2014 at 7:53