Esempio n. 1
0
        public void AddTypeReference(string moduleName, TypeBindingInfo typeBindingInfo, string[] elements, string jsName)
        {
            var ns   = CodeGenUtils.NormalizeEx(elements, jsName);
            var type = typeBindingInfo.type;

            _runtime.AddTypeReference(_moduleReg, type, register => typeBindingInfo.DoReflectBind(register), ns);
        }
Esempio n. 2
0
        /// <summary>
        /// 构造一个指定泛型参数的JS完整类型名
        /// </summary>
        public string MakeGenericJSFullTypeName(string templateArgs)
        {
            var name = CodeGenUtils.Concat(".", this.jsNamespace, this.jsPureName);

            // var name = string.IsNullOrEmpty(this.jsNamespace) ? this.jsPureName : this.jsNamespace + "." + this.jsPureName;
            return(string.Format("{0}<{1}>, ", name, templateArgs));
        }
Esempio n. 3
0
        public void AddTypeReference(string moduleName, TypeBindingInfo typeBindingInfo, string[] elements, string jsName)
        {
            var typeDB    = _runtime.GetTypeDB();
            var ns        = CodeGenUtils.NormalizeEx(elements, jsName);
            var type      = typeBindingInfo.type;
            var crossbind = typeBindingInfo.crossbind;

            _runtime.AddTypeReference(_moduleReg, type, typeDB.GetDynamicTypeBind(type, crossbind), ns);
        }
        public void AddModuleEntry(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 jsNamespace   = CodeGenUtils.Concat(", ", CodeGenUtils.ConcatAsLiteral(", ", elements), $"\"{CodeGenUtils.Normalize(typeBindingInfo.tsTypeNaming.jsName)}\"");

            AddStatement($"{runtimeVarName}.AddTypeReference({moduleVarName}, typeof({csType}), {csNamespace}.{csBindingName}.Bind, {jsNamespace});");
        }
Esempio n. 5
0
        public static int GetTSParameterCount(ParameterInfo[] parameters)
        {
            var len  = parameters.Length;
            var argc = len;

            for (var i = 0; i < len; i++)
            {
                var parameterType = parameters[i].ParameterType;
                if (CodeGenUtils.IsSpecialParameterType(parameterType))
                {
                    argc--;
                }
            }
            return(argc);
        }
Esempio n. 6
0
 protected override void EndInvokeBinding()
 {
     this.cg.cs.AppendLine("var val = NewBridgeClassObject(ctx, new_target, o, magic, {0});", CodeGenUtils.ToLiteral(this.disposable));
 }
Esempio n. 7
0
        // 写入默认构造函数 (struct 无参构造)
        private void WriteDefaultConstructorBinding()
        {
            var decalringTypeName = this.cg.bindingManager.GetCSTypeFullName(this.bindingInfo.decalringType);

            this.cg.cs.AppendLine("var o = new {0}();", decalringTypeName);
            this.cg.cs.AppendLine("var val = NewBridgeClassObject(ctx, new_target, o, magic, {0});", CodeGenUtils.ToLiteral(this.disposable));
            this.cg.cs.AppendLine("return val;");

            this.cg.tsDeclare.AppendLine($"{this.bindingInfo.jsName}()");
        }
Esempio n. 8
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);
            }

            string tsMethodRename;

            if (!this.cg.bindingManager.GetTSMethodRename(method, out tsMethodRename))
            {
                tsMethodRename = bindingInfo.jsName;
            }

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

            // if (method.Name.StartsWith("op_"))
            if (bindingInfo is OperatorBindingInfo)
            {
                prefix += "// js_op_overloading: ";
            }
            else
            {
                // var baseType = typeBindingInfo.type.BaseType;
                // if (baseType != null)
                // {
                //     //TODO: 需要检查 TypeBindingInfo 对此的命名修改
                //     if (baseType.GetMethods().Where(baseMethodInfo => baseMethodInfo.Name == tsMethodRename).Count() != 0)
                //     {
                //         prefix += "// @ts-ignore" + this.cg.tsDeclare.newline + this.cg.tsDeclare.tabString;
                //     }
                // }
            }

            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)
                {
                    ArrayUtility.RemoveAt(ref parameters, 0);
                }

                for (int i = 0, len = parameters.Length; i < len;)
                {
                    var parameter     = parameters[i];
                    var parameterType = parameter.ParameterType;
                    if (CodeGenUtils.IsSpecialParameterType(parameterType))
                    {
                        // 剔除 JSContext, JSRuntime
                        ArrayUtility.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);
        }
Esempio n. 9
0
        public TSTypeNaming(BindingManager bindingManager, Type type, TypeTransform typeTransform)
        {
            this.type = type;

            var naming          = typeTransform?.GetTypeNaming() ?? type.Name;
            var indexOfTypeName = naming.LastIndexOf('.');

            if (indexOfTypeName >= 0)
            {
                // 指定的命名中已经携带了"."
                var indexOfInnerTypeName = naming.IndexOf('+');
                if (indexOfInnerTypeName >= 0)
                {
                    this.jsModule = naming.Substring(0, indexOfInnerTypeName);
                    var rightName = naming.Substring(indexOfInnerTypeName + 1);
                    var lastIndexOfInnerTypeName = rightName.LastIndexOf('+');
                    if (lastIndexOfInnerTypeName >= 0)
                    {
                        this.jsNamespace = rightName.Substring(0, lastIndexOfInnerTypeName);
                        this.jsName      = rightName.Substring(lastIndexOfInnerTypeName + 1);
                    }
                    else
                    {
                        this.jsNamespace = "";
                        this.jsName      = rightName;
                    }
                }
                else
                {
                    this.jsModule    = naming.Substring(0, indexOfTypeName);
                    this.jsNamespace = "";
                    this.jsName      = naming.Substring(indexOfTypeName + 1);
                }

                var gArgIndex = this.jsName.IndexOf("<");
                if (gArgIndex < 0)
                {
                    this.jsPureName = this.jsName;
                }
                else
                {
                    this.jsPureName = this.jsName.Substring(0, gArgIndex);
                }
            }
            else
            {
                this.jsModule    = type.Namespace ?? "";
                this.jsNamespace = "";

                // 处理内部类层级
                var declaringType = type.DeclaringType;
                while (declaringType != null)
                {
                    this.jsNamespace = string.IsNullOrEmpty(this.jsNamespace) ? declaringType.Name : $"{declaringType.Name}.{this.jsNamespace}";
                    declaringType    = declaringType.DeclaringType;
                }

                if (type.IsGenericType)
                {
                    this.jsName     = naming.Contains("`") ? naming.Substring(0, naming.IndexOf('`')) : naming;
                    this.jsPureName = this.jsName;

                    if (type.IsGenericTypeDefinition)
                    {
                        if (!naming.Contains("<"))
                        {
                            this.jsName += "<";
                            var gArgs = type.GetGenericArguments();

                            for (var i = 0; i < gArgs.Length; i++)
                            {
                                this.jsName += gArgs[i].Name;
                                if (i != gArgs.Length - 1)
                                {
                                    this.jsName += ", ";
                                }
                            }
                            this.jsName += ">";
                        }
                    }
                    else
                    {
                        foreach (var gp in type.GetGenericArguments())
                        {
                            this.jsName += "_" + gp.Name;
                        }
                    }
                }
                else
                {
                    this.jsName = naming;

                    //TODO: 整理 jsPureName 的取值流程 (对于泛型中的嵌套的处理等)
                    var gArgIndex = this.jsName.IndexOf("<");
                    if (gArgIndex < 0)
                    {
                        this.jsPureName = this.jsName;
                    }
                    else
                    {
                        this.jsPureName = this.jsName.Substring(0, gArgIndex);
                    }
                }
            }

            if (string.IsNullOrEmpty(this.jsNamespace))
            {
                this.jsModuleAccess       = this.jsName;
                this.jsModuleImportAccess = this.jsPureName;
                this.jsLocalName          = "";
            }
            else
            {
                var i = this.jsNamespace.IndexOf('.');
                this.jsModuleAccess       = i < 0 ? this.jsNamespace : this.jsNamespace.Substring(0, i);
                this.jsModuleImportAccess = this.jsModuleAccess;
                this.jsLocalName          = CodeGenUtils.Concat(".", i < 0 ? "" : this.jsNamespace.Substring(i + 1), this.jsName);
            }

            if (this.jsModuleAccess.EndsWith("[]"))
            {
                this.jsModuleAccess = this.jsModuleAccess.Substring(0, this.jsModuleAccess.Length - 2);
            }

            this.jsDepth    = this.jsModuleAccess.Split('.').Length;
            this.jsFullName = CodeGenUtils.Concat(".", jsModule, jsNamespace, jsName);
        }
Esempio n. 10
0
        public string GetTSTypeFullName(Type type, bool isOut)
        {
            if (type == null || type == typeof(void))
            {
                return("void");
            }

            if (type.IsByRef)
            {
                if (isOut)
                {
                    return($"{this.cg.bindingManager.GetDefaultTypePrefix()}Out<{GetTSTypeFullName(type.GetElementType())}>");
                }
                return($"{this.cg.bindingManager.GetDefaultTypePrefix()}Ref<{GetTSTypeFullName(type.GetElementType())}>");
                // return GetTSTypeFullName(type.GetElementType());
            }

            List <string> names;

            if (this.cg.bindingManager.GetTSTypeNameMap(type, out names))
            {
                return(names.Count > 1 ? $"({String.Join(" | ", names)})" : names[0]);
            }

            if (type == typeof(Array))
            {
                return("Array<any>");
            }

            if (type == typeof(ScriptPromise))
            {
                return("Promise<void>");
            }

            if (type.IsSubclassOf(typeof(ScriptPromise)))
            {
                if (type.IsGenericType)
                {
                    var gt = type.GetGenericArguments()[0];
                    return("Promise<" + GetTSTypeFullName(gt) + ">");
                }
                return("Promise<any>");
            }

            if (type.IsArray)
            {
                var elementType = type.GetElementType();
                var tsFullName  = GetTSTypeFullName(elementType);
                var rank        = type.GetArrayRank();

                if (rank == 1)
                {
                    return("Array<" + tsFullName + ">");
                }
                return("Array<" + tsFullName + ", " + rank + ">");
            }

            var info = this.cg.bindingManager.GetExportedType(type);

            if (info != null)
            {
                var gDef = GetTSGenericTypeDefinition(type);
                if (!string.IsNullOrEmpty(gDef))
                {
                    return(gDef);
                }

                var tsTypeNaming = info.tsTypeNaming;
                if (tsTypeNaming.jsModule == this.tsModule)
                {
                    return(CodeGenUtils.Concat(".", tsTypeNaming.jsModuleAccess, tsTypeNaming.jsLocalName));
                }

                var localAlias = GetAlias(type);
                if (localAlias != null)
                {
                    return(CodeGenUtils.Concat(".", localAlias, tsTypeNaming.jsLocalName));
                }
                return(tsTypeNaming.jsFullName);
            }

            if (type.BaseType == typeof(MulticastDelegate))
            {
                var delegateBindingInfo = this.cg.bindingManager.GetDelegateBindingInfo(type);
                if (delegateBindingInfo != null)
                {
                    var nargs     = delegateBindingInfo.parameters.Length;
                    var ret       = GetTSTypeFullName(delegateBindingInfo.returnType);
                    var t_arglist = (nargs > 0 ? ", " : "") + GetTSArglistTypes(delegateBindingInfo.parameters, false);
                    var v_arglist = GetTSArglistTypes(delegateBindingInfo.parameters, true);
                    // return $"{CodeGenerator.NamespaceOfInternalScriptTypes}.Delegate{nargs}<{ret}{t_arglist}> | (({v_arglist}) => {ret})";
                    return($"({v_arglist}) => {ret}");
                }
            }

            if (type.IsGenericType)
            {
                if (type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    var gArgs   = type.GetGenericArguments();
                    var gArgsTS = GetTSTypeFullName(gArgs[0]);
                    return($"{this.cg.bindingManager.GetDefaultTypePrefix()}Nullable<{gArgsTS}>");
                }
            }
            else
            {
                if (type.IsGenericParameter)
                {
                    return(type.Name);
                }
            }

            return("any");
        }