コード例 #1
0
        // 写入绑定代码
        protected void WriteCSMethodBinding(T method, string argc, bool isVararg)
        {
            var parameters  = method.GetParameters();
            var isExtension = method.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute));

            if (isExtension)
            {
                ArrayUtility.RemoveAt(ref parameters, 0);
            }
            var parametersByRef = new List <ParameterInfo>();
            var caller          = this.cg.AppendGetThisCS(method);
            var returnType      = GetReturnType(method);

            if (returnType == null || returnType == typeof(void))
            {
                // 方法本身没有返回值
                this.BeginInvokeBinding();
                cg.cs.AppendLine($"{this.GetInvokeBinding(caller, method, isVararg, isExtension, argc, parameters, parametersByRef)};");
                this.EndInvokeBinding();
                if (parametersByRef.Count > 0)
                {
                    _WriteBackParametersByRef(isExtension, parametersByRef);
                }
                if (!method.IsStatic && method.DeclaringType.IsValueType) // struct 非静态方法 检查 Mutable 属性
                {
                    if (!string.IsNullOrEmpty(caller))
                    {
                        cg.cs.AppendLine($"duk_rebind_this(ctx, {caller});");
                    }
                }
                cg.cs.AppendLine("return 0;");
            }
            else
            {
                // 方法本身有返回值
                this.BeginInvokeBinding();
                cg.cs.AppendLine($"var ret = {this.GetInvokeBinding(caller, method, isVararg, isExtension, argc, parameters, parametersByRef)};");
                this.EndInvokeBinding();
                if (parametersByRef.Count > 0)
                {
                    _WriteBackParametersByRef(isExtension, parametersByRef);
                }
                cg.AppendPushValue(returnType, "ret");
                cg.cs.AppendLine("return 1;");
            }
        }
コード例 #2
0
        // 写入绑定代码
        protected void WriteCSMethodBinding(T method, string argc, bool isVararg)
        {
            var parameters  = method.GetParameters();
            var isExtension = method.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute));
            var isRaw       = method.IsDefined(typeof(JSCFunctionAttribute));

            if (isExtension)
            {
                ArrayUtility.RemoveAt(ref parameters, 0);
            }
            var parametersByRef = new List <ParameterInfo>();
            var caller          = this.cg.AppendGetThisCS(method);
            var returnType      = GetReturnType(method);

            if (isRaw)
            {
                do
                {
                    if (!isExtension)
                    {
                        if (returnType == typeof(int) && parameters.Length == 1)
                        {
                            var p = parameters[0];
                            if (p.ParameterType == typeof(IntPtr) && !p.IsOut)
                            {
                                cg.cs.AppendLine($"return {caller}.{method.Name}(ctx);");
                                return;
                            }
                        }
                        cg.bindingManager.Error($"Invalid JSCFunction definition: {method}");
                        break;
                    }
                    cg.bindingManager.Error($"Extension as JSCFunction is not supported: {method}");
                } while (false);
            }

            if (returnType == null || returnType == typeof(void))
            {
                // 方法本身没有返回值
                this.BeginInvokeBinding();
                cg.cs.AppendLine($"{this.GetInvokeBinding(caller, method, isVararg, isExtension, argc, parameters, parametersByRef)};");
                this.EndInvokeBinding();
                if (parametersByRef.Count > 0)
                {
                    _WriteBackParametersByRef(isExtension, parametersByRef);
                }
                if (!method.IsStatic && method.DeclaringType.IsValueType) // struct 非静态方法 检查 Mutable 属性
                {
                    if (!string.IsNullOrEmpty(caller))
                    {
                        cg.cs.AppendLine($"duk_rebind_this(ctx, {caller});");
                    }
                }
                cg.cs.AppendLine("return 0;");
            }
            else
            {
                // 方法本身有返回值
                this.BeginInvokeBinding();
                cg.cs.AppendLine($"var ret = {this.GetInvokeBinding(caller, method, isVararg, isExtension, argc, parameters, parametersByRef)};");
                this.EndInvokeBinding();
                if (parametersByRef.Count > 0)
                {
                    _WriteBackParametersByRef(isExtension, parametersByRef);
                }
                cg.AppendPushValue(returnType, "ret");
                cg.cs.AppendLine("return 1;");
            }
        }