Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jnienv-gen] fix p/invoke usage for .NET framework #460

Merged
merged 3 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 39 additions & 20 deletions build-tools/jnienv-gen/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ static void CreateDelegate (TextWriter o, JniFunction entry, HandleStyle style)
if (i >= 0) {
builder.Append (", ");
builder.AppendFormat ("{0} {1}",
entry.Parameters [i].Type.GetMarshalType (style, isReturn: false),
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);
var ptype = entry.Parameters [i].Type.GetManagedType (style, isReturn: false, isPinvoke: true);
if (ptype == "va_list")
return;
if (ptype == "char[]")
Expand Down Expand Up @@ -308,11 +308,11 @@ static void GenerateNativeMethods (TextWriter o, HandleStyle style)
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),
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), Escape (p.Name)))));
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 ();
Expand Down Expand Up @@ -653,8 +653,8 @@ protected TypeInfo (string jniType)
JniType = jniType;
}

public abstract string GetMarshalType (HandleStyle style, bool isReturn);
public abstract string GetManagedType (HandleStyle style, bool isReturn);
public abstract string GetMarshalType (HandleStyle style, bool isReturn, bool isPinvoke = false);
public abstract string GetManagedType (HandleStyle style, bool isReturn, bool isPinvoke = false);

public virtual string[] GetHandleCreationLogStatements (HandleStyle style, string method, string variable)
{
Expand All @@ -681,6 +681,10 @@ public virtual string[] VerifyParameter (HandleStyle style, string variable)

class BuiltinTypeInfo : TypeInfo {

/// <summary>
/// NOTE: .NET framework can't marshal this
/// </summary>
const string JniArgumentValue = "JniArgumentValue*";
string managed;

public BuiltinTypeInfo (string jni, string managed)
Expand All @@ -689,16 +693,31 @@ public BuiltinTypeInfo (string jni, string managed)
this.managed = managed;
}

public override string GetMarshalType (HandleStyle style, bool isReturn)
public override string GetMarshalType (HandleStyle style, bool isReturn, bool isPinvoke)
{
if (isPinvoke && managed == JniArgumentValue) {
return "IntPtr";
}
return managed;
}

public override string GetManagedType (HandleStyle style, bool isReturn)
public override string GetManagedType (HandleStyle style, bool isReturn, bool isPinvoke)
{
if (isPinvoke && managed == JniArgumentValue) {
return "IntPtr";
}
return managed;
}

public override string GetManagedToMarshalExpression (HandleStyle style, string variable)
{
var value = base.GetManagedToMarshalExpression (style, variable);
if (managed == JniArgumentValue) {
value = "(IntPtr) " + value;
}
return value;
}

public override string[] VerifyParameter (HandleStyle style, string variable)
{
if (managed != "IntPtr")
Expand All @@ -720,12 +739,12 @@ public BooleanTypeInfo (string jni)
{
}

public override string GetMarshalType (HandleStyle style, bool isReturn)
public override string GetMarshalType (HandleStyle style, bool isReturn, bool isPinvoke)
{
return "byte";
}

public override string GetManagedType (HandleStyle style, bool isReturn)
public override string GetManagedType (HandleStyle style, bool isReturn, bool isPinvoke)
{
return "bool";
}
Expand All @@ -750,12 +769,12 @@ public StringTypeInfo (string jni)
{
}

public override string GetMarshalType (HandleStyle style, bool isReturn)
public override string GetMarshalType (HandleStyle style, bool isReturn, bool isPinvoke)
{
return "string";
}

public override string GetManagedType (HandleStyle style, bool isReturn)
public override string GetManagedType (HandleStyle style, bool isReturn, bool isPinvoke)
{
return "string";
}
Expand Down Expand Up @@ -798,12 +817,12 @@ public JniReleaseArrayElementsModeTypeInfo ()
{
}

public override string GetMarshalType (HandleStyle style, bool isReturn)
public override string GetMarshalType (HandleStyle style, bool isReturn, bool isPinvoke)
{
return "int";
}

public override string GetManagedType (HandleStyle style, bool isReturn)
public override string GetManagedType (HandleStyle style, bool isReturn, bool isPinvoke)
{
return "JniReleaseArrayElementsMode";
}
Expand Down Expand Up @@ -839,12 +858,12 @@ public IdTypeInfo (string jni, string type)
this.type = type;
}

public override string GetMarshalType (HandleStyle style, bool isReturn)
public override string GetMarshalType (HandleStyle style, bool isReturn, bool isPinvoke)
{
return "IntPtr";
}

public override string GetManagedType (HandleStyle style, bool isReturn)
public override string GetManagedType (HandleStyle style, bool isReturn, bool isPinvoke)
{
switch (style) {
case HandleStyle.SafeHandle:
Expand Down Expand Up @@ -968,7 +987,7 @@ public ObjectReferenceTypeInfo (string jni, string safeType, string refType)
this.refType = refType;
}

public override string GetMarshalType (HandleStyle style, bool isReturn)
public override string GetMarshalType (HandleStyle style, bool isReturn, bool isPinvoke)
{
switch (style) {
case HandleStyle.SafeHandle:
Expand All @@ -981,7 +1000,7 @@ public override string GetMarshalType (HandleStyle style, bool isReturn)
return null;
}

public override string GetManagedType (HandleStyle style, bool isReturn)
public override string GetManagedType (HandleStyle style, bool isReturn, bool isPinvoke)
{
switch (style) {
case HandleStyle.SafeHandle:
Expand Down Expand Up @@ -1088,12 +1107,12 @@ public JavaVMPointerTypeInfo (string jni)
{
}

public override string GetMarshalType (HandleStyle style, bool isReturn)
public override string GetMarshalType (HandleStyle style, bool isReturn, bool isPinvoke)
{
return "out IntPtr";
}

public override string GetManagedType (HandleStyle style, bool isReturn)
public override string GetManagedType (HandleStyle style, bool isReturn, bool isPinvoke)
{
return "out IntPtr";
}
Expand Down
Loading