Пример #1
0
 private static string GetName(Type type, ClassTranslationInfo declaringTypeInfo)
 {
     {
         // PageAttribute
         var pageAttribute = type.GetCustomAttribute <PageAttribute>();
         if (pageAttribute != null)
         {
             return(pageAttribute.ModuleShortName);
         }
     }
     {
         // ModuleAttribute
         var moduleAttribute = type.GetCustomAttribute <PyModuleAttribute>();
         if (moduleAttribute != null)
         {
             return(moduleAttribute.ModuleShortName);
         }
     }
     if (declaringTypeInfo != null && declaringTypeInfo.ModuleName != null)
     {
         return(declaringTypeInfo.ModuleName.Name);
     }
     return(type.FullName?.Replace(".", "_").Replace("+", "__").Replace("<", "__").Replace(">", "__")
            ?? type.Name);
 }
Пример #2
0
        public PhpCodeModuleName(Type type, AssemblyTranslationInfo assemblyInfo, ClassTranslationInfo declaringTypeInfo)
        {
            if (assemblyInfo == null)
            {
                throw new ArgumentNullException("assemblyInfo");
            }
            if ((object)type == null)
            {
                throw new ArgumentNullException("type");
            }
            if ((object)type.DeclaringType != null && declaringTypeInfo == null)
            {
                throw new ArgumentNullException("declaringTypeInfo");
            }

            _assemblyInfo = assemblyInfo;

            {
                if (type.FullName == null)
                {
                    Name = type.Name;
                }
                else
                {
                    Name = type.FullName.Replace(".", "_").Replace("+", "__").Replace("<", "__").Replace(">", "__");
                }
                // take module name from parent, this can be overrided if nested class is decorated with attributes
                if (declaringTypeInfo != null && declaringTypeInfo.ModuleName != null)
                {
                    Name = declaringTypeInfo.ModuleName.Name;
                }
                var ats = type.GetCustomAttributes(false);

                #region ModuleAttribute
                {
                    var moduleAttribute = type.GetCustomAttribute <ModuleAttribute>();
                    if (moduleAttribute != null)
                    {
                        Name = moduleAttribute.ModuleShortName;
                        OptionalIncludePathPrefix = moduleAttribute.IncludePathPrefix;
                    }
                }
                #endregion
                #region PageAttribute
                {
                    var pageAttribute = type.GetCustomAttribute <PageAttribute>();
                    if (pageAttribute != null)
                    {
                        Name = pageAttribute.ModuleShortName;
                    }
                }
                #endregion
            }
        }
Пример #3
0
 public PyCodeModuleName(Type type, bool isExternalModule, ClassTranslationInfo declaringTypeInfo)
 {
     IsExternalModule = isExternalModule;
     if ((object)type == null)
     {
         throw new ArgumentNullException(nameof(type));
     }
     if ((object)type.DeclaringType != null && declaringTypeInfo == null)
     {
         throw new ArgumentNullException(nameof(declaringTypeInfo));
     }
     Name = Pn(GetName(type, declaringTypeInfo));
 }
Пример #4
0
        private void TranslateClass_AddModuleRequests(ClassTranslationInfo classTranslationInfo, PyCodeModule pyModule)
        {
            var moduleCodeRequests = new List <DependsOnModuleCodeRequest>();
            var codeRequests       = (pyModule as ICodeRelated).GetCodeRequests().ToArray();
            {
                var classCodeRequests = (from request in codeRequests.OfType <ClassCodeRequest>()
                                         select request.ClassName.FullName)
                                        .Distinct()
                                        .ToArray();

                foreach (var req in classCodeRequests)
                {
                    var m = Info.ClassTranslations.Values.Where(i => i.PyName.FullName == req).ToArray();
                    if (m.Length != 1)
                    {
                        throw new NotSupportedException();
                    }
                    var mm = m[0];
                    if (mm.DontIncludeModuleForClassMembers)
                    {
                        continue;
                    }
                    var includeModule = mm.IncludeModule;
                    if (includeModule == null || mm.ModuleName == pyModule.ModuleName)
                    {
                        continue;
                    }
                    var h = new DependsOnModuleCodeRequest(includeModule, "class request: " + req);
                    moduleCodeRequests.Add(h);
                }
            }
            {
                // converts DependsOnModuleCodeRequest
                var moduleRequests = (from i in codeRequests.OfType <DependsOnModuleCodeRequest>()
                                      where i.ModuleName != null
                                      select i).Concat(moduleCodeRequests).ToArray();
                if (moduleRequests.Any())
                {
                    foreach (var mReq in moduleRequests)
                    {
                        var ati = Info.GetOrMakeTranslationInfo(classTranslationInfo.Type.Assembly);
                        if (mReq.ModuleName == null || mReq.ModuleName == pyModule.ModuleName)
                        {
                            continue;
                        }
                        var usedAliasWhileImport = AppendImportModuleCodeRequest(mReq.ModuleName, pyModule, ati);
                        mReq.UseAlias = usedAliasWhileImport;
                    }
                }
            }
        }
Пример #5
0
        private HttpResponse ProcessRequest(HttpRequest req, Type type)
        {
            var y = new HttpResponse();
            ClassTranslationInfo ci = _translationInfo.GetOrMakeTranslationInfo(type);

            if (ci.PageMethod == null)
            {
                throw new Exception(string.Format("Page method not found in type {0}", type.FullName));
            }
            {
                // prepare sandbox
                Response.RuntimeResponse = y;
                Request.RuntimeRequest   = req;
                Script.Get    = req.Get;
                Script.Post   = req.Post;
                Script.Server = req.Server;
            }

            Action phpMain = (Action)Delegate.CreateDelegate(typeof(Action), ci.PageMethod);

            phpMain(); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Wywołanie metody
            // y.Echo("Mock output");
            return(y);
        }
Пример #6
0
 public void SetClassName(PhpQualifiedName phpClassName, ClassTranslationInfo classTi)
 {
     _className = phpClassName;
     ClassTi    = classTi;
 }
Пример #7
0
        private void TranslateClass(
            ClassTranslationInfo classTranslationInfo,
            FullInterfaceDeclaration[] interfaces,
            FullClassDeclaration[]     classes)
        {
            if (classTranslationInfo.Skip)
            {
                Debug.Write("");
            }
            var pyModule = GetOrMakeModuleByName(classTranslationInfo.ModuleName);

            // var assemblyTI = _info.GetOrMakeTranslationInfo(_info.CurrentAssembly);
            PyQualifiedName GetBaseClassName()
            {
                var netBaseType = classTranslationInfo.Type.BaseType;

                if ((object)netBaseType == null || netBaseType == typeof(object))
                {
                    return(PyQualifiedName.Empty);
                }
                var baseTypeTranslationInfo = _state.Principles.GetOrMakeTranslationInfo(netBaseType);

                if (baseTypeTranslationInfo.Skip)
                {
                    return(PyQualifiedName.Empty);
                }
                return(_state.Principles.GetPyType(netBaseType, true, null));
            }

            var pyClass = classTranslationInfo.ExportAsModule
                ? null
                : pyModule.FindOrCreateClass(classTranslationInfo.PyName, GetBaseClassName());

            Console.WriteLine(classTranslationInfo.ModuleName);
            _state.Principles.CurrentType = classTranslationInfo.Type;
            try
            {
                _state.Principles.CurrentAssembly = _state.Principles.CurrentType.Assembly;
                var fullname = classTranslationInfo.Type.FullName;
                var srcs     = classTranslationInfo.Type.IsInterface
                    ? interfaces
                               .Where(q => q.FullName == fullname)
                               .Select(q => q.ClassDeclaration)
                               .OfType <IClassOrInterface>().ToArray()
                    : classes
                               .Where(q => q.FullName == fullname)
                               .Select(q => q.ClassDeclaration)
                               .OfType <IClassOrInterface>().ToArray();
                var members = srcs.SelectMany(i => i.Members).ToArray();

                {
                    var constructors = members.OfType <ConstructorDeclaration>().ToArray();
                    if (pyClass == null && constructors.Length > 0)
                    {
                        throw new Exception("Class exported as module cannot have constructors");
                    }
                    if (constructors.Length > 1)
                    {
                        throw new Exception("Python supports only one constructor per class");
                    }
                    if (constructors.Any())
                    {
                        TranslateConstructor(pyClass, constructors.First());
                    }
                }

                foreach (var methodDeclaration in members.OfType <MethodDeclaration>())
                {
                    TranslateMethod(pyModule, pyClass, methodDeclaration);
                }
                foreach (var pDeclaration in members.OfType <CsharpPropertyDeclaration>())
                {
                    TranslateProperty(pyModule, pyClass, pDeclaration);
                }
                foreach (var constDeclaration in members.OfType <FieldDeclaration>())
                {
                    TranslateField(pyModule, pyClass, constDeclaration);
                }
            }
            finally
            {
                _state.Principles.CurrentType = null;
            }

            {
                if (classTranslationInfo.IsPage)
                {
                    var mti = MethodTranslationInfo.FromMethodInfo(classTranslationInfo.PageMethod,
                                                                   classTranslationInfo);
                    var callMain = new PyMethodCallExpression(mti.ScriptName);
                    callMain.SetClassName(
                        classTranslationInfo.PyName,
                        mti
                        );
                    pyModule.BottomCode.Statements.Add(new PyExpressionStatement(callMain));
                }
            }
            TranslateClass_AddModuleRequests(classTranslationInfo, pyModule);
        }
 public void SetClassName(PhpQualifiedName phpClassName, ClassTranslationInfo classTi )
 {
     _className = phpClassName;
     _classTi = classTi;
     
 }