首页 > 代码库 > Bridge.NET

Bridge.NET

  • 块作用域闭包问题

结果正确:1

容易引入JSB:1

技术分享
 1 public class Program
 2 {
 3     static List<Action> createActions()
 4     {
 5         List<Action> arr = new List<Action>();
 6         for (int i = 0; i < 10; i++)
 7         {
 8             {
 9             int j = i;
10             arr.Add(() =>
11                     {
12                 Console.WriteLine(j.ToString());
13             });
14         }
15         }
16         return arr;
17     }
18     static void bbtest()
19     {
20         var arr = createActions();
21         for (int i = 0; i < arr.Count; i++)
22         {
23             arr[i]();
24         }
25     }
26     public static void Main()
27     {
28         bbtest();
29         //Console.WriteLine("Hello World!");
30     }
31 }
View Code
技术分享
 1 /**
 2  * @compiler Bridge.NET 15.3.0
 3  */
 4 Bridge.assembly("Demo", function ($asm, globals) {
 5     "use strict";
 6 
 7     Bridge.define("Demo.Program", {
 8         statics: {
 9             createActions: function () {
10                 var arr = new (System.Collections.Generic.List$1(Function))();
11                 for (var i = 0; i < 10; i = (i + 1) | 0) {
12                     (function () {
13                         {
14                             var j = i;
15                             arr.add(function () {
16                                 Bridge.Console.log(j.toString());
17                             });
18                         }
19                     }).call(this);
20                 }
21                 return arr;
22             },
23             bbtest: function () {
24                 var arr = Demo.Program.createActions();
25                 for (var i = 0; i < arr.getCount(); i = (i + 1) | 0) {
26                     arr.getItem(i)();
27                 }
28             }
29         },
30         $main: function () {
31             Demo.Program.bbtest();
32             //Console.WriteLine("Hello World!");
33         }
34     });
35 });
View Code
技术分享
 1 0
 2 1
 3 2
 4 3
 5 4
 6 5
 7 6
 8 7
 9 8
10 9
View Code
  • ref/out

结果正确:1

容易引入JSB:1

技术分享
 1 public class Program
 2 {
 3     class Apple
 4     {
 5         public int price;
 6     }
 7     static void testRef(ref int v)
 8     {
 9         v++;
10     }
11     static void testOut(out Apple a)
12     {
13         a = new Apple();
14         a.price = 44;
15     }
16     public static void Main()
17     {
18         int v = 5;
19         testRef(ref v);
20         Console.WriteLine(v);
21         
22         Apple a = new Apple();
23         testOut(out a);
24     }
25 }
View Code
技术分享
 1 /**
 2  * @compiler Bridge.NET 15.3.0
 3  */
 4 Bridge.assembly("Demo", function ($asm, globals) {
 5     "use strict";
 6 
 7     Bridge.define("Demo.Program", {
 8         statics: {
 9             testRef: function (v) {
10                 v.v = (v.v + 1) | 0;
11             },
12             testOut: function (a) {
13                 a.v = new Demo.Program.Apple();
14                 a.v.price = 44;
15             }
16         },
17         $main: function () {
18             var v = { v : 5 };
19             Demo.Program.testRef(v);
20             Bridge.Console.log(v.v);
21 
22             var a = { v : new Demo.Program.Apple() };
23             Demo.Program.testOut(a);
24         }
25     });
26 
27     Bridge.define("Demo.Program.Apple", {
28         price: 0
29     });
30 });
View Code
技术分享
1 6
View Code
  • 重载函数

结果正确:1

容易引入JSB:0

技术分享
 1 public class Program
 2 {
 3     static void hello(int v)
 4     {
 5     }
 6     static void hello(string v)
 7     {
 8     }
 9     static void hello(int a, int b)
10     {
11     }
12     public static void Main()
13     {
14     }
15 }
View Code
技术分享
 1 /**
 2  * @compiler Bridge.NET 15.3.0
 3  */
 4 Bridge.assembly("Demo", function ($asm, globals) {
 5     "use strict";
 6 
 7     Bridge.define("Demo.Program", {
 8         statics: {
 9             hello: function (v) {
10             },
11             hello$2: function (v) {
12             },
13             hello$1: function (a, b) {
14             }
15         },
16         $main: function () {
17         }
18     });
19 });
View Code
  • 结构体

结果正确:1

容易引入JSB:1

技术分享
 1 public class Program
 2 {
 3     struct A
 4     {
 5         public int v;
 6     }
 7     static void Test(A a)
 8     {
 9         a.v = 4;
10     }
11     public static void Main()
12     {
13         A a = new A();
14         a.v = 5;
15         Test(a);
16         Console.WriteLine(a.v);
17     }
18 }
View Code
技术分享
 1 /**
 2  * @compiler Bridge.NET 15.3.0
 3  */
 4 Bridge.assembly("Demo", function ($asm, globals) {
 5     "use strict";
 6 
 7     Bridge.define("Demo.Program", {
 8         statics: {
 9             test: function (a) {
10                 a.v = 4;
11             }
12         },
13         $main: function () {
14             var a = new Demo.Program.A();
15             a.v = 5;
16             Demo.Program.test(a.$clone());
17             Bridge.Console.log(a.v);
18         }
19     });
20 
21     Bridge.define("Demo.Program.A", {
22         $kind: "struct",
23         statics: {
24             getDefaultValue: function () { return new Demo.Program.A(); }
25         },
26         v: 0,
27         ctor: function () {
28             this.$initialize();
29         },
30         getHashCode: function () {
31             var h = Bridge.addHash([65, this.v]);
32             return h;
33         },
34         equals: function (o) {
35             if (!Bridge.is(o, Demo.Program.A)) {
36                 return false;
37             }
38             return Bridge.equals(this.v, o.v);
39         },
40         $clone: function (to) {
41             var s = to || new Demo.Program.A();
42             s.v = this.v;
43             return s;
44         }
45     });
46 });
View Code
技术分享
1 5
View Code
  • is/as

结果正确:1

容易引入JSB:1

技术分享
 1 public class Program
 2 {
 3     class A{}
 4     class B{}
 5     static void Test(object obj)
 6     {
 7         Console.WriteLine((obj is A).ToString());
 8         Console.WriteLine((obj is B).ToString());
 9         Console.WriteLine(((obj as A) != null).ToString());
10         Console.WriteLine(((obj as B) != null).ToString());
11     }
12     public static void Main()
13     {
14         A a = new A();
15         Test(a);
16     }
17 }
View Code
技术分享
 1 /**
 2  * @compiler Bridge.NET 15.3.0
 3  */
 4 Bridge.assembly("Demo", function ($asm, globals) {
 5     "use strict";
 6 
 7     Bridge.define("Demo.Program", {
 8         statics: {
 9             test: function (obj) {
10                 Bridge.Console.log(System.Boolean.toString((Bridge.is(obj, Demo.Program.A))));
11                 Bridge.Console.log(System.Boolean.toString((Bridge.is(obj, Demo.Program.B))));
12                 Bridge.Console.log(System.Boolean.toString(((Bridge.as(obj, Demo.Program.A)) != null)));
13                 Bridge.Console.log(System.Boolean.toString(((Bridge.as(obj, Demo.Program.B)) != null)));
14             }
15         },
16         $main: function () {
17             var a = new Demo.Program.A();
18             Demo.Program.test(a);
19         }
20     });
21 
22     Bridge.define("Demo.Program.A");
23 
24     Bridge.define("Demo.Program.B");
25 });
View Code
技术分享
1 True
2 False
3 True
4 False
View Code

 协程

结果正确:0

容易引入JSB:0

技术分享
 1 public class Program
 2 {
 3     static IEnumerator Test()
 4     {
 5         yield return 100;
 6         Console.WriteLine(1);
 7         yield return 200;
 8         Console.WriteLine(2);
 9         yield return 300;
10         Console.WriteLine(3);
11         yield return 400;
12         Console.WriteLine(4);
13     }
14     public static void Main()
15     {
16         IEnumerator ie = Test();
17         while (ie.MoveNext())
18         {
19             Console.WriteLine("Current = " + ie.Current);
20         }
21     }
22 }
View Code
技术分享
 1 /**
 2  * @compiler Bridge.NET 15.3.0
 3  */
 4 Bridge.assembly("Demo", function ($asm, globals) {
 5     "use strict";
 6 
 7     Bridge.define("Demo.Program", {
 8         statics: {
 9             test: function () {
10                 var $yield = [];
11                 $yield.push(100);
12                 Bridge.Console.log(1);
13                 $yield.push(200);
14                 Bridge.Console.log(2);
15                 $yield.push(300);
16                 Bridge.Console.log(3);
17                 $yield.push(400);
18                 Bridge.Console.log(4);
19                 return System.Array.toEnumerator($yield);
20             }
21         },
22         $main: function () {
23             var ie = Demo.Program.test();
24             while (ie.System$Collections$IEnumerator$moveNext()) {
25                 Bridge.Console.log(System.String.concat("Current = ", ie.System$Collections$IEnumerator$getCurrent()));
26             }
27         }
28     });
29 });
View Code
技术分享
 1 // 错误输出!
 2 
 3 1
 4 2
 5 3
 6 4
 7 Current = 100
 8 Current = 200
 9 Current = 300
10 Current = 400
View Code
技术分享
 1 // 正确输出!
 2 
 3 Current = 100
 4 1
 5 Current = 200
 6 2
 7 Current = 300
 8 3
 9 Current = 400
10 4
View Code

 

Bridge.NET