public static string GetCallName(Type type, JSMgr.CallbackInfo aInfo, Oper op, int index, bool isStatic) { switch (op) { case Oper.GET_FIELD: case Oper.SET_FIELD: { JSMgr.CSCallbackField fun = aInfo.fields[index]; if (fun == null) { throw (new Exception("Field not found")); //return false; } return(fun.Method.ToString()); } break; case Oper.GET_PROPERTY: case Oper.SET_PROPERTY: { JSMgr.CSCallbackProperty fun = aInfo.properties[index]; if (fun == null) { throw (new Exception("Property not found")); //return false; } return(fun.Method.ToString()); } break; case Oper.METHOD: case Oper.CONSTRUCTOR: { JSMgr.MethodCallBackInfo[] arrMethod; if (op == Oper.METHOD) { arrMethod = aInfo.methods; } else { arrMethod = aInfo.constructors; } return(arrMethod [index].fun.Method.ToString()); } } return(type.ToString()); }
// entry for js -> c# public bool CallCallback(int iOP, int slot, int index, int isStatic, int argc) { jsCallCount++; this.jsObjID = 0; this.csObj = null; Oper op = (Oper)iOP; if (slot < 0 || slot >= JSMgr.allCallbackInfo.Count) { throw (new Exception("Bad slot: " + slot)); //return false; } JSMgr.CallbackInfo aInfo = JSMgr.allCallbackInfo[slot]; if (isStatic == 0) { this.jsObjID = JSApi.getObject((int)JSApi.GetType.Arg); if (this.jsObjID == 0) { throw (new Exception("Invalid this jsObjID")); //return false; } // for manual javascript code, this.csObj will be null this.csObj = JSMgr.getCSObj(jsObjID); //if (this.csObj == null) { // throw(new Exception("Invalid this csObj")); // return JSApi.JS_FALSE; //} --argc; } switch (op) { case Oper.GET_FIELD: case Oper.SET_FIELD: { this.bGet = (op == Oper.GET_FIELD); JSMgr.CSCallbackField fun = aInfo.fields[index]; if (fun == null) { throw (new Exception("Field not found")); //return false; } fun(this); } break; case Oper.GET_PROPERTY: case Oper.SET_PROPERTY: { this.bGet = (op == Oper.GET_PROPERTY); JSMgr.CSCallbackProperty fun = aInfo.properties[index]; if (fun == null) { throw (new Exception("Property not found")); //return false; } fun(this); } break; case Oper.METHOD: case Oper.CONSTRUCTOR: { JSMgr.MethodCallBackInfo[] arrMethod; if (op == Oper.METHOD) { arrMethod = aInfo.methods; } else { arrMethod = aInfo.constructors; } arrMethod[index].fun(this, argc); } break; } return(true); }
public bool CallCallback(int iOP, int slot, int index, int isStatic, int argc) { jsCallCount++; this.jsObjID = 0; this.csObj = null; Oper op = (Oper)iOP; if (slot < 0 || slot >= JSMgr.allCallbackInfo.Count) { throw (new Exception("Bad slot: " + slot)); //return false; } // if (jsCallCount == 1000 && JSEngine.inst.exceptionWhenTooMuch) { // Debug.LogError ("跨域调用次数太多 jsCallCount:"+jsCallCount); // throw (new Exception(">>>CallCallback jsCallCount is too large ! jsCallCount:"+jsCallCount)); // } JSMgr.CallbackInfo aInfo = JSMgr.allCallbackInfo[slot]; #if UNITY_EDITOR string tCallName = JSCSCallInfo.GetCallName(aInfo.type, aInfo, op, index, isStatic == 0); JSCSCallInfo tJSCSCallInfo = CallbackInfoList.Find(jSCSCallInfo => { return(jSCSCallInfo.CallName == tCallName); }); if (null == tJSCSCallInfo || string.IsNullOrEmpty(tJSCSCallInfo.CallName)) { tJSCSCallInfo = JSCSCallInfo.Create(aInfo.type, aInfo, op, index, isStatic == 0); CallbackInfoList.Add(tJSCSCallInfo); } else { tJSCSCallInfo.Count++; } if (JSEngine.inst.showStatistics) { jsCallInfoSb.AppendFormat("Type:{0} Op:{1} index:{2}\n", aInfo.type, op, index); } #endif if (isStatic == 0) { this.jsObjID = JSApi.getObject((int)JSApi.GetType.Arg); if (this.jsObjID == 0) { throw (new Exception("Invalid this jsObjID")); //return false; } // for manual javascript code, this.csObj will be null this.csObj = JSMgr.getCSObj(jsObjID); //if (this.csObj == null) { // throw(new Exception("Invalid this csObj")); // return JSApi.JS_FALSE; //} --argc; } switch (op) { case Oper.GET_FIELD: case Oper.SET_FIELD: { this.bGet = (op == Oper.GET_FIELD); JSMgr.CSCallbackField fun = aInfo.fields[index]; if (fun == null) { throw (new Exception("Field not found")); //return false; } fun(this); } break; case Oper.GET_PROPERTY: case Oper.SET_PROPERTY: { this.bGet = (op == Oper.GET_PROPERTY); JSMgr.CSCallbackProperty fun = aInfo.properties[index]; if (fun == null) { throw (new Exception("Property not found")); //return false; } fun(this); } break; case Oper.METHOD: case Oper.CONSTRUCTOR: { JSMgr.MethodCallBackInfo[] arrMethod; if (op == Oper.METHOD) { arrMethod = aInfo.methods; } else { arrMethod = aInfo.constructors; } arrMethod[index].fun(this, argc); } break; } return(true); }
// public static bool CSCallback() // { // if (JSVCall.bGet) // result = ((GameObject)JSVCall.jsObj).activeSelf; // else // { // object arg = JSValue_2_CSObject(typeof(bool), JSVCall.currentParamCount); // ((GameObject)JSVCall.jsObj).activeSelf = (bool)JSVCall.arg; // } // } public int CallCallback(IntPtr cx, uint argc, IntPtr vp) { this.Reset(cx, vp); // first 4 params are fixed 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.allCallbackInfo.Count) { Debug.LogError("Bad slot: " + slot); return(JSApi.JS_FALSE); } JSMgr.CallbackInfo aInfo = JSMgr.allCallbackInfo[slot]; currentParamCount = 4; if (!isStatic) { this.jsObj = JSApi.JSh_ArgvObject(cx, vp, 4); if (this.jsObj == IntPtr.Zero) { return(JSApi.JS_FALSE); } this.csObj = JSMgr.getCSObj(jsObj); if (this.csObj == null) { return(JSApi.JS_FALSE); } currentParamCount++; } switch (op) { case Oper.GET_FIELD: case Oper.SET_FIELD: { currIndex = currentParamCount; this.bGet = (op == Oper.GET_FIELD); JSMgr.CSCallbackField fun = aInfo.fields[index]; if (fun == null) { return(JSApi.JS_FALSE); } fun(this); } break; case Oper.GET_PROPERTY: case Oper.SET_PROPERTY: { currIndex = currentParamCount; this.bGet = (op == Oper.GET_PROPERTY); JSMgr.CSCallbackProperty fun = aInfo.properties[index]; if (fun == null) { return(JSApi.JS_FALSE); } fun(this); } break; case Oper.METHOD: case Oper.CONSTRUCTOR: { bool overloaded = JSApi.JSh_ArgvBool(cx, vp, currentParamCount); currentParamCount++; JSMgr.MethodCallBackInfo[] arrMethod; if (op == Oper.METHOD) { arrMethod = aInfo.methods; } else { arrMethod = aInfo.constructors; } // params count // for overloaded function, it's caculated by ExtractJSParams int jsParamCount = (int)argc - currentParamCount; if (!overloaded) { // for not-overloaded function int i = (int)argc; while (i > 0 && JSApi.JSh_ArgvIsUndefined(cx, vp, --i)) { jsParamCount--; } } else { if (!this.ExtractJSParams(currentParamCount, (int)argc - currentParamCount)) { return(JSApi.JS_FALSE); } string methodName = arrMethod[index].methodName; int i = index; while (true) { if (IsMethodMatch(arrMethod[i].arrCSParam)) { index = i; break; } i++; if (arrMethod[i].methodName != methodName) { Debug.LogError("Overloaded function can't find match: " + methodName); return(JSApi.JS_FALSE); } } jsParamCount = arrJSParamsLength; } currIndex = currentParamCount; arrMethod[index].fun(this, currentParamCount, jsParamCount); //Debug.Log(slot.ToString()+"/"+index.ToString()+"Call OK"); // if (overloaded) // { // string methodName = arrMethod[index].methodName; // // int i = index; // while (true) // { // if (IsMethodMatch(arrMethod[i].arrCSParam)) // { // index = i; // break; // } // i++; // if (arrMethod[i].methodName != methodName) // { // Debug.LogError("Overloaded function can't find match: " + methodName); // return JSApi.JS_FALSE; // } // } // } // // JSMgr.CSCallbackMethod fun; // // fun = arrMethod[index].fun; // arrCSParam = arrMethod[index].arrCSParam; // arrCSParamsLength = arrCSParam.Length; // // if (fun == null || arrCSParam == null) // return JSApi.JS_FALSE; // // if (!BuildMethodArgs(false)) // return JSApi.JS_FALSE; // // if (!fun(this, currentParamCount, (int)argc - currentParamCount)) // return JSApi.JS_FALSE; } break; } // this.PushResult(result); // return JSApi.JS_TRUE; return(JSApi.JS_TRUE); }