示例#1
0
        public EventBindingInfo(TypeBindingInfo typeBindingInfo, EventInfo eventInfo)
        {
            this.declaringType = typeBindingInfo.type;
            this.eventInfo     = eventInfo;

            if (this.isStatic)
            {
                this.name = "BindStaticEvent_" + eventInfo.Name;
            }
            else
            {
                this.name = "BindEvent_" + eventInfo.Name;
            }

            this.regName = typeBindingInfo.bindingManager.GetNamingAttribute(eventInfo);
        }
示例#2
0
        protected void WriteTSAllVariants(TypeBindingInfo typeBindingInfo, MethodBaseBindingInfo <T> bindingInfo)
        {
            var variants = bindingInfo.variants;

            //NOTE: 如果产生了无法在 typescript 中声明的方法, 则作标记, 并输出一条万能声明
            //      [key: string]: any
            foreach (var variantKV in variants)
            {
                foreach (var method in variantKV.Value.plainMethods)
                {
                    WriteTSDeclaration(typeBindingInfo, method.method, bindingInfo, method.isExtension);
                }

                foreach (var method in variantKV.Value.varargMethods)
                {
                    WriteTSDeclaration(typeBindingInfo, method.method, bindingInfo, method.isExtension);
                }
            }
        }
示例#3
0
 private void GenerateInternal(TypeBindingInfo typeBindingInfo)
 {
     using (var tsMod = new TSModuleCodeGen(this, typeBindingInfo))
     {
         _currentTSModule = tsMod;
         using (new TSNamespaceCodeGen(this, typeBindingInfo.tsTypeNaming.jsNamespace))
         {
             if (typeBindingInfo.IsEnum)
             {
                 using (new EnumCodeGen(this, typeBindingInfo))
                 {
                 }
             }
             else
             {
                 using (new ClassCodeGen(this, typeBindingInfo))
                 {
                 }
             }
         }
         _currentTSModule = null;
     }
 }
示例#4
0
        // 生成类型绑定
        public void Generate(TypeBindingInfo typeBindingInfo)
        {
            this.cs.enabled        = (typeBindingInfo.bindingFlags & TypeBindingFlags.BindingCode) != 0 && (typeBindingFlags & TypeBindingFlags.BindingCode) != 0;
            this.tsDeclare.enabled = (typeBindingInfo.bindingFlags & TypeBindingFlags.TypeDefinition) != 0 && (typeBindingFlags & TypeBindingFlags.TypeDefinition) != 0;
            var defs = string.Join(" && ", from def in typeBindingInfo.requiredDefines select def);

            using (new CSDebugCodeGen(this))
            {
                using (new EditorOnlyCodeGen(this, defs))
                {
                    using (new CSPlatformCodeGen(this, TypeBindingFlags.Default))
                    {
                        using (new CSTopLevelCodeGen(this, typeBindingInfo))
                        {
                            using (new CSNamespaceCodeGen(this, this.bindingManager.prefs.ns))
                            {
                                GenerateInternal(typeBindingInfo);
                            }
                        }
                    }
                }
            }
        }
示例#5
0
        public ClassCodeGen(CodeGenerator cg, TypeBindingInfo typeBindingInfo)
            : base(cg, typeBindingInfo)
        {
            this.cg.AppendJSDoc(this.typeBindingInfo.type);
            var transform        = this.typeBindingInfo.transform;
            var prefix           = this.typeBindingInfo.tsTypeNaming.topLevel ? "declare " : "";
            var superBindingInfo = this.cg.bindingManager.GetSuperTypeBindingInfo(this.typeBindingInfo);
            var super            = superBindingInfo != null?this.cg.currentTSModule.GetTSTypeFullName(superBindingInfo.type) : "";

            var interfaces  = this.cg.currentTSModule.GetTSInterfacesName(this.typeBindingInfo.type);
            var implements  = "";
            var jsClassName = this.typeBindingInfo.tsTypeNaming.jsName;
            var jsClassType = "";

            if (typeBindingInfo.requiredDefines.Count != 0)
            {
                var defs = string.Join(", ", from def in typeBindingInfo.requiredDefines select $"\"{def}\"");
                this.cg.tsDeclare.AppendLine($"@{this.cg.bindingManager.GetDefaultTypePrefix()}RequiredDefines({defs})");
            }

            if (typeBindingInfo.type.IsInterface)
            {
                jsClassType = "interface";

                if (string.IsNullOrEmpty(interfaces))
                {
                    if (!string.IsNullOrEmpty(super))
                    {
                        implements += $" extends {super}"; // something wrong
                    }
                }
                else
                {
                    implements += $" extends {interfaces}";

                    if (!string.IsNullOrEmpty(super))
                    {
                        implements += $", {super}"; // something wrong
                    }
                }
            }
            else
            {
                jsClassType = typeBindingInfo.type.IsAbstract ? "abstract class" : "class";

                if (!string.IsNullOrEmpty(super))
                {
                    implements += $" extends {super}";
                }

                if (!string.IsNullOrEmpty(interfaces))
                {
                    implements += $" implements {interfaces}";
                }
            }

            this.cg.tsDeclare.AppendLine($"{prefix}{jsClassType} {jsClassName}{implements} {{");
            this.cg.tsDeclare.AddTabLevel();

            // 生成函数体
            // 构造函数
            if (this.typeBindingInfo.constructors.available)
            {
                if (this.typeBindingInfo.transform.csConstructorOverride == null)
                {
                    using (new PInvokeGuardCodeGen(cg, typeof(Native.JSCFunctionMagic)))
                    {
                        using (new BindingConstructorDeclareCodeGen(cg, this.typeBindingInfo.constructors.csBindName))
                        {
                            using (new TryCatchGuradCodeGen(cg))
                            {
                                using (new ConstructorCodeGen(cg, this.typeBindingInfo))
                                {
                                }
                            }
                        }
                    }
                }

                using (new TSConstructorCodeGen(cg, this.typeBindingInfo, this.typeBindingInfo.constructors))
                {
                }
            }

            // 非静态成员方法
            foreach (var kv in this.typeBindingInfo.methods)
            {
                var methodBindingInfo = kv.Value;
                var jscOverride       = transform.GetCSMethodOverrideBinding(methodBindingInfo.jsName);
                if (jscOverride == null)
                {
                    using (new PInvokeGuardCodeGen(cg))
                    {
                        using (new BindingFuncDeclareCodeGen(cg, methodBindingInfo.csBindName))
                        {
                            using (new TryCatchGuradCodeGen(cg))
                            {
                                using (new MethodCodeGen(cg, this.typeBindingInfo, methodBindingInfo))
                                {
                                }
                            }
                        }
                    }
                }

                using (new TSMethodCodeGen <MethodInfo>(cg, this.typeBindingInfo, methodBindingInfo))
                {
                }
            }

            //TODO: C# 抽象类可以不提供方法实现, d.ts 需要补充声明
            // if (this.bindingInfo.type.IsAbstract && !this.bindingInfo.type.IsInterface)
            // {
            // }
            // 静态成员方法
            if (!typeBindingInfo.type.IsGenericTypeDefinition)
            {
                foreach (var kv in this.typeBindingInfo.staticMethods)
                {
                    var methodBindingInfo = kv.Value;
                    if (methodBindingInfo._cfunc != null)
                    {
                        continue;
                    }

                    var jscOverride = transform.GetCSMethodOverrideBinding(methodBindingInfo.jsName);
                    if (jscOverride == null)
                    {
                        using (new PInvokeGuardCodeGen(cg))
                        {
                            using (new BindingFuncDeclareCodeGen(cg, methodBindingInfo.csBindName))
                            {
                                using (new TryCatchGuradCodeGen(cg))
                                {
                                    using (new MethodCodeGen(cg, this.typeBindingInfo, methodBindingInfo))
                                    {
                                    }
                                }
                            }
                        }
                    }

                    using (new TSMethodCodeGen <MethodInfo>(cg, typeBindingInfo, methodBindingInfo))
                    {
                    }
                }
            }

            if (!typeBindingInfo.type.IsGenericTypeDefinition)
            {
                foreach (var operatorBindingInfo in this.typeBindingInfo.operators)
                {
                    using (new PInvokeGuardCodeGen(cg))
                    {
                        using (new BindingFuncDeclareCodeGen(cg, operatorBindingInfo.csBindName))
                        {
                            using (new TryCatchGuradCodeGen(cg))
                            {
                                using (new OperatorCodeGen(cg, this.typeBindingInfo, operatorBindingInfo))
                                {
                                }
                            }
                        }
                    }

                    using (new TSOperatorCodeGen(cg, typeBindingInfo, operatorBindingInfo))
                    {
                    }
                }
            }

            // 所有附加方法
            if (transform != null)
            {
                transform.ForEachAdditionalTSMethodDeclaration(decl =>
                {
                    this.cg.tsDeclare.AppendLine(decl);
                });
            }

            // 所有属性
            foreach (var kv in this.typeBindingInfo.properties)
            {
                var propertyBindingInfo = kv.Value;

                // 静态
                if (propertyBindingInfo.isStatic)
                {
                    // 可读属性
                    if (propertyBindingInfo.staticPair.getterName != null)
                    {
                        using (new PInvokeGuardCodeGen(cg, typeof(JSGetterCFunction)))
                        {
                            using (new BindingGetterFuncDeclareCodeGen(cg, propertyBindingInfo.staticPair.getterName))
                            {
                                using (new TryCatchGuradCodeGen(cg))
                                {
                                    using (new PropertyGetterCodeGen(cg, propertyBindingInfo))
                                    {
                                    }
                                }
                            }
                        }
                    }

                    // 可写属性
                    if (propertyBindingInfo.staticPair.setterName != null)
                    {
                        using (new PInvokeGuardCodeGen(cg, typeof(JSSetterCFunction)))
                        {
                            using (new BindingSetterFuncDeclareCodeGen(cg, propertyBindingInfo.staticPair.setterName))
                            {
                                using (new TryCatchGuradCodeGen(cg))
                                {
                                    using (new PropertySetterCodeGen(cg, propertyBindingInfo))
                                    {
                                    }
                                }
                            }
                        }
                    }
                }
                else // if (propertyBindingInfo.instancePair.IsValid())
                {
                    // 非静态
                    // 可读属性
                    if (propertyBindingInfo.instancePair.getterName != null)
                    {
                        using (new PInvokeGuardCodeGen(cg, typeof(JSGetterCFunction)))
                        {
                            using (new BindingGetterFuncDeclareCodeGen(cg, propertyBindingInfo.instancePair.getterName))
                            {
                                using (new TryCatchGuradCodeGen(cg))
                                {
                                    using (new PropertyGetterCodeGen(cg, propertyBindingInfo))
                                    {
                                    }
                                }
                            }
                        }
                    }
                    // 可写属性
                    if (propertyBindingInfo.instancePair.setterName != null)
                    {
                        using (new PInvokeGuardCodeGen(cg, typeof(JSSetterCFunction)))
                        {
                            using (new BindingSetterFuncDeclareCodeGen(cg, propertyBindingInfo.instancePair.setterName))
                            {
                                using (new TryCatchGuradCodeGen(cg))
                                {
                                    using (new PropertySetterCodeGen(cg, propertyBindingInfo))
                                    {
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // 所有字段
            foreach (var kv in this.typeBindingInfo.fields)
            {
                var fieldBindingInfo = kv.Value;

                // 可读
                if (fieldBindingInfo.getterName != null)
                {
                    using (new PInvokeGuardCodeGen(cg, typeof(JSGetterCFunction)))
                    {
                        using (new BindingGetterFuncDeclareCodeGen(cg, fieldBindingInfo.getterName))
                        {
                            using (new TryCatchGuradCodeGen(cg))
                            {
                                using (new FieldGetterCodeGen(cg, fieldBindingInfo))
                                {
                                }
                            }
                        }
                    }
                }

                // 可写
                if (fieldBindingInfo.setterName != null)
                {
                    using (new PInvokeGuardCodeGen(cg, typeof(JSSetterCFunction)))
                    {
                        using (new BindingSetterFuncDeclareCodeGen(cg, fieldBindingInfo.setterName))
                        {
                            using (new TryCatchGuradCodeGen(cg))
                            {
                                using (new FieldSetterCodeGen(cg, fieldBindingInfo))
                                {
                                }
                            }
                        }
                    }
                }
            }

            // 所有事件
            foreach (var kv in this.typeBindingInfo.events)
            {
                var eventBindingInfo = kv.Value;
                using (new PInvokeGuardCodeGen(cg))
                {
                    using (new BindingFuncDeclareCodeGen(cg, eventBindingInfo.name))
                    {
                        using (new TryCatchGuradCodeGen(cg))
                        {
                            using (new EventOperationCodeGen(cg, eventBindingInfo))
                            {
                            }
                        }
                    }
                }
            }

            // 所有委托 (Field/Property)
            foreach (var kv in this.typeBindingInfo.delegates)
            {
                var delegateBindingInfo = kv.Value;
                using (new PInvokeGuardCodeGen(cg))
                {
                    using (new BindingFuncDeclareCodeGen(cg, delegateBindingInfo.name))
                    {
                        using (new TryCatchGuradCodeGen(cg))
                        {
                            using (new DelegateOperationCodeGen(cg, delegateBindingInfo))
                            {
                            }
                        }
                    }
                }
            }
        }
示例#6
0
        public void AddModuleEntry(string moduleName, string runtimeVarName, string moduleVarName, TypeBindingInfo typeBindingInfo)
        {
            var csType           = this.cg.bindingManager.GetCSTypeFullName(typeBindingInfo.type);
            var csNamespace      = this.cg.bindingManager.prefs.ns;
            var csBindingName    = typeBindingInfo.csBindingName;
            var elements         = typeBindingInfo.tsTypeNaming.jsNamespace.Split('.');
            var jsNameNormalized = CodeGenUtils.Normalize(typeBindingInfo.tsTypeNaming.jsName);
            var jsNamespace      = CodeGenUtils.Concat(", ", CodeGenUtils.ConcatAsLiteral(", ", elements), $"\"{jsNameNormalized}\"");

            AddStatement($"{runtimeVarName}.AddTypeReference({moduleVarName}, typeof({csType}), {csNamespace}.{csBindingName}.Bind, {jsNamespace});");
            this.cg.bindingManager.callback.AddTypeReference(moduleName, typeBindingInfo, elements, jsNameNormalized);
        }
示例#7
0
 public CSTopLevelCodeGen(CodeGenerator cg, TypeBindingInfo typeBindingInfo)
 {
     this.cg = cg;
     this.AppendCommonHead();
     // this.cg.typescript.AppendLine("// {0} {1}", Environment.UserName, this.cg.bindingManager.dateTime);
 }
示例#8
0
 public bool OnTypeGenerating(TypeBindingInfo typeBindingInfo, int current, int total)
 {
     return(false);
 }
示例#9
0
 public TSMethodCodeGen(CodeGenerator cg, TypeBindingInfo typeBindingInfo, MethodBaseBindingInfo <T> bindingInfo)
     : base(cg)
 {
     this.bindingInfo = bindingInfo;
     WriteTSAllVariants(typeBindingInfo, this.bindingInfo);
 }
示例#10
0
        protected List <ParameterInfo> WriteTSDeclaration(TypeBindingInfo typeBindingInfo, T method, MethodBaseBindingInfo <T> bindingInfo, bool isExtension)
        {
            var    refParameters = new List <ParameterInfo>();
            string tsMethodDeclaration;

            this.cg.AppendJSDoc(method);

            if (typeBindingInfo.transform.GetTSMethodDeclaration(method, out tsMethodDeclaration) ||
                this.cg.bindingManager.GetTSMethodDeclaration(method, out tsMethodDeclaration))
            {
                this.cg.tsDeclare.AppendLine(tsMethodDeclaration);
                return(refParameters);
            }

            var tsMethodRename = bindingInfo.jsName;
            var isRaw          = method.IsDefined(typeof(JSCFunctionAttribute), false);
            //TODO: 需要处理参数类型归并问题, 因为如果类型没有导入 ts 中, 可能会在声明中出现相同参数列表的定义
            //      在 MethodVariant 中创建每个方法对应的TS类型名参数列表, 完全相同的不再输出
            var prefix = "";

            // if (method.Name.StartsWith("op_"))
            if (bindingInfo is OperatorBindingInfo)
            {
                prefix += "// js_op_overloading: ";
            }

            if (method.IsStatic && !isExtension)
            {
                prefix += "static ";
            }

            this.cg.tsDeclare.Append($"{prefix}{tsMethodRename}(");

            if (this.cg.bindingManager.prefs.verboseLog)
            {
                this.cg.bindingManager.Info($"WriteTSDeclaration: {method.Name} <isExtension: {isExtension}> => {tsMethodRename} {this.cg.tsDeclare.enabled}");
            }

            if (isRaw)
            {
                this.cg.tsDeclare.AppendL("...uncertain: any[]): any /* uncertain */");
                this.cg.tsDeclare.AppendLine();
            }
            else
            {
                var parameters = method.GetParameters();
                if (isExtension)
                {
                    CodeGenUtils.RemoveAt(ref parameters, 0);
                }

                for (int i = 0, len = parameters.Length; i < len;)
                {
                    var parameter     = parameters[i];
                    var parameterType = parameter.ParameterType;
                    if (Binding.Values.IsContextualType(parameterType))
                    {
                        // 剔除 JSContext, JSRuntime
                        CodeGenUtils.RemoveAt(ref parameters, i);
                        len--;
                    }
                    // else if (parameter.IsOut)
                    // {
                    //     ArrayUtility.RemoveAt(ref parameters, i);
                    //     len--;
                    //     refParameters.Add(parameter);
                    // }
                    else
                    {
                        // if (parameterType.IsByRef)
                        // {
                        //     refParameters.Add(parameter);
                        // }
                        i++;
                    }
                }

                for (int i = 0, len = parameters.Length; i < len; i++)
                {
                    var parameter        = parameters[i];
                    var parameter_prefix = "";
                    var parameterType    = parameter.ParameterType;

                    if (parameter.IsDefined(typeof(ParamArrayAttribute), false) && i == parameters.Length - 1)
                    {
                        var elementType      = parameterType.GetElementType();
                        var elementTS        = this.cg.currentTSModule.GetTSTypeFullName(elementType);
                        var parameterVarName = this.cg.bindingManager.GetTSVariable(parameter);
                        this.cg.tsDeclare.AppendL($"{parameter_prefix}...{parameterVarName}: {elementTS}[]");
                    }
                    else
                    {
                        var parameterTS      = this.cg.currentTSModule.GetTSTypeFullName(parameter.ParameterType, parameter.IsOut);
                        var parameterVarName = this.cg.bindingManager.GetTSVariable(parameter);
                        this.cg.tsDeclare.AppendL($"{parameter_prefix}{parameterVarName}: {parameterTS}");
                    }

                    if (i != parameters.Length - 1)
                    {
                        this.cg.tsDeclare.AppendL(", ");
                    }
                }
                this.cg.tsDeclare.AppendL($")");
                WriteTSReturn(method, refParameters);
            }

            return(refParameters);
        }
示例#11
0
 public void AddTypeReference(string moduleName, TypeBindingInfo typeBindingInfo, string[] elements, string jsName)
 {
 }
示例#12
0
 public virtual void OnPostGenerateType(BindingManager bindingManager, TypeBindingInfo bindingInfo)
 {
 }
示例#13
0
 public TSOperatorCodeGen(CodeGenerator cg, TypeBindingInfo typeBindingInfo, OperatorBindingInfo bindingInfo)
     : base(cg)
 {
     this.bindingInfo = bindingInfo;
     WriteTSAllVariants(typeBindingInfo, this.bindingInfo);
 }
 public void AddTypeReference(string moduleName, TypeBindingInfo typeBindingInfo)
 {
     _runtime.AddTypeReference(_moduleReg, typeBindingInfo.type, register => typeBindingInfo.DoReflectBind(register, _moduleReg), typeBindingInfo.tsTypeNaming.jsFullNameForReflectBind);
 }
示例#15
0
        public FieldBindingInfo(TypeBindingInfo typeBindingInfo, FieldInfo fieldInfo)
        {
            do
            {
                if (fieldInfo.IsLiteral)
                {
                    try
                    {
                        var cv     = fieldInfo.GetRawConstantValue();
                        var cvType = cv.GetType();
                        if (cvType == typeof(string))
                        {
                            constantValue = $"\"{cv}\"";
                            break;
                        }

                        if (cvType == typeof(int) ||
                            cvType == typeof(uint) ||
                            cvType == typeof(byte) ||
                            cvType == typeof(sbyte) ||
                            cvType == typeof(short) ||
                            cvType == typeof(ushort) ||
                            cvType == typeof(bool))
                        {
                            constantValue = $"{cv}";
                            break;
                        }

                        if (cvType == typeof(float))
                        {
                            var fcv = (float)cv;
                            if (!float.IsInfinity(fcv) &&
                                !float.IsNaN(fcv))
                            {
                                constantValue = $"{cv}";
                                break;
                            }
                        }

                        // if (cvType.IsPrimitive && cvType.IsValueType)
                        // {
                        //     constantValue = $"{cv}";
                        //     break;
                        // }
                    }
                    catch (Exception)
                    {
                    }
                }

                if (fieldInfo.IsStatic)
                {
                    this.getterName = "BindStaticRead_" + fieldInfo.Name;
                    if (!fieldInfo.IsInitOnly && !fieldInfo.IsLiteral)
                    {
                        this.setterName = "BindStaticWrite_" + fieldInfo.Name;
                    }
                }
                else
                {
                    this.getterName = "BindRead_" + fieldInfo.Name;
                    if (!fieldInfo.IsInitOnly && !fieldInfo.IsLiteral)
                    {
                        this.setterName = "BindWrite_" + fieldInfo.Name;
                    }
                }
            } while (false);

            this.regName   = typeBindingInfo.bindingManager.GetNamingAttribute(fieldInfo);
            this.fieldInfo = fieldInfo;
        }