sq / JSIL

CIL to Javascript Compiler
http://jsil.org/
Other
1.73k stars 241 forks source link

Expandoobject #1018

Open thelazydogsback opened 8 years ago

thelazydogsback commented 8 years ago

Trying: dynamic e = new ExpandoObject(); in tryJsil produces the following:

Unhandled exception: TypeError: Cannot read property 'ExpandoObject' of undefined

Also, what type can I use on the C# side for dynamic objects such that the usage is completely erased by the compiler in favor of using native objects on the JS side?

For example, I'd want:

dynamic obj1 = new ExpandoOrWhatever()
dynamic obj2 = new ExpandoOrWhatever()
obj2.bar = 42;
obj1.foo = obj2;

to be compiled to:

obj1 = {}
obj2 = {}
obj2.bar = 42;
obj1.foo = obj1;
// obj1 = { foo: {bar: 42}}

thanks

kg commented 8 years ago

I would suggest writing a small helper function that returns ExpandoObject in .NET and Verbatim.Expression("{}") in JS. There might actually be a simpler way to do this, but I forget. You can use Builtins.IsJavascript to do the branch, I think. The code might not be fully erased but it should work the way you want.

D-R-R commented 8 years ago

The above example is working well for me in VS2013, but in VS2015, I end up with a much more verbose output using System.Runtime.CompilerServices.CallSite to invoke every operation in each expression. Searching reveals issue #531 where apparently the new Roslyn compiler creates issues with generating the concise output.

Is there a workaround or other technique to get the above result even with VS2015, or is that only available with the earlier version? Thank you in advance for your time.

iskiselev commented 8 years ago

With latest version of JSIL translation of this sample should use all C#-dynamic mechanics, but should work with JS. The only way to create clear JS from C# is to use Verbatium helpers.