示例#1
0
        public TypeRef GetTypeFromReference(MetadataReader mr, TypeReferenceHandle handle, byte rawTypeKind)
        {
            var type_ref   = mr.GetTypeReference(handle);
            var @namespace = mr.GetString(type_ref.Namespace);
            var name       = mr.GetString(type_ref.Name);

            if (!type_ref.ResolutionScope.IsNil)
            {
                if (type_ref.ResolutionScope.Kind == HandleKind.ModuleDefinition)
                {
                    var api = this.api_namespace_map[@namespace];
                    return(new TypeRef.User(api.top_level_types[api.type_name_fqn_map[name]]));
                }
                else if (type_ref.ResolutionScope.Kind == HandleKind.TypeReference)
                {
                    TypeGenInfo enclosing_type_ref = this.resolveEnclosingType(mr, (TypeReferenceHandle)type_ref.ResolutionScope);
                    Debug.Assert(@namespace.Length == 0, "I thought all nested types had empty namespaces");
                    return(new TypeRef.User(enclosing_type_ref.getNestedTypeByName(name)));
                }
                else if (type_ref.ResolutionScope.Kind == HandleKind.AssemblyReference)
                {
                    // This occurs for System.Guid, not sure if it is supposed to
                    if (@namespace == "System")
                    {
                        if (name == "Guid")
                        {
                            return(TypeRef.Guid.Instance);
                        }
                    }
                    else if (@namespace == "Windows.System")
                    {
                        if (name == "DispatcherQueueController")
                        {
                            return(new TypeRef.UnhandledClrType(@namespace, name));
                        }
                    }
                    throw new InvalidOperationException();
                }
            }
            throw new InvalidDataException(string.Format(
                                               "unexpected type reference resolution scope IsNil {0} and/or Kind {1}",
                                               type_ref.ResolutionScope.IsNil,
                                               type_ref.ResolutionScope.Kind));
        }
示例#2
0
        private TypeGenInfo resolveEnclosingType(MetadataReader mr, TypeReferenceHandle type_ref_handle)
        {
            var type_ref   = mr.GetTypeReference(type_ref_handle);
            var @namespace = mr.GetString(type_ref.Namespace);
            var name       = mr.GetString(type_ref.Name);

            if (!type_ref.ResolutionScope.IsNil)
            {
                if (type_ref.ResolutionScope.Kind == HandleKind.ModuleDefinition)
                {
                    Api api = this.api_namespace_map[@namespace];
                    return(api.top_level_types[api.type_name_fqn_map[name]]);
                }

                if (type_ref.ResolutionScope.Kind == HandleKind.TypeReference)
                {
                    TypeGenInfo enclosing_type_ref = this.resolveEnclosingType(mr, (TypeReferenceHandle)type_ref.ResolutionScope);
                    Debug.Assert(@namespace.Length == 0, "I thought all nested types had empty namespaces");
                    return(enclosing_type_ref.getNestedTypeByName(name));
                }
            }

            throw new NotImplementedException("unexpected ResolutionScope for enclosing type");
        }
示例#3
0
        public static void GenerateCode(string saveTo, bool tsOnly = false)
        {
            filters = Configure.GetFilters();
            var configure = Configure.GetConfigureByTags(new List <string>()
            {
                "Puerts.BindingAttribute",
                "Puerts.BlittableCopyAttribute",
                "Puerts.TypingAttribute",
            });

            var genTypes = configure["Puerts.BindingAttribute"].Select(kv => kv.Key)
                           .Where(o => o is Type)
                           .Cast <Type>()
                           .Where(t => !t.IsGenericTypeDefinition);

            var blittableCopyTypes = new HashSet <Type>(configure["Puerts.BlittableCopyAttribute"].Select(kv => kv.Key)
                                                        .Where(o => o is Type)
                                                        .Cast <Type>()
                                                        .Where(t => t.IsValueType && !t.IsPrimitive)
                                                        .Distinct());

            var tsTypes = configure["Puerts.TypingAttribute"].Select(kv => kv.Key)
                          .Where(o => o is Type)
                          .Cast <Type>()
                          .Where(t => !t.IsGenericTypeDefinition)
                          .Concat(genTypes)
                          .Distinct();

            using (var jsEnv = new JsEnv())
            {
                var templateGetter = jsEnv.Eval <Func <string, Func <object, string> > >("require('puerts/gencode/main.js')");
                var wrapRender     = templateGetter("type.tpl");

                if (!tsOnly)
                {
                    var typeGenInfos = new List <TypeGenInfo>();
                    foreach (var type in genTypes)
                    {
                        if (type.IsEnum || type.IsArray || (IsDelegate(type) && type != typeof(Delegate)))
                        {
                            continue;
                        }
                        TypeGenInfo typeGenInfo = ToTypeGenInfo(type);
                        typeGenInfo.BlittableCopy = blittableCopyTypes.Contains(type);
                        typeGenInfos.Add(typeGenInfo);
                        string filePath    = saveTo + typeGenInfo.WrapClassName + ".cs";
                        string fileContext = wrapRender(typeGenInfo);
                        using (StreamWriter textWriter = new StreamWriter(filePath, false, Encoding.UTF8))
                        {
                            textWriter.Write(fileContext);
                            textWriter.Flush();
                        }
                    }

                    var autoRegisterRender = templateGetter("autoreg.tpl");
                    using (StreamWriter textWriter = new StreamWriter(saveTo + "AutoStaticCodeRegister.cs", false, Encoding.UTF8))
                    {
                        string fileContext = autoRegisterRender(typeGenInfos.ToArray());
                        textWriter.Write(fileContext);
                        textWriter.Flush();
                    }
                }

                var typingRender = templateGetter("typing.tpl");
                using (StreamWriter textWriter = new StreamWriter(saveTo + "Typing/csharp/index.d.ts", false, Encoding.UTF8))
                {
                    string fileContext = typingRender(ToTypingGenInfo(tsTypes));
                    textWriter.Write(fileContext);
                    textWriter.Flush();
                }
            }
        }
示例#4
0
 public User(TypeGenInfo info)
 {
     this.info = info;
 }