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 }

 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 });

 1 0
 2 1
 3 2
 4 3
 5 4
 6 5
 7 6
 8 7
 9 8
10 9

  • 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 }

 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 });

1 6

  • 重载函数

结果正确: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 }

 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 });

  • 结构体

结果正确: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 }

 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 });

1 5

  • 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 }

 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 });

1 True
2 False
3 True
4 False

协程

结果正确: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 }

 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 });

 1 // 错误输出!
 2
 3 1
 4 2
 5 3
 6 4
 7 Current = 100
 8 Current = 200
 9 Current = 300
10 Current = 400

 1 // 正确输出!
 2
 3 Current = 100
 4 1
 5 Current = 200
 6 2
 7 Current = 300
 8 3
 9 Current = 400
10 4

时间: 2024-11-03 20:47:23

Bridge.NET的相关文章

设计模式 -- 桥接模式(Bridge Pattern)

桥接模式 Bridge Pattern 结构设计模式 定义: 分离抽象部分和实现部分,使他们独立运行. 避免使用继承导致系统类个数暴增,可以考虑桥接模式. 桥接模式将继承关系转化为关联关系,减少耦合,减少代码量. 例如: public interface Shape { public void bepaint(String color); } public abstract class Color { Shape shape; public void setShape(Shape shape)

Bridge模式

Bridge模式 Bridge模式 在面向对象的开发过程中,要做到2点:1.高内聚(cohesion),2.松耦合(coupling).但是在实际开发过程中难以把握,例如会遇到这样的问题: 1)客户给了一个需求,之后我们用一个类A来实现. 2)客户的需求有了变化,要用到2个算法来实现.于是我们通过定义一个抽象基类A,在用两个具体类A1和A2实现这两个算法. 3)客户需求又有了变化,要求在2个操作系统上实现.我们再抽象一个层次,不同操作系统抽象为A0和A1,每个操作系统上有2个算法,在实现为A00

OpenStack —— 网络进阶Linux Bridge(七)

一.配置Linux Bridge Neutron默认使用ML2作为core plugin,其配置位于/etc/neutron/neutron.conf,控制节点和计算节点都需要在各自的neutron.conf 中配置core_plugin选项. 然后需要让ML2使用linux-bridge mechanism driver,ML2的配置文件位于/etc/neutron/plugins/ml2/ml2_conf.ini. [ml2] # ... mechanism_drivers = linuxb

9,桥接模式(Bridge Pattern)是将抽象部分与实际部分分离,使它们都可以独立的变化。

Bridge 模式又叫做桥接模式,是构造型的设计模式之一.Bridge模式基于类的最小设计原则,通过使用封装,聚合以及继承等行为来让不同的类承担不同的责任. 它的主要特点是把抽象(abstraction)与行为实现(implementation)分离开来,从而可以保持各部分的独立性以及应对它们的功能扩展.  适用于: 桥接模式(Bridge Pattern)是将抽象部分与实现部分分离(解耦合),使它们都可以独立的变化. 车 安装 发动机 :不同型号的车,安装不同型号的发动机        将"车

使用Linux Bridge 搭建vxlan 实现 虚拟机跨物理机通信

#实验环境: #本次实验要让192.168.1.3 跨物理节点 ping 通 192.168.1.2 #两台物理机: KVM_1 192.168.174.134 KVM_2 192.168.174.135 #在KVM_1主机上操作 #安装KVM相关软件 [[email protected]_1 ~]#  yum -y install qemu-kvm libvirt virt-install bridge-utils [[email protected]_1 ~]# systemctl star

Linux虚拟机安装配置准备工作之--- VMware ( Bridge )

前言: 宿主机所在网段:192.168.1.X    Mask 255.255.255.0 虚拟机所在网段:192.168.2.X    Mask 255.255.255.0 宿主机:192.168.1.45 客户机:192.168.1.102 虚拟机:192.168.2.16 因为公司192.168.1.X这个网段的IP几将用完,所以我准备给虚拟机分配192.168.2.X 这个网段的IP地址.但是公司的其它同事(包括宿主机本身都在192.168.1.X)这网段 之内.如何让有需要的的同事可以

Linux Network Bridge

1. About Network Bridge A network bridge is a Link Layer device which forwards traffic between networks based on MAC addresses and is therefore also referred to as a Layer 2 device. It makes forwarding decisions based on tables of MAC addresses which

Linux : Bridge support not available: brctl not found

Getting this error message while trying to bring up your newly configured bridge interface on RHEL/CentOS Linux? You are missing "bridge-utils" package. Simply install it as followed and bring up the interface again : yum install bridge-utils NO

设计模式之Bridge(桥接)(转)

Bridge定义 : 将抽象和行为划分开来,各自独立,但能动态的结合. 为什么使用? 通常,当一个抽象类或接口有多个具体实现(concrete subclass),这些concrete之间关系可能有以下两种: 1. 这多个具体实现之间恰好是并列的,如前面举例,打桩,有两个concrete class:方形桩和圆形桩;这两个形状上的桩是并列的,没有概念上的重复,那么我们只要使用继承就可以了. 2.实际应用上,常常有可能在这多个concrete class之间有概念上重叠.那么需要我们把抽象共同部分

Can Microsoft’s exFAT file system bridge the gap between OSes?

转自:http://arstechnica.com/information-technology/2013/06/review-is-microsofts-new-data-sharing-system-a-cross-platform-savior/ With Apple's licensing of Microsoft's exFAT file system, it seems like we finally have a good option for OS X and Windows d