Ejemplo 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);
        }
Ejemplo 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));
        }
        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 jsNamespace   = CodeGenUtils.Concat(", ", CodeGenUtils.ConcatAsLiteral(", ", typeBindingInfo.tsTypeNaming.jsNamespaceSlice), $"\"{typeBindingInfo.tsTypeNaming.jsNameNormalized}\"");

            AddStatement($"{runtimeVarName}.AddTypeReference({moduleVarName}, typeof({csType}), {csNamespace}.{csBindingName}.Bind, {jsNamespace});");
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
 protected override void EndInvokeBinding()
 {
     this.cg.cs.AppendLine("var val = NewBridgeClassObject(ctx, new_target, o, magic, {0});", CodeGenUtils.ToLiteral(this.disposable));
 }
Ejemplo n.º 6
0
        // 写入默认构造函数 (struct 无参构造)
        private void WriteDefaultConstructorBinding()
        {
            var decalringTypeName = this.cg.bindingManager.GetCSTypeFullName(this.methodBindingInfo.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.methodBindingInfo.jsName}()");
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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");
        }