Example #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)));
            }
        }
Example #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()));
            }
        }
Example #3
0
        static public int __set(IntPtr ctx)
        {
            try {
                // read params
                object csObject = Bind.RequireThis(ctx);
                string member   = Native.duk_require_string_s(ctx, 0);

                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));
                }

                Type valType = prop != null?prop.GetType() : finfo.GetType();

                object value = null;
                fillArg(ctx, 1, valType, out value);
                if (prop != null)
                {
                    prop.SetValue(csObject, value, null);
                }
                else
                {
                    finfo.SetValue(csObject, value);
                }
                return(0);
            }
            catch (Exception e) {
                return(Native.duk_throw_error(ctx, e.ToString()));
            }
        }