Example #1
0
        private AbcMethod DefineMetodWrapper(IMethod method, bool init)
        {
            var abcMethod = method.AbcMethod();

            if (abcMethod == null)
            {
                return(null);
            }
            if (!ShouldWrap(method))
            {
                return(abcMethod);
            }

            var          provname = ++MethodWrappperCounter;
            const string prefix   = "wrap_";
            var          name     = _generator.Abc.DefineName(QName.PfxPublic(prefix + provname));
            var          instance = abcMethod.Instance;
            bool         addParam = false;

            instance = FixInstance(instance);

            var wrapper = instance.DefineMethod(
                Sig.@static(name, AvmTypeCode.Object),
                code =>
            {
                int n          = abcMethod.Parameters.Count;
                bool hasReturn = false;
                if (method.IsConstructor)
                {
                    if (init)
                    {
                        //addParam = true;
                        if (abcMethod.IsInitializer)
                        {
                            //code.ThrowNotSupportedException();
                            //return;
                            //addParam = true;
                            //code.LoadArguments(n + 1);
                            //code.Add(InstructionCode.Callstatic, am, n);
                        }
                        else
                        {
                            addParam = true;
                            code.LoadArguments(n + 1);
                            code.Call(abcMethod);
                        }
                    }
                    else
                    {
                        hasReturn = true;
                        code.Getlex(abcMethod);
                        if (abcMethod.IsInitializer)
                        {
                            code.LoadArguments(n);
                            code.Construct(n);
                        }
                        else
                        {
                            code.Construct(0);
                            code.Dup();
                            code.LoadArguments(n);
                            code.Call(abcMethod);
                        }
                    }
                }
                else
                {
                    hasReturn = !abcMethod.IsVoid;
                    if (method.IsStatic)
                    {
                        code.Getlex(abcMethod);
                    }
                    else
                    {
                        addParam = true;
                        ++n;
                    }

                    code.LoadArguments(n);
                    code.Call(abcMethod);
                }

                if (!hasReturn)
                {
                    code.PushNull();
                }
                code.ReturnValue();
            });

            if (addParam)
            {
                wrapper.Parameters.Add(Abc.CreateParameter(AvmTypeCode.Object, "obj"));
            }
            _generator.MethodBuilder.CopyParameters(wrapper, abcMethod);

            return(wrapper);
        }