static int WrapChar(IntPtr cx, uint argc, IntPtr vp) { Char b = (Char)JSApi.JSh_ArgvInt(cx, vp, 0); var w = new Wrap(b); IntPtr jsObj = JSApi.JSh_NewMyClass(cx, JSMgr.mjsFinalizer); JSApi.JSh_DefineFunction(cx, jsObj, "Value", Marshal.GetFunctionPointerForDelegate(new JSApi.JSNative(GetChar)), 0 /* narg */, 0); JSMgr.addJSCSRelation(jsObj, w); JSApi.JSh_SetRvalObject(cx, vp, jsObj); return(JSApi.JS_TRUE); }
public void returnObject(string className, object csObj) { JSApi.JSh_SetJsvalUndefined(ref this.valReturn); if (csObj == null) { JSApi.JSh_SetRvalJSVAL(cx, vp, ref this.valReturn); return; } if (!csObj.GetType().IsArray) { IntPtr jsObj; //jsObj = JSMgr.getJSObj(csObj); //if (jsObj == IntPtr.Zero) { // always add a new jsObj jsObj = JSApi.JSh_NewObjectAsClass(cx, JSMgr.glob, /*className*/ csObj.GetType().Name, JSMgr.mjsFinalizer); if (jsObj != IntPtr.Zero) { JSMgr.addJSCSRelation(jsObj, csObj); } } if (jsObj == IntPtr.Zero) { JSApi.JSh_SetJsvalUndefined(ref this.valReturn); } else { JSApi.JSh_SetJsvalObject(ref this.valReturn, jsObj); } } else { Array arr = csObj as Array; if (arr.Length > 0 && arr.GetValue(0).GetType().IsArray) { Debug.LogWarning("cs return [][] may cause problems."); } IntPtr jsArr = JSApi.JSh_NewArrayObjectS(cx, arr.Length); for (int i = 0; i < arr.Length; i++) { JSApi.jsval subVal = CSObject_2_JSValue(arr.GetValue(i)); JSApi.JSh_SetElement(cx, jsArr, (uint)i, ref subVal); } JSApi.JSh_SetJsvalObject(ref this.valReturn, jsArr); } JSApi.JSh_SetRvalJSVAL(cx, vp, ref this.valReturn); }
public void InitScript() { if (!JSEngine.inited) { return; } go = JSApi.JSh_NewObjectAsClass(JSMgr.cx, JSMgr.glob, "GameObject", JSMgr.mjsFinalizer); if (go == IntPtr.Zero) { return; } JSApi.JSh_AddObjectRoot(JSMgr.cx, ref go); JSMgr.addJSCSRelation(go, gameObject); IntPtr ptrScript = JSMgr.GetScript(jsScriptName); if (ptrScript == IntPtr.Zero) { Debug.Log("ptrScript is null)"); enabled = false; return; } if (!JSMgr.ExecuteScript(ptrScript, go)) { Debug.Log("---------- ExecuteScript fail"); enabled = false; return; } funAwake = JSApi.JSh_GetFunction(JSMgr.cx, go, "Awake"); funStart = JSApi.JSh_GetFunction(JSMgr.cx, go, "Start"); funUpdate = JSApi.JSh_GetFunction(JSMgr.cx, go, "Update"); funDestroy = JSApi.JSh_GetFunction(JSMgr.cx, go, "Destroy"); funOnGUI = JSApi.JSh_GetFunction(JSMgr.cx, go, "OnGUI"); if (funAwake != IntPtr.Zero) { JSMgr.vCall.CallJSFunction(go, funAwake, null); } inited = true; }
// CS -> JS public JSApi.jsval CSObject_2_JSValue(object csObj) { JSApi.jsval val = new JSApi.jsval(); JSApi.JSh_SetJsvalUndefined(ref val); if (csObj == null) { return(val); } Type t = csObj.GetType(); if (t == typeof(void)) { return(val); } else if (t == typeof(string)) { JSApi.JSh_SetJsvalString(cx, ref val, (string)csObj); } else if (t.IsEnum) { JSApi.JSh_SetJsvalInt(ref val, (int)csObj); } else if (t.IsPrimitive) { if (t == typeof(System.Boolean)) { JSApi.JSh_SetJsvalBool(ref val, (bool)csObj); } else if (t == typeof(System.UInt32) || t == typeof(System.Int32)) { int v = (Int32)csObj; JSApi.JSh_SetJsvalInt(ref val, v); } 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)) { JSApi.JSh_SetJsvalInt(ref val, (Int32)(Int64)csObj); } else if (t == typeof(System.Single) || t == typeof(System.Double)) { JSApi.JSh_SetJsvalDouble(ref val, (double)csObj); } else { Debug.Log("CS -> JS: Unknown primitive type: " + t.ToString()); } } // else if (t.IsValueType) // { // // } else if (t.IsArray) { Debug.LogError("CSObject_2_JSValue: Cannot handle array here"); // todo // return [][] may cause problems // Array arr = csObj as Array; // if (arr.Length > 0 && arr.GetValue(0).GetType().IsArray) // { // Debug.LogWarning("cs return [][] may cause problems."); // } // // IntPtr jsArr = JSApi.JSh_NewArrayObjectS(cx, arr.Length); // // for (int i = 0; i < arr.Length; i++) // { // JSApi.jsval subVal = CSObject_2_JSValue(arr.GetValue(i)); // JSApi.JSh_SetElement(cx, jsArr, (uint)i, ref subVal); // } // JSApi.JSh_SetJsvalObject(ref val, jsArr); } else// if (typeof(UnityEngine.Object).IsAssignableFrom(t) || t.IsClass || t.IsValueType) { IntPtr jsObj; // jsObj = JSMgr.getJSObj(csObj); // if (jsObj == IntPtr.Zero) { // always add a new jsObj jsObj = JSApi.JSh_NewObjectAsClass(cx, JSMgr.glob, t.Name, JSMgr.mjsFinalizer); if (jsObj != IntPtr.Zero) { JSMgr.addJSCSRelation(jsObj, csObj); } } if (jsObj == IntPtr.Zero) { JSApi.JSh_SetJsvalUndefined(ref val); } else { JSApi.JSh_SetJsvalObject(ref val, jsObj); } } // else // { // Debug.Log("CS -> JS: Unknown CS type: " + t.ToString()); // JSApi.JSh_SetJsvalUndefined(ref val); // } return(val); }