Exemplo n.º 1
0
        static TextFile GenEnum()
        {
            TextFile tf = new TextFile();

            string typeName = type.ToString();
            // tf.AddLine().Add("// {0}", typeName);

            // remove name space
            int lastDot = typeName.LastIndexOf('.');

            if (lastDot >= 0)
            {
                typeName = typeName.Substring(lastDot + 1);
            }

            if (typeName.IndexOf('+') >= 0)
            {
                return(null);
            }

            TextFile tfDef = tf.Add("Bridge.define(\"{0}\", {{", JSNameMgr.JsFullName(type)).In();

            tfDef.Add("$kind: \"enum\",");
            TextFile tfSta = tfDef.Add("statics: {").In();

            Type uType = Enum.GetUnderlyingType(type);

            FieldInfo[] fields = type.GetFields(BindingFlags.GetField | BindingFlags.Public | BindingFlags.Static);
            for (int i = 0; i < fields.Length; i++)
            {
                string v = "";
                if (uType == typeof(ulong))
                {
                    v = System.Convert.ToUInt64(fields[i].GetValue(null)).ToString();
                }
                else
                {
                    v = System.Convert.ToInt64(fields[i].GetValue(null)).ToString();
                }

                tfSta.Add("{0}: {1}{2}", fields[i].Name, v, i == fields.Length - 1 ? "" : ",");
            }
            tfSta.BraceOut();
            //tfDef.BraceOutSC();
            tfDef.Out().Add("});");

            return(tf);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
 public static string SharpKitClassName(Type type)
 {
     return(JSNameMgr.JsFullName(type));
 }