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:? 重载函数是以$1 $2结尾的。可以的,看下面第3个代码,对重载函数按一定规则进行排序。
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 string MethodToString(MethodInfo m)
2 {
3 StringBuilder sb = new StringBuilder();
4
5 sb.Append(m.ReturnType.ToString()).Append(" ");
6 sb.Append(m.Name).Append(" ");
7 sb.Append(m.GetGenericArguments().Length).Append(" ");
8
9 foreach (var p in m.GetParameters())
10 {
11 sb.Append(p.ParameterType.ToString()).Append(" ");
12 }
13 return sb.ToString();
14 }
结构体
结果正确: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
抢救办法(同当前JSB的做法):
1 Bridge = {
2 Console: {
3 log: function (msg) {
4 print(msg);
5 }
6 }
7 }
8
9 var statics = {
10 test: function* () {
11
12 yield (100);
13 Bridge.Console.log(1);
14 yield (200);
15 Bridge.Console.log(2);
16 yield (300);
17 Bridge.Console.log(3);
18 yield (400);
19 Bridge.Console.log(4);
20
21 }
22 };
23
24 var p = statics.test();
25 while (true) {
26 var obj = p.next();
27 if (obj.done) {
28 break;
29 }
30 print(obj.value);
31 }
泛型
64位整数
结果正确:1
容易引入JSB:? 可能需要包装成字符串
1 public class Program
2 {
3 public static void Main()
4 {
5 long l = 1152921504606846976L;
6 Console.WriteLine(l.ToString());
7 }
8 }
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 $main: function () {
9 var l = System.Int64([0,268435456]);
10 Bridge.Console.log(l.toString());
11 }
12 });
13 });
1 1152921504606846976
编译时能否引用其他dll
可以吗:不可以,跟 duocode 一样
可以处理吗?可以,给 UnityEngine.dll 做壳
Bridge默认把函数名首字母改成了小写
可以处理吗?可以,bridge.json 里加入 "preserveMemberCase": true
Bridge.js里有一些跟浏览器相关的代码
搜索 navigator
todo
需要判断Bridge.js已经支持哪些类,如果他已经支持,需要可以选择使用c#版还是使用js版
todo
// finish list
property命名:getXXX, setXXX
泛型函数后缀:无(sharpkit后缀$1 $2)
泛型函数泛型参数:放前面传进去(跟sharpkit一样)
泛型函数类型传递给C#:Bridge.Reflection.getTypeFullName(t0)
params处理:无需处理,他自己传递了数组
Indexer:getItem 或 getItem$1 或 getItem$2
Operator:一样
继承格式
interface
导出枚举
暂时去掉JS Vector2 Vector3 Vector4 实现
inherits 如果是 System.Object,忽略
field get/set 处理
JSGenerator 里 lstNames 是什么 用于输出都导出哪些了。删了
cs导出,参数是数组,代码不好看
IsInheritanceRel
64位整数拆分
JSBindingSettings放到editor下
导出的类和Bridge已有的类 2者的关系如何处理?--如果bridge有,就不能导出
协程
导出System.DateTime不行,因为他的interface找不到 - it'ok
Bridge工程生成js文件有一部分是不要的,如何去除?--csw.cs, csw.js
hashtable 导出后,加载 Gen1 发生错误--ok,排序有问题,interface排到hashtable后面去了
结构体问题:Bridge对结构体参数会先$clone一份,我们的处理是,对于导出的结构体,不要$clone,即$clone简单返回this。
// ignore list
生成的外壳代码需要加sealed(除了MonoBehaviour 和 interface)
不支持yield break
// todo list
System.Object UnityEngine.Object js Object
Cs 导出后一些 Manual-JS - 没处理完全
Bridge.assembly要看js代码,是否需要?
Bridge.assembly("Demo1", function ($asm, globals) {........});
Bridge 的匿名函数处理好像很奇怪?
jscomponent 怎么处理
// 测试列表
协程 ok
静态构造函数 ok
http://www.cnblogs.com/answerwinner/p/4478735.html 中第17条 ok
泛型类、泛型函数 ok
js List 试用 ok
js 使用 c# 带 ref/out 参数的函数 ok
js 使用 c# 带 params 参数的函数
// 特殊
1. 如果在Bridge工程添加文件,之后需要手动把对System.dll的引用去掉
2.
posted on 2016-10-26 12:25 AnswerWinner 阅读(483) 评论(0) 编辑 收藏 举报