Exemplo n.º 1
0
        public string GetDelegateTypeName(HandleStyle style)
        {
            StringBuilder name = new StringBuilder();

            if (ReturnType == null || ReturnType.JniType == "void")
            {
                name.Append("JniAction_");
            }
            else
            {
                name.Append("JniFunc_");
            }
            name.Append("JNIEnvPtr");
            for (int i = 0; i < Parameters.Length; i++)
            {
                var pt = Parameters [i].Type.GetMarshalType(style, isReturn: false);
                if (pt == "va_list")
                {
                    return(null);
                }

                name.AppendFormat("_").Append(pt.FixupType());
            }

            string rt = GetMarshalReturnType(style);

            if (rt != "void")
            {
                name.Append("_").Append(rt.FixupType());
            }

            return(name.ToString());
        }
Exemplo n.º 2
0
        public override string[] VerifyParameter(HandleStyle style, string variable)
        {
            var variableName = variable.StartsWith("@")
                                ? variable.Substring(1)
                                : variable;

            switch (style)
            {
            case HandleStyle.SafeHandle:
            case HandleStyle.JIIntPtr:
            case HandleStyle.JIIntPtrPinvokeWithErrors:
                return(new [] {
                    string.Format("if ({0} == null)", variable),
                    string.Format("\tthrow new ArgumentNullException (\"{0}\");", variableName),
                    string.Format("if (!{0}.IsValid)", variable),
                    string.Format("\tthrow new ArgumentException (\"Handle value is not valid.\", \"{0}\");", variableName),
                    string.Format("System.Diagnostics.Debug.Assert ({0}{1}.IsStatic);", IsStatic ? "" : "!", variableName),
                });

            case HandleStyle.XAIntPtr:
                return(new[] {
                    string.Format("if ({0} == IntPtr.Zero)", variable),
                    string.Format("\tthrow new ArgumentException (\"Handle value cannot be null.\", \"{0}\");", variableName),
                });
            }
            return(new string [0]);
        }
Exemplo n.º 3
0
 static void WriteSection(TextWriter o, HandleStyle style, string define, string specificNamespace)
 {
     o.WriteLine("#if {0}", define);
     o.WriteLine("namespace");
     o.WriteLine("#if _NAMESPACE_PER_HANDLE");
     o.WriteLine("\t{0}", specificNamespace);
     o.WriteLine("#else");
     o.WriteLine("\tJava.Interop");
     o.WriteLine("#endif");
     o.WriteLine("{");
     o.WriteLine();
     if (style != HandleStyle.JIIntPtrPinvokeWithErrors)
     {
         GenerateDelegates(o, style);
         o.WriteLine();
     }
     GenerateTypes(o, style);
     o.WriteLine();
     switch (style)
     {
     case HandleStyle.JIIntPtr:
     case HandleStyle.SafeHandle:
     case HandleStyle.XAIntPtr:
         GenerateJniNativeInterfaceInvoker(o, style);
         break;
     }
     o.WriteLine("}");
     o.WriteLine("#endif  // {0}", define);
 }
Exemplo n.º 4
0
        static void GenerateNativeMethods(TextWriter o, HandleStyle style)
        {
            o.WriteLine("\tstatic partial class NativeMethods {");
            o.WriteLine();
            o.WriteLine("\t\tconst string JavaInteropLib = \"java-interop\";");
            foreach (var entry in JNIEnvEntries)
            {
                if (entry.Parameters == null)
                {
                    continue;
                }
                if (entry.IsPrivate || entry.CustomWrapper)
                {
                    continue;
                }

                o.WriteLine();
                o.WriteLine("\t\t[DllImport (JavaInteropLib, CallingConvention=CallingConvention.Cdecl, CharSet=CharSet.Ansi)]");
                o.WriteLine("\t\tinternal static extern unsafe {0} {1} (IntPtr jnienv{2}{3}{4});",
                            entry.ReturnType.GetMarshalType(style, isReturn: true, isPinvoke: true),
                            GetPinvokeName(entry.Name),
                            entry.Throws ? ", out IntPtr thrown" : "",
                            entry.Parameters.Length != 0 ? ", " : "",
                            string.Join(", ", entry.Parameters.Select(p => string.Format("{0} {1}", p.Type.GetMarshalType(style, isReturn: false, isPinvoke: true), Escape(p.Name)))));
            }
            o.WriteLine("\t}");
            o.WriteLine();
        }
Exemplo n.º 5
0
 static void GenerateDelegates(TextWriter o, HandleStyle style)
 {
     created_delegates = new HashSet <string> ();
     foreach (var e in JNIEnvEntries)
     {
         CreateDelegate(o, e, style);
     }
 }
Exemplo n.º 6
0
 public override string GetManagedType(HandleStyle style, bool isReturn, bool isPinvoke)
 {
     if (isPinvoke && managed == JniArgumentValue)
     {
         return("IntPtr");
     }
     return(managed);
 }
Exemplo n.º 7
0
 public string GetMarshalReturnType(HandleStyle style)
 {
     if (ReturnType == null)
     {
         return("void");
     }
     return(ReturnType.GetMarshalType(style, isReturn: true));
 }
Exemplo n.º 8
0
 public override string[] GetHandleCreationLogStatements(HandleStyle style, string method, string variable)
 {
     if (method == "NewLocalRef" || method == "ExceptionOccurred")
     {
         return(base.GetHandleCreationLogStatements(style, method, variable));
     }
     return(new[] {
         string.Format("JniEnvironment.LogCreateLocalRef ({0});", variable),
     });
 }
Exemplo n.º 9
0
        public override string GetManagedToMarshalExpression(HandleStyle style, string variable)
        {
            var value = base.GetManagedToMarshalExpression(style, variable);

            if (managed == JniArgumentValue)
            {
                value = "(IntPtr) " + value;
            }
            return(value);
        }
Exemplo n.º 10
0
        public override string[] VerifyParameter(HandleStyle style, string variable)
        {
            var variableName = variable.StartsWith("@")
                                ? variable.Substring(1)
                                : variable;

            return(new[] {
                string.Format("if ({0} == null)", variable),
                string.Format("\tthrow new ArgumentNullException (\"{0}\");", variableName),
            });
        }
Exemplo n.º 11
0
 public override string GetManagedToMarshalExpression(HandleStyle style, string variable)
 {
     switch (style)
     {
     case HandleStyle.SafeHandle:
     case HandleStyle.JIIntPtr:
     case HandleStyle.JIIntPtrPinvokeWithErrors:
         return(string.Format("{0}.ID", variable));
     }
     return(variable);
 }
Exemplo n.º 12
0
        static void GenerateJniNativeInterfaceInvoker(TextWriter o, HandleStyle style)
        {
            o.WriteLine("\tpartial class JniEnvironmentInvoker {");
            o.WriteLine();
            o.WriteLine("\t\tinternal JniNativeInterfaceStruct env;");
            o.WriteLine();
            o.WriteLine("\t\tpublic unsafe JniEnvironmentInvoker (JniNativeInterfaceStruct* p)");
            o.WriteLine("\t\t{");
            o.WriteLine("\t\t\tenv = *p;");

            foreach (var e in JNIEnvEntries)
            {
                if (!e.Prebind)
                {
                    continue;
                }
                var d = e.GetDelegateTypeName(style);
                if (e.GetDelegateTypeName(style) == null)
                {
                    continue;
                }
                o.WriteLine("\t\t\t{0}", Initialize(e, "", d));
            }

            o.WriteLine("\t\t}");
            o.WriteLine();

            foreach (var e in JNIEnvEntries)
            {
                var d = e.GetDelegateTypeName(style);
                if (d == null)
                {
                    continue;
                }
                o.WriteLine();
                if (e.Prebind)
                {
                    o.WriteLine("\t\tpublic readonly {0} {1};\n", d, e.Name);
                }
                else
                {
                    o.WriteLine("\t\t{0} _{1};", d, e.Name);
                    o.WriteLine("\t\tpublic {0} {1} {{", d, e.Name);
                    o.WriteLine("\t\t\tget {");
                    o.WriteLine("\t\t\t\tif (_{0} == null)\n\t\t\t\t\t{1}", e.Name, Initialize(e, "_", d));
                    o.WriteLine("\t\t\t\treturn _{0};\n\t\t\t}}", e.Name);
                    o.WriteLine("\t\t}");
                }
            }

            o.WriteLine("\t}");
        }
Exemplo n.º 13
0
        public override string GetManagedType(HandleStyle style, bool isReturn, bool isPinvoke)
        {
            switch (style)
            {
            case HandleStyle.SafeHandle:
            case HandleStyle.JIIntPtr:
            case HandleStyle.JIIntPtrPinvokeWithErrors:
                return("JniObjectReference");

            case HandleStyle.XAIntPtr:
                return("IntPtr");
            }
            return("TODO");
        }
Exemplo n.º 14
0
        public override string GetMarshalType(HandleStyle style, bool isReturn, bool isPinvoke)
        {
            switch (style)
            {
            case HandleStyle.SafeHandle:
                return(isReturn ? safeType : "JniReferenceSafeHandle");

            case HandleStyle.JIIntPtr:
            case HandleStyle.JIIntPtrPinvokeWithErrors:
            case HandleStyle.XAIntPtr:
                return("jobject");
            }
            return(null);
        }
Exemplo n.º 15
0
        public override string GetManagedType(HandleStyle style, bool isReturn)
        {
            switch (style)
            {
            case HandleStyle.SafeHandle:
            case HandleStyle.JIIntPtr:
            case HandleStyle.JIIntPtrPinvokeWithErrors:
                return(type);

            case HandleStyle.XAIntPtr:
                return("IntPtr");
            }
            return("TODO_" + style);;
        }
Exemplo n.º 16
0
        static void RaiseException(TextWriter o, JniFunction entry, HandleStyle style)
        {
            if (!entry.Throws)
            {
                return;
            }

            o.WriteLine();
            o.WriteLine("\t\t\tException __e = JniEnvironment.GetExceptionForLastThrowable ({0});",
                        style == HandleStyle.JIIntPtrPinvokeWithErrors ? "thrown" : "");
            o.WriteLine("\t\t\tif (__e != null)");
            o.WriteLine("\t\t\t\tExceptionDispatchInfo.Capture (__e).Throw ();");
            o.WriteLine();
        }
Exemplo n.º 17
0
        public override string[] VerifyParameter(HandleStyle style, string variable)
        {
            if (managed != "IntPtr")
            {
                return(new string [0]);
            }
            var variableName = variable.StartsWith("@")
                                ? variable.Substring(1)
                                : variable;

            return(new[] {
                string.Format("if ({0} == IntPtr.Zero)", variable),
                string.Format("\tthrow new ArgumentException (\"'{0}' must not be IntPtr.Zero.\", \"{0}\");", variableName),
            });
        }
Exemplo n.º 18
0
        static void CreateDelegate(TextWriter o, JniFunction entry, HandleStyle style)
        {
            StringBuilder builder        = new StringBuilder();
            bool          has_char_array = false;

            string name = entry.GetDelegateTypeName(style);

            if (name == null)
            {
                return;
            }

            builder.AppendFormat("\tunsafe delegate {0} {1} ({2} env", entry.GetMarshalReturnType(style), name, GetJniEnvironmentPointerType(style));
            for (int i = 0; i < entry.Parameters.Length; i++)
            {
                if (i >= 0)
                {
                    builder.Append(", ");
                    builder.AppendFormat("{0} {1}",
                                         entry.Parameters [i].Type.GetMarshalType(style, isReturn: false, isPinvoke: true),
                                         Escape(entry.Parameters [i].Name));
                }

                var ptype = entry.Parameters [i].Type.GetManagedType(style, isReturn: false, isPinvoke: true);
                if (ptype == "va_list")
                {
                    return;
                }
                if (ptype == "char[]")
                {
                    has_char_array = true;
                }
            }
            builder.Append(");");

            if (created_delegates.Contains(name))
            {
                return;
            }

            created_delegates.Add(name);
            if (entry.Name == "NewString" || has_char_array)
            {
                o.WriteLine("\t[UnmanagedFunctionPointerAttribute (CallingConvention.Cdecl, CharSet=CharSet.Unicode)]");
            }
            o.WriteLine(builder.ToString());
        }
Exemplo n.º 19
0
        public override string[] GetMarshalToManagedStatements(HandleStyle style, string variable, JniFunction entry)
        {
            switch (style)
            {
            case HandleStyle.SafeHandle:
            case HandleStyle.JIIntPtr:
            case HandleStyle.JIIntPtrPinvokeWithErrors:
                return(new [] {
                    string.Format("return new JniObjectReference ({0}, {1});", variable, refType),
                });

            case HandleStyle.XAIntPtr:
                return(new[] {
                    string.Format("return {0};", variable),
                });
            }
            return(new string [0]);
        }
Exemplo n.º 20
0
        public override string[] GetMarshalToManagedStatements(HandleStyle style, string variable, JniFunction entry)
        {
            switch (style)
            {
            case HandleStyle.SafeHandle:
            case HandleStyle.JIIntPtr:
            case HandleStyle.JIIntPtrPinvokeWithErrors:
                return(new[] {
                    string.Format("if ({0} == IntPtr.Zero)", variable),
                    string.Format("\treturn null;"),
                    string.Format("return new {0} ({1}, {2}, {3}, isStatic: {4});", type, entry.Parameters [1].Name, entry.Parameters [2].Name, variable, IsStatic ? "true" : "false"),
                });

            case HandleStyle.XAIntPtr:
                return(new[] {
                    string.Format("return {0};", variable),
                });
            }
            return(new string [0]);
        }
Exemplo n.º 21
0
        public override string[] GetMarshalToManagedStatements(HandleStyle style, string variable, JniFunction entry)
        {
            switch (style)
            {
            case HandleStyle.SafeHandle:
            case HandleStyle.XAIntPtr:
                return(new [] {
                    string.Format("JniEnvironment.LogCreateLocalRef ({0});", variable),
                    string.Format("return {0};", variable),
                });

            case HandleStyle.JIIntPtr:
            case HandleStyle.JIIntPtrPinvokeWithErrors:
                return(new [] {
                    string.Format("JniEnvironment.LogCreateLocalRef ({0});", variable),
                    string.Format("return new JniObjectReference ({0}, JniObjectReferenceType.Local);", variable),
                });
            }
            return(null);
        }
Exemplo n.º 22
0
        static void NullCheckParameters(TextWriter o, ParamInfo[] ps, HandleStyle style)
        {
            bool haveChecks = false;

            for (int i = 0; i < ps.Length; i++)
            {
                var p = ps [i];
                if (p.CanBeNull)
                {
                    continue;
                }
                var pn = Escape(p.Name);
                foreach (var line in p.Type.VerifyParameter(style, pn))
                {
                    haveChecks = true;
                    o.WriteLine("\t\t\t{0}", line);
                }
            }
            if (haveChecks)
            {
                o.WriteLine();
            }
        }
Exemplo n.º 23
0
        public IpcHandleType(JArray json)
        {
            switch (json[1][0].Value <string>())
            {
            case "copy":
                Style = HandleStyle.Copy;
                break;

            case "move":
                Style = HandleStyle.Move;
                break;

            case "unknown":
                Style = HandleStyle.Unknown;
                break;

            case { } x: throw new NotImplementedException(x);
            }
            if (json[1].Count() >= 2)
            {
                Type = Parse(json[1][1].Value <JArray>());
            }
        }
Exemplo n.º 24
0
        static void GenerateTypes(TextWriter o, HandleStyle style)
        {
            if (style == HandleStyle.JIIntPtrPinvokeWithErrors)
            {
                GenerateNativeMethods(o, style);
            }

            var visibilities = new Dictionary <string, string> {
                { ArrayOperationsCategory, "public" },
                { ClassesCategory, "public" },
                { ExceptionsCategory, "public" },
                { InstanceFieldsCategory, "public" },
                { InstanceMethodsCategory, "public" },
                { MonitorOperationsCategory, "public" },
                { NIOSupportCategory, "public" },
                { ObjectOperationsCategory, "public" },
                { ReferencesCatgeory, "public" },
                { StaticFieldsCategory, "public" },
                { StaticMethodsCategory, "public" },
                { StringOperationsCategory, "public" },
            };

            o.WriteLine("\tpartial class JniEnvironment {");
            foreach (var t in JNIEnvEntries
                     .Select(e => e.DeclaringType ?? "JniEnvironment")
                     .Distinct()
                     .OrderBy(t => t))
            {
                string visibility;
                if (!visibilities.TryGetValue(t, out visibility))
                {
                    visibility = "internal";
                }
                GenerateJniEnv(o, t, visibility, style);
            }
            o.WriteLine("\t}");
        }
Exemplo n.º 25
0
        public override string[] VerifyParameter(HandleStyle style, string variable)
        {
            var variableName = variable.StartsWith("@")
                                ? variable.Substring(1)
                                : variable;

            switch (style)
            {
            case HandleStyle.SafeHandle:
            case HandleStyle.JIIntPtr:
            case HandleStyle.JIIntPtrPinvokeWithErrors:
                return(new [] {
                    string.Format("if (!{0}.IsValid)", variable),
                    string.Format("\tthrow new ArgumentException (\"Handle must be valid.\", \"{0}\");", variableName),
                });

            case HandleStyle.XAIntPtr:
                return(new [] {
                    string.Format("if ({0} == IntPtr.Zero)", variable),
                    string.Format("\tthrow new ArgumentException (\"`{0}` must not be IntPtr.Zero.\", \"{0}\");", variableName),
                });
            }
            return(new string [0]);
        }
Exemplo n.º 26
0
 public override string GetManagedToMarshalExpression(HandleStyle style, string variable)
 {
     return(string.Format("((int) {0})", variable));
 }
Exemplo n.º 27
0
 public override string[] GetMarshalToManagedStatements(HandleStyle style, string variable, JniFunction entry)
 {
     return(new string[] {
         string.Format("return (JniReleaseArrayElementsMode) {0};", variable),
     });
 }
Exemplo n.º 28
0
 public override string GetManagedType(HandleStyle style, bool isReturn, bool isPinvoke)
 {
     return("JniReleaseArrayElementsMode");
 }
Exemplo n.º 29
0
 public override string GetMarshalType(HandleStyle style, bool isReturn, bool isPinvoke)
 {
     return("int");
 }
Exemplo n.º 30
0
 public override string GetManagedType(HandleStyle style, bool isReturn, bool isPinvoke)
 {
     return("string");
 }