コード例 #1
0
        static public int __get(IntPtr ctx)
        {
            try {
                // read params
                object   csObject = Bind.RequireThis(ctx);
                string   member   = Native.duk_require_string_s(ctx, 0);
                RET_TYPE retType  = (RET_TYPE)Native.duk_require_int(ctx, 1);

                Type         type  = csObject.GetType();
                FieldInfo    finfo = null;
                PropertyInfo prop  = type.GetProperty(member);
                if (prop == null)
                {
                    finfo = type.GetField(member);
                }

                if (finfo == null && prop == null)
                {
                    throw new Exception(string.Format("Attr<{0}> not in {1} !", member, type.Name));
                }

                object ret = (prop != null) ? prop.GetValue(csObject, null) : finfo.GetValue(csObject);

                // handle return value
                return(pushRetVal(ctx, ret, retType, member, type, 2));
            }
            catch (Exception e) {
                return(Native.duk_throw_error(ctx, Convert.ToString(e)));
            }
        }
コード例 #2
0
        static public int __call(IntPtr ctx)
        {
            try {
                // read params
                int      memberid     = Native.duk_require_int(ctx, 0);
                string   member       = Native.duk_require_string_s(ctx, 1);
                bool     isStaticCall = member.IndexOf(".") > 0;
                object   csObject     = isStaticCall ? null : Bind.RequireThis(ctx);
                RET_TYPE retType      = (RET_TYPE)Native.duk_require_int(ctx, 2);
                int      fromStackPos = retType == RET_TYPE.CLASSOBJECT ? 4 : 3;

                MethodInfo       m         = null;
                Type             type      = null;
                string           memberKey = getMemberKey(csObject, isStaticCall, member, memberid);
                DynBindMgr.MInfo mInfo     = DynBindMgr.GetMethodInfo(memberKey);
                if (mInfo == null)
                {
                    string methodName = null;
                    getNeedInfo(csObject, isStaticCall, member, memberid, out methodName, out type);
                    m = getMethod(ctx, methodName, fromStackPos, type);
                    DynBindMgr.SetMethodInfo(memberKey, type, m);
                }
                else
                {
                    type = mInfo.classType;
                    m    = mInfo.method;
                }

                if (m == null)
                {
                    throw new Exception(string.Format("Not find method[{0}] in {1}.", member, type.Name));
                }

                // handle args
                object[] args;
                fillArgs(ctx, fromStackPos, m.GetParameters(), out args);
                object ret = m.Invoke(m.IsStatic ? null : csObject, args);
                if (ret == null)
                {
                    return(0);
                }

                // handle return value
                return(pushRetVal(ctx, ret, retType, member, type, 3));
            }
            catch (Exception e) {
                return(Native.duk_throw_error(ctx, e.ToString()));
            }
        }
コード例 #3
0
        static private int pushRetVal(IntPtr ctx, object ret, RET_TYPE retType, string member, Type type, int tsTypePos)
        {
            Type t = ret.GetType();

            if (retType == RET_TYPE.CLASSOBJECT)
            {
                Bind.PushDynCsObject(ctx, ret, tsTypePos);
            }
            else if (t.IsEnum || t.Name == "Int32")
            {
                Native.duk_push_int(ctx, Convert.ToInt32(ret));
            }
            else if (t.Name == "String")
            {
                Native.duk_push_string(ctx, Convert.ToString(ret));
            }
            else
            {
                throw new Exception(string.Format("Return value is invalid! attr<{0}> in {1} return type is {2}.", member, type.Name, t.Name));
            }
            return(1);
        }