//--- Constructors --- public DekiScriptInvocationTargetDescriptor(DreamAccess access, bool isProperty, bool isIdempotent, string name, DekiScriptParameter[] parameters, DekiScriptType returnType, string description, string transform, IDekiScriptInvocationTarget target) { this.Access = access; this.IsProperty = isProperty; this.IsIdempotent = isIdempotent; this.Name = name; this.Parameters = parameters; this.ReturnType = returnType; this.Description = description; this.Transform = transform; this.Target = target; }
public DekiScriptExpressionInvocationTarget(DreamAccess access, DekiScriptParameter[] parameters, DekiScriptExpression expr, DekiScriptEnv env) { if(parameters == null) { throw new ArgumentNullException("parameters"); } if(expr == null) { throw new ArgumentNullException("expr"); } this.Access = access; this.Parameters = parameters; this.Expression = expr; _env = env; }
//--- Constructors --- public DekiScriptNativeInvocationTarget(object subject, MethodInfo method, Parameter[] parameters) { this.Method = method; // check if method is a coroutine Type nativeReturnType; ConstructorInfo resultTypeConstructor = null; ParameterInfo[] methodParams = method.GetParameters(); bool isCoroutine = (method.ReturnType == typeof(IEnumerator <IYield>)); if (isCoroutine) { // check if enough parameters are present if (methodParams.Length == 0) { throw new ArgumentException("handler is missing Result<T> parameter"); } // check that the last parameter is of type Result<T> Type lastParam = methodParams[methodParams.Length - 1].ParameterType; if (!lastParam.IsGenericType || (lastParam.GetGenericTypeDefinition() != typeof(Result <>))) { throw new ArgumentException(string.Format("handler last parameter must be generic type Result<T>, but is {0}", lastParam.FullName)); } resultTypeConstructor = lastParam.GetConstructor(Type.EmptyTypes); nativeReturnType = lastParam.GetGenericArguments()[0]; // remove last parameter from array since it represents the return type methodParams = ArrayUtil.Resize(methodParams, methodParams.Length - 1); } else { nativeReturnType = method.ReturnType; } ReturnType = DekiScriptLiteral.AsScriptType(nativeReturnType); // check if first parameter is a DekiScriptRuntime bool usesRuntime = false; if ((methodParams.Length > 0) && (methodParams[methodParams.Length - 1].ParameterType.IsA <DekiScriptRuntime>())) { usesRuntime = true; methodParams = ArrayUtil.Resize(methodParams, methodParams.Length - 1); } // retrieve method parameters and their attributes Parameters = new DekiScriptParameter[methodParams.Length]; for (int i = 0; i < methodParams.Length; ++i) { ParameterInfo param = methodParams[i]; Parameter details = parameters[i] ?? Parameter.Default; // add hint parameter Parameters[i] = new DekiScriptParameter(param.Name, DekiScriptLiteral.AsScriptType(param.ParameterType), details.Optional, details.Hint, param.ParameterType, DekiScriptNil.Value); } // determine access rights if (method.IsPrivate || method.IsFamily) { this.Access = DreamAccess.Private; } else if (method.IsAssembly) { this.Access = DreamAccess.Internal; } else { this.Access = DreamAccess.Public; } // create invocation callback if (resultTypeConstructor != null) { if (usesRuntime) { // invoke coroutine with runtime _invoke = (runtime, args) => { var arguments = new object[args.Length + 2]; AResult result = (AResult)resultTypeConstructor.Invoke(null); arguments[arguments.Length - 1] = result; arguments[arguments.Length - 2] = runtime; Array.Copy(args, arguments, args.Length); new Coroutine(method, result).Invoke(() => (IEnumerator <IYield>)method.InvokeWithRethrow(subject, arguments)); result.Block(); return(result.UntypedValue); }; } else { // invoke coroutine without runtime _invoke = (runtime, args) => { var arguments = new object[args.Length + 1]; AResult result = (AResult)resultTypeConstructor.Invoke(null); arguments[arguments.Length - 1] = result; Array.Copy(args, arguments, args.Length); new Coroutine(method, result).Invoke(() => (IEnumerator <IYield>)method.InvokeWithRethrow(subject, arguments)); result.Block(); return(result.UntypedValue); }; } } else { if (usesRuntime) { // invoke method with runtime _invoke = (runtime, args) => { var arguments = new object[args.Length + 1]; arguments[arguments.Length - 1] = runtime; Array.Copy(args, arguments, args.Length); return(method.InvokeWithRethrow(subject, arguments)); }; } else { // invoke method without runtime _invoke = (runtime, args) => method.InvokeWithRethrow(subject, args); } } }
public override DekiScriptLiteral InvokeMap(DekiScriptRuntime runtime, DekiScriptMap args) { return(InvokeHelper(runtime, DekiScriptParameter.ValidateToList(Parameters, args))); }
//--- Constructors --- public DekiScriptNativeInvocationTarget(object subject, MethodInfo method, Parameter[] parameters) { this.Method = method; // check if method is a coroutine Type nativeReturnType; ConstructorInfo resultTypeConstructor = null; ParameterInfo[] methodParams = method.GetParameters(); bool isCoroutine = (method.ReturnType == typeof(IEnumerator<IYield>)); if(isCoroutine) { // check if enough parameters are present if(methodParams.Length == 0) { throw new ArgumentException("handler is missing Result<T> parameter"); } // check that the last parameter is of type Result<T> Type lastParam = methodParams[methodParams.Length - 1].ParameterType; if(!lastParam.IsGenericType || (lastParam.GetGenericTypeDefinition() != typeof(Result<>))) { throw new ArgumentException(string.Format("handler last parameter must be generic type Result<T>, but is {0}", lastParam.FullName)); } resultTypeConstructor = lastParam.GetConstructor(Type.EmptyTypes); nativeReturnType = lastParam.GetGenericArguments()[0]; // remove last parameter from array since it represents the return type methodParams = ArrayUtil.Resize(methodParams, methodParams.Length - 1); } else { nativeReturnType = method.ReturnType; } ReturnType = DekiScriptLiteral.AsScriptType(nativeReturnType); // check if first parameter is a DekiScriptRuntime bool usesRuntime = false; if((methodParams.Length > 0) && (methodParams[methodParams.Length - 1].ParameterType.IsA<DekiScriptRuntime>())) { usesRuntime = true; methodParams = ArrayUtil.Resize(methodParams, methodParams.Length - 1); } // retrieve method parameters and their attributes Parameters = new DekiScriptParameter[methodParams.Length]; for(int i = 0; i < methodParams.Length; ++i) { ParameterInfo param = methodParams[i]; Parameter details = parameters[i] ?? Parameter.Default; // add hint parameter Parameters[i] = new DekiScriptParameter(param.Name, DekiScriptLiteral.AsScriptType(param.ParameterType), details.Optional, details.Hint, param.ParameterType, DekiScriptNil.Value); } // determine access rights if(method.IsPrivate || method.IsFamily) { this.Access = DreamAccess.Private; } else if(method.IsAssembly) { this.Access = DreamAccess.Internal; } else { this.Access = DreamAccess.Public; } // create invocation callback if(resultTypeConstructor != null) { if(usesRuntime) { // invoke coroutine with runtime _invoke = (runtime, args) => { var arguments = new object[args.Length + 2]; AResult result = (AResult)resultTypeConstructor.Invoke(null); arguments[arguments.Length - 1] = result; arguments[arguments.Length - 2] = runtime; Array.Copy(args, arguments, args.Length); new Coroutine(method, result).Invoke(() => (IEnumerator<IYield>)method.InvokeWithRethrow(subject, arguments)); result.Block(); return result.UntypedValue; }; } else { // invoke coroutine without runtime _invoke = (runtime, args) => { var arguments = new object[args.Length + 1]; AResult result = (AResult)resultTypeConstructor.Invoke(null); arguments[arguments.Length - 1] = result; Array.Copy(args, arguments, args.Length); new Coroutine(method, result).Invoke(() => (IEnumerator<IYield>)method.InvokeWithRethrow(subject, arguments)); result.Block(); return result.UntypedValue; }; } } else { if(usesRuntime) { // invoke method with runtime _invoke = (runtime, args) => { var arguments = new object[args.Length + 1]; arguments[arguments.Length - 1] = runtime; Array.Copy(args, arguments, args.Length); return method.InvokeWithRethrow(subject, arguments); }; } else { // invoke method without runtime _invoke = (runtime, args) => method.InvokeWithRethrow(subject, args); } } }
//--- Constructors --- public DekiScriptExpressionInvocationTarget(DreamAccess access, DekiScriptParameter[] parameters, DekiScriptExpression expr) : this(access, parameters, expr, null) { }