예제 #1
0
    static StringBuilder BuildRegisterFunction(ClassCallbackNames ccbn, JSMgr.ATypeInfo ti)
    {
        string        fmt = @"
public static void __Register()
[[
    JSMgr.CallbackInfo ci = new JSMgr.CallbackInfo();
    ci.type = typeof({0});
    ci.fields = new JSMgr.CSCallbackField[]
    [[
{1}
    ]];
    ci.properties = new JSMgr.CSCallbackProperty[]
    [[
{2}
    ]];
    ci.constructors = new JSMgr.MethodCallBackInfo[]
    [[
{3}
    ]];
    ci.methods = new JSMgr.MethodCallBackInfo[]
    [[
{4}
    ]];
    JSMgr.allCallbackInfo.Add(ci);
]]
";
        StringBuilder sb  = new StringBuilder();

        StringBuilder sbField    = new StringBuilder();
        StringBuilder sbProperty = new StringBuilder();
        StringBuilder sbCons     = new StringBuilder();
        StringBuilder sbMethod   = new StringBuilder();

        for (int i = 0; i < ccbn.fields.Count; i++)
        {
            sbField.AppendFormat("        {0},\n", ccbn.fields[i]);
        }
        for (int i = 0; i < ccbn.properties.Count; i++)
        {
            sbProperty.AppendFormat("        {0},\n", ccbn.properties[i]);
        }
        for (int i = 0; i < ccbn.constructors.Count; i++)
        {
            if (ccbn.constructors.Count == 1 && ti.constructors.Length == 0) // no constructors   add a default  so ...
            {
                sbCons.AppendFormat("        new JSMgr.MethodCallBackInfo({0}, '{2}', {1}),\n", ccbn.constructors[i], ccbn.constructorsCSParam[i], type.Name);
            }
            else
            {
                sbCons.AppendFormat("        new JSMgr.MethodCallBackInfo({0}, '{2}', {1}),\n", ccbn.constructors[i], ccbn.constructorsCSParam[i], ti.constructors[i].Name);
            }
        }
        for (int i = 0; i < ccbn.methods.Count; i++)
        {
            sbMethod.AppendFormat("        new JSMgr.MethodCallBackInfo({0}, '{2}', {1}),\n", ccbn.methods[i], ccbn.methodsCSParam[i], ti.methods[i].Name);
        }

        sb.AppendFormat(fmt, GetTypeFullName(ccbn.type), sbField, sbProperty, sbCons, sbMethod);
        return(sb);
    }
예제 #2
0
    public int CallReflection(IntPtr cx, uint argc, IntPtr vp)
    {
        this.Reset(cx, vp);

        this.op = (Oper)JSApi.JSh_ArgvInt(cx, vp, 0);
        int  slot     = JSApi.JSh_ArgvInt(cx, vp, 1);
        int  index    = JSApi.JSh_ArgvInt(cx, vp, 2);
        bool isStatic = JSApi.JSh_ArgvBool(cx, vp, 3);

        if (slot < 0 || slot >= JSMgr.allTypeInfo.Count)
        {
            Debug.LogError("Bad slot: " + slot);
            return(JSApi.JS_FALSE);
        }
        JSMgr.ATypeInfo aInfo = JSMgr.allTypeInfo[slot];

        currentParamCount = 4;
        object csObj = null;

        if (!isStatic)
        {
            IntPtr jsObj = JSApi.JSh_ArgvObject(cx, vp, 4);
            if (jsObj == IntPtr.Zero)
            {
                return(JSApi.JS_FALSE);
            }

            csObj = JSMgr.getCSObj(jsObj);
            if (csObj == null)
            {
                return(JSApi.JS_FALSE);
            }

            currentParamCount++;
        }

        //object result = null;

        switch (op)
        {
        case Oper.GET_FIELD:
        {
            result = aInfo.fields[index].GetValue(csObj);
        }
        break;

        case Oper.SET_FIELD:
        {
            FieldInfo field = aInfo.fields[index];
            field.SetValue(csObj, JSValue_2_CSObject(field.FieldType, currentParamCount));
        }
        break;

        case Oper.GET_PROPERTY:
        {
            result = aInfo.properties[index].GetValue(csObj, null);
        }
        break;

        case Oper.SET_PROPERTY:
        {
            PropertyInfo property = aInfo.properties[index];
            property.SetValue(csObj, JSValue_2_CSObject(property.PropertyType, currentParamCount), null);
        }
        break;

        case Oper.METHOD:
        case Oper.CONSTRUCTOR:
        {
            bool overloaded = JSApi.JSh_ArgvBool(cx, vp, currentParamCount);
            currentParamCount++;

            if (!this.ExtractJSParams(currentParamCount, (int)argc - currentParamCount))
            {
                return(JSApi.JS_FALSE);
            }

            if (overloaded)
            {
                MethodBase[] methods = aInfo.methods;
                if (op == Oper.CONSTRUCTOR)
                {
                    methods = aInfo.constructors;
                }

                if (-1 == MatchOverloadedMethod(methods, index))
                {
                    return(JSApi.JS_FALSE);
                }
            }
            else
            {
                m_Method = aInfo.methods[index];
                if (op == Oper.CONSTRUCTOR)
                {
                    m_Method = aInfo.constructors[index];
                }
            }

            this.ExtractCSParams();

            //!!!!!!!!!!!!!!!!!!!!
            if (!BuildMethodArgs(true))
            {
                return(JSApi.JS_FALSE);
            }

            object[] cp = new object[callParamsLength];
            for (int i = 0; callParamsLength > i; i++)
            {
                cp[i] = callParams[i];
            }

            result = this.m_Method.Invoke(csObj, cp);
        }
        break;
        }

        this.PushResult(result);
        return(JSApi.JS_TRUE);
    }