Exemplo n.º 1
0
        void EngineListener_MethodCall(int mIndex, int methodKind, IntPtr metArgs)
        {
            switch (methodKind)
            {
            case 1:
            {
                //property get
                if (mIndex == 0)
                {
                    return;
                }
                //------------------------------------------
                JsMethodDefinition getterMethod = registerProperties[mIndex].GetterMethod;

                if (getterMethod != null)
                {
                    getterMethod.InvokeMethod(new ManagedMethodArgs(this, metArgs));
                }
            }
            break;

            case 2:
            {
                //property set
                if (mIndex == 0)
                {
                    return;
                }
                //------------------------------------------
                JsMethodDefinition setterMethod = registerProperties[mIndex].SetterMethod;
                if (setterMethod != null)
                {
                    setterMethod.InvokeMethod(new ManagedMethodArgs(this, metArgs));
                }
            }
            break;

            default:
            {
                if (mIndex == 0)
                {
                    return;
                }
                JsMethodDefinition foundMet = registerMethods[mIndex];
                if (foundMet != null)
                {
                    foundMet.InvokeMethod(new ManagedMethodArgs(this, metArgs));
                }
            }
            break;
            }
        }
            public virtual JsMethodDefinition GetJsMethod()
            {
                if (onlyOneMember != null)
                {
                    return(onlyOneMember);
                }
                else
                {
                    //if we have > 1 member
                    //create a 'wrapper' method for all method in this group
                    //------
                    //analyze method based on arg count and the arg type
                    //quite complex
                    //------
                    //this is a default .... resolver
                    //you can provide a special method overload resolution
                    //for each method group

                    if (sameArgNumMethodLists == null)
                    {
                        sameArgNumMethodLists = new Dictionary <int, SameArgMethodList>();
                        int j = members.Count;
                        for (int i = 0; i < j; ++i)
                        {
                            JsMethodDefinition met  = members[i];
                            ParameterInfo[]    pars = met.Parameters;
                            if (pars != null)
                            {
                                SameArgMethodList existingMethods;
                                int count = pars.Length;
                                if (!sameArgNumMethodLists.TryGetValue(count, out existingMethods))
                                {
                                    existingMethods = new SameArgMethodList(count, met);
                                    sameArgNumMethodLists.Add(count, existingMethods);
                                }
                                else
                                {
                                    existingMethods.AddMore(met);
                                }
                            }
                            else
                            {
                                //delegate base?
                                //
                                //TODO : review this again
                                return(null);
                            }
                        }
                    }
                    return(new JsMethodDefinition(this.Name, args =>
                    {
                        //how to select the best over method based on input args:
                        //this resolve at runtime=> may be slow
                        //TODO: review here again

                        //num of arg not match
                        //TODO &
                        //LIMITATION: this version dose NOT support
                        //1. default parameter
                        //2. param args
                        //invoke method
                        var thisArg = args.GetThisArg();
                        //actual input arg count
                        int actualArgCount = args.ArgCount;

                        SameArgMethodList foundMets;
                        if (!sameArgNumMethodLists.TryGetValue(actualArgCount, out foundMets))
                        {
                            //so if num of arg not match the return
                            args.SetResultUndefined(); //?
                            return;
                        }

                        //-----------
                        //select best method by checking each vars
                        //TODO: how to find  hints ?
                        //best match args
                        object[] inputArgs = new object[actualArgCount];
                        Type[] inputArgsTypes = new Type[actualArgCount];
                        for (int i = 0; i < actualArgCount; ++i)
                        {
                            object arg_v = args.GetArgAsObject(i);
                            inputArgs[i] = arg_v;
                            if (arg_v == null)
                            {
                                //review here again
                                inputArgsTypes[i] = null;//null value
                            }
                            else
                            {
                                inputArgsTypes[i] = arg_v.GetType();
                            }
                        }
                        JsMethodDefinition selectedMet = foundMets.SelectBestMethod(inputArgsTypes);
                        if (selectedMet == null)
                        {
                            //if not found
                            //TODO: review here , throw exception?
                            args.SetResultUndefined(); //?
                            return;
                        }

                        selectedMet.InvokeMethod(args);
                    }));
                }
            }