Ejemplo n.º 1
0
        public static void OnBegin()
        {
            GeneratorHelp.ClearTypeInfo();
            // clear generated enum files
            string p = JSMgr.jsGenFile;

            if (!File.Exists(p))
            {
                int i = p.Replace('\\', '/').LastIndexOf('/');
                Directory.CreateDirectory(p.Substring(0, i));
            }
            File.Delete(p);
        }
        public static void GenWraps(Type[] arrEnums, Type[] arrClasses, HashSet <string> bridgeTypes)
        {
            GeneratorHelp.ClearTypeInfo();

            Dictionary <Type, TypeStatus> dict = new Dictionary <Type, TypeStatus>();
            Action <Type> onNewType            = null;

            onNewType = (nt) =>
            {
                while (true)
                {
                    if (nt.IsByRef || nt.IsArray)
                    {
                        nt = nt.GetElementType();
                        continue;
                    }
                    if (nt.IsGenericType && !nt.IsGenericTypeDefinition)
                    {
                        foreach (var ga in nt.GetGenericArguments())
                        {
                            onNewType(ga);
                        }

                        nt = nt.GetGenericTypeDefinition();
                        continue;
                    }
                    if (nt.IsGenericParameter)
                    {
                        return;
                    }
                    break;
                }

                if (!bridgeTypes.Contains(nt.FullName) &&
                    !dict.ContainsKey(nt))
                {
                    dict.Add(nt, new TypeStatus());
                }
            };

            Func <Type, TypeStatus> getParent = (type) =>
            {
                if (dict.ContainsKey(type))
                {
                    return(dict[type]);
                }
                return(null);
            };

            foreach (var type in arrEnums)
            {
                onNewType(type);
            }

            foreach (var type in arrClasses)
            {
                onNewType(type);
            }

            while (true)
            {
                Type[] keys = new Type[dict.Count];
                dict.Keys.CopyTo(keys, 0);

                foreach (Type type in keys)
                {
                    TypeStatus ts = dict[type];
                    if (ts.status != TypeStatus.Status.Wait)
                    {
                        continue;
                    }

                    if (ShouldIgnoreType(type))
                    {
                        ts.status = TypeStatus.Status.Ignored;
                        continue;
                    }

                    if (type.IsEnum)
                    {
                        GenEnum(type, ts, getParent, onNewType);
                    }
                    else if (typeof(Delegate).IsAssignableFrom(type))
                    {
                        GenDelegate(type, ts, getParent, onNewType);
                    }
                    else
                    {
                        GenInterfaceOrStructOrClass(type, ts, getParent, onNewType);
                    }
                }

                bool bContinue = false;
                foreach (var kv in dict)
                {
                    if (kv.Value.status == TypeStatus.Status.Wait)
                    {
                        bContinue = true;
                        break;
                    }
                }

                if (!bContinue)
                {
                    break;
                }
            }

            TextFile tfAll = new TextFile();

            tfAll.Add("#pragma warning disable 626, 824");
            foreach (var kv in dict)
            {
                if (kv.Value.status == TypeStatus.Status.Exported &&
                    !kv.Value.IsInnerType)
                {
                    tfAll.Add(kv.Value.tf.Ch);
                    tfAll.AddLine();
                }
            }
            tfAll.Add("#pragma warning restore 626, 824");
            File.WriteAllText(JSBindingSettings.CswFilePath, tfAll.Format(-1));
        }
        static void GenInterfaceOrStructOrClass(Type type, TypeStatus ts,
                                                Func <Type, TypeStatus> getParent, Action <Type> onNewType)
        {
            TextFile tfFile = null;

            if (type.DeclaringType != null)
            {
                ts.IsInnerType = true;

                TypeStatus tsParent = getParent(type.DeclaringType);
                if (tsParent == null || tsParent.status == TypeStatus.Status.Wait)
                {
                    return;
                }

                if (tsParent.status == TypeStatus.Status.Ignored)
                {
                    ts.status = TypeStatus.Status.Ignored;
                    return;
                }

                tfFile = tsParent.tf.FindByTag("epos");
            }

            if (tfFile == null)
            {
                tfFile = new TextFile();
            }

            ts.tf     = tfFile;
            ts.status = TypeStatus.Status.Exported;

            GeneratorHelp.ATypeInfo ti = GeneratorHelp.CreateTypeInfo(type);

            StringBuilder sb   = new StringBuilder();
            TextFile      tfNs = tfFile;

            //string dir = Dir;
            if (type.DeclaringType == null &&
                !string.IsNullOrEmpty(type.Namespace))
            {
                tfNs = tfFile.Add("namespace {0}", type.Namespace).BraceIn();
                tfNs.BraceOut();
            }

            tfNs.Add("[Bridge.FileName(\"csw\")]");

            TextFile tfClass = null;

            sb.Remove(0, sb.Length);
            {
                if (type.IsPublic || type.IsNestedPublic)
                {
                    sb.Append("public ");
                }

                if (type.IsClass)
                {
                    if (type.IsAbstract && type.IsSealed)
                    {
                        sb.Append("static ");
                    }
                    //else if (type.IsAbstract)
                    //    sb.Append("abstract ");
                    //else if (type.IsSealed)
                    //    sb.Append("sealed ");

                    //if (type.is)
                }

                if (type.IsInterface)
                {
                    sb.Append("interface ");
                }
                else if (type.IsValueType)
                {
                    sb.Append("struct ");
                }
                else
                {
                    sb.Append("class ");
                }

                string className = type.CsFullName(CsNameOption.CompilableWithT);
                int    dot       = className.LastIndexOf(".");
                if (dot >= 0)
                {
                    className = className.Substring(dot + 1);
                }
                sb.Append(className);

                Type   vBaseType  = type.ValidBaseType();
                Type[] interfaces = type.GetDeclaringInterfaces();
                if (vBaseType != null || interfaces.Length > 0)
                {
                    sb.Append(" : ");

                    args a = new args();
                    if (vBaseType != null)
                    {
                        a.Add(typefn(vBaseType, type.Namespace));
                        onNewType(vBaseType);
                    }
                    foreach (var i in interfaces)
                    {
                        a.Add(typefn(i, type.Namespace));
                        onNewType(i);
                    }

                    sb.Append(a.ToString());
                }

                tfClass = tfNs.Add(sb.ToString()).BraceIn();
                tfClass.BraceOut();
            }

            tfClass.AddTag("epos");

            for (int i = 0; i < ti.Fields.Count; i++)
            {
                MemberInfoEx infoEx = ti.Fields[i];
                FieldInfo    field  = infoEx.member as FieldInfo;
                tfClass.Add("public {0}{1} {2};", (field.IsStatic ? "static " : ""), typefn(field.FieldType, type.Namespace), field.Name);

                onNewType(field.FieldType);
            }
            if (ti.Fields.Count > 0)
            {
                tfClass.AddLine();
            }

            for (int i = 0; i < ti.Cons.Count; i++)
            {
                MemberInfoEx    infoEx = ti.Cons[i];
                ConstructorInfo con    = infoEx.member as ConstructorInfo;

                if (type.IsValueType)
                {
                    // 结构体不需要无参数构造函数
                    if (con == null || con.GetParameters().Length == 0)
                    {
                        continue;
                    }
                }

                string ctorName = type.Name;
                if (type.IsGenericTypeDefinition)
                {
                    int flag = ctorName.LastIndexOf('`');
                    if (flag >= 0)
                    {
                        ctorName = ctorName.Substring(0, flag);
                    }
                }
                tfClass.Add("public extern {0}({1});", ctorName, con == null ? "" : Ps2String(type, con.GetParameters()));

                if (con != null)
                {
                    foreach (var p in con.GetParameters())
                    {
                        onNewType(p.ParameterType);
                    }
                }
            }
            if (ti.Cons.Count > 0)
            {
                tfClass.AddLine();
            }

            handlePros(tfClass, type, ti, onNewType);

            if (ti.Pros.Count > 0)
            {
                tfClass.AddLine();
            }

            handleMethods(tfClass, type, ti, onNewType);
            if (!type.IsInterface)
            {
                handleInterfaceProblems(tfClass, type, onNewType);
            }
        }
        static void handleInterfaceProblems(TextFile tfClass, Type cType, Action <Type> OnNewType)
        {
            Type[] interfaces = cType.GetValidInterfaces();
            Action <Type, MethodInfo> actionMethod = (iType, method) =>
            {
                StringBuilder sbDef = new StringBuilder();

                sbDef.Append("extern ");
                if (!(method.IsSpecialName && method.Name == "op_Implicit"))
                {
                    sbDef.Append(typefn(method.ReturnType, cType.Namespace, CsNameOption.CompilableWithT) + " ");
                }

                OnNewType(method.ReturnType);

                sbDef.Append(iType.CsFullName(CsNameOption.CompilableWithT) + ".");                 // 这句是重点
                sbDef.Append(MethodNameString(cType, method));

                if (method.IsGenericMethodDefinition)
                {
                    Type[] argus  = method.GetGenericArguments();
                    args   t_args = new args();
                    foreach (var a in argus)
                    {
                        t_args.Add(a.Name);
                    }

                    sbDef.Append(t_args.Format(args.ArgsFormat.GenericT));
                }

                sbDef.Append("(");
                ParameterInfo[] ps = method.GetParameters();
                {
                    sbDef.Append(Ps2String(cType, ps));
                    sbDef.Append(");");

                    foreach (var p in ps)
                    {
                        OnNewType(p.ParameterType);
                    }
                }

                tfClass.Add(sbDef.ToString());
            };

            Action <Type, PropertyInfo> actionPro = (iType, pro) =>
            {
                OnNewType(pro.PropertyType);
                ParameterInfo[] ps = pro.GetIndexParameters();

                args iargs     = new args();
                bool isIndexer = (ps.Length > 0);
                if (isIndexer)
                {
                    for (int j = 0; j < ps.Length; j++)
                    {
                        iargs.AddFormat("{0} {1}", typefn(ps[j].ParameterType, cType.Namespace), ps[j].Name);
                        OnNewType(ps[j].ParameterType);
                    }
                }

                MethodInfo getm = pro.GetGetMethod();
                MethodInfo setm = pro.GetSetMethod();

                bool canGet = getm != null && getm.IsPublic;
                bool canSet = setm != null && setm.IsPublic;

                string getset = "";
                {
                    if (canGet)
                    {
                        getset += "get;";
                    }
                    if (canSet)
                    {
                        getset += " set;";
                    }
                }

                string vo = string.Empty;

                if (isIndexer)
                {
                    tfClass.Add("extern {4}{0} {1}{2} {{ {3} }}",
                                typefn(pro.PropertyType, cType.Namespace),
                                iType.CsFullName(CsNameOption.CompilableWithT) + ".this", // 这句是重点
                                iargs.Format(args.ArgsFormat.Indexer),
                                getset,
                                vo);
                }
                else
                {
                    tfClass.Add("extern {3}{0} {1} {{ {2} }}",
                                typefn(pro.PropertyType, cType.Namespace),
                                iType.CsFullName(CsNameOption.CompilableWithT) + "." +                     // 这句是重点
                                pro.Name,
                                getset,
                                vo);
                }
            };

            foreach (Type iType in interfaces)
            {
                GeneratorHelp.ATypeInfo ti = GeneratorHelp.CreateTypeInfo(iType);
                for (int i = 0; i < ti.Methods.Count; i++)
                {
                    MemberInfoEx infoEx = ti.Methods[i];
                    MethodInfo   method = infoEx.member as MethodInfo;
                    actionMethod(iType, method);
                }
                for (int i = 0; i < ti.Pros.Count; i++)
                {
                    MemberInfoEx infoEx = ti.Pros[i];
                    PropertyInfo pro    = infoEx.member as PropertyInfo;
                    actionPro(iType, pro);
                }
                // 特殊处理,BridgeProj工程
                if (iType == typeof(ICollection))
                {
                    tfClass.Add("bool System.Collections.ICollection.IsReadOnly { get; }");
                }
            }
        }
Ejemplo n.º 5
0
        public static TextFile GenerateClass()
        {
            GeneratorHelp.ATypeInfo ti;
            int slot = GeneratorHelp.AddTypeInfo(type, out ti);

            TextFile tfDef   = new TextFile();
            TextFile tfClass = null;
            bool     gtd     = type.IsGenericTypeDefinition;

            if (!gtd)
            {
                tfClass = tfDef.Add("Bridge.define(\"{0}\", {{", JSNameMgr.JsFullName(type))
                          .In();
                tfDef.Add("});");
            }
            else
            {
                args TNames = new args();
                foreach (var T in type.GetGenericArguments())
                {
                    TNames.Add(T.Name);
                }

                tfClass = tfDef.Add("Bridge.define(\"{0}\", function ({1}) {{ return {{", JSNameMgr.JsFullName(type), TNames.Format(args.ArgsFormat.OnlyList))
                          .In();
                tfDef.Add("}});");
            }

            // base type, interfaces
            {
                Type   vBaseType  = type.ValidBaseType();
                Type[] interfaces = type.GetInterfaces();
                if (vBaseType != null || interfaces.Length > 0)
                {
                    args a = new args();
                    // 这里baseType要在前面
                    // Bridge.js:
                    // var noBase = extend ? extend[0].$kind === "interface" : true;
                    //
                    // 可以忽略object基类
                    // Bridge.js:
                    // if (!extend) {
                    //     extend = [Object].concat(interfaces);
                    // }
                    if (vBaseType != null)
                    {
                        a.Add(JSNameMgr.JsFullName(vBaseType));
                    }
                    foreach (var i in interfaces)
                    {
                        a.Add(JSNameMgr.JsFullName(i));
                    }
                    tfClass.Add("inherits: [{0}],", a.ToString());
                }
            }

            if (type.IsInterface)
            {
                tfClass.Add("$kind: \"interface\",");
            }
            else if (type.IsValueType)
            {
                tfClass.Add("$kind: \"struct\",");
            }

            TextFile tfStatic = tfClass.Add("statics: {").In();

            tfStatic.Out().Add("},");

            TextFile tfInst = tfClass.Add("");

            if (type.IsValueType)
            {
                tfStatic.Add("getDefaultValue: function () {{ return new {0}(); }},", JSNameMgr.JsFullName(type));

                tfInst.Add("equals: function (o) {")
                .In()
                .Add("if (!Bridge.is(o, {0})) {{", JSNameMgr.JsFullName(type))
                .In()
                .Add("return false;")
                .BraceOut()
                .Add(() =>
                {
                    StringBuilder sb = new StringBuilder();
                    if (ti.Fields.Count == 0)
                    {
                        sb.Append("return true;");
                    }
                    else
                    {
                        for (int f = 0; f < ti.Fields.Count; f++)
                        {
                            if (ti.Fields[f].Ignored)
                            {
                                continue;
                            }

                            sb.AppendFormat("Bridge.equals(this.{0}, o.{0})", ti.Fields[f].member.Name);
                            if (f < ti.Fields.Count - 1)
                            {
                                sb.AppendFormat(" && ");
                            }
                            else
                            {
                                sb.Append(";");
                            }
                        }
                    }
                    return(new TextFile().Add(sb.ToString()).Ch);
                })
                .BraceOutComma();

                tfInst.Add("$clone: function (to) {")
                .In()
                .Add("return this; // don't clone").AddLine()
                .Add("var s = to || new {0}();", JSNameMgr.JsFullName(type))
                .Add(() =>
                {
                    TextFile tf = new TextFile();
                    for (int f = 0; f < ti.Fields.Count; f++)
                    {
                        if (ti.Fields[f].Ignored)
                        {
                            continue;
                        }

                        tf.Add("s.{0} = this.{0};", ti.Fields[f].member.Name);
                    }
                    return(tf.Ch);
                })
                .Add("return s;")
                .BraceOutComma();
            }

            BuildConstructors(tfInst, type, ti.Cons, slot);
            BuildFields(tfStatic, tfInst, type, ti.Fields, slot);
            BuildProperties(tfStatic, tfInst, type, ti.Pros, slot);
            BuildMethods(tfStatic, tfInst, type, ti.Methods, slot);

            return(tfDef);
        }