static int AddJSComponent(IntPtr cx, uint argc, IntPtr vp) { if (argc != 2) { return(0); } IntPtr jsObj = JSApi.JSh_ArgvObject(cx, vp, 0); object csObj = JSMgr.getCSObj(jsObj); if (csObj == null || !(csObj is GameObject)) { return(0); } GameObject go = (GameObject)csObj; if (!JSApi.JSh_ArgvIsString(cx, vp, 1)) { return(0); } string jsScriptName = JSApi.JSh_ArgvStringS(cx, vp, 1); JSComponent jsComp = go.AddComponent <JSComponent>(); jsComp.jsScriptName = jsScriptName; jsComp.InitScript(); JSApi.JSh_SetRvalObject(cx, vp, jsComp.go); return(1); }
// only for parameters // public stJSCS getValueTypeObject() // { // IntPtr jsObj = JSApi.JSh_ArgvObject(cx, vp, currIndex++); // object csObj = JSMgr.getCSObj(jsObj); // return new stJSCS(jsObj, csObj); // } public stJSCS getJSCSObject() { IntPtr jsObj = JSApi.JSh_ArgvObject(cx, vp, currIndex++); object csObj = JSMgr.getCSObj(jsObj); return(new stJSCS(jsObj, csObj)); }
public JSValueWrap.Wrap getWrap() { IntPtr jsObj = JSApi.JSh_ArgvObject(cx, vp, currIndex++); object csObj = JSMgr.getCSObj(jsObj); return((JSValueWrap.Wrap)csObj); }
static int GetChar(IntPtr cx, uint argc, IntPtr vp) { IntPtr jsObj = JSApi.JSh_ThisObject(cx, vp); Wrap csObj = (Wrap)JSMgr.getCSObj(jsObj); JSApi.JSh_SetRvalInt(cx, vp, (int)csObj.obj); return(JSApi.JS_TRUE); }
static int GetSingle(IntPtr cx, uint argc, IntPtr vp) { IntPtr jsObj = JSApi.JSh_ThisObject(cx, vp); Wrap csObj = (Wrap)JSMgr.getCSObj(jsObj); JSApi.JSh_SetRvalDouble(cx, vp, (double)csObj.obj); return(JSApi.JS_TRUE); }
// public static int getFunctionS(int e) // { // int funID = JSApi.getFunction(e); // if (JSEngine.inst != null && JSEngine.inst.forceProtectJSFunction) // { // if (!JSApi.isTracedS(funID)) // { // protectedFunCount++; // JSApi.setTraceS(funID, true); // } // } // return funID; // } /// <summary> /// Gets the JavaScript function. /// it's wrapped with CSRepresentedObject. /// This function is almost the same as JSDataMgr.getObject /// </summary> /// <param name="e">The e.</param> /// <returns></returns> public static CSRepresentedObject getFunctionS(int e) { int funID = JSApi.getFunction(e); object obj = JSMgr.getCSObj(funID); if (obj == null) { obj = new CSRepresentedObject(funID, true); } return((CSRepresentedObject)obj); }
static int GetJSComponent(IntPtr cx, uint argc, IntPtr vp) { if (argc != 1 && argc != 2) { return(0); } IntPtr jsObj = JSApi.JSh_ArgvObject(cx, vp, 0); object csObj = JSMgr.getCSObj(jsObj); if (csObj == null || !(csObj is GameObject)) { return(0); } GameObject go = (GameObject)csObj; if (argc == 1) { JSComponent jsComp = go.GetComponent <JSComponent>(); if (jsComp == null) { JSApi.JSh_SetRvalUndefined(cx, vp); } else { JSApi.JSh_SetRvalObject(cx, vp, jsComp.go); } return(1); } else { if (!JSApi.JSh_ArgvIsString(cx, vp, 1)) { return(0); } string jsScriptName = JSApi.JSh_ArgvStringS(cx, vp, 1); JSComponent[] jsComps = go.GetComponents <JSComponent>(); foreach (var v in jsComps) { if (v.jsScriptName == jsScriptName) { JSApi.JSh_SetRvalObject(cx, vp, v.go); return(1); } } JSApi.JSh_SetRvalUndefined(cx, vp); return(1); } }
static int RemoveJSComponent(IntPtr cx, uint argc, IntPtr vp) { if (argc != 1 && argc != 2) { return(0); } IntPtr jsObj = JSApi.JSh_ArgvObject(cx, vp, 0); object csObj = JSMgr.getCSObj(jsObj); if (csObj == null || !(csObj is GameObject)) { return(0); } GameObject go = (GameObject)csObj; if (argc == 1) { JSComponent jsComp = go.GetComponent <JSComponent>(); if (jsComp != null) { UnityEngine.Object.Destroy(jsComp); } return(1); } else { if (!JSApi.JSh_ArgvIsString(cx, vp, 1)) { return(0); } string jsScriptName = JSApi.JSh_ArgvStringS(cx, vp, 1); JSComponent[] jsComps = go.GetComponents <JSComponent>(); foreach (JSComponent v in jsComps) { if (v.jsScriptName == jsScriptName) { UnityEngine.Object.Destroy(v); return(1); } } return(1); } }
public object getObject(int e) { int jsObjID = JSApi.getObject(e); if (jsObjID == 0) { // no error return(null); } object csObj = JSMgr.getCSObj(jsObjID); if (csObj == null) { csObj = new CSRepresentedObject(jsObjID); } return(csObj); }
public object getObject(Type typeParam = null) { IntPtr jsObj = JSApi.JSh_ArgvObject(cx, vp, currIndex++); if (jsObj == IntPtr.Zero) { return(null); } object csObj = JSMgr.getCSObj(jsObj); if (csObj is JSValueWrap.Wrap) { return(((JSValueWrap.Wrap)csObj).obj); } else if (csObj != null) { return(csObj); } if (!JSApi.JSh_IsArrayObject(cx, jsObj)) { return(null); } // array params don't work. // code must be generated, cann't be dynamically run. // because type is unknown during run-time. Type typeElement = typeParam.GetElementType(); JSApi.jsval valElement = new JSApi.jsval(); int length = JSApi.JSh_GetArrayLength(cx, jsObj); object[] arr = new object[length]; for (int i = 0; i < length; i++) { JSApi.JSh_GetElement(cx, jsObj, (uint)i, ref valElement); object csObjElement = JSValue_2_CSObject(typeElement, ref valElement); arr[i] = csObjElement; } return(arr); }
/* * ExtractJSParams * * write into lstJSParam * * RETURN * false -- fail * true -- success * * for primitive, enum, string: not handled */ public bool ExtractJSParams(int start, int count) { arrJSParamsLength = 0; for (int i = 0; i < count; i++) { int index = i + start; bool bUndefined = JSApi.JSh_ArgvIsUndefined(cx, vp, index); if (bUndefined) { return(true); } JSParam jsParam = arrJSParam[arrJSParamsLength++]; //new JSParam(); jsParam.index = index; jsParam.isNull = JSApi.JSh_ArgvIsNull(cx, vp, index); jsParam.isArray = false; jsParam.csObj = null; IntPtr jsObj = JSApi.JSh_ArgvObject(cx, vp, index); if (jsObj == IntPtr.Zero) { jsParam.csObj = null; } // else if (false/*JSApi.JSh_IsArrayObject(cx, jsObj)*/) // { // jsParam.isArray = true; // Debug.LogError("parse js array to cs is not supported"); // } else { object csObj = JSMgr.getCSObj(jsObj); if (csObj == null) { Debug.Log("ExtractJSParams: CSObject is not found"); return(false); } jsParam.csObj = csObj; } //lstJSParam.Add(jsParam); } return(true); }
public object getWhatever() { int i = currIndex++; if (JSApi.JSh_ArgvIsBool(cx, vp, i)) { return(JSApi.JSh_ArgvBool(cx, vp, i)); } else if (JSApi.JSh_ArgvIsInt32(cx, vp, i)) { return(JSApi.JSh_ArgvInt(cx, vp, i)); } else if (JSApi.JSh_ArgvIsDouble(cx, vp, i)) { return(JSApi.JSh_ArgvDouble(cx, vp, i)); } else if (JSApi.JSh_ArgvIsString(cx, vp, i)) { return(JSApi.JSh_ArgvStringS(cx, vp, i)); } else if (JSApi.JSh_ArgvIsObject(cx, vp, i)) { IntPtr jsObj = JSApi.JSh_ArgvObject(cx, vp, i); object csObj = JSMgr.getCSObj(jsObj); if (csObj is JSValueWrap.Wrap) { return(((JSValueWrap.Wrap)csObj).obj); } else { return(csObj); } } else if (JSApi.JSh_ArgvIsNullOrUndefined(cx, vp, i)) { return(null); } return(null); }
// 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); }
public object getDelegate(eGetType e) { switch (e) { case eGetType.GetARGV: { // TODO check: index must ++ int jsObjID = JSApi.getObject((int)JSApi.GetType.Arg); if (jsObjID == 0) { return(null); } object csObj = JSMgr.getCSObj(jsObjID); return(csObj); // IntPtr jsObj = JSApi.JSh_ArgvObject(JSMgr.cx, vc.vp, vc.currIndex++); // if (jsObj == IntPtr.Zero) // return null; // // object csObj = JSMgr.getCSObj(jsObj); // return csObj; } // break; !! // case eGetType.GetARGVRefOut: // { // jsval val = new jsval(); // JSApi.JSh_SetJsvalUndefined(ref val); // getJSValueOfParam(ref val, vc.currIndex++); // // IntPtr jsObj = JSApi.JSh_GetJsvalObject(ref val); // if (jsObj == IntPtr.Zero) // return null; // // JSApi.JSh_GetUCProperty(JSMgr.cx, jsObj, "__nativeObj", -1, ref val); // IntPtr __nativeObj = JSApi.JSh_GetJsvalObject(ref val); // if (__nativeObj == IntPtr.Zero) // return null; // // object csObj = JSMgr.getCSObj(__nativeObj); // return csObj; // } // break; // case eGetType.Jsval: // { // jsval val = new jsval(); // JSApi.JSh_SetJsvalUndefined(ref val); // // IntPtr jsObj = JSApi.JSh_GetJsvalObject(ref vc.valTemp); // if (jsObj == IntPtr.Zero) // return null; // // object csObj = JSMgr.getCSObj(jsObj); // return csObj; // } // break; default: Debug.LogError("Not Supported"); break; } return(null); }
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); }
// index means // lstJSParam[index] // lstCSParam[index] // ps[index] // for calling property/field public object JSValue_2_CSObject(Type t, int paramIndex) { if (t.IsArray) { Debug.LogError("JSValue_2_CSObject: could not pass an array"); return(null); } if (t.IsByRef) { t = t.GetElementType(); } if (t == typeof(string)) { return(JSApi.JSh_ArgvStringS(cx, vp, paramIndex)); } else if (t.IsEnum) { return(JSApi.JSh_ArgvInt(cx, vp, paramIndex)); } else if (t.IsPrimitive) { if (t == typeof(System.Boolean)) { return(JSApi.JSh_ArgvBool(cx, vp, paramIndex)); } else if (t == typeof(System.Char) || t == typeof(System.Byte) || t == typeof(System.SByte) || t == typeof(System.UInt16) || t == typeof(System.Int16) || t == typeof(System.UInt32) || t == typeof(System.Int32) || t == typeof(System.UInt64) || t == typeof(System.Int64)) { return(JSApi.JSh_ArgvInt(cx, vp, paramIndex)); } else if (t == typeof(System.Single) || t == typeof(System.Double)) { return(JSApi.JSh_ArgvDouble(cx, vp, paramIndex)); } else { Debug.Log("ConvertJSValue2CSValue: Unknown primitive type: " + t.ToString()); } } // else if (t.IsValueType) // { // // } else// if (typeof(UnityEngine.Object).IsAssignableFrom(t) || t.IsValueType) { if (JSApi.JSh_ArgvIsNull(cx, vp, paramIndex)) { return(null); } IntPtr jsObj = JSApi.JSh_ArgvObject(cx, vp, paramIndex); if (jsObj == IntPtr.Zero) { return(null); } object csObject = JSMgr.getCSObj(jsObj); return(csObject); } // else // { // Debug.Log("ConvertJSValue2CSValue: Unknown CS type: " + t.ToString()); // } return(null); }
// 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 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); }