示例#1
0
        private static string GetTypeCheckerAndConverterLambdas(Type type)
        {
            string rtType = TX.ToWinRT(type);

            string[] jsToElementType = ToWinRT(type, TX.MainModel.Types.ContainsKey(type));

            string checkType;

            if (type == typeof(String))
            {
                // if the expected winRT type is String, allow non winrtWrapped object to be passed in and converted to Platform::String^
                checkType = "(!NodeRT::Utils::IsWinRtWrapper({0}))";
            }
            else
            {
                checkType = TypeCheck(type, TX.MainModel.Types.ContainsKey(type));
            }

            return
                ("                 [](Local<Value> value) -> bool {{\r\n" +
                 "                   return " + ReplaceBracketsWithDoubleBrackets(String.Format(checkType, "value")) + ";\r\n" +
                 "                 }},\r\n" +
                 "                 [](Local<Value> value) -> " + rtType + " {{\r\n" +
                 "                   return " + ReplaceBracketsWithDoubleBrackets(String.Format(jsToElementType[1], "value")) + ";\r\n" +
                 "                 }}");
        }
示例#2
0
        public static string[] ToReadOnlyKeyValueCollection(string collectionName, Type keyType, Type valueType)
        {
            string keyRtType = TX.ToWinRT(keyType);

            string[] keyTypeToJs  = ToJS(keyType, TX.MainModel.Types.ContainsKey(keyType));
            string   keyCheckType = TypeCheck(keyType, TX.MainModel.Types.ContainsKey(keyType));

            string[] jsToKeyElementType = ToWinRT(keyType, TX.MainModel.Types.ContainsKey(keyType));

            string valueRtType = TX.ToWinRT(valueType);

            string[] valueTypeToJs = ToJS(valueType, TX.MainModel.Types.ContainsKey(valueType));

            // note that double curl braces here are used because String.Format will
            string creatorFunction = "NodeRT::Collections::" + collectionName + "Wrapper<" + keyRtType + "," + valueRtType + ">::Create" + collectionName + "Wrapper({0}, \r\n" +
                                     "            [](" + keyRtType + " val) -> Local<Value> {{\r\n" +
                                     "              return " + ReplaceBracketsWithDoubleBrackets(String.Format(keyTypeToJs[1], "val")) + ";\r\n" +
                                     "            }},\r\n" +
                                     "            [](Local<Value> value) -> bool {{\r\n" +
                                     "              return " + ReplaceBracketsWithDoubleBrackets(String.Format(keyCheckType, "value")) + ";\r\n" +
                                     "            }},\r\n" +
                                     "            [](Local<Value> value) -> " + keyRtType + " {{\r\n" +
                                     "              return " + ReplaceBracketsWithDoubleBrackets(String.Format(jsToKeyElementType[1], "value")) + ";\r\n" +
                                     "            }},\r\n" +
                                     "            [](" + valueRtType + " val) -> Local<Value> {{\r\n" +
                                     "              return " + ReplaceBracketsWithDoubleBrackets(String.Format(valueTypeToJs[1], "val")) + ";\r\n" +
                                     "            }}\r\n" +
                                     "          )";

            return(new[] { "NodeRT::Collections::Create" + collectionName + "<" + keyRtType + "," + valueRtType + ">", creatorFunction });
        }
示例#3
0
        private static string[] GetKeyValueTypeAndLambdas(Type keyType, Type valueType)
        {
            string keyRtType   = TX.ToWinRT(keyType);
            string valueRtType = TX.ToWinRT(valueType);

            string templateVals = (keyType == typeof(String)) ? "<" + valueRtType + ">" : "<" + keyRtType + ", " + valueRtType + ">";

            return(new[] { templateVals,
                           GetTypeCheckerAndConverterLambdas(keyType) + ",\r\n" + GetTypeCheckerAndConverterLambdas(valueType) + "\r\n" });
        }
示例#4
0
        public static string ForEachParameter(IEnumerable <ParameterInfo> parameters, string format, int lenToRemove)
        {
            var sb = new System.Text.StringBuilder();

            foreach (var param in parameters)
            {
                sb.AppendFormat(format, param.Name, TX.ToWinRT(param.ParameterType));
            }
            if (sb.Length > 0)
            {
                sb.Remove(sb.Length - lenToRemove, lenToRemove);
            }
            return(sb.ToString());
        }
示例#5
0
        public static string ForEachEvent(IEnumerable <dynamic> events, string format, int lenToRemove)
        {
            var sb = new System.Text.StringBuilder();

            foreach (var e in events)
            {
                sb.AppendFormat(format, e.EventInfo.Name, Uncap(e.EventInfo.Name), TX.ToWinRT(e.EventInfo.EventHandlerType));
            }
            if (sb.Length > 0)
            {
                sb.Remove(sb.Length - lenToRemove, lenToRemove);
            }
            return(sb.ToString());
        }
示例#6
0
        public static string[] ToReadOnlyCollection(string collectionName, Type elementType)
        {
            string elementRtType = TX.ToWinRT(elementType);

            string[] elementTypeToJs = ToJS(elementType, TX.MainModel.Types.ContainsKey(elementType));

            // note that double curl braces here are used because String.Format will
            string creatorFunction = "NodeRT::Collections::" + collectionName + "Wrapper<" + elementRtType + ">::Create" + collectionName + "Wrapper({0}, \r\n" +
                                     "            [](" + elementRtType + " val) -> Local<Value> {{\r\n" +
                                     "              return " + ReplaceBracketsWithDoubleBrackets(String.Format(elementTypeToJs[1], "val")) + ";\r\n" +
                                     "            }}\r\n" +
                                     "          )";

            return(new[] { "NodeRT::Collections::Create" + collectionName + "<" + elementRtType + ">", creatorFunction });
        }
示例#7
0
        public static string ForEachType(IEnumerable <Type> types, string format, int lenToRemove)
        {
            var sb      = new System.Text.StringBuilder();
            var counter = 0;

            foreach (var type in types)
            {
                sb.AppendFormat(format, type.Name, TX.ToWinRT(type), counter);
                counter++;
            }
            if (sb.Length > 0)
            {
                sb.Remove(sb.Length - lenToRemove, lenToRemove);
            }
            return(sb.ToString());
        }
示例#8
0
        public static string TypeCheck(Type type, bool typeIsInNameSpace = false)
        {
            // The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single.
            if (type.IsPrimitive)
            {
                if (type == typeof(Byte) ||
                    type == typeof(SByte) ||
                    type == typeof(Int16) ||
                    type == typeof(UInt16) ||
                    type == typeof(Int32)
                    )
                {
                    return("{0}->IsInt32()");
                }
                else if (type == typeof(UInt32))
                {
                    return("{0}->IsUint32()");
                }
                else if (type == typeof(Int64) ||
                         type == typeof(UInt64))
                {
                    return("{0}->IsNumber()");
                }
                else if (type == typeof(Double) ||
                         type == typeof(Single) ||
                         type == typeof(IntPtr) ||
                         type == typeof(UIntPtr))
                {
                    return("{0}->IsNumber()");
                }
                else if (type == typeof(Boolean))
                {
                    return("{0}->IsBoolean()");
                }
                else if (type == typeof(Char))
                {
                    return("{0}->IsString()");
                }
            }

            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                return(TypeCheck(type.GetGenericArguments()[0], TX.MainModel.Types.ContainsKey(type.GetGenericArguments()[0])));
            }

            if (type == typeof(Guid))
            {
                return("NodeRT::Utils::IsGuid({0})");
            }

            if (type.FullName == "Windows.UI.Color")
            {
                return("NodeRT::Utils::IsColor({0})");
            }

            if (type.FullName == "Windows.Foundation.Point")
            {
                return("NodeRT::Utils::IsPoint({0})");
            }

            if (type.FullName == "Windows.Foundation.Size")
            {
                return("NodeRT::Utils::IsSize({0})");
            }

            if (type.FullName == "Windows.Foundation.Rect")
            {
                return("NodeRT::Utils::IsRect({0})");
            }

            if (type.IsEnum)
            {
                return("{0}->IsInt32()");
            }

            if (type == typeof(String))
            {
                return("{0}->IsString()");
            }

            if (type == typeof(DateTimeOffset) || type == typeof(DateTime))
            {
                return("{0}->IsDate()");
            }

            if (type == typeof(TimeSpan))
            {
                return("{0}->IsNumber()");
            }

            // this if clause should come after IsEnum, since an enum will also return true for IsValueType
            if (type.IsValueType && !(type.IsGenericType && type.FullName.StartsWith("System.Collections.Generic.KeyValuePair`2")))
            {
                return("Is" + type.Name + "JsObject({0})");
            }

            string wrapperTest = "NodeRT::Utils::IsWinRtWrapperOf<" + TX.ToWinRT(type, true) + ">({0})";

            string jsType;

            if (IsWinrtCollection(type, out jsType))
            {
                // in case the type is a winrt collection, we allow to pass a JS Object/Array instead of a wrapped collection
                return("(" + wrapperTest + " || " + "{0}->Is" + jsType + "())");
            }

            return(wrapperTest);
        }
示例#9
0
        public static string[] ToWinRT(Type type, bool typeIsInNameSpace = false)
        {
            // The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single.
            if (type.IsPrimitive)
            {
                if (type == typeof(Byte))
                {
                    return(new[] { "unsigned char", "static_cast<unsigned char>(Nan::To<int32_t>({0}).FromMaybe(0))" });
                }
                else if (type == typeof(SByte))
                {
                    return(new[] { "char", "static_cast<char>(Nan::To<int32_t>({0}).FromMaybe(0))" });
                }
                else if (type == typeof(Int16))
                {
                    return(new[] { "short", "static_cast<short>(Nan::To<int32_t>({0}).FromMaybe(0))" });
                }
                else if (type == typeof(UInt16))
                {
                    return(new[] { "unsigned short", "static_cast<unsigned short>(Nan::To<int32_t>({0}).FromMaybe(0))" });
                }
                else if (type == typeof(Int32))
                {
                    return(new[] { "int", "static_cast<int>(Nan::To<int32_t>({0}).FromMaybe(0))" });
                }
                else if (type == typeof(UInt32))
                {
                    return(new[] { "unsigned int", "static_cast<unsigned int>(Nan::To<uint32_t>({0}).FromMaybe(0))" });
                }
                else if (type == typeof(Int64))
                {
                    return(new[] { "__int64", "Nan::To<int64_t>({0}).FromMaybe(0)" });
                }
                else if (type == typeof(UInt64))
                {
                    return(new[] { "unsigned __int64", "static_cast<unsigned __int64>(Nan::To<int64_t>({0}).FromMaybe(0))" });
                }
                else if (type == typeof(IntPtr))
                {
                    return(new[] { "__int64", "Nan::To<int64_t>({0}).FromMaybe(0)" });
                }
                else if (type == typeof(UIntPtr))
                {
                    return(new[] { "unsigned __int64", "static_cast<unsigned __int64>(Nan::To<int64_t>({0}).FromMaybe(0))" });
                }
                else if (type == typeof(Single))
                {
                    return(new[] { "float", "static_cast<float>(Nan::To<double>({0}).FromMaybe(0.0))" });
                }
                else if (type == typeof(Double))
                {
                    return(new[] { "double", "Nan::To<double>({0}).FromMaybe(0.0)" });
                }
                else if (type == typeof(Boolean))
                {
                    return(new[] { "bool", "Nan::To<bool>({0}).FromMaybe(false)" });
                }
                else if (type == typeof(Char))
                {
                    return(new[] { "wchar_t", "NodeRT::Utils::GetFirstChar({0})" });
                }
            }

            if (type == typeof(String))
            {
                return(new[] { "Platform::String^", "ref new Platform::String(NodeRT::Utils::StringToWchar(v8::String::Value({0})))" });
            }

            if (type == typeof(DateTime) || type == typeof(DateTimeOffset))
            {
                return(new[] { "::Windows::Foundation::DateTime", "NodeRT::Utils::DateTimeFromJSDate({0})" });
            }

            if (type == typeof(Guid))
            {
                return(new[] { "::Platform::Guid", "NodeRT::Utils::GuidFromJs({0})" });
            }

            if (type == typeof(Exception))
            {
                return(new[] { "::Windows::Foundation::HResult", "NodeRT::Utils::HResultFromJsInt32(Nan::To<int32_t>({0}).FromMaybe(0))" });
            }

            if (type == typeof(TimeSpan))
            {
                return(new[] { "::Windows::Foundation::TimeSpan", "NodeRT::Utils::TimeSpanFromMilli(Nan::To<int64_t>({0}).FromMaybe(0))" });
            }

            if (type.FullName == "Windows.UI.Color")
            {
                return(new[] { "::Windows::UI::Color", "NodeRT::Utils::ColorFromJs({0})" });
            }

            if (type.FullName == "Windows.Foundation.Point")
            {
                return(new[] { "::Windows::Foundation::Point", "NodeRT::Utils::PointFromJs({0})" });
            }

            if (type.FullName == "Windows.Foundation.Rect")
            {
                return(new[] { "::Windows::Foundation::Rect", "NodeRT::Utils::RectFromJs({0})" });
            }

            if (type.FullName == "Windows.Foundation.Size")
            {
                return(new[] { "::Windows::Foundation::Size", "NodeRT::Utils::SizeFromJs({0})" });
            }

            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                var genericArgConversionInfo = ToWinRT(type.GetGenericArguments()[0]);
                return(new[] { "::Platform::IBox<" + genericArgConversionInfo[0] + ">^", "ref new ::Platform::Box<" + genericArgConversionInfo[0] + ">(" + genericArgConversionInfo[1] + ")" });
            }

            var winrtFullType = TX.ToWinRT(type);

            if (type.IsEnum)
            {
                return(new[] { winrtFullType, "static_cast<" + winrtFullType + ">(Nan::To<int32_t>({0}).FromMaybe(0))" });
            }

            // this if clause should come after IsEnum, since an enum will also return true for IsValueType
            if (type.IsValueType && !(type.IsGenericType && type.FullName.StartsWith("System.Collections.Generic.KeyValuePair`2")))
            {
                return(new[] { winrtFullType, type.Name + "FromJsObject({0})" });
            }


            string jsType;

            if (IsWinrtCollection(type, out jsType))
            {
                return(new[] { winrtFullType, JsToWinrtCollection(type, jsType, winrtFullType, typeIsInNameSpace) });
            }

            if (typeIsInNameSpace)
            {
                return(new[] { winrtFullType, "Unwrap" + type.Name + "({0})" });
            }

            return(new[] { winrtFullType, "dynamic_cast<" + winrtFullType + ">(NodeRT::Utils::GetObjectInstance({0}))" });
        }
示例#10
0
 public static string[] GetValueTypeAndLambdas(Type valueType)
 {
     return(new[] { "<" + TX.ToWinRT(valueType) + ">", GetTypeCheckerAndConverterLambdas(valueType) + "\r\n" });
 }