Created
March 20, 2012 14:34
-
-
Save kg/2136227 to your computer and use it in GitHub Desktop.
JSIL Code Sample: Struct Copy Elimination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public static class Program { | |
public static CustomType ReturnArgument (CustomType arg) { | |
return arg; | |
} | |
public static void Main (string[] args) { | |
var a = new CustomType(1); | |
var b = new CustomType(2); | |
var c = a + b; | |
Console.WriteLine("a={0}, b={1}, c={2}", a, b, c); | |
c = ReturnArgument(a + b); | |
Console.WriteLine("a={0}, b={1}, c={2}", a, b, c); | |
Console.WriteLine("{0}", a + c); | |
} | |
} | |
public struct CustomType { | |
public int Value; | |
public CustomType (int value) { | |
Value = value; | |
} | |
public static CustomType operator + (CustomType lhs, CustomType rhs) { | |
return new CustomType(lhs.Value + rhs.Value); | |
} | |
public override string ToString () { | |
return String.Format("{0}", Value); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JSIL.MakeStaticClass("Program", true, [], function ($) { | |
$.Method({Static:true , Public:true }, "Main", | |
$sig.make(279443, null, [$jsilcore.TypeRef("System.Array", [$.String])], []), | |
function Program_Main (args) { | |
var a = new $asm00.CustomType(), b = new $asm00.CustomType(); | |
$asm00.CustomType.prototype._ctor.call(a, 1); | |
$asm00.CustomType.prototype._ctor.call(b, 2); | |
var c = $asm00.CustomType.op_Addition(a, b); | |
$asm01.System.Console.WriteLine("a={0}, b={1}, c={2}", a, b, c); | |
c = $asm00.Program.ReturnArgument($asm00.CustomType.op_Addition(a, b)); | |
$asm01.System.Console.WriteLine("a={0}, b={1}, c={2}", a, b, c); | |
$asm01.System.Console.WriteLine("{0}", $asm00.CustomType.op_Addition(a, c)); | |
} | |
); | |
$.Method({Static:true , Public:true }, "ReturnArgument", | |
$sig.make(279442, $asm00.TypeRef("CustomType"), [$asm00.TypeRef("CustomType")], []), | |
function Program_ReturnArgument (arg) { | |
return arg; | |
} | |
); | |
}); | |
JSIL.MakeStruct($asm01.TypeRef("System.ValueType"), "CustomType", true, [], function ($) { | |
$.Method({Static:false, Public:true }, ".ctor", | |
$sig.make(279458, null, [$.Int32], []), | |
function CustomType__ctor (value) { | |
this.Value = value; | |
} | |
); | |
$.Method({Static:true , Public:true }, "op_Addition", | |
$sig.make(279460, $.Type, [$.Type, $.Type], []), | |
function CustomType_op_Addition (lhs, rhs) { | |
return new $asm00.CustomType((lhs.Value + rhs.Value)); | |
} | |
); | |
$.Method({Static:false, Public:true }, "toString", | |
$sig.make(279462, $.String, [], []), | |
function CustomType_toString () { | |
return $asm01.System.String.Format("{0}", this.Value); | |
} | |
); | |
$.Field({Static:false, Public:true }, "Value", $.Int32); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment