Esempio n. 1
0
        public override Expression CreateReturnValueFromManagedExpression(JniValueMarshalerContext context, ParameterExpression sourceValue)
        {
            CreateParameterFromManagedExpression(context, sourceValue, 0);
            var r = context.LocalVariables [sourceValue + "_ref"];

            return(ReturnObjectReferenceToJni(context, sourceValue.Name, r));
        }
Esempio n. 2
0
 public override Expression CreateReturnValueFromManagedExpression(JniValueMarshalerContext context, ParameterExpression sourceValue)
 {
     return(Expression.Call(
                typeof(JNIEnv),
                "ToLocalJniHandle",
                null,
                sourceValue));
 }
Esempio n. 3
0
        public override Expression CreateParameterFromManagedExpression(JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize)
        {
            var r = Expression.Variable(MarshalType, sourceValue.Name + "_p");

            context.LocalVariables.Add(r);
            context.CreationStatements.Add(Expression.Assign(r, Expression.Field(sourceValue, "Value")));
            return(r);
        }
Esempio n. 4
0
        Expression CreateSelf(JniValueMarshalerContext context, ParameterExpression sourceValue)
        {
            var self = Expression.Variable(GetType(), sourceValue.Name + "_marshaler");

            context.LocalVariables.Add(self);
            context.CreationStatements.Add(Expression.Assign(self, Expression.New(GetType())));
            return(self);
        }
Esempio n. 5
0
        public override Expression CreateParameterToManagedExpression(JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize, Type targetType)
        {
            var c = typeof(MyLegacyColor).GetConstructor(new[] { typeof(int) });
            var v = Expression.Variable(typeof(MyLegacyColor), sourceValue.Name + "_val");

            context.LocalVariables.Add(v);
            context.CreationStatements.Add(Expression.Assign(v, Expression.New(c, sourceValue)));
            return(v);
        }
Esempio n. 6
0
        public override Expression CreateParameterFromManagedExpression(JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize)
        {
            var r = CreateIntermediaryExpressionFromManagedExpression(context, sourceValue);
            var h = Expression.Variable(typeof(IntPtr), sourceValue.Name + "_handle");

            context.LocalVariables.Add(h);
            context.CreationStatements.Add(Expression.Assign(h, Expression.Property(r, "Handle")));

            return(h);
        }
Esempio n. 7
0
        public override Expression CreateParameterToManagedExpression(JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize, Type?targetType)
        {
            Func <IntPtr, string?> m = JniEnvironment.Strings.ToString;

            var value = Expression.Variable(targetType, sourceValue.Name + "_val");

            context.LocalVariables.Add(value);
            context.CreationStatements.Add(Expression.Assign(value, Expression.Call(m.GetMethodInfo(), sourceValue)));

            return(value);
        }
        protected Expression ReturnObjectReferenceToJni(JniValueMarshalerContext context, string namePrefix, Expression sourceValue)
        {
            Func <JniObjectReference, IntPtr> m = JniEnvironment.References.NewReturnToJniRef;
            var r = Expression.Variable(MarshalType, namePrefix + "_rtn");

            context.LocalVariables.Add(r);
            context.CreationStatements.Add(
                Expression.Assign(r,
                                  Expression.Call(m.GetMethodInfo(), sourceValue)));
            return(r);
        }
Esempio n. 9
0
        public override Expression CreateReturnValueFromManagedExpression(JniValueMarshalerContext context, ParameterExpression sourceValue)
        {
            Func <string, JniObjectReference> m = JniEnvironment.Strings.NewString;

            var obj = Expression.Variable(typeof(JniObjectReference), sourceValue.Name + "_ref");

            context.LocalVariables.Add(obj);
            context.CreationStatements.Add(Expression.Assign(obj, Expression.Call(m.GetMethodInfo(), sourceValue)));
            context.CleanupStatements.Add(DisposeObjectReference(obj));
            return(ReturnObjectReferenceToJni(context, sourceValue.Name, obj));
        }
Esempio n. 10
0
        public virtual Expression              CreateParameterToManagedExpression(JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize = 0, Type targetType = null)
        {
            Func <IntPtr, Type, object> m = CreateValue;

            var self = CreateSelf(context, sourceValue);

            var call = Expression.Call(self, m.GetMethodInfo(), sourceValue, Expression.Constant(targetType, typeof(Type)));

            return(targetType == null
                                ? (Expression)call
                                : Expression.Convert(call, targetType));
        }
Esempio n. 11
0
        Expression CreateIntermediaryExpressionFromManagedExpression(JniValueMarshalerContext context, ParameterExpression sourceValue)
        {
            var r = Expression.Variable(typeof(JniObjectReference), sourceValue.Name + "_ref");

            context.LocalVariables.Add(r);
            context.CreationStatements.Add(
                Expression.IfThenElse(
                    test:       Expression.Equal(Expression.Constant(null), sourceValue),
                    ifTrue:     Expression.Assign(r, Expression.New(typeof(JniObjectReference))),
                    ifFalse:    Expression.Assign(r, Expression.Property(Expression.Convert(sourceValue, typeof(IJavaPeerable)), "PeerReference"))));

            return(r);
        }
        void CheckExpression(JniValueMarshalerContext context, string expected, Expression ret)
        {
            var body    = Expression.Block(context.CreationStatements.Concat(new[] { ret }));
            var cleanup = context.CleanupStatements.Any()
                                ? (Expression)Expression.Block(context.CleanupStatements.Reverse())
                                : (Expression)Expression.Empty();
            var expr  = Expression.TryFinally(body, cleanup);
            var block = Expression.Block(context.LocalVariables, expr);

            Console.WriteLine("# jonp: expected: {0}", GetType().Name);
            Console.WriteLine(block.ToCSharpCode());
            Assert.AreEqual(expected, block.ToCSharpCode());
        }
Esempio n. 13
0
        public override Expression CreateParameterToManagedExpression(JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize, Type targetType)
        {
            var r = Expression.Variable(targetType, sourceValue + "_val");

            context.LocalVariables.Add(r);
            context.CreationStatements.Add(
                Expression.Assign(r,
                                  Expression.Call(
                                      Expression.Property(context.Runtime, "ValueManager"),
                                      "GetValue",
                                      new[] { targetType },
                                      sourceValue)));
            return(r);
        }
Esempio n. 14
0
        public override Expression CreateParameterFromManagedExpression(JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize)
        {
            Func <string, JniObjectReference> m = JniEnvironment.Strings.NewString;

            var obj = Expression.Variable(typeof(JniObjectReference), sourceValue.Name + "_ref");
            var hdl = Expression.Variable(MarshalType, sourceValue.Name + "_handle");

            context.LocalVariables.Add(obj);
            context.LocalVariables.Add(hdl);
            context.CreationStatements.Add(Expression.Assign(obj, Expression.Call(m.GetMethodInfo(), sourceValue)));
            context.CreationStatements.Add(Expression.Assign(hdl, Expression.Property(obj, "Handle")));
            context.CleanupStatements.Add(DisposeObjectReference(obj));
            return(hdl);
        }
Esempio n. 15
0
        public override Expression CreateParameterToManagedExpression(JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize, Type?targetType)
        {
            var r = Expression.Variable(targetType, sourceValue.Name + "_val");

            context.LocalVariables.Add(r);
            context.CreationStatements.Add(
                Expression.Assign(r,
                                  Expression.Call(
                                      typeof(JavaConvert),
                                      "FromJniHandle",
                                      new[] { targetType },
                                      sourceValue,
                                      Expression.Field(null, typeof(JniHandleOwnership), "DoNotTransfer"))));
            return(r);
        }
        public void CreateReturnValueFromManagedExpression()
        {
            var runtime = Expression.Variable(typeof(JniRuntime), "__jvm");
            var value   = Expression.Variable(typeof(T), "__value");
            var context = new JniValueMarshalerContext(runtime)
            {
                LocalVariables =
                {
                    runtime,
                    value,
                },
            };
            var ret = marshaler.CreateReturnValueFromManagedExpression(context, value);

            CheckExpression(context, GetExpectedReturnValueFromManagedExpression(runtime.Name, value.Name, ret), ret);
        }
        void CheckExpression(JniValueMarshalerContext context, string expected, Expression ret)
        {
            var body    = Expression.Block(context.CreationStatements.Concat(new[] { ret }));
            var cleanup = context.CleanupStatements.Any()
                                ? (Expression)Expression.Block(context.CleanupStatements.Reverse())
                                : (Expression)Expression.Empty();
            var expr  = Expression.TryFinally(body, cleanup);
            var block = Expression.Block(context.LocalVariables, expr);
            // Convert line endings to Unix style to support building and running on different OS types.
            string blockCSharp = block.ToCSharpCode();

            blockCSharp = blockCSharp.Replace("\r\n", "\n");
            expected    = expected.Replace("\r\n", "\n");
            Console.WriteLine("# jonp: expected: {0}", GetType().Name);
            Console.WriteLine(blockCSharp);
            Assert.AreEqual(expected, blockCSharp);
        }
Esempio n. 18
0
        public override Expression CreateParameterFromManagedExpression(JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize)
        {
            var r = Expression.Variable(typeof(JniObjectReference), sourceValue.Name + "_ref");

            context.LocalVariables.Add(r);
            context.CreationStatements.Add(
                Expression.IfThenElse(
                    test:       Expression.Equal(Expression.Constant(null), sourceValue),
                    ifTrue:     Expression.Assign(r, Expression.New(typeof(JniObjectReference))),
                    ifFalse:    Expression.Assign(r, Expression.Property(sourceValue, "PeerReference"))));
            context.CleanupStatements.Add(DisposeObjectReference(r));

            var h = Expression.Variable(typeof(IntPtr), sourceValue + "_handle");

            context.LocalVariables.Add(h);
            context.CreationStatements.Add(Expression.Assign(h, Expression.Property(r, "Handle")));
            return(h);
        }
Esempio n. 19
0
        public virtual Expression              CreateParameterFromManagedExpression(JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize)
        {
            Func <object, ParameterAttributes, JniValueMarshalerState> c = CreateArgumentState;
            DestroyArgumentStateCb d = DestroyArgumentState;

            var self  = CreateSelf(context, sourceValue);
            var state = Expression.Variable(typeof(JniValueMarshalerState), sourceValue.Name + "_state");
            var ret   = Expression.Variable(MarshalType, sourceValue.Name + "_val");

            context.LocalVariables.Add(state);
            context.LocalVariables.Add(ret);
            context.CreationStatements.Add(Expression.Assign(state, Expression.Call(self, c.GetMethodInfo(), Expression.Convert(sourceValue, typeof(object)), Expression.Constant(synchronize, typeof(ParameterAttributes)))));
            context.CreationStatements.Add(
                Expression.Assign(ret,
                                  Expression.Property(
                                      Expression.Property(state, "ReferenceValue"),
                                      "Handle")));
            context.CleanupStatements.Add(Expression.Call(self, d.GetMethodInfo(), Expression.Convert(sourceValue, typeof(object)), state, Expression.Constant(synchronize)));

            return(ret);
        }
Esempio n. 20
0
        public virtual Expression              CreateReturnValueFromManagedExpression(JniValueMarshalerContext context, ParameterExpression sourceValue)
        {
            CreateParameterFromManagedExpression(context, sourceValue, 0);
            var s = context.LocalVariables [sourceValue + "_state"];

            return(ReturnObjectReferenceToJni(context, sourceValue.Name, Expression.Property(s, "ReferenceValue")));
        }
Esempio n. 21
0
 public override Expression CreateReturnValueFromManagedExpression(JniValueMarshalerContext context, ParameterExpression sourceValue)
 {
     return(ReturnObjectReferenceToJni(context, sourceValue.Name, CreateIntermediaryExpressionFromManagedExpression(context, sourceValue)));
 }
Esempio n. 22
0
 public override Expression CreateParameterToManagedExpression(JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize, Type targetType)
 {
     return(ValueMarshaler.CreateParameterToManagedExpression(context, sourceValue, synchronize, targetType));
 }
 public override Expression CreateParameterFromManagedExpression(JniValueMarshalerContext context, ParameterExpression sourceValue, ParameterAttributes synchronize)
 {
     return(sourceValue);
 }
 public override Expression CreateReturnValueFromManagedExpression(JniValueMarshalerContext context, ParameterExpression sourceValue)
 {
     return(sourceValue);
 }
Esempio n. 25
0
        public LambdaExpression CreateMarshalToManagedExpression(MethodInfo method, JavaCallableAttribute callable, Type type = null)
        {
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }
            type = type ?? method.DeclaringType;

            var methodParameters = method.GetParameters();

            CheckMarshalTypesMatch(method, callable?.Signature, methodParameters);

            bool direct = IsDirectMethod(methodParameters);

            var jnienv  = Expression.Parameter(typeof(IntPtr), direct ? methodParameters [0].Name : "__jnienv");
            var context = Expression.Parameter(typeof(IntPtr), direct ? methodParameters [1].Name : (method.IsStatic ? "__class" : "__this"));

            var envp     = Expression.Variable(typeof(JniTransition), "__envp");
            var jvm      = Expression.Variable(typeof(JniRuntime), "__jvm");
            var vm       = Expression.Variable(typeof(JniRuntime.JniValueManager), "__vm");
            var envpVars = new List <ParameterExpression> ()
            {
                envp,
                jvm,
            };

            int peerableParametersCount = 0;

            for (int i = 0; i < methodParameters.Length; ++i)
            {
                var marshaler = GetParameterMarshaler(methodParameters [i]);

                if (typeof(IJavaPeerable).GetTypeInfo().IsAssignableFrom(methodParameters [i].ParameterType.GetTypeInfo()))
                {
                    peerableParametersCount++;
                }
            }

            bool useVmVariable = (!method.IsStatic || peerableParametersCount > 0) && !direct;

            if (useVmVariable)
            {
                envpVars.Add(vm);
            }

            var envpBody = new List <Expression> ()
            {
                Expression.Assign(envp, CreateJniTransition(jnienv)),
            };

            var waitForGCBridge = typeof(JniRuntime.JniValueManager)
                                  .GetRuntimeMethod(nameof(JniRuntime.JniValueManager.WaitForGCBridgeProcessing), new Type [0]);
            var marshalBody = new List <Expression> ()
            {
                Expression.Assign(jvm, GetRuntime()),
            };

            if (useVmVariable)
            {
                marshalBody.Add(Expression.Assign(vm, Expression.Property(jvm, "ValueManager")));
                marshalBody.Add(Expression.Call(vm, waitForGCBridge));
            }
            else
            {
                marshalBody.Add(Expression.Call(Expression.Property(jvm, "ValueManager"), waitForGCBridge));
            }

            Expression self             = null;
            var        marshalerContext = new JniValueMarshalerContext(jvm, useVmVariable ? vm : null);

            if (!method.IsStatic)
            {
                var selfMarshaler = Runtime.ValueManager.GetValueMarshaler(type);
                self = selfMarshaler.CreateParameterToManagedExpression(marshalerContext, context, 0, type);
            }

            var marshalParameters = new List <ParameterExpression> (methodParameters.Length);
            var invokeParameters  = new List <Expression> (methodParameters.Length);

            for (int i = 0; i < methodParameters.Length; ++i)
            {
                var marshaler = GetParameterMarshaler(methodParameters [i]);
                ParameterExpression np;
                if (i > 1 || !direct)
                {
                    np = Expression.Parameter(marshaler.MarshalType, methodParameters [i].Name);
                }
                else
                {
                    if (i == 0)
                    {
                        np = jnienv;
                    }
                    else if (i == 1)
                    {
                        np = context;
                    }
                    else
                    {
                        throw new InvalidOperationException("Should not be reached.");
                    }
                }
                var p = marshaler.CreateParameterToManagedExpression(marshalerContext, np, methodParameters [i].Attributes, methodParameters [i].ParameterType);
                marshalParameters.Add(np);
                invokeParameters.Add(p);
            }

            marshalBody.AddRange(marshalerContext.CreationStatements);

            Expression invoke = method.IsStatic
                                ? Expression.Call(method, invokeParameters)
                                : Expression.Call(self, method, invokeParameters);
            Expression ret = null;

            if (method.ReturnType == typeof(void))
            {
                envpVars.AddRange(marshalerContext.LocalVariables);

                marshalBody.Add(invoke);
                envpBody.Add(
                    Expression.TryCatchFinally(
                        Expression.Block(marshalBody),
                        CreateDisposeJniEnvironment(envp, marshalerContext.CleanupStatements),
                        CreateMarshalException(envp, jvm, null)));
            }
            else
            {
                var rmarshaler = GetParameterMarshaler(method.ReturnParameter);
                var jniRType   = rmarshaler.MarshalType;
                var exit       = Expression.Label(jniRType, "__exit");
                var mret       = Expression.Variable(method.ReturnType, "__mret");
                envpVars.Add(mret);
                marshalBody.Add(Expression.Assign(mret, invoke));
                marshalerContext.CreationStatements.Clear();
                ret = rmarshaler.CreateReturnValueFromManagedExpression(marshalerContext, mret);
                marshalBody.AddRange(marshalerContext.CreationStatements);
                marshalBody.Add(Expression.Return(exit, ret));

                envpVars.AddRange(marshalerContext.LocalVariables);

                envpBody.Add(
                    Expression.TryCatchFinally(
                        Expression.Block(marshalBody),
                        CreateDisposeJniEnvironment(envp, marshalerContext.CleanupStatements),
                        CreateMarshalException(envp, jvm, exit)));

                envpBody.Add(Expression.Label(exit, Expression.Default(jniRType)));
            }

            var funcTypeParams = new List <Type> ();
            var bodyParams     = new List <ParameterExpression> ();

            if (!direct)
            {
                funcTypeParams.Add(typeof(IntPtr));
                funcTypeParams.Add(typeof(IntPtr));
                bodyParams.Add(jnienv);
                bodyParams.Add(context);
            }
            foreach (var p in marshalParameters)
            {
                funcTypeParams.Add(p.Type);
            }
            var marshalerType = GetMarshalerType(ret?.Type, funcTypeParams, method.DeclaringType);

            bodyParams.AddRange(marshalParameters);
            var body = Expression.Block(envpVars, envpBody);

            return(marshalerType == null
                                ? Expression.Lambda(body, bodyParams)
                                : Expression.Lambda(marshalerType, body, bodyParams));
        }